Skip to content

Commit 83b0cae

Browse files
authored
fix: only trigger Kokoro jobs once (#331)
1 parent 3674d7c commit 83b0cae

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

autorelease/trigger.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def trigger_kokoro_build_for_pull_request(
5151
)
5252
return
5353

54+
# If the Kokoro job has already been triggered, don't trigger again.
55+
if "labels" in pull and any(
56+
"name" in label and label["name"] == "autorelease: triggered"
57+
for label in pull["labels"]
58+
):
59+
return
60+
5461
# Determine language.
5562
lang = common.guess_language(gh, pull["base"]["repo"]["full_name"])
5663

@@ -82,6 +89,7 @@ def trigger_kokoro_build_for_pull_request(
8289
sha=sha,
8390
env_vars={"AUTORELEASE_PR": pull_request_url},
8491
)
92+
gh.update_pull_labels(pull, add=["autorelease: triggered"])
8593

8694

8795
def main(github_token, kokoro_credentials) -> reporter.Reporter:

tests/test_autorelease_trigger.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_trigger_kokoro_build_for_pull_request_triggers_kokoro(trigger_build):
9696

9797
trigger.trigger_kokoro_build_for_pull_request(Mock(), github, issue, Mock())
9898
trigger_build.assert_called_once()
99+
github.update_pull_labels.assert_called_once()
99100

100101

101102
@patch("autorelease.trigger.LANGUAGE_ALLOWLIST", [])
@@ -137,3 +138,25 @@ def test_trigger_kokoro_build_for_pull_request_skips_kokoro_if_no_job_name(
137138
}
138139
trigger.trigger_kokoro_build_for_pull_request(Mock(), github, issue, Mock())
139140
trigger_build.assert_not_called()
141+
142+
143+
@patch("autorelease.trigger.LANGUAGE_ALLOWLIST", ["php"])
144+
@patch("autorelease.kokoro.trigger_build")
145+
def test_trigger_kokoro_build_for_pull_request_skips_kokoro_if_already_triggered(
146+
trigger_build,
147+
):
148+
github = Mock()
149+
github.get_url.return_value = {
150+
"merged_at": "2021-01-01T09:00:00.000Z",
151+
"base": {"repo": {"full_name": "googleapis/google-cloud-php"}},
152+
"html_url": "https://github.com/googleapis/google-cloud-php/pulls/5",
153+
"labels": [{"id": 12345, "name": "autorelease: triggered"}],
154+
}
155+
issue = {
156+
"pull_request": {
157+
"url": "https://api.github.com/googleapis/google-cloud-php/pull/5"
158+
},
159+
"merged_at": "2021-01-01T09:00:00.000Z",
160+
}
161+
trigger.trigger_kokoro_build_for_pull_request(Mock(), github, issue, Mock())
162+
trigger_build.assert_not_called()

0 commit comments

Comments
 (0)