|
1 | 1 | import { info, setFailed, getInput, warning } from '@actions/core'
|
2 | 2 | import { execFile } from 'child_process'
|
3 |
| -import { join } from 'path' |
| 3 | +import path from 'path' |
| 4 | +import axios from 'axios' |
4 | 5 |
|
5 |
| -try { |
6 |
| - checkInputs() |
7 |
| - const child = execFile(join(__dirname, 'entrypoint.sh'), [], { shell: true }) |
| 6 | +checkInputs().then(() => { |
| 7 | + const child = execFile(path.join(__dirname, 'entrypoint.sh'), [], { shell: true }) |
8 | 8 | child.stdout?.pipe(process.stdout)
|
9 | 9 | child.stderr?.pipe(process.stderr)
|
10 |
| -} catch (err) { |
| 10 | +}).catch(err => { |
11 | 11 | console.error(err)
|
12 | 12 | setFailed(err instanceof Error ? err.message : err)
|
13 |
| -} |
| 13 | +}) |
| 14 | + |
| 15 | +async function checkInputs() { |
| 16 | + const eventPath = process.env.GITHUB_EVENT_PATH, |
| 17 | + event = eventPath && require(eventPath), |
| 18 | + isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'), |
| 19 | + sha = (event?.pull_request?.head?.sha || process.env.GITHUB_SHA) as string, |
| 20 | + defaultRef = isPR |
| 21 | + ? event?.pull_request?.head?.ref as string |
| 22 | + : process.env.GITHUB_REF?.substring(11) |
| 23 | + |
| 24 | + const actualRef = setDefault('ref', defaultRef || '') |
14 | 25 |
|
15 |
| -function checkInputs() { |
16 |
| - const eventPath = process.env.GITHUB_EVENT_PATH |
17 |
| - const author = eventPath && require(eventPath)?.head_commit?.author |
| 26 | + let author = event?.head_commit?.author |
| 27 | + if (sha && !author) { |
| 28 | + info('Unable to get commit from workflow event: trying with the GitHub API...') |
| 29 | + |
| 30 | + // https://docs.github.com/en/rest/reference/repos#get-a-commit--code-samples |
| 31 | + const url = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/commits/${sha}`, |
| 32 | + headers = process.env.GITHUB_TOKEN ? { |
| 33 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}` |
| 34 | + } : undefined, |
| 35 | + commit = (await axios.get(url, { headers })).data |
| 36 | + |
| 37 | + author = commit?.commit?.author |
| 38 | + } |
18 | 39 |
|
19 | 40 | if (author) {
|
20 | 41 | setDefault('author_name', author.name)
|
21 | 42 | setDefault('author_email', author.email)
|
22 |
| - } else { |
23 |
| - if (!getInput('author_name') || !getInput('author_email')) warning(`Unable to fetch author info: couldn't find ${!eventPath ? 'event path' : !require(eventPath)?.head_commit ? 'commit' : 'commit author'}.`) |
| 43 | + } |
| 44 | + |
| 45 | + if (!getInput('author_name') || !getInput('author_email')) { |
| 46 | + const reason = !eventPath |
| 47 | + ? 'event path' |
| 48 | + : isPR |
| 49 | + ? sha |
| 50 | + ? 'fetch commit' |
| 51 | + : 'find commit sha' |
| 52 | + : !event?.head_commit |
| 53 | + ? 'find commit' |
| 54 | + : 'find commit author' |
| 55 | + warning(`Unable to fetch author info: couldn't ${reason}.`) |
24 | 56 | setDefault('author_name', 'Add & Commit Action')
|
25 | 57 | setDefault('author_email', '[email protected]')
|
26 | 58 | }
|
27 | 59 |
|
28 |
| - setDefault('ref', process.env.GITHUB_REF?.substring(11) || '') |
29 |
| - |
30 | 60 | info(`Using '${getInput('author_name')} <${getInput('author_email')}>' as author.`)
|
| 61 | + if (isPR) info(`Running for a PR, the action will use '${actualRef}' as ref.`) |
31 | 62 | }
|
32 | 63 |
|
33 | 64 | function setDefault(input: string, value: string) {
|
34 | 65 | const key = 'INPUT_' + input.toUpperCase()
|
35 | 66 | if (!process.env[key]) process.env[key] = value
|
| 67 | + return process.env[key] as string |
36 | 68 | }
|
0 commit comments