Skip to content

Commit 5cf320c

Browse files
committed
feat(run-task): add ability to fetch arbitrary extra refs
Bug: 2028208
1 parent 31b2a36 commit 5cf320c

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/taskgraph/run-task/run-task

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import time
3131
import urllib.error
3232
import urllib.request
3333
from pathlib import Path
34-
from typing import Dict, Optional
34+
from typing import Dict, List, Optional
3535

3636
SECRET_BASEURL_TPL = "{}/secrets/v1/secret/{{}}".format(
3737
os.environ.get("TASKCLUSTER_PROXY_URL", "http://taskcluster").rstrip("/")
@@ -640,6 +640,7 @@ def git_checkout(
640640
ssh_key_file: Optional[Path],
641641
ssh_known_hosts_file: Optional[Path],
642642
shallow: bool = False,
643+
extra_refs: Optional[List[str]] = None,
643644
):
644645
assert head_ref or head_rev
645646

@@ -734,6 +735,9 @@ def git_checkout(
734735
env=env,
735736
)
736737

738+
for ref in extra_refs or []:
739+
git_fetch(destination_path, f"{ref}:{ref}", remote=head_repo, env=env)
740+
737741
args = [
738742
"git",
739743
"checkout",
@@ -932,6 +936,8 @@ def collect_vcs_options(args, project, name):
932936
head_rev = os.environ.get(f"{env_prefix}_HEAD_REV")
933937
pip_requirements = os.environ.get(f"{env_prefix}_PIP_REQUIREMENTS")
934938
private_key_secret = os.environ.get(f"{env_prefix}_SSH_SECRET_NAME")
939+
if extra_refs := os.environ.get(f"{env_prefix}_EXTRA_REFS"):
940+
extra_refs = json.loads(extra_refs)
935941

936942
store_path = os.environ.get("HG_STORE_PATH")
937943

@@ -965,6 +971,7 @@ def collect_vcs_options(args, project, name):
965971
"ssh-secret-name": private_key_secret,
966972
"pip-requirements": pip_requirements,
967973
"shallow-clone": shallow_clone,
974+
"extra-refs": extra_refs,
968975
}
969976

970977

@@ -1014,6 +1021,7 @@ def vcs_checkout_from_args(options):
10141021
ssh_key_file,
10151022
ssh_known_hosts_file,
10161023
shallow=options.get("shallow-clone", False),
1024+
extra_refs=options.get("extra-refs"),
10171025
)
10181026
elif options["repo-type"] == "hg":
10191027
revision = hg_checkout(

test/test_scripts_run_task.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import json
23
import os
34
import site
45
import stat
@@ -179,6 +180,17 @@ def test_install_pip_requirements_with_uv(
179180
{"shallow-clone": True},
180181
id="git_with_shallow_clone",
181182
),
183+
pytest.param(
184+
{},
185+
{
186+
"REPOSITORY_TYPE": "git",
187+
"HEAD_REPOSITORY": "https://github.com/example/repo",
188+
"HEAD_REV": "abc123",
189+
"EXTRA_REFS": json.dumps(["refs/notes/taskgraph", "refs/notes/other"]),
190+
},
191+
{"extra-refs": ["refs/notes/taskgraph", "refs/notes/other"]},
192+
id="git_with_extra_refs",
193+
),
182194
],
183195
)
184196
def test_collect_vcs_options(
@@ -216,6 +228,7 @@ def test_collect_vcs_options(
216228
"shallow-clone": False,
217229
"ssh-secret-name": env.get("SSH_SECRET_NAME"),
218230
"store-path": env.get("HG_STORE_PATH"),
231+
"extra-refs": None,
219232
}
220233
if "PIP_REQUIREMENTS" in env:
221234
expected["pip-requirements"] = os.path.join(
@@ -638,3 +651,36 @@ def test_main_abspath_environment(mocker, run_main):
638651
assert env.get("MOZ_UV_HOME") == "/builds/worker/dir/uv"
639652
for key in envvars:
640653
assert env[key] == "/builds/worker/file"
654+
655+
656+
def test_git_checkout_extra_refs(mock_stdin, run_task_mod, mock_git_repo, tmp_path):
657+
"""extra_refs are fetched into the local repo during checkout."""
658+
# Add a notes ref to the source repo
659+
rev = mock_git_repo["main"][-1]
660+
subprocess.check_call(
661+
["git", "notes", "--ref=refs/notes/taskgraph", "add", "-m", "test", rev],
662+
cwd=mock_git_repo["path"],
663+
)
664+
665+
destination = tmp_path / "destination"
666+
run_task_mod.git_checkout(
667+
destination_path=str(destination),
668+
head_repo=mock_git_repo["path"],
669+
base_repo=mock_git_repo["path"],
670+
base_rev=None,
671+
head_ref="main",
672+
head_rev=None,
673+
ssh_key_file=None,
674+
ssh_known_hosts_file=None,
675+
extra_refs=["refs/notes/taskgraph"],
676+
)
677+
678+
# Verify the notes ref is available locally
679+
result = subprocess.run(
680+
["git", "notes", "--ref=refs/notes/taskgraph", "show", rev],
681+
cwd=str(destination),
682+
capture_output=True,
683+
text=True,
684+
)
685+
assert result.returncode == 0
686+
assert "test" in result.stdout

0 commit comments

Comments
 (0)