Skip to content

Commit 6150599

Browse files
Merge pull request #4 from zentered/fix/node24-mock-compat
fix: use named imports and resolve mock specifiers for Node 24 compat
2 parents d28aa3e + 3c93ec3 commit 6150599

6 files changed

Lines changed: 28 additions & 35 deletions

File tree

‎dist/index.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/lint-pr.js‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as core from '@actions/core'
1+
import { info, warning, error, setFailed } from '@actions/core'
22
import { context as githubContext, getOctokit } from '@actions/github'
33
import lint from '@commitlint/lint'
44
import parserPreset from 'conventional-changelog-conventionalcommits'
@@ -14,7 +14,7 @@ export async function lintPR() {
1414
const client = getOctokit(GITHUB_TOKEN)
1515

1616
if (!githubContext.payload.pull_request) {
17-
core.setFailed(actionMessage.fail.pull_request.not_found)
17+
setFailed(actionMessage.fail.pull_request.not_found)
1818
return
1919
}
2020

@@ -31,10 +31,10 @@ export async function lintPR() {
3131
repo,
3232
pull_number
3333
})
34-
core.info(`Found PR title: ${pullRequest.title}`)
34+
info(`Found PR title: ${pullRequest.title}`)
3535

3636
if (pullRequest.user?.login === 'dependabot[bot]') {
37-
core.info('Skipping lint for dependabot PR')
37+
info('Skipping lint for dependabot PR')
3838
return
3939
}
4040

@@ -58,30 +58,30 @@ export async function lintPR() {
5858
})
5959

6060
commitReport.warnings.forEach((warn) =>
61-
core.warning(`Commit message: ${warn.message}`)
61+
warning(`Commit message: ${warn.message}`)
6262
)
6363
commitReport.errors.forEach((err) =>
64-
core.error(`Commit message: ${err.message}`)
64+
error(`Commit message: ${err.message}`)
6565
)
6666

6767
if (!commitReport.valid) {
68-
core.setFailed(actionMessage.fail.commit.lint)
68+
setFailed(actionMessage.fail.commit.lint)
6969
}
7070

7171
if (COMMIT_TITLE_MATCH && pullRequest.title !== commitMessageSubject) {
72-
core.setFailed(actionMessage.fail.commit.commit_title_match)
72+
setFailed(actionMessage.fail.commit.commit_title_match)
7373
}
7474
} else {
7575
const titleReport = await lint(pullRequest.title, lintRules, {
7676
parserOpts
7777
})
7878
titleReport.warnings.forEach((warn) =>
79-
core.warning(`PR title: ${warn.message}`)
79+
warning(`PR title: ${warn.message}`)
8080
)
81-
titleReport.errors.forEach((err) => core.error(`PR title: ${err.message}`))
81+
titleReport.errors.forEach((err) => error(`PR title: ${err.message}`))
8282

8383
if (!titleReport.valid) {
84-
core.setFailed(actionMessage.fail.pull_request.lint)
84+
setFailed(actionMessage.fail.pull_request.lint)
8585
}
8686
}
8787
}

‎src/lint-rules.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import configConventional from '@commitlint/config-conventional'
3-
import * as core from '@actions/core'
3+
import { warning } from '@actions/core'
44

55
import actionMessage from './action-message.js'
66

