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

feat: add support for pull_request_target event

Previously, the action was treating pull_request_target event as a regular push,
which was causing some issues.
This commit is contained in:
wagoid 2021-04-05 09:08:06 -03:00
parent ab798dde93
commit de51303046
3 changed files with 71 additions and 61 deletions

View file

@ -9,6 +9,8 @@ const gitCommits = require('./gitCommits')
const generateOutputs = require('./generateOutputs') const generateOutputs = require('./generateOutputs')
const pullRequestEvent = 'pull_request' 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, GITHUB_SHA } = process.env
@ -45,7 +47,8 @@ const getRangeForPushEvent = () => {
} }
const getRangeForEvent = async () => { const getRangeForEvent = async () => {
if (GITHUB_EVENT_NAME !== pullRequestEvent) return getRangeForPushEvent() if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
return getRangeForPushEvent()
const octokit = github.getOctokit(core.getInput('token')) const octokit = github.getOctokit(core.getInput('token'))
const { owner, repo, number } = eventContext.issue const { owner, repo, number } = eventContext.issue

View file

@ -261,75 +261,80 @@ describe('Commit Linter action', () => {
td.verify(core.setFailed(contains('wrong commit from another branch'))) td.verify(core.setFailed(contains('wrong commit from another branch')))
}) })
describe('when there are multiple commits failing in the pull request', () => { describe.each(['pull_request', 'pull_request_target'])(
let expectedResultsOutput 'when there are multiple commits failing in the %s event',
const firstMessage = 'wrong message 1' (eventName) => {
const secondMessage = 'wrong message 2' let expectedResultsOutput
const firstMessage = 'wrong message 1'
const secondMessage = 'wrong message 2'
beforeEach(async () => { beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional') cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js') td.when(core.getInput('configFile')).thenReturn(
await gitEmptyCommit(cwd, 'message from before push') './commitlint.config.js',
await gitEmptyCommit(cwd, firstMessage) )
await gitEmptyCommit(cwd, secondMessage) await gitEmptyCommit(cwd, 'message from before push')
await createPullRequestEventPayload(cwd) await gitEmptyCommit(cwd, firstMessage)
const [, first, to] = await getCommitHashes(cwd) await gitEmptyCommit(cwd, secondMessage)
updatePullRequestEnvVars(cwd, to) await createPullRequestEventPayload(cwd)
td.when( const [, first, to] = await getCommitHashes(cwd)
listCommits({ updatePullRequestEnvVars(cwd, to, { eventName })
owner: 'wagoid', td.when(
repo: 'commitlint-github-action', listCommits({
pull_number: '1', owner: 'wagoid',
}), repo: 'commitlint-github-action',
).thenResolve({ pull_number: '1',
data: [first, to].map((sha) => ({ sha })), }),
).thenResolve({
data: [first, to].map((sha) => ({ sha })),
})
td.replace(process, 'cwd', () => cwd)
expectedResultsOutput = [
{
hash: to,
message: secondMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
{
hash: first,
message: firstMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
]
}) })
td.replace(process, 'cwd', () => cwd)
expectedResultsOutput = [ it('should NOT show errors for a message from before the push', async () => {
{ await runAction()
hash: to,
message: secondMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
{
hash: first,
message: firstMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
]
})
it('should NOT show errors for a message from before the push', async () => { td.verify(core.setFailed(contains('message from before push')), {
await runAction() times: 0,
})
td.verify(core.setFailed(contains('message from before push')), {
times: 0,
}) })
})
it('should show errors for the first wrong message', async () => { it('should show errors for the first wrong message', async () => {
await runAction() await runAction()
td.verify(core.setFailed(contains(firstMessage))) td.verify(core.setFailed(contains(firstMessage)))
}) })
it('should show errors for the second wrong message', async () => { it('should show errors for the second wrong message', async () => {
await runAction() await runAction()
td.verify(core.setFailed(contains(secondMessage))) td.verify(core.setFailed(contains(secondMessage)))
}) })
it('should generate a JSON output of the errors', async () => { it('should generate a JSON output of the errors', async () => {
await runAction() await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput)) td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
}) })
}) },
)
describe('when it fails to fetch commits', () => { describe('when it fails to fetch commits', () => {
beforeEach(async () => { beforeEach(async () => {

View file

@ -66,10 +66,12 @@ exports.createPullRequestEventPayload = async (cwd) => {
await writeFile(eventPath, JSON.stringify(payload), 'utf8') await writeFile(eventPath, JSON.stringify(payload), 'utf8')
} }
exports.updatePullRequestEnvVars = (cwd, to) => { exports.updatePullRequestEnvVars = (cwd, to, options = {}) => {
const { eventName = 'pull_request' } = options
updateEnvVars({ updateEnvVars({
GITHUB_WORKSPACE: cwd, GITHUB_WORKSPACE: cwd,
GITHUB_EVENT_NAME: 'pull_request', GITHUB_EVENT_NAME: eventName,
GITHUB_SHA: to, GITHUB_SHA: to,
}) })
} }