Skip to content

Commit 27a89ad

Browse files
authored
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
1 parent d5f44e7 commit 27a89ad

File tree

6 files changed

+86
-34
lines changed

6 files changed

+86
-34
lines changed

lib/entrypoint.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ if ! git diff --cached --quiet --exit-code; then
5252

5353
git fetch
5454

55-
# Verify if the branch needs to be created
56-
if ! git rev-parse --verify --quiet "$INPUT_REF"; then
57-
echo "Creating branch..."
58-
git branch "$INPUT_REF"
59-
fi
60-
61-
# Switch to branch from current workflow run
62-
echo "Switching branch..."
63-
git checkout "$INPUT_REF"
55+
# Switch branch (create a new one if it doesn't exist)
56+
echo "Switching/creating branch..."
57+
git checkout "$INPUT_REF" 2>/dev/null || git checkout -b "$INPUT_REF"
6458

6559
echo "Pulling from remote..."
6660
git fetch && git pull

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
},
2727
"homepage": "https://github.com/EndBug/add-and-commit#readme",
2828
"dependencies": {
29-
"@actions/core": "^1.1.3"
29+
"@actions/core": "^1.1.3",
30+
"axios": "^0.19.2"
3031
},
3132
"devDependencies": {
3233
"@types/node": "^12.7.12",

src/entrypoint.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ if ! git diff --cached --quiet --exit-code; then
5252

5353
git fetch
5454

55-
# Verify if the branch needs to be created
56-
if ! git rev-parse --verify --quiet "$INPUT_REF"; then
57-
echo "Creating branch..."
58-
git branch "$INPUT_REF"
59-
fi
60-
61-
# Switch to branch from current workflow run
62-
echo "Switching branch..."
63-
git checkout "$INPUT_REF"
55+
# Switch branch (create a new one if it doesn't exist)
56+
echo "Switching/creating branch..."
57+
git checkout "$INPUT_REF" 2>/dev/null || git checkout -b "$INPUT_REF"
6458

6559
echo "Pulling from remote..."
6660
git fetch && git pull

src/main.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
11
import { info, setFailed, getInput, warning } from '@actions/core'
22
import { execFile } from 'child_process'
3-
import { join } from 'path'
3+
import path from 'path'
4+
import axios from 'axios'
45

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 })
88
child.stdout?.pipe(process.stdout)
99
child.stderr?.pipe(process.stderr)
10-
} catch (err) {
10+
}).catch(err => {
1111
console.error(err)
1212
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 || '')
1425

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+
}
1839

1940
if (author) {
2041
setDefault('author_name', author.name)
2142
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}.`)
2456
setDefault('author_name', 'Add & Commit Action')
2557
setDefault('author_email', '[email protected]')
2658
}
2759

28-
setDefault('ref', process.env.GITHUB_REF?.substring(11) || '')
29-
3060
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.`)
3162
}
3263

3364
function setDefault(input: string, value: string) {
3465
const key = 'INPUT_' + input.toUpperCase()
3566
if (!process.env[key]) process.env[key] = value
67+
return process.env[key] as string
3668
}

0 commit comments

Comments
 (0)