Skip to content

Commit

Permalink
fix(github-actions): better commit & branch detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jan 17, 2023
1 parent 7adf2cb commit 2131752
Showing 1 changed file with 27 additions and 62 deletions.
89 changes: 27 additions & 62 deletions packages/core/src/ci-environment/services/github-actions.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
import { execSync } from "child_process";
import { existsSync, readFileSync } from "node:fs";
import type { Service, Context } from "../types";

const getSha = ({ env }: Context) => {
const isPr =
env.GITHUB_EVENT_NAME === "pull_request" ||
env.GITHUB_EVENT_NAME === "pull_request_target";

if (isPr) {
const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/;
const mergeCommitMessage = execSync("git show --no-patch --format=%P")
.toString()
.trim();
// console.log(
// `Handling PR with parent hash(es) '${mergeCommitMessage}' of current commit.`
// );
if (mergeCommitRegex.exec(mergeCommitMessage)) {
const mergeCommit = mergeCommitMessage.split(" ")[1];
// console.log(
// `Fixing merge commit SHA ${process.env.GITHUB_SHA} -> ${mergeCommit}`
// );
return mergeCommit;
} else if (mergeCommitMessage === "") {
console.error(
`Error: automatic detection of commit SHA failed.
Please run "actions/checkout" with "fetch-depth: 2". Example:
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
`
);
process.exit(1);
} else {
console.error(
`Commit with SHA ${process.env.GITHUB_SHA} is not a valid commit`
);
process.exit(1);
}
}

return process.env.GITHUB_SHA ?? null;
};

const getBranch = ({ env }: Context) => {
if (env.GITHUB_HEAD_REF) {
return env.GITHUB_HEAD_REF;
Expand All @@ -64,28 +20,37 @@ const getRepository = ({ env }: Context) => {
return env.GITHUB_REPOSITORY.split("/")[1];
};

const getPrNumber = ({ env }: Context) => {
const branchRegex = /refs\/pull\/(\d+)/;
const branchMatches = branchRegex.exec(env.GITHUB_REF || "");
if (branchMatches) {
return Number(branchMatches[1]);
}

return null;
interface EventPayload {
pull_request?: {
head: {
sha: string;
ref: string;
};
number: number;
};
}

const readEventPayload = ({ env }: Context): EventPayload | null => {
if (!env.GITHUB_EVENT_PATH) return null;
if (!existsSync(env.GITHUB_EVENT_PATH)) return null;
return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf-8"));
};

const service: Service = {
name: "GitHub Actions",
detect: ({ env }) => Boolean(env.GITHUB_ACTIONS),
config: ({ env }) => ({
commit: getSha({ env }),
branch: getBranch({ env }),
owner: env.GITHUB_REPOSITORY_OWNER || null,
repository: getRepository({ env }),
jobId: env.GITHUB_JOB || null,
runId: env.GITHUB_RUN_ID || null,
prNumber: getPrNumber({ env }),
}),
config: ({ env }) => {
const payload = readEventPayload({ env });
return {
commit: payload?.pull_request?.head.sha || process.env.GITHUB_SHA || null,
branch: payload?.pull_request?.head.ref || getBranch({ env }) || null,
owner: env.GITHUB_REPOSITORY_OWNER || null,
repository: getRepository({ env }),
jobId: env.GITHUB_JOB || null,
runId: env.GITHUB_RUN_ID || null,
prNumber: payload?.pull_request?.number || null,
};
},
};

export default service;

0 comments on commit 2131752

Please sign in to comment.