* Fetch a pull request via the REST API and return its head ref. Uses the * per-repo token from GH_AW_REPO_TOKENS when present, falling back to * GITHUB_TOKEN; falls back to anonymous (works for public repositories) when * no token is available. Returns null on any failure so the caller can decid
({ owner, repo, pullNumber })
| 174 | * @returns {Promise<string|null>} |
| 175 | */ |
| 176 | async function fetchPullRequestHeadRef({ owner, repo, pullNumber }) { |
| 177 | const apiUrl = process.env.GITHUB_API_URL || "https://api.github.com"; |
| 178 | const url = `${apiUrl}/repos/${owner}/${repo}/pulls/${pullNumber}`; |
| 179 | /** @type {Record<string, string>} */ |
| 180 | const headers = { |
| 181 | Accept: "application/vnd.github+json", |
| 182 | "X-GitHub-Api-Version": "2022-11-28", |
| 183 | "User-Agent": "gh-aw-apply-samples", |
| 184 | }; |
| 185 | const token = selectTokenForRepo(owner, repo); |
| 186 | if (token) headers["Authorization"] = `Bearer ${token}`; |
| 187 | try { |
| 188 | const resp = await fetch(url, { headers }); |
| 189 | if (!resp.ok) { |
| 190 | core.warning(`apply_samples: GET ${url} returned HTTP ${resp.status}`); |
| 191 | return null; |
| 192 | } |
| 193 | const body = await resp.json(); |
| 194 | // @ts-ignore - REST API response shape is well-defined; trust GitHub PR endpoint contract |
| 195 | const ref = body && body.head && typeof body.head.ref === "string" ? body.head.ref : null; |
| 196 | return ref || null; |
| 197 | } catch (err) { |
| 198 | core.warning(`apply_samples: failed to fetch PR ${owner}/${repo}#${pullNumber}: ${getErrorMessage(err)}`); |
| 199 | return null; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Derive the pull request head ref for the triggering event. |
no test coverage detected