Skip to content

Commit

Permalink
add support for codebuild github PR refs pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mufiAmazon authored and jlhood committed May 11, 2024
1 parent fa55103 commit 7fe17d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ def __init__(self, build_event):

def get_pr_id(self):
"""If this build was for a PR branch, returns the PR ID, otherwise returns None."""
matches = re.match(r'^pr\/(\d+)', self._get_build_details().get('sourceVersion', ""))
source_version = self._get_build_details().get('sourceVersion', "")
matches = re.match(r'^pr\/(\d+)', source_version)
if not matches:
return None
# check for refs pattern
pattern = r'refs/pull/(\d+)/head'
matches = re.search(pattern, source_version)
if not matches:
return None
return int(matches.group(1))

@property
Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_get_pr_id_is_pr(mocker, mock_codebuild):
assert build_obj.get_pr_id() == 123


def test_get_pr_id_is_pr_refs_pattern(mocker, mock_codebuild):
_mock_build_details('refs/pull/123/head^{2adff25fccedf7c08117b3fc93a7eca896c19060}')
build_obj = build.Build(_mock_build_event())
assert build_obj.get_pr_id() == 123


def test_is_pr_build_not_pr(mocker, mock_codebuild):
_mock_build_details('master')
build_obj = build.Build(_mock_build_event())
Expand Down

0 comments on commit 7fe17d8

Please sign in to comment.