Skip to content

Commit de51303

Browse files
committed
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.
1 parent ab798dd commit de51303

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed

src/action.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const gitCommits = require('./gitCommits')
99
const generateOutputs = require('./generateOutputs')
1010

1111
const pullRequestEvent = 'pull_request'
12+
const pullRequestTargetEvent = 'pull_request_target'
13+
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
1214

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

@@ -45,7 +47,8 @@ const getRangeForPushEvent = () => {
4547
}
4648

4749
const getRangeForEvent = async () => {
48-
if (GITHUB_EVENT_NAME !== pullRequestEvent) return getRangeForPushEvent()
50+
if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
51+
return getRangeForPushEvent()
4952

5053
const octokit = github.getOctokit(core.getInput('token'))
5154
const { owner, repo, number } = eventContext.issue

src/action.test.js

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -261,75 +261,80 @@ describe('Commit Linter action', () => {
261261
td.verify(core.setFailed(contains('wrong commit from another branch')))
262262
})
263263

264-
describe('when there are multiple commits failing in the pull request', () => {
265-
let expectedResultsOutput
266-
const firstMessage = 'wrong message 1'
267-
const secondMessage = 'wrong message 2'
268-
269-
beforeEach(async () => {
270-
cwd = await git.bootstrap('fixtures/conventional')
271-
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
272-
await gitEmptyCommit(cwd, 'message from before push')
273-
await gitEmptyCommit(cwd, firstMessage)
274-
await gitEmptyCommit(cwd, secondMessage)
275-
await createPullRequestEventPayload(cwd)
276-
const [, first, to] = await getCommitHashes(cwd)
277-
updatePullRequestEnvVars(cwd, to)
278-
td.when(
279-
listCommits({
280-
owner: 'wagoid',
281-
repo: 'commitlint-github-action',
282-
pull_number: '1',
283-
}),
284-
).thenResolve({
285-
data: [first, to].map((sha) => ({ sha })),
264+
describe.each(['pull_request', 'pull_request_target'])(
265+
'when there are multiple commits failing in the %s event',
266+
(eventName) => {
267+
let expectedResultsOutput
268+
const firstMessage = 'wrong message 1'
269+
const secondMessage = 'wrong message 2'
270+
271+
beforeEach(async () => {
272+
cwd = await git.bootstrap('fixtures/conventional')
273+
td.when(core.getInput('configFile')).thenReturn(
274+
'./commitlint.config.js',
275+
)
276+
await gitEmptyCommit(cwd, 'message from before push')
277+
await gitEmptyCommit(cwd, firstMessage)
278+
await gitEmptyCommit(cwd, secondMessage)
279+
await createPullRequestEventPayload(cwd)
280+
const [, first, to] = await getCommitHashes(cwd)
281+
updatePullRequestEnvVars(cwd, to, { eventName })
282+
td.when(
283+
listCommits({
284+
owner: 'wagoid',
285+
repo: 'commitlint-github-action',
286+
pull_number: '1',
287+
}),
288+
).thenResolve({
289+
data: [first, to].map((sha) => ({ sha })),
290+
})
291+
td.replace(process, 'cwd', () => cwd)
292+
293+
expectedResultsOutput = [
294+
{
295+
hash: to,
296+
message: secondMessage,
297+
valid: false,
298+
errors: ['subject may not be empty', 'type may not be empty'],
299+
warnings: [],
300+
},
301+
{
302+
hash: first,
303+
message: firstMessage,
304+
valid: false,
305+
errors: ['subject may not be empty', 'type may not be empty'],
306+
warnings: [],
307+
},
308+
]
286309
})
287-
td.replace(process, 'cwd', () => cwd)
288310

289-
expectedResultsOutput = [
290-
{
291-
hash: to,
292-
message: secondMessage,
293-
valid: false,
294-
errors: ['subject may not be empty', 'type may not be empty'],
295-
warnings: [],
296-
},
297-
{
298-
hash: first,
299-
message: firstMessage,
300-
valid: false,
301-
errors: ['subject may not be empty', 'type may not be empty'],
302-
warnings: [],
303-
},
304-
]
305-
})
306-
307-
it('should NOT show errors for a message from before the push', async () => {
308-
await runAction()
311+
it('should NOT show errors for a message from before the push', async () => {
312+
await runAction()
309313

310-
td.verify(core.setFailed(contains('message from before push')), {
311-
times: 0,
314+
td.verify(core.setFailed(contains('message from before push')), {
315+
times: 0,
316+
})
312317
})
313-
})
314318

315-
it('should show errors for the first wrong message', async () => {
316-
await runAction()
319+
it('should show errors for the first wrong message', async () => {
320+
await runAction()
317321

318-
td.verify(core.setFailed(contains(firstMessage)))
319-
})
322+
td.verify(core.setFailed(contains(firstMessage)))
323+
})
320324

321-
it('should show errors for the second wrong message', async () => {
322-
await runAction()
325+
it('should show errors for the second wrong message', async () => {
326+
await runAction()
323327

324-
td.verify(core.setFailed(contains(secondMessage)))
325-
})
328+
td.verify(core.setFailed(contains(secondMessage)))
329+
})
326330

327-
it('should generate a JSON output of the errors', async () => {
328-
await runAction()
331+
it('should generate a JSON output of the errors', async () => {
332+
await runAction()
329333

330-
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
331-
})
332-
})
334+
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
335+
})
336+
},
337+
)
333338

334339
describe('when it fails to fetch commits', () => {
335340
beforeEach(async () => {

src/testUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ exports.createPullRequestEventPayload = async (cwd) => {
6666
await writeFile(eventPath, JSON.stringify(payload), 'utf8')
6767
}
6868

69-
exports.updatePullRequestEnvVars = (cwd, to) => {
69+
exports.updatePullRequestEnvVars = (cwd, to, options = {}) => {
70+
const { eventName = 'pull_request' } = options
71+
7072
updateEnvVars({
7173
GITHUB_WORKSPACE: cwd,
72-
GITHUB_EVENT_NAME: 'pull_request',
74+
GITHUB_EVENT_NAME: eventName,
7375
GITHUB_SHA: to,
7476
})
7577
}

0 commit comments

Comments
 (0)