Skip to content

Commit 63dbbed

Browse files
authored
Support actions on github pull requests (bug 1641282) (#426)
- use "pr-action" instead of "action" hook and scope when registering an action from a pull-request graph - pass the base repo url and branch in the action payload: the base repo is what the scope should be based on, and the base repo+branch need to be passed back to action tasks so they can check out the right thing
1 parent 7fe73e7 commit 63dbbed

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/taskgraph/actions/registry.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def action_builder(parameters, graph_config, decision_task_id):
163163
actionPerm = "generic" if generic else cb_name
164164

165165
# gather up the common decision-task-supplied data for this action
166-
repo_param = "head_repository"
167166
repository = {
168-
"url": parameters[repo_param],
167+
"url": parameters["head_repository"],
169168
"project": parameters["project"],
170169
"level": parameters["level"],
170+
"base_url": parameters["base_repository"],
171171
}
172172

173173
revision = parameters["head_rev"]
@@ -179,6 +179,9 @@ def action_builder(parameters, graph_config, decision_task_id):
179179
branch = parameters.get("head_ref")
180180
if branch:
181181
push["branch"] = branch
182+
base_branch = parameters.get("base_ref")
183+
if base_branch and branch != base_branch:
184+
push["base_branch"] = base_branch
182185

183186
action = {
184187
"name": name,
@@ -213,11 +216,16 @@ def action_builder(parameters, graph_config, decision_task_id):
213216
if "/" in actionPerm:
214217
raise Exception("`/` is not allowed in action names; use `-`")
215218

219+
if parameters["tasks_for"].startswith("github-pull-request"):
220+
hookId = f"in-tree-pr-action-{level}-{actionPerm}/{tcyml_hash}"
221+
else:
222+
hookId = f"in-tree-action-{level}-{actionPerm}/{tcyml_hash}"
223+
216224
rv.update(
217225
{
218226
"kind": "hook",
219227
"hookGroupId": f"project-{trustDomain}",
220-
"hookId": f"in-tree-action-{level}-{actionPerm}/{tcyml_hash}",
228+
"hookId": hookId,
221229
"hookPayload": {
222230
# provide the decision-task parameters as context for triggerHook
223231
"decision": {
@@ -293,16 +301,20 @@ def sanity_check_task_scope(callback, parameters, graph_config):
293301

294302
actionPerm = "generic" if action.generic else action.cb_name
295303

296-
repo_param = "head_repository"
297-
raw_url = parameters[repo_param]
304+
raw_url = parameters["base_repository"]
298305
parsed_url = parse(raw_url)
299-
expected_scope = f"assume:{parsed_url.taskcluster_role_prefix}:action:{actionPerm}"
306+
action_scope = f"assume:{parsed_url.taskcluster_role_prefix}:action:{actionPerm}"
307+
pr_action_scope = (
308+
f"assume:{parsed_url.taskcluster_role_prefix}:pr-action:{actionPerm}"
309+
)
300310

301311
# the scope should appear literally; no need for a satisfaction check. The use of
302312
# get_current_scopes here calls the auth service through the Taskcluster Proxy, giving
303313
# the precise scopes available to this task.
304-
if expected_scope not in taskcluster.get_current_scopes():
305-
raise ValueError(f"Expected task scope {expected_scope} for this action")
314+
if not set((action_scope, pr_action_scope)) & set(taskcluster.get_current_scopes()):
315+
raise ValueError(
316+
f"Expected task scope {action_scope} or {pr_action_scope} for this action"
317+
)
306318

307319

308320
def trigger_action_callback(

test/test_actions_registry.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,40 @@
1212
("non-existing-action", {}, [], pytest.raises(ValueError)),
1313
(
1414
"retrigger",
15-
{"head_repository": "https://some.git.repo"},
15+
{"base_repository": "https://some.git.repo"},
1616
[],
1717
pytest.raises(InvalidRepoUrlError),
1818
),
1919
(
2020
"retrigger",
21-
{"head_repository": "https://hg.mozilla.org/try"},
21+
{"base_repository": "https://hg.mozilla.org/try"},
2222
["unrelated:scope"],
2323
pytest.raises(ValueError),
2424
),
2525
(
2626
"retrigger",
27-
{"head_repository": "https://hg.mozilla.org/mozilla-central"},
27+
{"base_repository": "https://hg.mozilla.org/mozilla-central"},
2828
["assume:repo:hg.mozilla.org/mozilla-central:action:generic"],
2929
does_not_raise(),
3030
),
3131
(
3232
"retrigger",
33-
{"head_repository": "https://github.com/taskcluster/taskgraph"},
33+
{"base_repository": "https://github.com/taskcluster/taskgraph"},
3434
["assume:repo:github.com/taskcluster/taskgraph:action:generic"],
3535
does_not_raise(),
3636
),
3737
(
3838
"retrigger",
39-
{"head_repository": "[email protected]:mozilla-mobile/firefox-android.git"},
39+
{"base_repository": "[email protected]:mozilla-mobile/firefox-android.git"},
4040
["assume:repo:github.com/mozilla-mobile/firefox-android:action:generic"],
4141
does_not_raise(),
4242
),
43+
(
44+
"retrigger",
45+
{"base_repository": "[email protected]:mozilla-mobile/firefox-android.git"},
46+
["assume:repo:github.com/mozilla-mobile/firefox-android:pr-action:generic"],
47+
does_not_raise(),
48+
),
4349
),
4450
)
4551
def test_sanity_check_task_scope(

0 commit comments

Comments
 (0)