diff --git a/packages/core/src/ci-environment/services/github-actions.ts b/packages/core/src/ci-environment/services/github-actions.ts index 9827514..d878417 100644 --- a/packages/core/src/ci-environment/services/github-actions.ts +++ b/packages/core/src/ci-environment/services/github-actions.ts @@ -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; @@ -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;