Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to commitlint v11 #62

Merged
merged 3 commits into from
Oct 31, 2020
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "build"
on:
pull_request:
push:

jobs:
build: # make sure build/ci work properly
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"pack": "ncc build",
"test": "jest",
"build:pack": "npm run build && npm run pack",
"all": "npm run build && npm run format && npm run lint && npm run pack"
"all": "npm run build && npm run test && npm run format && npm run lint && npm run pack"
},
"repository": {
"type": "git",
Expand All @@ -25,10 +26,11 @@
"author": "Julien Karst",
"license": "MIT",
"devDependencies": {
"@commitlint/types": "11.0.0",
"@types/jest": "26.0.14",
"@types/node": "12.12.37",
"@typescript-eslint/parser": "4.3.0",
"@zeit/ncc": "0.22.3",
"@vercel/ncc": "0.24.1",
"eslint": "7.10.0",
"eslint-plugin-github": "3.4.1",
"eslint-plugin-jest": "23.20.0",
Expand All @@ -42,8 +44,11 @@
},
"dependencies": {
"@actions/core": "1.2.6",
"@actions/github": "2.1.1",
"@commitlint/lint": "8.3.5",
"@commitlint/load": "8.3.5"
"@actions/github": "4.0.0",
"@commitlint/lint": "11.0.0",
"@commitlint/load": "11.0.0"
},
"resolutions": {
"import-fresh": "JulienKode/import-fresh#fix/issue-when-there-no-parent-module"
}
}
57 changes: 57 additions & 0 deletions src/linter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
LintOptions,
ParserOptions,
ParserPreset,
QualifiedConfig
} from '@commitlint/types'
import load from '@commitlint/load'
import lint from '@commitlint/lint'

function selectParserOpts(parserPreset: ParserPreset) {
if (typeof parserPreset !== 'object') {
return undefined
}

if (typeof parserPreset.parserOpts !== 'object') {
return undefined
}

return parserPreset.parserOpts
}

function getLintOptions(configuration: QualifiedConfig): LintOptions {
const parserOpts = selectParserOpts(configuration.parserPreset)
const opts: LintOptions & {parserOpts: ParserOptions} = {
parserOpts: {},
plugins: {},
ignores: [],
defaultIgnores: true
}
if (parserOpts) {
opts.parserOpts = parserOpts
}
if (configuration.plugins) {
opts.plugins = configuration.plugins
}
if (configuration.ignores) {
opts.ignores = configuration.ignores
}
if (!configuration.defaultIgnores) {
opts.defaultIgnores = false
}
return opts
}

export async function lintPullRequest(title: string, configPath: string) {
const configuration = await load({}, {file: configPath, cwd: process.cwd()})

const options = getLintOptions(configuration)

const result = await lint(title, configuration.rules, options)

if (result.valid) return
const errorMessage = result.errors
.map(({message, name}: any) => `${name}:${message}`)
.join('\n')
throw new Error(errorMessage)
}
19 changes: 1 addition & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import lint from '@commitlint/lint'
import load from '@commitlint/load'
import {lintPullRequest} from './linter'

async function run(): Promise<void> {
try {
Expand All @@ -27,20 +26,4 @@ function getPrTitle(): string | undefined {
return pullRequest.title
}

export async function lintPullRequest(title: string, configPath: string) {
const opts = await load({}, {file: configPath, cwd: process.cwd()})

const result = await lint(
title,
opts.rules,
opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}
)
if (result.valid === true) return

const errorMessage = result.errors
.map(({message, name}: any) => `${name}:${message}`)
.join('\n')
throw new Error(errorMessage)
}

run()
21 changes: 21 additions & 0 deletions test/linter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {lintPullRequest} from '../src/linter'

test('should lint the title correctly', async () => {
// Given
const title = 'feat(valid): scope'

// Expect
await expect(
lintPullRequest(title, './commitlint.config.js')
).resolves.toBeUndefined()
})

test('should raise errors', async () => {
// Given
const title = 'feat(INVALID): scope'

// Expect
await expect(
lintPullRequest(title, './commitlint.config.js')
).rejects.toThrow()
})
Loading