Skip to content

Commit

Permalink
feat!: use github event payload and API to list commits resolves #456
Browse files Browse the repository at this point in the history
That way we guarantee we're linting the same commits that appear on github.

BREAKING CHANGE: "firstParent" option has been removed
  • Loading branch information
wagoid committed Jul 22, 2023
1 parent 2be323b commit a31f4b5
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 334 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ If the config file doesn't exist, [config-conventional](https://github.com/conve

Details on the configuration file can be found on [the commitlint website](https://commitlint.js.org/#/reference-configuration).

### `firstParent`

When set to true, we follow only the first parent commit when seeing a merge commit.

This helps to ignore errors in commits that were already present in your default branch (e.g. `master`) before adding conventional commit checks. More info in [git-log docs](https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent).

Default: `true`

### `failOnWarnings`

Whether you want to fail on warnings or not.
Expand Down
16 changes: 5 additions & 11 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,26 @@ description: Lints Pull Request commit messages with commitlint
author: Wagner Santos
inputs:
configFile:
description: Commitlint config file. If the file doesn't exist, config-conventional settings will be
description:
Commitlint config file. If the file doesn't exist, config-conventional settings will be
loaded as a fallback.
default: ./commitlint.config.js
required: false
firstParent:
description: >
When set to true, we follow only the first parent commit when seeing a merge commit.
More info in git-log docs
https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent
default: "true"
required: false
failOnWarnings:
description: Whether you want to fail on warnings or not
default: "false"
default: 'false'
required: false
failOnErrors:
description: Whether you want to fail on errors or not
default: "true"
default: 'true'
required: true
helpURL:
description: Link to a page explaining your commit message convention
default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
required: false
commitDepth:
description: When set to a valid Integer value - X, considers only the latest X number of commits.
default: ""
default: ''
required: false
token:
description: >
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"conventional-changelog-conventionalcommits": "^4.6.3",
"conventional-changelog-lint-config-canonical": "^1.0.0",
"dargs": "^8.1.0",
"execa": "^5.1.1",
"lerna": "^5.1.4"
},
"devDependencies": {
Expand Down
68 changes: 16 additions & 52 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { context as eventContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint'
import { format } from '@commitlint/format'
import load from '@commitlint/load'
import gitCommits from './gitCommits'
import generateOutputs from './generateOutputs'

const pullRequestEvent = 'pull_request'
const pullRequestTargetEvent = 'pull_request_target'
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]

const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
const { GITHUB_EVENT_NAME } = process.env

const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))

Expand All @@ -23,34 +22,18 @@ const getCommitDepth = () => {
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
}

const pushEventHasOnlyOneCommit = (from) => {
const gitEmptySha = '0000000000000000000000000000000000000000'
const getPushEventCommits = () => {
const mappedCommits = eventContext.payload.commits.map((commit) => ({
message: commit.message,
hash: commit.id,
}))

return from === gitEmptySha
return mappedCommits
}

const getRangeForPushEvent = () => {
let from = eventContext.payload.before
const to = GITHUB_SHA

if (eventContext.payload.forced) {
// When a commit is forced, "before" field from the push event data may point to a commit that doesn't exist
console.warn(
'Commit was forced, checking only the latest commit from push instead of a range of commit messages',
)
from = null
}

if (pushEventHasOnlyOneCommit(from)) {
from = null
}

return [from, to]
}

const getRangeForEvent = async () => {
const getEventCommits = async () => {
if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
return getRangeForPushEvent()
return getPushEventCommits()

const octokit = getOctokit(getInput('token'))
const { owner, repo, number } = eventContext.issue
Expand All @@ -60,30 +43,11 @@ const getRangeForEvent = async () => {
pull_number: number,
per_page: 100,
})
const commitShas = commits.map((commit) => commit.sha)
const [from] = commitShas
const to = commitShas[commitShas.length - 1]
// Git revision range doesn't include the "from" field in "git log", so for "from" we use the parent commit of PR's first commit
const fromParent = `${from}^1`

return [fromParent, to]
}

function getHistoryCommits(from, to) {
const options = {
from,
to,
}

if (getInput('firstParent') === 'true') {
options.firstParent = true
}

if (!from) {
options.maxCount = 1
}

return gitCommits(options)
return commits.map((commit) => ({
message: commit.commit.message,
hash: commit.sha,
}))
}

function getOptsFromConfig(config) {
Expand Down Expand Up @@ -125,8 +89,8 @@ const handleOnlyWarnings = (formattedResults) => {
}
}

const showLintResults = async ([from, to]) => {
let commits = await getHistoryCommits(from, to)
const showLintResults = async (eventCommits) => {
let commits = eventCommits
const commitDepth = getCommitDepth()
if (commitDepth) {
commits = commits?.slice(0, commitDepth)
Expand Down Expand Up @@ -164,7 +128,7 @@ const exitWithMessage = (message) => (error) => {
}

const commitLinterAction = () =>
getRangeForEvent()
getEventCommits()
.catch(
exitWithMessage("error trying to get list of pull request's commits"),
)
Expand Down
Loading

0 comments on commit a31f4b5

Please sign in to comment.