UNPKG

zephyr-agent

Version:
104 lines (103 loc) • 3.63 kB
import "node:module"; import { readFile } from "node:fs/promises"; import { getEmails, stringifyId } from "./utils.mjs"; const gitHubCiIdentityProvider = { provider: 'github', detect: isGitHubActions, infer: inferGitHubIdentity }; function isGitHubActions(env) { return 'true' === env['GITHUB_ACTIONS']; } async function inferGitHubIdentity(env) { const event = await readGitHubEventPayload(env); const eventEmails = getGitHubEventEmails(event, env); const noreplyEmail = getGitHubNoreplyEmail(env, event); const emails = getEmails([ ...eventEmails, noreplyEmail ]); const providerSubject = getGitHubActorId(env, event); const username = getGitHubActor(env, event); const providerActorType = getGitHubActorType(env, event); if (eventEmails.length > 0 || providerSubject) return { provider: 'github', email: emails[0], emails, issuer: getGitHubIssuer(env), providerSubject, username, ...providerActorType && { providerActorType }, source: eventEmails.length > 0 ? 'event' : 'noreply' }; if (noreplyEmail) return { provider: 'github', email: noreplyEmail, emails: [ noreplyEmail ], issuer: getGitHubIssuer(env), providerSubject, username, ...providerActorType && { providerActorType }, source: 'noreply' }; } async function readGitHubEventPayload(env) { const eventPath = env['GITHUB_EVENT_PATH']?.trim(); if (!eventPath) return; try { return JSON.parse(await readFile(eventPath, 'utf8')); } catch { return; } } function getGitHubEventEmails(event, env) { if (!event) return []; const matchingCommit = getGitHubMatchingCommit(event, env); return getEmails([ matchingCommit?.author?.email, matchingCommit?.committer?.email, event.head_commit?.author?.email, event.head_commit?.committer?.email, event.pusher?.email ]); } function getGitHubMatchingCommit(event, env) { const sha = env['GITHUB_SHA']?.trim(); if (!sha) return; if (event.head_commit?.id === sha) return event.head_commit; return event.commits?.find((commit)=>commit.id === sha); } function getGitHubNoreplyEmail(env, event) { const actor = getGitHubActor(env, event); const actorId = getGitHubActorId(env, event); if (!actor) return; return actorId ? `${actorId}+${actor}@users.noreply.github.com` : `${actor}@users.noreply.github.com`; } function getGitHubActor(env, event) { return env['GITHUB_ACTOR']?.trim() || event?.sender?.login?.trim(); } function getGitHubActorId(env, event) { return env['GITHUB_ACTOR_ID']?.trim() || stringifyId(event?.sender?.id); } function getGitHubActorType(env, event) { const senderType = event?.sender?.type?.trim().toLowerCase(); if ('bot' !== senderType && 'user' !== senderType) return; const envActorId = env['GITHUB_ACTOR_ID']?.trim(); const senderId = stringifyId(event?.sender?.id); if (envActorId && senderId) return envActorId === senderId ? senderType : void 0; const envActor = env['GITHUB_ACTOR']?.trim().toLowerCase(); const senderLogin = event?.sender?.login?.trim().toLowerCase(); if (envActor && senderLogin && envActor !== senderLogin) return; return senderType; } function getGitHubIssuer(env) { return (env['GITHUB_SERVER_URL']?.trim() || 'https://github.com').replace(/\/+$/, '').toLowerCase(); } export { gitHubCiIdentityProvider }; //# sourceMappingURL=github.mjs.map