From a727ecd68553d63594b66ff39398acf9024e0810 Mon Sep 17 00:00:00 2001 From: Jeremy Waller Date: Tue, 9 Nov 2021 13:49:12 -0600 Subject: [PATCH 1/3] fix regex parsing --- src/github_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github_proxy.py b/src/github_proxy.py index f95208f..17a65d7 100644 --- a/src/github_proxy.py +++ b/src/github_proxy.py @@ -111,7 +111,7 @@ def _init_github_info(self): ' parameter to specify a token to use when writing to GitHub.'.format(config.PROJECT_NAME)) github_location = project_details['source']['location'] - matches = re.search(r'github\.com\/(.+)\/(.+)\.git$', github_location) + matches = re.search(r'github\.com\/(.+)\/(.+)$', github_location) if not matches: raise RuntimeError( 'Could not parse GitHub owner/repo name from AWS CodeBuild project {}. location={}'.format( From 99294a5b2b0b28f9cdceb74f35e3e10cbb524400 Mon Sep 17 00:00:00 2001 From: Jeremy Waller Date: Tue, 9 Nov 2021 14:31:26 -0600 Subject: [PATCH 2/3] test update --- test/unit/test_github_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/test_github_proxy.py b/test/unit/test_github_proxy.py index fc47250..cc2fc6f 100644 --- a/test/unit/test_github_proxy.py +++ b/test/unit/test_github_proxy.py @@ -6,7 +6,7 @@ GITHUB_OWNER = 'gh-user' GITHUB_REPO = 'gh-repo' -GITHUB_LOCATION = 'https://github.com/{}/{}.git'.format(GITHUB_OWNER, GITHUB_REPO) +GITHUB_LOCATION = 'https://github.com/{}/{}'.format(GITHUB_OWNER, GITHUB_REPO) CODEBUILD_GITHUB_TOKEN = 'shhh!!' SECRETS_MANAGER_GITHUB_TOKEN = "don't tell!!" BUILD_STATUS = 'SUCCEEDED' From 8fb068c37961758266d1b0822673923d48bca4a4 Mon Sep 17 00:00:00 2001 From: Vasile Groza Date: Mon, 6 Jun 2022 10:13:44 +0300 Subject: [PATCH 3/3] feat: add support for projects owned bu ORGs this will make possible to interact with repositories owned by organizations --- src/config.py | 1 + src/github_proxy.py | 9 +++++++-- template.yml | 8 ++++++++ test/unit/conftest.py | 1 + test/unit/test_constants.py | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/config.py b/src/config.py index 9d9a753..762159f 100644 --- a/src/config.py +++ b/src/config.py @@ -5,6 +5,7 @@ LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO') BUCKET_NAME = os.getenv('BUILD_LOGS_BUCKET_NAME') PROJECT_NAME = os.getenv('CODEBUILD_PROJECT_NAME') +PROJECT_UNDER_ORG = os.getenv('GITHUB_PROJECT_UNDER_ORG') == 'true' EXPIRATION_IN_DAYS = int(os.getenv('EXPIRATION_IN_DAYS')) BUILD_LOGS_API_ENDPOINT = os.getenv('BUILD_LOGS_API_ENDPOINT') GITHUB_OAUTH_TOKEN_SECRET_ARN = os.getenv('GITHUB_OAUTH_TOKEN_SECRET_ARN') diff --git a/src/github_proxy.py b/src/github_proxy.py index 17a65d7..ed6589e 100644 --- a/src/github_proxy.py +++ b/src/github_proxy.py @@ -75,7 +75,11 @@ def delete_previous_comments(self, build): def _get_repo(self): if not hasattr(self, '_repo'): gh_client = self._get_client() - self._repo = gh_client.get_user(self._github_owner).get_repo(self._github_repo) + LOG.debug("Owner:%s, repo:%s", self._github_owner, self._github_repo) + if config.PROJECT_UNDER_ORG: + self._repo = gh_client.get_organization(self._github_owner).get_repo(self._github_repo) + else: + self._repo = gh_client.get_user(self._github_owner).get_repo(self._github_repo) return self._repo def _get_client(self): @@ -111,6 +115,7 @@ def _init_github_info(self): ' parameter to specify a token to use when writing to GitHub.'.format(config.PROJECT_NAME)) github_location = project_details['source']['location'] + LOG.debug("Extracting repository from location:%s", github_location) matches = re.search(r'github\.com\/(.+)\/(.+)$', github_location) if not matches: raise RuntimeError( @@ -118,4 +123,4 @@ def _init_github_info(self): config.PROJECT_NAME, github_location)) self._github_owner = matches.group(1) - self._github_repo = matches.group(2) + self._github_repo = matches.group(2).replace('/', '') diff --git a/template.yml b/template.yml index d8c356e..9b0892a 100644 --- a/template.yml +++ b/template.yml @@ -52,6 +52,13 @@ Parameters: - "false" Default: "false" Description: Set to "true" to delete previously posted PR comments before posting a new one. + GithubProjectUnderOrg: + Type: String + AllowedValues: + - "true" + - "false" + Default: "false" + Description: "Set to true if the project is owned by organization" CommentOnSuccess: Type: String AllowedValues: @@ -110,6 +117,7 @@ Resources: LOG_LEVEL: !Ref LogLevel BUILD_LOGS_BUCKET_NAME: !Ref BuildLogs CODEBUILD_PROJECT_NAME: !Ref CodeBuildProjectName + GITHUB_PROJECT_UNDER_ORG: !Ref GithubProjectUnderOrg EXPIRATION_IN_DAYS: !Ref ExpirationInDays BUILD_LOGS_API_ENDPOINT: !Sub "https://${BuildLogsApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/buildlogs" GITHUB_OAUTH_TOKEN_SECRET_ARN: !If diff --git a/test/unit/conftest.py b/test/unit/conftest.py index 60b862a..5586c66 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -12,6 +12,7 @@ # set expected config environment variables to test constants os.environ['BUILD_LOGS_BUCKET_NAME'] = test_constants.BUCKET_NAME os.environ['CODEBUILD_PROJECT_NAME'] = test_constants.PROJECT_NAME +os.environ['GITHUB_PROJECT_UNDER_ORG'] = test_constants.GITHUB_PROJECT_UNDER_ORG os.environ['EXPIRATION_IN_DAYS'] = str(test_constants.EXPIRATION_IN_DAYS) os.environ['BUILD_LOGS_API_ENDPOINT'] = test_constants.BUILD_LOGS_API_ENDPOINT os.environ['GITHUB_OAUTH_TOKEN_SECRET_ARN'] = test_constants.GITHUB_OAUTH_TOKEN_SECRET_ARN diff --git a/test/unit/test_constants.py b/test/unit/test_constants.py index fbb970e..c16fb4d 100644 --- a/test/unit/test_constants.py +++ b/test/unit/test_constants.py @@ -5,6 +5,7 @@ BUCKET_NAME = "TestBucket" PROJECT_NAME = 'TestProject' +GITHUB_PROJECT_UNDER_ORG = 'false' EXPIRATION_IN_DAYS = 20 BUILD_LOGS_API_ENDPOINT = 'https://foo.com/bar' GITHUB_OAUTH_TOKEN_SECRET_ARN = ''