Skip to content

Commit

Permalink
Mark github actions as cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Dec 3, 2024
1 parent 5b6d0eb commit 283bf67
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
70 changes: 70 additions & 0 deletions .github/actions/ref_from_sha/index.cjs
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion .github/actions/ref_from_sha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/post_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
id: ref_from_sha
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
entrypoint: ./index.cjs

- uses: mcky/[email protected]
if: ${{ steps.ref_from_sha.outputs.pr_number }}
Expand Down

0 comments on commit 283bf67

Please sign in to comment.