From 9bd4b74b064e92fe998f5d1ac3814325ade38942 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 7 Jul 2020 15:58:11 +0200 Subject: [PATCH] feat(dev-infra): commit message validation should skip lines consisting of URLs (#37890) The dev-infra commit message validation optionally can check for lines to not exceed a given amount of characters. This is desired for most commit messages, but sometimes not actionable if a long URL is inserted into the commit message. With this commit, we skip the max line length check for lines that start with an URL. PR Close #37890 --- dev-infra/commit-message/validate.spec.ts | 9 +++++++++ dev-infra/commit-message/validate.ts | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dev-infra/commit-message/validate.spec.ts b/dev-infra/commit-message/validate.spec.ts index ad1bf5b1e9..edc0bab802 100644 --- a/dev-infra/commit-message/validate.spec.ts +++ b/dev-infra/commit-message/validate.spec.ts @@ -77,6 +77,15 @@ describe('validate-commit-message.js', () => { config.commitMessage.maxLineLength} characters`); }); + it('should skip max length limit for URLs', () => { + const msg = 'fix(compiler): this is just an usual commit message tile\n\n' + + 'This is a normal commit message body which does not exceed the max length\n' + + 'limit. For more details see the following super long URL:\n\n' + + 'https://github.com/angular/components/commit/e2ace018ddfad10608e0e32932c43dcfef4095d7#diff-9879d6db96fd29134fc802214163b95a'; + + expect(validateCommitMessage(msg)).toBe(VALID); + }); + it('should validate "(): " format', () => { const msg = 'not correct format'; diff --git a/dev-infra/commit-message/validate.ts b/dev-infra/commit-message/validate.ts index a9b01f1c4d..7108a63cac 100644 --- a/dev-infra/commit-message/validate.ts +++ b/dev-infra/commit-message/validate.ts @@ -22,6 +22,7 @@ const REVERT_PREFIX_RE = /^revert:? /i; const TYPE_SCOPE_RE = /^(\w+)(?:\(([^)]+)\))?\:\s(.+)$/; const COMMIT_HEADER_RE = /^(.*)/i; const COMMIT_BODY_RE = /^.*\n\n([\s\S]*)$/; +const COMMIT_BODY_URL_LINE_RE = /^https?:\/\/.*$/; /** Parse a full commit message into its composite parts. */ export function parseCommitMessage(commitMsg: string) { @@ -156,7 +157,13 @@ export function validateCommitMessage( } const bodyByLine = commit.body.split('\n'); - if (bodyByLine.some(line => line.length > config.maxLineLength)) { + const lineExceedsMaxLength = bodyByLine.some(line => { + // Check if any line exceeds the max line length limit. The limit is ignored for + // lines that just contain an URL (as these usually cannot be wrapped or shortened). + return line.length > config.maxLineLength && !COMMIT_BODY_URL_LINE_RE.test(line); + }); + + if (lineExceedsMaxLength) { printError( `The commit message body contains lines greater than ${config.maxLineLength} characters`); return false;