Skip to content
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: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions src/lint-pr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as core from '@actions/core'
import { info, warning, error, setFailed } from '@actions/core'
import { context as githubContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint'
import parserPreset from 'conventional-changelog-conventionalcommits'
Expand All @@ -14,7 +14,7 @@ export async function lintPR() {
const client = getOctokit(GITHUB_TOKEN)

if (!githubContext.payload.pull_request) {
core.setFailed(actionMessage.fail.pull_request.not_found)
setFailed(actionMessage.fail.pull_request.not_found)
return
}

Expand All @@ -31,10 +31,10 @@ export async function lintPR() {
repo,
pull_number
})
core.info(`Found PR title: ${pullRequest.title}`)
info(`Found PR title: ${pullRequest.title}`)

if (pullRequest.user?.login === 'dependabot[bot]') {
core.info('Skipping lint for dependabot PR')
info('Skipping lint for dependabot PR')
return
}

Expand All @@ -58,30 +58,30 @@ export async function lintPR() {
})

commitReport.warnings.forEach((warn) =>
core.warning(`Commit message: ${warn.message}`)
warning(`Commit message: ${warn.message}`)
)
commitReport.errors.forEach((err) =>
core.error(`Commit message: ${err.message}`)
error(`Commit message: ${err.message}`)
)

if (!commitReport.valid) {
core.setFailed(actionMessage.fail.commit.lint)
setFailed(actionMessage.fail.commit.lint)
}

if (COMMIT_TITLE_MATCH && pullRequest.title !== commitMessageSubject) {
core.setFailed(actionMessage.fail.commit.commit_title_match)
setFailed(actionMessage.fail.commit.commit_title_match)
}
} else {
const titleReport = await lint(pullRequest.title, lintRules, {
parserOpts
})
titleReport.warnings.forEach((warn) =>
core.warning(`PR title: ${warn.message}`)
warning(`PR title: ${warn.message}`)
)
titleReport.errors.forEach((err) => core.error(`PR title: ${err.message}`))
titleReport.errors.forEach((err) => error(`PR title: ${err.message}`))

if (!titleReport.valid) {
core.setFailed(actionMessage.fail.pull_request.lint)
setFailed(actionMessage.fail.pull_request.lint)
}
}
}
6 changes: 3 additions & 3 deletions src/lint-rules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import configConventional from '@commitlint/config-conventional'
import * as core from '@actions/core'
import { warning } from '@actions/core'

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

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

// if $GITHUB_WORKSPACE is not set, the checkout action has not run so we can't import the rules file
if (RULES_PATH && !GITHUB_WORKSPACE) {
core.warning(actionMessage.warning.action.checkout)
warning(actionMessage.warning.action.checkout)
} else if (RULES_PATH && GITHUB_WORKSPACE) {
const configPath = path.resolve(GITHUB_WORKSPACE, RULES_PATH)
try {
const localRules = await import(configPath)
overrideRules = localRules.default?.rules ?? localRules.rules ?? {}
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') {
core.warning(actionMessage.warning.action.rules_not_found)
warning(actionMessage.warning.action.rules_not_found)
} else {
throw e
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import { setFailed } from '@actions/core'
import { lintPR } from './lint-pr.js'

lintPR().catch((err) => {
core.setFailed(err.message)
setFailed(err.message)
throw err
})
27 changes: 10 additions & 17 deletions test/lint-pr.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, before, beforeEach, mock } from 'node:test'
import assert from 'node:assert/strict'
import actionConfigFixture from './fixtures/action-config.js'
import actionMessage from '../src/action-message.js'

const commitFixture = {
Expand All @@ -25,7 +24,6 @@ const prFixture = {

let pullsGetResponse = { data: prFixture }
let listCommitsResponse = { data: [{ commit: commitFixture }] }
let actionConfigResponse = actionConfigFixture

const mockCore = {
info: mock.fn(),
Expand All @@ -43,21 +41,13 @@ const githubClient = {
}
}

const mockGetActionConfig = mock.fn(() => actionConfigResponse)

mock.module('@actions/core', { exports: mockCore })
mock.module('@actions/core', { namedExports: mockCore })
mock.module('@actions/github', {
exports: {
namedExports: {
getOctokit: mock.fn(() => githubClient),
context: { payload: { pull_request: contextPrFixture } }
}
})
mock.module('../src/utils.js', {
exports: {
getActionConfig: mockGetActionConfig,
getCommitSubject: (msg = '') => msg.split('\n')[0]
}
})

let lintPR

Expand All @@ -68,11 +58,14 @@ before(async () => {
function resetMocks() {
pullsGetResponse = { data: prFixture }
listCommitsResponse = { data: [{ commit: commitFixture }] }
actionConfigResponse = actionConfigFixture
process.env.INPUT_COMMITTITLEMATCH = 'true'
process.env.INPUT_IGNORECOMMITS = 'false'
process.env.GITHUB_TOKEN = 'asdf'
delete process.env.INPUT_COMMITLINTRULESPATH
delete process.env.GITHUB_WORKSPACE
for (const fn of [
mockCore.info, mockCore.warning, mockCore.error, mockCore.setFailed,
githubClient.rest.pulls.get, githubClient.rest.pulls.listCommits,
mockGetActionConfig
githubClient.rest.pulls.get, githubClient.rest.pulls.listCommits
]) fn.mock.resetCalls()
}

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

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

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

await lintPR()
Expand Down
2 changes: 1 addition & 1 deletion test/lint-rules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mockCore = {
setFailed: mock.fn()
}

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

let getLintRules

Expand Down
Loading