# -*- coding: utf-8 -*-"""This module defines the ``codebuild-config.json`` file schema, that defines whichCodeBuild project the git repo should use to run CI job."""importtypingasTimportdataclassesfromsuperjsonimportjsonfromaws_codecommitimportbetter_botofromboto_session_managerimportBotoSesManagerfrom.importlogger
[docs]@dataclasses.dataclassclassBuildJobConfig:""" Per CodeBuild project configuration. One git repo can map to multiple CodeBuild projects. """project_name:str=dataclasses.field()is_batch_job:bool=dataclasses.field()buildspec:T.Optional[str]=dataclasses.field(default=None)env_var:dict=dataclasses.field(default_factory=dict)@classmethoddeffrom_dict(cls,dct:dict)->"BuildJobConfig":returncls(project_name=dct["project_name"],is_batch_job=dct["is_batch_job"],buildspec=dct.get("buildspec"),env_var=dct.get("env_var",{}),)
[docs]@dataclasses.dataclassclassCodebuildConfig:""" The ``codebuild-config.json`` file that defines which CodeBuild project it should use to run CI job. By default, it is at the root folder of the git repo. The data structure looks like this: .. code-block:: javascript { "jobs": [ { "project_name": "my-codebuild-project-name", "is_batch_job": false, "buildspec": "the path to the buildspec.yml file", "env_var": { "key1": "value1", "key2": "value2" } }, { ... }, ... ] """jobs:T.List[BuildJobConfig]=dataclasses.field(default_factory=list)@classmethoddeffrom_dict(cls,dct:dict)->"CodebuildConfig":returncls(jobs=[BuildJobConfig.from_dict(d)fordindct["jobs"]])
[docs]@classmethoddeffrom_codecommit_repo(cls,bsm:BotoSesManager,repo_name:str,commit_id:str,)->"CodebuildConfig":""" Get the ``codebuild-config.json`` file from the CodeCommit repo. Read more about the :class:`~aws_ci_bot.code_build_config.CodebuildConfig` file. """file_path="codebuild-config.json"logger.info(f"Get codebuild config from {file_path!r}")file=better_boto.get_file(bsm=bsm,repo_name=repo_name,file_path=file_path,commit_id=commit_id,)returnCodebuildConfig.from_dict(json.loads(file.get_text(),ignore_comments=True))