forked from actions/setup-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add PR support * add debug steps * fix: use console * fix: fix buil error * [auto] build: update compiled version * fix: better branch handling * [auto] build: update compiled version * chore: remove debug steps * fix: fetch author using GitHub API when in PR * [auto] build: update compiled version * feat: extend API functionality to non-PR runs * [auto] build: update compiled version
- Loading branch information
Showing
6 changed files
with
86 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,68 @@ | ||
import { info, setFailed, getInput, warning } from '@actions/core' | ||
import { execFile } from 'child_process' | ||
import { join } from 'path' | ||
import path from 'path' | ||
import axios from 'axios' | ||
|
||
try { | ||
checkInputs() | ||
const child = execFile(join(__dirname, 'entrypoint.sh'), [], { shell: true }) | ||
checkInputs().then(() => { | ||
const child = execFile(path.join(__dirname, 'entrypoint.sh'), [], { shell: true }) | ||
child.stdout?.pipe(process.stdout) | ||
child.stderr?.pipe(process.stderr) | ||
} catch (err) { | ||
}).catch(err => { | ||
console.error(err) | ||
setFailed(err instanceof Error ? err.message : err) | ||
} | ||
}) | ||
|
||
async function checkInputs() { | ||
const eventPath = process.env.GITHUB_EVENT_PATH, | ||
event = eventPath && require(eventPath), | ||
isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'), | ||
sha = (event?.pull_request?.head?.sha || process.env.GITHUB_SHA) as string, | ||
defaultRef = isPR | ||
? event?.pull_request?.head?.ref as string | ||
: process.env.GITHUB_REF?.substring(11) | ||
|
||
const actualRef = setDefault('ref', defaultRef || '') | ||
|
||
function checkInputs() { | ||
const eventPath = process.env.GITHUB_EVENT_PATH | ||
const author = eventPath && require(eventPath)?.head_commit?.author | ||
let author = event?.head_commit?.author | ||
if (sha && !author) { | ||
info('Unable to get commit from workflow event: trying with the GitHub API...') | ||
|
||
// https://docs.github.com/en/rest/reference/repos#get-a-commit--code-samples | ||
const url = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/commits/${sha}`, | ||
headers = process.env.GITHUB_TOKEN ? { | ||
Authorization: `Bearer ${process.env.GITHUB_TOKEN}` | ||
} : undefined, | ||
commit = (await axios.get(url, { headers })).data | ||
|
||
author = commit?.commit?.author | ||
} | ||
|
||
if (author) { | ||
setDefault('author_name', author.name) | ||
setDefault('author_email', author.email) | ||
} else { | ||
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'}.`) | ||
} | ||
|
||
if (!getInput('author_name') || !getInput('author_email')) { | ||
const reason = !eventPath | ||
? 'event path' | ||
: isPR | ||
? sha | ||
? 'fetch commit' | ||
: 'find commit sha' | ||
: !event?.head_commit | ||
? 'find commit' | ||
: 'find commit author' | ||
warning(`Unable to fetch author info: couldn't ${reason}.`) | ||
setDefault('author_name', 'Add & Commit Action') | ||
setDefault('author_email', 'actions@github.com') | ||
} | ||
|
||
setDefault('ref', process.env.GITHUB_REF?.substring(11) || '') | ||
|
||
info(`Using '${getInput('author_name')} <${getInput('author_email')}>' as author.`) | ||
if (isPR) info(`Running for a PR, the action will use '${actualRef}' as ref.`) | ||
} | ||
|
||
function setDefault(input: string, value: string) { | ||
const key = 'INPUT_' + input.toUpperCase() | ||
if (!process.env[key]) process.env[key] = value | ||
return process.env[key] as string | ||
} |