def create_bot_signature(bot_user: User, signature: Signature) -> Optional[Signature]: cla.log.debug(f'create_bot_signature - locating Bot Signature for: {bot_user.get_user_name()}...') project: Project = cla.utils.get_project_instance() try: project.load(signature.get_signature_project_id()) except DoesNotExist as err: cla.log.warning(f'create_bot_signature - unable to load project by id: {signature.get_signature_project_id()}' f' Unable to create bot: {bot_user}') return None the_company: Company = cla.utils.get_company_instance() try: the_company.load(signature.get_signature_reference_id()) except DoesNotExist as err: cla.log.warning(f'create_bot_signature - unable to load company by id: {signature.get_signature_reference_id()}' f' Unable to create bot: {bot_user}') return None bot_sig: Signature = cla.utils.get_signature_instance() # First, before we create a new one, grab a list of employee signatures for this company/project existing_sigs: List[Signature] = bot_sig.get_employee_signatures_by_company_project_model( company_id=bot_user.get_user_company_id(), project_id=signature.get_signature_project_id()) # Check to see if we have an existing signature for this user/company/project combo for sig in existing_sigs: if sig.get_signature_reference_id() == bot_user.get_user_id(): cla.log.debug('create_bot_signature - found existing bot signature ' f'for user: {bot_user} ' f'with company: {the_company} ' f'for project: {project}') return sig # Didn't find an existing signature, let's create a new one cla.log.debug(f'create_bot_signature - creating Bot Signature: {bot_user.get_user_name()}...') bot_sig.set_signature_id(str(uuid.uuid4())) bot_sig.set_signature_project_id(signature.get_signature_project_id()) bot_sig.set_signature_reference_id(bot_user.get_user_id()) bot_sig.set_signature_document_major_version(signature.get_signature_document_major_version()) bot_sig.set_signature_document_minor_version(signature.get_signature_document_minor_version()) bot_sig.set_signature_approved(True) bot_sig.set_signature_signed(True) bot_sig.set_signature_type('cla') bot_sig.set_signature_reference_type('user') bot_sig.set_signature_user_ccla_company_id(bot_user.get_user_company_id()) bot_sig.set_note(f'{datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")} Added as part of ' f'{project.get_project_name()}, approval list by ' f'{the_company.get_company_name()}') bot_sig.save() cla.log.debug(f'create_bot_signature - created Bot Signature: {bot_sig}') return bot_sig
def user_signed_project_signature(user: User, project_id: str, latest_major_version=True): """ Helper function to check if a user has signed a project signature tied to a repository. Will consider both ICLA and employee signatures. :param user: The user object to check for. :type user: cla.models.model_interfaces.User :param project_id: The project to check for. :type project_id: string :param latest_major_version: True means only the latest document major version will be considered. :type latest_major_version: bool :return: Whether or not the user has an signature that's signed and approved for this project. :rtype: boolean """ # Check if we have an ICLA for this user cla.log.debug(f'checking to see if user has signed an ICLA, user: {user}, project: {project_id}') signature = user.get_latest_signature(project_id) icla_pass = False if signature is not None: icla_pass = user_icla_check(user, project_id, signature, latest_major_version) else: cla.log.debug(f'ICLA signature NOT found for User: {user} on project: {project_id}') # If we passed the ICLA check - good, return true, no need to check CCLA if icla_pass: cla.log.debug(f'ICLA signature check passed for User: {user} on project: {project_id} - skipping CCLA check') return True else: cla.log.debug(f'ICLA signature check failed for User: {user} on project: {project_id} - will now check CCLA') # Check if we have an CCLA for this user company_id = user.get_user_company_id() cla.log.debug('checking to see if user has signed an CCLA, ' f'user: {user}, project_id: {project_id}, company_id: {company_id}') ccla_pass = False if company_id is not None: cla.log.debug('user has company_id set - getting latest signature for ' f'user: {user}, project_id: {project_id}, company_id: {company_id}') signature = user.get_latest_signature(project_id, company_id=company_id) # Don't check the version for employee signatures. if signature is not None: ccla_pass = user_ccla_check(user, project_id, signature) else: cla.log.debug(f'User: {user} is NOT associated with a company - unable to check for a CCLA.') if ccla_pass: cla.log.debug(f'CCLA signature check passed for User: {user} on project: {project_id}') return True else: cla.log.debug(f'CCLA signature check failed for User: {user} on project: {project_id}') cla.log.debug(f'User: {user} failed both ICLA and CCLA checks') return False