Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
11 changes: 8 additions & 3 deletions src/github_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -111,11 +115,12 @@ 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)
LOG.debug("Extracting repository from location:%s", 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(
config.PROJECT_NAME, github_location))

self._github_owner = matches.group(1)
self._github_repo = matches.group(2)
self._github_repo = matches.group(2).replace('/', '')
8 changes: 8 additions & 0 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_github_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down