diff --git a/.github/actions/ref_from_sha/branch_from_sha.js b/.github/actions/ref_from_sha/branch_from_sha.cjs similarity index 100% rename from .github/actions/ref_from_sha/branch_from_sha.js rename to .github/actions/ref_from_sha/branch_from_sha.cjs diff --git a/.github/actions/ref_from_sha/index.cjs b/.github/actions/ref_from_sha/index.cjs new file mode 100644 index 000000000..9356f47e3 --- /dev/null +++ b/.github/actions/ref_from_sha/index.cjs @@ -0,0 +1,70 @@ +/** + * Given the GITHUB_SHA, find the pr and ref it is the head of. + * + * Intended for use with Vercel `deployment_status` events which incorrectly + * set the GITHUB_REF to the commit SHA value. + */ + +const core = require("@actions/core"); +const github = require("@actions/github"); + +const prFromSha = require("./pr_from_sha"); +const branchFromSha = require("./branch_from_sha"); + +async function run() { + try { + const githubToken = core.getInput("github_token"); + + const octokit = github.getOctokit(githubToken); + + const owner = github.context.payload.repository.owner.login; + const repo = github.context.payload.repository.name; + if (!owner || !repo) { + throw new Error( + `Could not determine repo details, got: owner "${owner} and repo "${repo}".`, + ); + } + + let sha = github.context.sha; + if (!sha) { + throw new Error(`SHA not supplied.`); + } + + // Get the *first* PR that has the given SHA as the head of the feature branch. + const pullRequest = await prFromSha(octokit, { owner, repo }, sha); + let headRef; + let prNumber = undefined; + if (pullRequest !== null) { + // There is a PR with this SHA as the head of the feature branch + headRef = pullRequest.head.ref; + // DEBUG + core.debug(`Pull Request:\n${JSON.stringify(pullRequest, null, 2)}`); + prNumber = pullRequest.number; + } else { + // The SHA is not on a PR feature branch, get from first matching general branch, prefer `main`. + const branch = await branchFromSha(octokit, { owner, repo }, sha); + if (branch !== null) { + headRef = branch.name; + } else { + // No refs to be had. + core.error(`Could not find pull request or branch for SHA: ${sha}`); + } + } + + /** @todo handle release tags? v1.2.3 */ + const fullHeadRef = `refs/heads/${headRef}`; + core.setOutput("branch_name", headRef); + core.setOutput("head_ref", fullHeadRef); + core.setOutput("pr_number", prNumber); + core.info( + `Found pull request or branch for for SHA: ${sha} with ref: ${fullHeadRef}.${ + prNumber ? `Found PR number: ${prNumber}` : "" + }`, + ); + } catch (error) { + core.error(error); + core.setFailed(error.message); + } +} + +run(); diff --git a/.github/actions/ref_from_sha/index.js b/.github/actions/ref_from_sha/index.js index 9356f47e3..9d7c8f7c4 100644 --- a/.github/actions/ref_from_sha/index.js +++ b/.github/actions/ref_from_sha/index.js @@ -33,7 +33,7 @@ async function run() { // Get the *first* PR that has the given SHA as the head of the feature branch. const pullRequest = await prFromSha(octokit, { owner, repo }, sha); let headRef; - let prNumber = undefined; + let prNumber; if (pullRequest !== null) { // There is a PR with this SHA as the head of the feature branch headRef = pullRequest.head.ref; diff --git a/.github/actions/ref_from_sha/pr_from_sha.js b/.github/actions/ref_from_sha/pr_from_sha.cjs similarity index 100% rename from .github/actions/ref_from_sha/pr_from_sha.js rename to .github/actions/ref_from_sha/pr_from_sha.cjs diff --git a/.github/actions/ref_from_sha/test/e2e.js b/.github/actions/ref_from_sha/test/e2e.cjs similarity index 100% rename from .github/actions/ref_from_sha/test/e2e.js rename to .github/actions/ref_from_sha/test/e2e.cjs diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 415a33f65..ffe5958cd 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -28,6 +28,7 @@ jobs: id: ref_from_sha with: github_token: ${{ secrets.GITHUB_TOKEN }} + entrypoint: ./index.cjs - name: Install Playwright run: npx playwright install --with-deps chromium webkit - name: Inject Doppler env vars diff --git a/.github/workflows/post_deployment.yml b/.github/workflows/post_deployment.yml index 6b1d6c6ba..e20f76463 100644 --- a/.github/workflows/post_deployment.yml +++ b/.github/workflows/post_deployment.yml @@ -35,6 +35,7 @@ jobs: id: ref_from_sha with: github_token: ${{ secrets.GITHUB_TOKEN }} + entrypoint: ./index.cjs - uses: mcky/find-and-replace-pull-request-body@v1.1.6-mcky if: ${{ steps.ref_from_sha.outputs.pr_number }}