CI / policy (push) Successful in 48s
CI / test (push) Canceled after 0s
CI / benchmark (push) Canceled after 0s
CI / build (push) Canceled after 0s
CI / security (push) Canceled after 0s
CI / images (push) Canceled after 0s
CI / validate (push) Canceled after 1m8s
CI / policy (pull_request) Successful in 9s
CI / validate (pull_request) Successful in 1m6s
CI / test (pull_request) Successful in 3m30s
CI / benchmark (pull_request) Failing after 5m39s
CI / build (pull_request) Successful in 1m5s
CI / security (pull_request) Successful in 1m27s
CI / required (push) Failing after 1s
CI / images (pull_request) Failing after 18s
CI / required (pull_request) Failing after 1s
36 lines
1.5 KiB
JavaScript
36 lines
1.5 KiB
JavaScript
import { readFileSync } from 'node:fs';
|
|
|
|
// Gitea exposes its event payload through the GitHub-compatible runner variables.
|
|
export function readCiContext() {
|
|
const env = process.env;
|
|
const eventName = env.GITHUB_EVENT_NAME;
|
|
if (!['push', 'pull_request', 'workflow_dispatch'].includes(eventName)) {
|
|
throw new Error('Only push, pull_request and workflow_dispatch events are supported.');
|
|
}
|
|
const event = JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8'));
|
|
const commit = env.GITHUB_SHA;
|
|
if (!/^[a-f0-9]{40}$/.test(commit ?? '')) throw new Error('A commit SHA is required.');
|
|
const pullRequest = eventName === 'pull_request' ? event.pull_request : undefined;
|
|
if (eventName === 'pull_request' && pullRequest?.head?.sha !== commit) {
|
|
throw new Error('CI must validate the pull request head commit.');
|
|
}
|
|
const tag = env.GITHUB_REF?.startsWith('refs/tags/') ? env.GITHUB_REF.slice(10) : '';
|
|
const branch = pullRequest
|
|
? pullRequest.head.ref
|
|
: env.GITHUB_REF?.startsWith('refs/heads/')
|
|
? env.GITHUB_REF.slice(11)
|
|
: '';
|
|
if (!tag && !branch) throw new Error('A branch or tag ref is required.');
|
|
const slug = branch
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]/g, '-')
|
|
.slice(0, 63)
|
|
.replace(/^-+|-+$/g, '');
|
|
const publish =
|
|
eventName === 'push' ||
|
|
(eventName === 'pull_request' &&
|
|
Boolean(env.GITHUB_REPOSITORY) &&
|
|
pullRequest.head.repo?.full_name === env.GITHUB_REPOSITORY);
|
|
return { commit, tag, branch, slug, publish, pullRequest, before: event.before };
|
|
}
|