@@ -11,15 +11,15 @@ export default async function getLintRules(actionConfig) {
1111

1212
// if $GITHUB_WORKSPACE is not set, the checkout action has not run so we can't import the rules file
1313
if (RULES_PATH && !GITHUB_WORKSPACE) {
14-
core.warning(actionMessage.warning.action.checkout)
14+
warning(actionMessage.warning.action.checkout)
1515
} else if (RULES_PATH && GITHUB_WORKSPACE) {
1616
const configPath = path.resolve(GITHUB_WORKSPACE, RULES_PATH)
1717
try {
1818
const localRules = await import(configPath)
1919
overrideRules = localRules.default?.rules ?? localRules.rules ?? {}
2020
} catch (e) {
2121
if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') {
22-
core.warning(actionMessage.warning.action.rules_not_found)
22+
warning(actionMessage.warning.action.rules_not_found)
2323
} else {
2424
throw e
2525
}

‎src/main.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as core from '@actions/core'
1+
import { setFailed } from '@actions/core'
22
import { lintPR } from './lint-pr.js'
33

44
lintPR().catch((err) => {
5-
core.setFailed(err.message)
5+
setFailed(err.message)
66
throw err
77
})

‎test/lint-pr.spec.js‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, before, beforeEach, mock } from 'node:test'
22
import assert from 'node:assert/strict'
3-
import actionConfigFixture from './fixtures/action-config.js'
43
import actionMessage from '../src/action-message.js'
54

65
const commitFixture = {
@@ -25,7 +24,6 @@ const prFixture = {
2524

2625
let pullsGetResponse = { data: prFixture }
2726
let listCommitsResponse = { data: [{ commit: commitFixture }] }
28-
let actionConfigResponse = actionConfigFixture
2927

3028
const mockCore = {
3129
info: mock.fn(),
@@ -43,21 +41,13 @@ const githubClient = {
4341
}
4442
}
4543

46-
const mockGetActionConfig = mock.fn(() => actionConfigResponse)
47-
48-
mock.module('@actions/core', { exports: mockCore })
44+
mock.module('@actions/core', { namedExports: mockCore })
4945
mock.module('@actions/github', {
50-
exports: {
46+
namedExports: {
5147
getOctokit: mock.fn(() => githubClient),
5248
context: { payload: { pull_request: contextPrFixture } }
5349
}
5450
})
55-
mock.module('../src/utils.js', {
56-
exports: {
57-
getActionConfig: mockGetActionConfig,
58-
getCommitSubject: (msg = '') => msg.split('\n')[0]
59-
}
60-
})
6151

6252
let lintPR
6353

@@ -68,11 +58,14 @@ before(async () => {
6858
function resetMocks() {
6959
pullsGetResponse = { data: prFixture }
7060
listCommitsResponse = { data: [{ commit: commitFixture }] }
71-
actionConfigResponse = actionConfigFixture
61+
process.env.INPUT_COMMITTITLEMATCH = 'true'
62+
process.env.INPUT_IGNORECOMMITS = 'false'
63+
process.env.GITHUB_TOKEN = 'asdf'
64+
delete process.env.INPUT_COMMITLINTRULESPATH
65+
delete process.env.GITHUB_WORKSPACE
7266
for (const fn of [
7367
mockCore.info, mockCore.warning, mockCore.error, mockCore.setFailed,
74-
githubClient.rest.pulls.get, githubClient.rest.pulls.listCommits,
75-
mockGetActionConfig
68+
githubClient.rest.pulls.get, githubClient.rest.pulls.listCommits
7669
]) fn.mock.resetCalls()
7770
}
7871

@@ -105,7 +98,7 @@ describe('lintPR', () => {
10598
describe('when pull request has one commit', () => {
10699
describe('when IGNORE_COMMITS is true', () => {
107100
it('passes if commit message is not conventional', async () => {
108-
actionConfigResponse = { ...actionConfigFixture, IGNORE_COMMITS: true }
101+
process.env.INPUT_IGNORECOMMITS = 'true'
109102
listCommitsResponse = { data: [{ commit: { ...commitFixture, message: 'not conventional' } }] }
110103

111104
await lintPR()
@@ -124,7 +117,7 @@ describe('lintPR', () => {
124117

125118
describe('when COMMIT_TITLE_MATCH is false', () => {
126119
it('passes when pr title does not match the commit subject', async () => {
127-
actionConfigResponse = { ...actionConfigFixture, COMMIT_TITLE_MATCH: false }
120+
process.env.INPUT_COMMITTITLEMATCH = 'false'
128121
pullsGetResponse = { data: { ...prFixture, title: 'feat: does not match commit' } }
129122

130123
await lintPR()

‎test/lint-rules.spec.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const mockCore = {
1111
setFailed: mock.fn()
1212
}
1313

14-
mock.module('@actions/core', { exports: mockCore })
14+
mock.module('@actions/core', { namedExports: mockCore })
1515

1616
let getLintRules
1717

0 commit comments

Comments
 (0)