-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathaction-context.ts
29 lines (26 loc) · 969 Bytes
/
action-context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { readFileSync } from 'fs'
// Parses the action event payload sets repo and owner to an object from runner environment
export function getActionContext() {
if (!process.env.GITHUB_EVENT_PATH) {
if (!process.env.CI) {
console.warn(
`
If you're trying to run this locally, to simulate this create a file like this:
echo '{"repository": {"owner": {"login": "github"}, "name": "docs-internal"}}' > /tmp/event-path.json
export GITHUB_EVENT_PATH=/tmp/event-path.json
`.trim(),
)
}
throw new Error('process.env.GITHUB_EVENT_PATH is not set')
}
const context = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
if (context.repository) {
context.owner = context.repository.owner.login
context.repo = context.repository.name
} else {
const [owner, repo] = process.env.GITHUB_REPOSITORY?.split('/') || []
context.owner = owner
context.repo = repo
}
return context
}