Skip to content

Commit

Permalink
feat: Add use description input and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavsinai-philips committed Dec 28, 2023
1 parent 59797d1 commit 88d6ac1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
# Optional
with:
cl-config: commitlint-cjs.config.cjs
useDescription: true # if to take into account the PR description of not, defaults to false
```
The above action only check's out the current repository to fetch the commitlint configuration file.
PNPM and node is used to install necessary dependencies, then config-conventional is used as a default config.
Expand All @@ -54,6 +55,8 @@ When using the above configuration, `pnpm-lock.yaml` is required. Please use npm
### Inputs
#### `cl-config`
**Optional** Path to commit lint config. Default : `'commitlint.config.js'`
#### `useDescription`
**Optional** if to take into account the PR description of not, default: false

### Outputs
#### `lint-status`
Expand Down
23 changes: 15 additions & 8 deletions __tests__/lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process';
import { describe, expect, it } from 'vitest';
import load from '@commitlint/load';
import { testLintOptions, verifyTitle } from '../src/lint';
import { testLintOptions, verifyPr } from '../src/lint';

const { getLintOptions, convertESMtoCJS } = testLintOptions;

Expand Down Expand Up @@ -42,7 +42,7 @@ const emptyConfigOptionNoParserOpts = {
describe('commitlint', async () => {
const emptyConfig = await load({});
const defaultConfig = await load({ extends: '@commitlint/config-conventional' });
const currentConfig = await load({}, { file: 'commitlint.config.js', cwd: process.cwd() });
const currentConfig = await load({}, { file: 'commitlint.config.mjs', cwd: process.cwd() });

it('configurations return proper extensions and rules', () => {
expect(emptyConfig).toHaveProperty('extends', ['@commitlint/config-conventional']);
Expand All @@ -59,15 +59,22 @@ describe('commitlint', async () => {
});

it('throw error on incorrect title', async () => {
await expect(verifyTitle('foo: bar')).rejects.toThrowError(/check failed/);
await expect(verifyTitle('foo: bar', 'something.config.js')).rejects.toThrowError(/subject-case/);
await expect(verifyTitle('test: add tests', 'commitlint.config.js')).rejects.toThrowError(/sentence-case/);
await expect(verifyPr({ number: 1, title: 'foo: bar' })).rejects.toThrowError(/check failed/);
await expect(verifyPr({ number: 1, title: 'foo: bar' }, 'something.config.js')).rejects.toThrowError(/subject-case/);
await expect(verifyPr({ number: 1, title: 'test: add tests' }, 'commitlint.config.js')).rejects.toThrowError(/sentence-case/);
});

it('take the description too', async () => {
let longString = '';
while (longString.length < 200)
longString = longString.concat('long long long ');

await expect(verifyPr({ number: 1, title: 'foo: bar', description: longString }, 'something.config.js', true)).rejects.toThrowError(/check failed/);
});
it('return true if title is valid', async () => {
await expect(verifyTitle('fix: Add new commets')).resolves.toEqual(true);
await expect(verifyTitle('feat: Title is short and nice!', 'something.config.js')).resolves.toEqual(true);
await expect(verifyTitle('test: Add test suites', 'commitlint.config.js')).resolves.toEqual(true);
await expect(verifyPr({ number: 1, title: 'fix: Add new commets' })).resolves.toEqual(true);
await expect(verifyPr({ number: 1, title: 'feat: Title is short and nice!' }, 'something.config.js')).resolves.toEqual(true);
await expect(verifyPr({ number: 1, title: 'test: Add test suites' }, 'commitlint.config.mjs')).resolves.toEqual(true);
});

it('return error if file for esm conversion does not exist', async () => {
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: Path to commit lint config (commitlint.config.js)
default: commitlint.config.js
required: false
useDescription:
description: if to take into account the description of the PR too when validating
default: false
required: false
outputs:
lint-status:
description: The status of the PR lint
Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253083,13 +253083,14 @@ function run() {
return __awaiter(this, void 0, void 0, function* () {
const pullRequestPayload = github.context.payload.pull_request;
const configPayload = core.getInput('cl-config');
const useDescription = !!core.getInput('useDescription');
if (!(pullRequestPayload === null || pullRequestPayload === void 0 ? void 0 : pullRequestPayload.title))
throw new Error('Pull Request or Title not found!');
const pullRequestObject = {
title: pullRequestPayload.title,
number: pullRequestPayload.number,
};
yield (0, lint_1.verifyTitle)(pullRequestObject.title, configPayload);
yield (0, lint_1.verifyPr)(pullRequestObject, configPayload, useDescription);
});
}
run().catch(errHandle_1.default);
Expand Down Expand Up @@ -253138,7 +253139,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.verifyTitle = exports.testLintOptions = void 0;
exports.verifyPr = exports.testLintOptions = void 0;
const node_process_1 = __importDefault(__nccwpck_require__(97742));
const fs = __importStar(__nccwpck_require__(87561));
const load_1 = __importDefault(__nccwpck_require__(90046));
Expand Down Expand Up @@ -253197,7 +253198,7 @@ exports.testLintOptions = {
* @param {string} configPath - The configuration path of the commitlint config fetched from current working directory
* @return {Promise<boolean>} - Returns true if linter passes, throws {@link Error} if failing
*/
function verifyTitle(title, configPath = '') {
function verifyPr(pr, configPath = '', useDescription) {
return __awaiter(this, void 0, void 0, function* () {
const outputConfig = () => __awaiter(this, void 0, void 0, function* () {
if (fs.existsSync(configPath)) {
Expand All @@ -253209,7 +253210,8 @@ function verifyTitle(title, configPath = '') {
}
});
const commitlintConfig = yield outputConfig();
const linterResult = yield (0, lint_1.default)(title, commitlintConfig.rules, getLintOptions(commitlintConfig));
const message = useDescription ? `${pr.title}\n\n${pr.description}` : pr.title;
const linterResult = yield (0, lint_1.default)(message, commitlintConfig.rules, getLintOptions(commitlintConfig));
if (linterResult.valid) {
(0, core_1.setOutput)('lint-status', '✅ Commitlint tests passed!\n');
(0, core_1.setOutput)('lint-details', linterResult);
Expand All @@ -253225,7 +253227,7 @@ function verifyTitle(title, configPath = '') {
}
});
}
exports.verifyTitle = verifyTitle;
exports.verifyPr = verifyPr;


/***/ }),
Expand Down
11 changes: 4 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import * as github from '@actions/github';
import * as core from '@actions/core';
import handleError from './errHandle';
import { verifyTitle } from './lint';

type pullRequest = {
title: string
number: number
};
import { verifyPr } from './lint';
import type { pullRequest } from './interfaces';

async function run(): Promise<void> {
const pullRequestPayload = github.context.payload.pull_request;
const configPayload = core.getInput('cl-config');
const useDescription = !!core.getInput('useDescription');

if (!pullRequestPayload?.title)
throw new Error('Pull Request or Title not found!');
Expand All @@ -20,7 +17,7 @@ async function run(): Promise<void> {
number: pullRequestPayload.number,
};

await verifyTitle(pullRequestObject.title, configPayload);
await verifyPr(pullRequestObject, configPayload, useDescription);
}

run().catch(handleError);
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type pullRequest = {
title: string
description?: string
number: number
};
7 changes: 4 additions & 3 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import load from '@commitlint/load';
import lint from '@commitlint/lint';
import { setOutput } from '@actions/core';
import logWithTile from './log';
import type { pullRequest } from './interfaces';

const defaultConfig = {
extends: '@commitlint/config-conventional',
Expand Down Expand Up @@ -61,7 +62,7 @@ export const testLintOptions = {
* @param {string} configPath - The configuration path of the commitlint config fetched from current working directory
* @return {Promise<boolean>} - Returns true if linter passes, throws {@link Error} if failing
*/
export async function verifyTitle(title: string, configPath: string = ''): Promise<boolean> {
export async function verifyPr(pr: pullRequest, configPath: string = '', useDescription?: boolean): Promise<boolean> {
const outputConfig = async () => {
if (fs.existsSync(configPath)) {
await convertESMtoCJS(configPath, 'commitlint-cjs.config.cjs');
Expand All @@ -73,8 +74,8 @@ export async function verifyTitle(title: string, configPath: string = ''): Promi
};

const commitlintConfig: QualifiedConfig = await outputConfig();

const linterResult = await lint(title, commitlintConfig.rules, getLintOptions(commitlintConfig));
const message = useDescription ? `${pr.title}\n\n${pr.description}` : pr.title;
const linterResult = await lint(message, commitlintConfig.rules, getLintOptions(commitlintConfig));

if (linterResult.valid) {
setOutput('lint-status', '✅ Commitlint tests passed!\n');
Expand Down

0 comments on commit 88d6ac1

Please sign in to comment.