Skip to content

feat!: use github event payload and API to list commits #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ jobs:
DOCKER_REGISTRY_URL: registry.hub.docker.com
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: '16.5.0'
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: sed -i -E "s/(docker:.+)/Dockerfile/" ./action.yml
- run: echo -n '' > .dockerignore
- uses: actions/setup-node@v3
Expand All @@ -33,8 +31,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: '16.5.0'
Expand Down
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v5
```

Alternatively, you can run on other event types such as `on: [push]`. In that case the action will lint the push event's commit(s) instead of linting commits from a pull request. You can also combine `push` and `pull_request` together in the same workflow.

**Note**: It's necessary that you specify the `fetch-depth` argument to `actions/checkout@v2` step. By default they fetch only latest commit of the branch, but we need more commits since we validate a range of commit messages.

## Inputs

You can supply these inputs to the `wagoid/commitlint-github-action@v5` step.
Expand All @@ -38,14 +34,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 Expand Up @@ -147,8 +135,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '14'
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
69 changes: 17 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,66 +22,32 @@ 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
const { data: commits } = await octokit.rest.pulls.listCommits({
owner,
repo,
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 @@ -124,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 @@ -163,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