2020-07-21 15:33:05 +00:00
|
|
|
// Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/main/src/context.ts
|
2019-07-29 17:09:32 +00:00
|
|
|
import {WebhookPayload} from './interfaces'
|
2019-08-30 17:02:45 +00:00
|
|
|
import {readFileSync, existsSync} from 'fs'
|
|
|
|
import {EOL} from 'os'
|
2019-07-29 17:09:32 +00:00
|
|
|
|
|
|
|
export class Context {
|
|
|
|
/**
|
|
|
|
* Webhook payload object that triggered the workflow
|
|
|
|
*/
|
|
|
|
payload: WebhookPayload
|
|
|
|
|
|
|
|
eventName: string
|
|
|
|
sha: string
|
|
|
|
ref: string
|
|
|
|
workflow: string
|
|
|
|
action: string
|
|
|
|
actor: string
|
2020-06-26 03:25:13 +00:00
|
|
|
job: string
|
2023-11-28 14:38:41 +00:00
|
|
|
runAttempt: number
|
2020-06-26 03:25:13 +00:00
|
|
|
runNumber: number
|
|
|
|
runId: number
|
2021-05-07 18:05:08 +00:00
|
|
|
apiUrl: string
|
|
|
|
serverUrl: string
|
|
|
|
graphqlUrl: string
|
2019-07-29 17:09:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydrate the context from the environment
|
|
|
|
*/
|
|
|
|
constructor() {
|
2019-08-30 17:02:45 +00:00
|
|
|
this.payload = {}
|
|
|
|
if (process.env.GITHUB_EVENT_PATH) {
|
|
|
|
if (existsSync(process.env.GITHUB_EVENT_PATH)) {
|
|
|
|
this.payload = JSON.parse(
|
|
|
|
readFileSync(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'})
|
|
|
|
)
|
|
|
|
} else {
|
2019-12-06 12:52:04 +00:00
|
|
|
const path = process.env.GITHUB_EVENT_PATH
|
|
|
|
process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${EOL}`)
|
2019-08-30 17:02:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 17:09:32 +00:00
|
|
|
this.eventName = process.env.GITHUB_EVENT_NAME as string
|
|
|
|
this.sha = process.env.GITHUB_SHA as string
|
|
|
|
this.ref = process.env.GITHUB_REF as string
|
|
|
|
this.workflow = process.env.GITHUB_WORKFLOW as string
|
|
|
|
this.action = process.env.GITHUB_ACTION as string
|
|
|
|
this.actor = process.env.GITHUB_ACTOR as string
|
2020-06-26 03:25:13 +00:00
|
|
|
this.job = process.env.GITHUB_JOB as string
|
2023-11-28 14:38:41 +00:00
|
|
|
this.runAttempt = parseInt(process.env.GITHUB_RUN_ATTEMPT as string, 10)
|
2020-06-26 03:25:13 +00:00
|
|
|
this.runNumber = parseInt(process.env.GITHUB_RUN_NUMBER as string, 10)
|
|
|
|
this.runId = parseInt(process.env.GITHUB_RUN_ID as string, 10)
|
2021-05-07 18:05:08 +00:00
|
|
|
this.apiUrl = process.env.GITHUB_API_URL ?? `https://api.github.com`
|
|
|
|
this.serverUrl = process.env.GITHUB_SERVER_URL ?? `https://github.com`
|
|
|
|
this.graphqlUrl =
|
|
|
|
process.env.GITHUB_GRAPHQL_URL ?? `https://api.github.com/graphql`
|
2019-07-29 17:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get issue(): {owner: string; repo: string; number: number} {
|
|
|
|
const payload = this.payload
|
|
|
|
|
|
|
|
return {
|
|
|
|
...this.repo,
|
2020-01-21 15:44:38 +00:00
|
|
|
number: (payload.issue || payload.pull_request || payload).number
|
2019-07-29 17:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get repo(): {owner: string; repo: string} {
|
|
|
|
if (process.env.GITHUB_REPOSITORY) {
|
|
|
|
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
|
|
|
|
return {owner, repo}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.payload.repository) {
|
|
|
|
return {
|
|
|
|
owner: this.payload.repository.owner.login,
|
|
|
|
repo: this.payload.repository.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
"context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|