[docs]@dataclasses.dataclassclassCodeCommitEventHandler:""" Handle CodeCommit event. Determine whether we should trigger a CodeBuild job? trigger what build project? how to trigger it? include what env var? :param bsm: where the original event is stored. :param cc_event: the CodeCommit event object. :param s3_console_url: where the original event is stored. :param s3_uri: where the original event is stored. """bsm:BotoSesManager=dataclasses.field()cc_event:CodeCommitEvent=dataclasses.field()s3_console_url:str=dataclasses.field()s3_uri:str=dataclasses.field()deflog_cc_event(self):logger.header("Handle CodeCommit event","-",60)logger.info(f"- detected event type = {self.cc_event.event_type!r}")logger.info(f"- event description = {self.cc_event.event_description!r}")@propertydefpr_commit_console_url(self)->str:""" Return the link can jump to AWS CodeCommit Pull Request commit console. It only works when the CodeCommit event is a PR event. """returnconsole.browse_pr(aws_region=self.bsm.aws_region,repo_name=self.cc_event.repo_name,pr_id=self.cc_event.pr_id,commits_tab=True,)@propertydefcomment_body_before_run_build_job(self)->str:lines=["## 🌴 A build run is triggered, let's relax.","",f"- commit id: [{self.cc_event.source_commit[:7]}]({self.pr_commit_console_url})",f'- commit message: "{self.cc_event.commit_message.strip()}"',f'- committer name: "{self.cc_event.committer_name.strip()}"',]return"\n".join(lines)defget_comment_body_after_run_build_job(self,build_job_run:BuildJobRun)->str:lines=["## 🌴 A build run is triggered, let's relax.","",f"- build run id: [{build_job_run.project_name}:{build_job_run.run_id}]({build_job_run.console_url})",f"- commit id: [{self.cc_event.source_commit[:7]}]({self.pr_commit_console_url})",f'- commit message: "{self.cc_event.commit_message.strip()}"',f'- committer name: "{self.cc_event.committer_name.strip()}"',]return"\n".join(lines)
[docs]defrun_build_job(self,build_job_config:BuildJobConfig,additional_env_var:dict,)->BuildJobRun:""" Based on build job config from the ``codebuild-config.json`` file, run the CodeBuild job. :param build_job_config: :param additional_env_var: :return: """# prepare argumentkwargs=dict(bsm=self.bsm,projectName=build_job_config.project_name,)ifbuild_job_config.buildspec:kwargs["buildspecOverride"]=build_job_config.buildspeckwargs["sourceVersion"]=self.cc_event.source_commit# use the env var defined in ``codebuild-config.json`` fileenv_var=build_job_config.env_var.copy()# merge additional env varenv_var.update(additional_env_var)# add additional env var based on the build typeifbuild_job_config.is_batch_job:logger.info(f"invoke codebuild.start_build_batch API, "f"source version = {self.cc_event.source_commit!r}")start_build_function=start_build_batchenv_var[f"{CI_DATA_PREFIX}BUILD_TYPE"]="batch build"else:logger.info(f"invoke codebuild.start_build API, "f"source version = {self.cc_event.source_commit!r}")start_build_function=start_buildenv_var[f"{CI_DATA_PREFIX}BUILD_TYPE"]="single build"# set env var in kwargskwargs["environmentVariablesOverride"]=[{"name":key,"value":value,"type":"PLAINTEXT",}forkey,valueinenv_var.items()]# run build jobres=start_build_function(**kwargs)# parse API responsebuild_job_run=BuildJobRun.from_start_build_response(res)returnbuild_job_run
defrun_build_job_and_post_comment(self,build_job_config:BuildJobConfig,):withcc_boto.CommentThread(bsm=self.bsm)asthread:post_comment_kwargs=dict(repo_name=self.cc_event.repo_name,content=self.comment_body_before_run_build_job,before_commit_id=self.cc_event.target_commit,after_commit_id=self.cc_event.source_commit,)ifself.cc_event.pr_id:post_comment_kwargs["pr_id"]=self.cc_event.pr_idlogger.info(f"post comment on PR {self.cc_event.pr_id}")else:logger.info(f"post comment on Commit {self.cc_event.source_commit}")comment=thread.post_comment(**post_comment_kwargs)ci_data=CIData(event_s3_console_url=self.s3_console_url,event_s3_uri=self.s3_uri,event_type=self.cc_event.event_type,comment_id=comment.comment_id,commit_id=self.cc_event.source_commit,commit_message=self.cc_event.commit_message,committer_name=self.cc_event.committer_name,branch_name=self.cc_event.source_branch,pr_id=self.cc_event.pr_id,pr_from_branch=self.cc_event.source_branch,pr_to_branch=self.cc_event.target_branch,pr_from_commit_id=self.cc_event.source_commit,pr_to_commit_id=self.cc_event.target_commit,)# start buildbuild_job_run=self.run_build_job(build_job_config=build_job_config,additional_env_var=ci_data.to_env_var(),)# update the first comment with build job run console urlcc_boto.update_comment(bsm=self.bsm,comment_id=comment.comment_id,content=self.get_comment_body_after_run_build_job(build_job_run),)defaction_start_build(self):logger.header("Trigger build jobs","-",60)cb_config=CodebuildConfig.from_codecommit_repo(bsm=self.bsm,repo_name=self.cc_event.repo_name,commit_id=self.cc_event.source_commit,)forjobincb_config.jobs:self.run_build_job_and_post_comment(job)defexecute(self):self.log_cc_event()action=check_what_to_do(self.cc_event)ifaction==CodeCommitHandlerActionEnum.nothing:returnelifaction==CodeCommitHandlerActionEnum.start_build:self.action_start_build()