|
| 1 | +import { existsSync, readFileSync } from 'fs' |
| 2 | +import { EOL } from 'os' |
| 3 | +import { WebhookPayload } from './interfaces.js' |
| 4 | + |
| 5 | +export class Context { |
| 6 | + /** |
| 7 | + * Webhook payload object that triggered the workflow |
| 8 | + */ |
| 9 | + payload: WebhookPayload |
| 10 | + |
| 11 | + eventName: string |
| 12 | + sha: string |
| 13 | + ref: string |
| 14 | + workflow: string |
| 15 | + action: string |
| 16 | + actor: string |
| 17 | + job: string |
| 18 | + runAttempt: number |
| 19 | + runNumber: number |
| 20 | + runId: number |
| 21 | + apiUrl: string |
| 22 | + serverUrl: string |
| 23 | + graphqlUrl: string |
| 24 | + |
| 25 | + /** |
| 26 | + * Hydrate the context from the environment |
| 27 | + */ |
| 28 | + constructor() { |
| 29 | + this.payload = {} |
| 30 | + |
| 31 | + if (process.env.GITHUB_EVENT_PATH) { |
| 32 | + console.log(process.env.GITHUB_EVENT_PATH) |
| 33 | + if (existsSync(process.env.GITHUB_EVENT_PATH)) { |
| 34 | + this.payload = JSON.parse( |
| 35 | + readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' }) |
| 36 | + ) |
| 37 | + } else { |
| 38 | + const path = process.env.GITHUB_EVENT_PATH |
| 39 | + process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${EOL}`) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + this.eventName = process.env.GITHUB_EVENT_NAME as string |
| 44 | + this.sha = process.env.GITHUB_SHA as string |
| 45 | + this.ref = process.env.GITHUB_REF as string |
| 46 | + this.workflow = process.env.GITHUB_WORKFLOW as string |
| 47 | + this.action = process.env.GITHUB_ACTION as string |
| 48 | + this.actor = process.env.GITHUB_ACTOR as string |
| 49 | + this.job = process.env.GITHUB_JOB as string |
| 50 | + this.runAttempt = parseInt(process.env.GITHUB_RUN_ATTEMPT as string, 10) |
| 51 | + this.runNumber = parseInt(process.env.GITHUB_RUN_NUMBER as string, 10) |
| 52 | + this.runId = parseInt(process.env.GITHUB_RUN_ID as string, 10) |
| 53 | + this.apiUrl = process.env.GITHUB_API_URL ?? 'https://api.github.com' |
| 54 | + this.serverUrl = process.env.GITHUB_SERVER_URL ?? 'https://github.com' |
| 55 | + this.graphqlUrl = |
| 56 | + process.env.GITHUB_GRAPHQL_URL ?? 'https://api.github.com/graphql' |
| 57 | + } |
| 58 | + |
| 59 | + get issue(): { owner: string; repo: string; number: number } { |
| 60 | + const payload = this.payload |
| 61 | + |
| 62 | + /* istanbul ignore next */ |
| 63 | + return { |
| 64 | + ...this.repo, |
| 65 | + number: (payload.issue || payload.pull_request || payload).number |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + get repo(): { owner: string; repo: string } { |
| 70 | + if (process.env.GITHUB_REPOSITORY) { |
| 71 | + const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/') |
| 72 | + return { owner, repo } |
| 73 | + } |
| 74 | + |
| 75 | + /* istanbul ignore next */ |
| 76 | + if (this.payload.repository) { |
| 77 | + return { |
| 78 | + owner: this.payload.repository.owner.login, |
| 79 | + repo: this.payload.repository.name |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /* istanbul ignore next */ |
| 84 | + throw new Error( |
| 85 | + "context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'" |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
0 commit comments