From ea214b47cfe44429478292bd3be320b1cb1d33e6 Mon Sep 17 00:00:00 2001 From: Gilbertsoft <25326036+gilbertsoft@users.noreply.github.com> Date: Wed, 19 Aug 2020 17:56:06 +0200 Subject: [PATCH] [FEATURE] Add option to exclude the PR description from the check Resolves #12 --- README.md | 1 + __tests__/input-helper.test.ts | 21 +++++++++++++++++++++ action.yml | 4 ++++ dist/index.js | 8 +++++--- package.json | 2 +- src/input-helper.ts | 9 ++++++--- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e9d14cb..2983216 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ jobs: with: pattern: '^[^#].{74}' error: 'The maximum line length of 74 characters is exceeded.' + excludeDescription: '1' - name: Check for Resolves / Fixes uses: gsactions/commit-message-checker@v1 with: diff --git a/__tests__/input-helper.test.ts b/__tests__/input-helper.test.ts index c94f247..8c26af6 100644 --- a/__tests__/input-helper.test.ts +++ b/__tests__/input-helper.test.ts @@ -166,6 +166,27 @@ describe('input-helper tests', () => { expect(checkerArguments.messages[0]).toBe('some-title\n\nsome-body') }) + it('excludes pull_request body payload', () => { + mockGitHub.context = { + eventName: 'pull_request', + payload: { + pull_request: { + title: 'some-title', + body: 'some-body' + } + } + } + inputs.pattern = 'some-pattern' + inputs.error = 'some-error' + inputs.excludeDescription = '1' + const checkerArguments: ICheckerArguments = inputHelper.getInputs() + expect(checkerArguments).toBeTruthy() + expect(checkerArguments.pattern).toBe('some-pattern') + expect(checkerArguments.error).toBe('some-error') + expect(checkerArguments.messages).toBeTruthy() + expect(checkerArguments.messages[0]).toBe('some-title') + }) + it('push payload is optional', () => { mockGitHub.context = { eventName: 'push', diff --git a/action.yml b/action.yml index 1a018d2..b136b7d 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: error: description: 'A error message which will be returned in case of an error.' required: true + excludeDescription: + description: 'Setting this input to 1 will exclude the Pull Request description from the check.' + required: false + default: '' runs: using: node12 main: dist/index.js diff --git a/dist/index.js b/dist/index.js index 92d49df..1ef628c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8741,8 +8741,10 @@ function getInputs() { result.flags = core.getInput('flags'); // Get error message result.error = core.getInput('error', { required: true }); + // Get excludeDescription + const excludeDescription = core.getInput('excludeDescription') !== ''; // Get error message - result.messages = getMessages(); + result.messages = getMessages(excludeDescription); return result; } exports.getInputs = getInputs; @@ -8752,7 +8754,7 @@ exports.getInputs = getInputs; * * @returns string[] */ -function getMessages() { +function getMessages(excludeDescription) { const messages = []; switch (github.context.eventName) { case 'pull_request': { @@ -8760,7 +8762,7 @@ function getMessages() { github.context.payload.pull_request && github.context.payload.pull_request.title) { let message = github.context.payload.pull_request.title; - if (github.context.payload.pull_request.body) { + if (github.context.payload.pull_request.body && !excludeDescription) { message = message.concat('\n\n', github.context.payload.pull_request.body); } messages.push(message); diff --git a/package.json b/package.json index 163c275..27882d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gsactions/commit-message-checker", - "version": "0.2.0", + "version": "0.3.0", "description": "GitHub Action that checks commit messages of pushes and pull request against a regex pattern", "keywords": [ "github", diff --git a/src/input-helper.ts b/src/input-helper.ts index 84799dd..d85fcc3 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -39,8 +39,11 @@ export function getInputs(): ICheckerArguments { // Get error message result.error = core.getInput('error', {required: true}) + // Get excludeDescription + const excludeDescription = core.getInput('excludeDescription') !== '' + // Get error message - result.messages = getMessages() + result.messages = getMessages(excludeDescription) return result } @@ -51,7 +54,7 @@ export function getInputs(): ICheckerArguments { * * @returns string[] */ -function getMessages(): string[] { +function getMessages(excludeDescription: boolean): string[] { const messages: string[] = [] switch (github.context.eventName) { @@ -62,7 +65,7 @@ function getMessages(): string[] { github.context.payload.pull_request.title ) { let message: string = github.context.payload.pull_request.title - if (github.context.payload.pull_request.body) { + if (github.context.payload.pull_request.body && !excludeDescription) { message = message.concat( '\n\n', github.context.payload.pull_request.body