feat: add PR support (#49)
* 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
This commit is contained in:
@@ -52,15 +52,9 @@ if ! git diff --cached --quiet --exit-code; then
|
||||
|
||||
git fetch
|
||||
|
||||
# Verify if the branch needs to be created
|
||||
if ! git rev-parse --verify --quiet "$INPUT_REF"; then
|
||||
echo "Creating branch..."
|
||||
git branch "$INPUT_REF"
|
||||
fi
|
||||
|
||||
# Switch to branch from current workflow run
|
||||
echo "Switching branch..."
|
||||
git checkout "$INPUT_REF"
|
||||
# Switch branch (create a new one if it doesn't exist)
|
||||
echo "Switching/creating branch..."
|
||||
git checkout "$INPUT_REF" 2>/dev/null || git checkout -b "$INPUT_REF"
|
||||
|
||||
echo "Pulling from remote..."
|
||||
git fetch && git pull
|
||||
|
||||
File diff suppressed because one or more lines are too long
33
package-lock.json
generated
33
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "add-and-commit",
|
||||
"version": "4.2.0",
|
||||
"version": "4.3.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -342,6 +342,14 @@
|
||||
"integrity": "sha512-3YDiu347mtVtjpyV3u5kVqQLP242c06zwDOgpeRnybmXlYYsLbtTrUBUm8i8srONt+FWobl5aibnU1030PeeuA==",
|
||||
"dev": true
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.19.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
|
||||
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
|
||||
"requires": {
|
||||
"follow-redirects": "1.5.10"
|
||||
}
|
||||
},
|
||||
"balanced-match": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
||||
@@ -798,6 +806,29 @@
|
||||
"integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==",
|
||||
"dev": true
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.5.10",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
|
||||
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
|
||||
"requires": {
|
||||
"debug": "=3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
}
|
||||
}
|
||||
},
|
||||
"forever-agent": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
|
||||
|
||||
@@ -26,7 +26,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/EndBug/add-and-commit#readme",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.1.3"
|
||||
"@actions/core": "^1.1.3",
|
||||
"axios": "^0.19.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^12.7.12",
|
||||
|
||||
@@ -52,15 +52,9 @@ if ! git diff --cached --quiet --exit-code; then
|
||||
|
||||
git fetch
|
||||
|
||||
# Verify if the branch needs to be created
|
||||
if ! git rev-parse --verify --quiet "$INPUT_REF"; then
|
||||
echo "Creating branch..."
|
||||
git branch "$INPUT_REF"
|
||||
fi
|
||||
|
||||
# Switch to branch from current workflow run
|
||||
echo "Switching branch..."
|
||||
git checkout "$INPUT_REF"
|
||||
# Switch branch (create a new one if it doesn't exist)
|
||||
echo "Switching/creating branch..."
|
||||
git checkout "$INPUT_REF" 2>/dev/null || git checkout -b "$INPUT_REF"
|
||||
|
||||
echo "Pulling from remote..."
|
||||
git fetch && git pull
|
||||
|
||||
58
src/main.ts
58
src/main.ts
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
function checkInputs() {
|
||||
const eventPath = process.env.GITHUB_EVENT_PATH
|
||||
const author = eventPath && require(eventPath)?.head_commit?.author
|
||||
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 || '')
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user