5
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2025-11-07 08:06:54 +00:00

fix: using compareCommits for push event commit query (#801)

* fix: fixed logic in push event API request to only get commits from before to after event

* fix: getting events from first push using push event payload
This commit is contained in:
Brian Triplett 2024-09-04 11:15:40 -04:00 committed by GitHub
parent a2bc521d74
commit 47ff1315a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 353 additions and 97 deletions

View file

@ -12,6 +12,7 @@ const pullRequestTargetEvent = 'pull_request_target'
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent] const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
const { GITHUB_EVENT_NAME } = process.env const { GITHUB_EVENT_NAME } = process.env
const FIRST_COMMIT_SHA = '0000000000000000000000000000000000000000'
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile')) const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
@ -22,13 +23,30 @@ const getCommitDepth = () => {
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0) return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
} }
const getPushEventCommits = () => { const getPushEventCommits = async () => {
const mappedCommits = eventContext.payload.commits.map((commit) => ({ const octokit = getOctokit(getInput('token'))
const { owner, repo } = eventContext.issue
const { before, after } = eventContext.payload
if (before === FIRST_COMMIT_SHA) {
return eventContext.payload.commits.map((commit) => ({
message: commit.message, message: commit.message,
hash: commit.id, hash: commit.id,
})) }))
}
return mappedCommits const { data: comparison } = await octokit.rest.repos.compareCommits({
owner,
repo,
head: after,
base: before,
per_page: 100,
})
return comparison.commits.map((commit) => ({
message: commit.commit.message,
hash: commit.sha,
}))
} }
const getPullRequestEventCommits = async () => { const getPullRequestEventCommits = async () => {

View file

@ -4,11 +4,11 @@ import { git } from '@commitlint/test'
import { jest, describe, it } from '@jest/globals' import { jest, describe, it } from '@jest/globals'
import * as td from 'testdouble' import * as td from 'testdouble'
import { import {
updatePushEnvVars,
createPushEventPayload,
createPullRequestEventPayload,
updatePullRequestEnvVars,
buildResponseCommit, buildResponseCommit,
createPullRequestEventPayload,
createPushEventPayload,
updatePullRequestEnvVars,
updatePushEnvVars,
} from './testUtils.mjs' } from './testUtils.mjs'
const resultsOutputId = 'results' const resultsOutputId = 'results'
@ -19,7 +19,8 @@ const {
const initialEnv = { ...process.env } const initialEnv = { ...process.env }
const mockListCommits = td.func('listCommits') const mockListPullCommits = td.func('listCommits')
const mockCompareCommits = td.func('compareCommits')
const mockCore = td.object(['getInput', 'setFailed', 'setOutput']) const mockCore = td.object(['getInput', 'setFailed', 'setOutput'])
@ -30,7 +31,10 @@ jest.unstable_mockModule('@actions/github', () => {
constructor() { constructor() {
this.rest = { this.rest = {
pulls: { pulls: {
listCommits: mockListCommits, listCommits: mockListPullCommits,
},
repos: {
compareCommits: mockCompareCommits,
}, },
} }
} }
@ -81,6 +85,19 @@ describe('Commit Linter action', () => {
], ],
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [buildResponseCommit('wrong-message', 'wrong message')],
},
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
await runAction() await runAction()
@ -111,6 +128,19 @@ describe('Commit Linter action', () => {
], ],
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [buildResponseCommit('wrong-message', 'wrong message')],
},
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
await runAction() await runAction()
@ -129,6 +159,20 @@ describe('Commit Linter action', () => {
], ],
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [buildResponseCommit('wrong-message', 'wrong message')],
},
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
await runAction() await runAction()
@ -140,17 +184,23 @@ describe('Commit Linter action', () => {
it('should fail for push range with wrong messages', async () => { it('should fail for push range with wrong messages', async () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit('wrong-message-1', 'wrong message 1'),
id: 'wrong-message-1', buildResponseCommit('wrong-message-2', 'wrong message 2'),
message: 'wrong message 1',
},
{
id: 'wrong-message-2',
message: 'wrong message 2',
},
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -163,17 +213,23 @@ describe('Commit Linter action', () => {
it('should pass for push range with wrong messages with failOnErrors set to false', async () => { it('should pass for push range with wrong messages with failOnErrors set to false', async () => {
td.when(mockCore.getInput('failOnErrors')).thenReturn('false') td.when(mockCore.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit('wrong-message-1', 'wrong message 1'),
id: 'wrong-message-1', buildResponseCommit('wrong-message-2', 'wrong message 2'),
message: 'wrong message 1',
},
{
id: 'wrong-message-2',
message: 'wrong message 2',
},
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -190,17 +246,23 @@ describe('Commit Linter action', () => {
it('should pass for push range with correct messages with failOnErrors set to false', async () => { it('should pass for push range with correct messages with failOnErrors set to false', async () => {
td.when(mockCore.getInput('failOnErrors')).thenReturn('false') td.when(mockCore.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit('correct-message-1', 'chore: correct message 1'),
id: 'correct-message-1', buildResponseCommit('correct-message-2', 'chore: correct message 2'),
message: 'chore: correct message 1',
},
{
id: 'correct-message-2',
message: 'chore: correct message 2',
},
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -214,17 +276,23 @@ describe('Commit Linter action', () => {
it('should pass for push range with correct messages', async () => { it('should pass for push range with correct messages', async () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit('correct-message-1', 'chore: correct message 1'),
id: 'correct-message-1', buildResponseCommit('correct-message-2', 'chore: correct message 2'),
message: 'chore: correct message 1',
},
{
id: 'correct-message-2',
message: 'chore: correct message 2',
},
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -241,12 +309,25 @@ describe('Commit Linter action', () => {
td.when(mockCore.getInput('configFile')).thenReturn( td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.yml', './commitlint.config.yml',
) )
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit(
message: 'chore(wrong): not including package scope', 'correct-message',
}, 'chore(wrong): not including package scope',
),
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -263,13 +344,25 @@ describe('Commit Linter action', () => {
td.when(mockCore.getInput('configFile')).thenReturn( td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.yml', './commitlint.config.yml',
) )
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit(
id: 'correct-message', 'correct-message',
message: 'chore(second-package): this works', 'chore(second-package): this works',
}, ),
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -285,13 +378,24 @@ describe('Commit Linter action', () => {
td.when(mockCore.getInput('configFile')).thenReturn( td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs', './commitlint.config.mjs',
) )
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit(
id: 'wrong-message', 'wrong-message',
message: 'ib-21212121212121: without jira ticket', 'ib-21212121212121: without jira ticket',
}, ),
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -325,8 +429,19 @@ describe('Commit Linter action', () => {
'./commitlint.config.mjs', './commitlint.config.mjs',
) )
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {}) await createPushEventPayload(cwd, { commits: [] }, 'bbbbb', 'aaaaa')
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
head: 'aaaaa',
base: 'bbbbb',
per_page: 100,
}),
).thenResolve({
data: { commits: [] },
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log') td.replace(console, 'log')
@ -336,6 +451,37 @@ describe('Commit Linter action', () => {
td.verify(console.log('Lint free! 🎉')) td.verify(console.log('Lint free! 🎉'))
}) })
it('should get commits from event for a first push', async () => {
const commit = {
id: '0000000000000000000000000000000000000000',
message: 'chore: correct message',
}
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(
cwd,
{ commits: [commit] },
'0000000000000000000000000000000000000000',
)
updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
head: 'aaaaa',
base: '0000000000000000000000000000000000000000',
per_page: 100,
}),
).thenResolve({
data: { commits: [buildResponseCommit(commit.id, commit.message)] },
})
td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log')
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCompareCommits(), { times: 0 })
})
describe.each(['pull_request', 'pull_request_target'])( describe.each(['pull_request', 'pull_request_target'])(
'when there are multiple commits failing in the %s event', 'when there are multiple commits failing in the %s event',
(eventName) => { (eventName) => {
@ -354,7 +500,7 @@ describe('Commit Linter action', () => {
await createPullRequestEventPayload(cwd) await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd, { eventName }) updatePullRequestEnvVars(cwd, { eventName })
td.when( td.when(
mockListCommits({ mockListPullCommits({
owner: 'wagoid', owner: 'wagoid',
repo: 'commitlint-github-action', repo: 'commitlint-github-action',
pull_number: '1', pull_number: '1',
@ -420,7 +566,7 @@ describe('Commit Linter action', () => {
await createPullRequestEventPayload(cwd) await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd) updatePullRequestEnvVars(cwd)
td.when( td.when(
mockListCommits({ mockListPullCommits({
owner: 'wagoid', owner: 'wagoid',
repo: 'commitlint-github-action', repo: 'commitlint-github-action',
pull_number: '1', pull_number: '1',
@ -457,6 +603,17 @@ describe('Commit Linter action', () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd()) cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { commits: [commit] }) await createPushEventPayload(cwd, { commits: [commit] })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
head: 'aaaaa',
base: 'bbbbb',
per_page: 100,
}),
).thenResolve({
data: { commits: [buildResponseCommit(commit.id, commit.message)] },
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log') td.replace(console, 'log')
}) })
@ -507,6 +664,25 @@ describe('Commit Linter action', () => {
await createPushEventPayload(cwd, { await createPushEventPayload(cwd, {
commits: [commitWithWarning, correctCommit], commits: [commitWithWarning, correctCommit],
}) })
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [
buildResponseCommit(
commitWithWarning.id,
commitWithWarning.message,
),
buildResponseCommit(correctCommit.id, correctCommit.message),
],
},
})
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log') td.replace(console, 'log')
@ -580,6 +756,26 @@ describe('Commit Linter action', () => {
await createPushEventPayload(cwd, { await createPushEventPayload(cwd, {
commits: [wrongCommit, commitWithWarning], commits: [wrongCommit, commitWithWarning],
}) })
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [
buildResponseCommit(wrongCommit.id, wrongCommit.message),
buildResponseCommit(
commitWithWarning.id,
commitWithWarning.message,
),
],
},
})
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log') td.replace(console, 'log')
@ -635,14 +831,24 @@ describe('Commit Linter action', () => {
describe('when commit contains required signed-off-by message', () => { describe('when commit contains required signed-off-by message', () => {
beforeEach(async () => { beforeEach(async () => {
cwd = await git.bootstrap('fixtures/signed-off-by', process.cwd()) cwd = await git.bootstrap('fixtures/signed-off-by', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [ commits: [
{ buildResponseCommit(
id: 'correct-commit', 'correct-commit',
message:
'chore: correct message\n\nsome context without leading blank line.\n\nSigned-off-by: John Doe <john.doe@example.com>', 'chore: correct message\n\nsome context without leading blank line.\n\nSigned-off-by: John Doe <john.doe@example.com>',
}, ),
], ],
},
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -660,13 +866,19 @@ describe('Commit Linter action', () => {
describe('when a different helpUrl is provided in the config', () => { describe('when a different helpUrl is provided in the config', () => {
beforeEach(async () => { beforeEach(async () => {
cwd = await git.bootstrap('fixtures/custom-help-url', process.cwd()) cwd = await git.bootstrap('fixtures/custom-help-url', process.cwd())
await createPushEventPayload(cwd, { await createPushEventPayload(cwd)
commits: [ td.when(
{ mockCompareCommits({
id: 'wrong-commit', owner: 'wagoid',
message: 'wrong message', repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [buildResponseCommit('wrong-commit', 'wrong message')],
}, },
],
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
@ -698,6 +910,22 @@ describe('Commit Linter action', () => {
], ],
}) })
updatePushEnvVars(cwd) updatePushEnvVars(cwd)
td.when(
mockCompareCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
per_page: 100,
base: 'bbbbb',
head: 'aaaaa',
}),
).thenResolve({
data: {
commits: [
buildResponseCommit('correct-commit', 'chore: correct message 2'),
buildResponseCommit(incorrectCommit.id, incorrectCommit.message),
],
},
})
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log') td.replace(console, 'log')
}) })

View file

@ -19,16 +19,26 @@ export const updatePushEnvVars = (cwd) => {
export const createPushEventPayload = async ( export const createPushEventPayload = async (
cwd, cwd,
{ forced = false, headCommit = null, commits = [] }, commits = null,
before = null,
after = null,
) => { ) => {
const payload = { const payload = {
forced, forced: false,
head_commit: headCommit, head_commit: null,
commits, before: before || 'bbbbb',
after: after || 'aaaaa',
commits: commits || [
{
id: 'ignored',
message: 'but needed for triggering',
},
],
} }
const eventPath = path.join(cwd, 'pushEventPayload.json') const eventPath = path.join(cwd, 'pushEventPayload.json')
updateEnvVars({ GITHUB_EVENT_PATH: eventPath }) updateEnvVars({ GITHUB_EVENT_PATH: eventPath })
updateEnvVars({ GITHUB_REPOSITORY: 'wagoid/commitlint-github-action' })
await writeFile(eventPath, JSON.stringify(payload), 'utf8') await writeFile(eventPath, JSON.stringify(payload), 'utf8')
} }