Skip to content

Commit 23cd801

Browse files
committed
feat: add ability to run commitlint on events that are not pull requests
For any event that is not of type `pull_request`, we consider there will be a `GITHBU_SHA` defined and run lint against only this specific commit sha. This supports not only events of type `pull` but also other events that provide us a `GITHBU_SHA`, like `check_suite`.
1 parent 8bf6195 commit 23cd801

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed

.github/workflows/test.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Test
2+
on: [push]
3+
4+
jobs:
5+
commitlint:
6+
runs-on: ubuntu-latest
7+
env:
8+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9+
steps:
10+
- uses: actions/checkout@v1
11+
- uses: ./

package-lock.json

+29-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"homepage": "https://github.com/wagoid/commitlint-github-action",
1717
"dependencies": {
1818
"@actions/core": "1.1.1",
19+
"@actions/exec": "1.0.1",
1920
"@actions/github": "1.1.0",
2021
"@commitlint/config-angular": "8.2.0",
2122
"@commitlint/config-conventional": "7.6.0",
@@ -24,9 +25,9 @@
2425
"@commitlint/format": "8.2.0",
2526
"@commitlint/lint": "8.2.0",
2627
"@commitlint/load": "8.2.0",
27-
"@commitlint/read": "8.2.0",
2828
"commitlint-config-jira": "1.0.9",
29-
"conventional-changelog-lint-config-canonical": "1.0.0"
29+
"conventional-changelog-lint-config-canonical": "1.0.0",
30+
"git-raw-commits": "2.0.2"
3031
},
3132
"devDependencies": {
3233
"conventional-changelog-cli": "2.0.23",

run.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
const { existsSync, readFileSync } = require('fs')
1+
const { existsSync } = require('fs')
22
const { resolve } = require('path')
33
const core = require('@actions/core')
44
const github = require('@actions/github')
5-
const read = require('@commitlint/read')
5+
const exec = require('@actions/exec')
66
const lint = require('@commitlint/lint')
77
const { format } = require('@commitlint/format')
88
const load = require('@commitlint/load')
9+
const gitRawCommits = require('git-raw-commits')
910

10-
const githubToken = process.env.GITHUB_TOKEN
11+
const pullRequestEvent = 'pull_request'
12+
13+
const { GITHUB_TOKEN, GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
1114

1215
const configPath = resolve(
1316
process.env.GITHUB_WORKSPACE,
1417
core.getInput('configFile'),
1518
)
1619

1720
const getRangeFromPullRequest = async () => {
18-
const octokit = new github.GitHub(githubToken)
21+
if (GITHUB_EVENT_NAME !== pullRequestEvent) return [null, GITHUB_SHA]
22+
23+
const octokit = new github.GitHub(GITHUB_TOKEN)
1924
const { owner, repo, number } = github.context.issue
2025
const { data: commits } = await octokit.pulls.listCommits({
2126
owner,
@@ -29,8 +34,30 @@ const getRangeFromPullRequest = async () => {
2934
return [from, to]
3035
}
3136

37+
function getHistoryCommits(from, to) {
38+
const options = {
39+
from,
40+
to,
41+
}
42+
43+
if (!from) {
44+
options.maxCount = 1
45+
}
46+
47+
return new Promise((resolve, reject) => {
48+
const data = []
49+
50+
gitRawCommits(options)
51+
.on('data', chunk => data.push(chunk.toString('utf-8')))
52+
.on('error', reject)
53+
.on('end', () => {
54+
resolve(data)
55+
})
56+
})
57+
}
58+
3259
const showLintResults = async ([from, to]) => {
33-
const commits = await read({ from, to })
60+
const commits = await getHistoryCommits(from, to)
3461
const config = existsSync(configPath)
3562
? await load({}, { file: configPath })
3663
: {}
@@ -46,18 +73,17 @@ const showLintResults = async ([from, to]) => {
4673
},
4774
)
4875

49-
if (formattedResults.length) {
50-
process.stderr.write(formattedResults)
51-
process.exit(1)
76+
if (formattedResults) {
77+
core.setFailed(
78+
`You have commit messages with errors\n\n${formattedResults}`,
79+
)
5280
} else {
5381
console.log('Lint free! 🎉')
5482
}
5583
}
5684

5785
const exitWithMessage = message => error => {
58-
console.log(message)
59-
console.error(error)
60-
process.exit(1)
86+
core.setFailed(`${message}\n${error}`)
6187
}
6288

6389
const main = () =>

0 commit comments

Comments
 (0)