Skip to content

Commit 0c9f3b1

Browse files
committed
Allow extra refs to be marked as optional in run-task
Also mark the notes/decision-parameters one as optional in this repo. This prevents failures if a fork doesn't have any notes at `refs/notes/decision-parameters`. I chose to pay for the `ls-remote` time if an optional ref failed to fetch to avoid having to parse the stderr from git fetch which felt very fragile. It adds a potential ~1.5s to fetches per optional remote that is missing which isn't ideal, but I don't have anything better at this point in time. The new log message is slightly misleading since technically ls-remote might find one ref but not another and it'd be reported that both are missing but I don't want to have to ls-remote for each ref, adding 1.5s each time, to fix this.
1 parent fa30e4b commit 0c9f3b1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

.taskcluster.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ tasks:
226226
- $if: 'isPullRequest'
227227
then:
228228
TASKGRAPH_PULL_REQUEST_NUMBER: '${event.pull_request.number}'
229-
TASKGRAPH_EXTRA_REFS: {$json: ["refs/notes/decision-parameters"]}
229+
TASKGRAPH_EXTRA_REFS: {$json: ["?refs/notes/decision-parameters"]}
230230
- $if: 'tasks_for == "action" || tasks_for == "pr-action"'
231231
then:
232232
ACTION_TASK_GROUP_ID: '${action.taskGroupId}' # taskGroupId of the target task

src/taskgraph/run-task/run-task

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ def git_fetch(
574574
tags: bool = False,
575575
shallow: bool = False,
576576
env: Optional[Dict[str, str]] = None,
577+
optional: bool = False,
577578
):
578579
args = ["git", "fetch"]
579580
if tags:
@@ -584,6 +585,30 @@ def git_fetch(
584585
args.append("--depth=1")
585586

586587
args.extend([remote, *set(targets)])
588+
if not optional:
589+
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
590+
return
591+
592+
try:
593+
retry_required_command(
594+
b"vcs", args, cwd=destination_path, extra_env=env, retries=0
595+
)
596+
return
597+
except SystemExit:
598+
# Ignore the first failure to check if the ref even exists
599+
pass
600+
601+
ref_names = [t.split(":", 1)[0] for t in targets]
602+
ls = subprocess.run(
603+
["git", "ls-remote", "--exit-code", remote, *ref_names],
604+
cwd=destination_path,
605+
capture_output=True,
606+
)
607+
608+
if ls.returncode == 2:
609+
print_line(b"vcs", b"optional refs %r not present on remote, skipping\n" % ref_names)
610+
return
611+
587612
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
588613

589614

@@ -735,11 +760,15 @@ def git_checkout(
735760
env=env,
736761
)
737762

738-
if extra_refs:
763+
for ref in extra_refs or []:
764+
optional = ref.startswith("?")
765+
if optional:
766+
ref = ref[1:]
739767
git_fetch(
740768
destination_path,
741-
*[f"{ref}:{ref}" for ref in extra_refs],
769+
f"{ref}:{ref}",
742770
remote=head_repo,
771+
optional=optional,
743772
env=env,
744773
)
745774

0 commit comments

Comments
 (0)