Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for codebuild github PR refs pattern #45

Merged
merged 1 commit into from
May 11, 2024
Merged
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
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