Skip to content

Commit

Permalink
feat: add PR support (actions#49)
Browse files Browse the repository at this point in the history
* 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
EndBug committed Jul 31, 2020
1 parent d5f44e7 commit 27a89ad
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 34 deletions.
12 changes: 3 additions & 9 deletions lib/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 3 additions & 9 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 45 additions & 13 deletions src/main.ts
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
}

0 comments on commit 27a89ad

Please sign in to comment.