MCPcopy Create free account
hub / github.com/github/gh-aw / derivePrHeadRef

Function derivePrHeadRef

actions/setup/js/apply_samples.cjs:217–256  ·  view source on GitHub ↗

* Derive the pull request head ref for the triggering event. * * Resolution order: * 1. `pull_request.head.ref` from the event payload (pull_request and * pull_request_target events). * 2. PR API lookup using `issue.number` when the payload describes an * issue_comment on a pull

(entry)

Source from the content-addressed store, hash-verified

215 * @returns {Promise<string|null>}
216 */
217async function derivePrHeadRef(entry) {
218 const payload = readEventPayload();
219
220 // 1. pull_request* events expose the head ref directly.
221 const directRef = payload?.pull_request?.head?.ref;
222 if (typeof directRef === "string" && directRef.trim()) {
223 return directRef.trim();
224 }
225
226 // Determine the target repo for any API lookups.
227 // Resolution order:
228 // a. entry.arguments.repo — explicit per-sample override (cross-repo workflows).
229 // b. target-repo from the safe-outputs config file (GH_AW_SAFE_OUTPUTS_CONFIG_PATH)
230 // for the tool — covers siderepo workflow_dispatch where the sample arguments
231 // carry `pull_request_number` but not a `repo` override (issue #41292).
232 // c. GITHUB_REPOSITORY — host repo fallback.
233 let repoSlug = "";
234 if (typeof entry.arguments.repo === "string" && entry.arguments.repo.trim()) {
235 repoSlug = entry.arguments.repo.trim();
236 } else {
237 repoSlug = readConfiguredTargetRepo(entry.tool) || process.env.GITHUB_REPOSITORY || "";
238 }
239 const [owner, repo] = repoSlug.split("/");
240 if (!owner || !repo) return null;
241
242 // 2. issue_comment / pull_request_review_comment with a PR-linked issue.
243 if (payload?.issue?.pull_request && typeof payload.issue.number === "number") {
244 const ref = await fetchPullRequestHeadRef({ owner, repo, pullNumber: payload.issue.number });
245 if (ref) return ref;
246 }
247
248 // 3. Explicit pull_request_number on the sample arguments.
249 const argNumber = Number(entry.arguments.pull_request_number);
250 if (Number.isFinite(argNumber) && argNumber > 0) {
251 const ref = await fetchPullRequestHeadRef({ owner, repo, pullNumber: argNumber });
252 if (ref) return ref;
253 }
254
255 return null;
256}
257
258/**
259 * Read the configured `target-repo` for a given safe-output tool from the

Callers 1

preStagePatchFunction · 0.85

Calls 3

readEventPayloadFunction · 0.85
readConfiguredTargetRepoFunction · 0.85
fetchPullRequestHeadRefFunction · 0.85

Tested by

no test coverage detected