From bb62cf24d29c0f70a4368853c89391295a592c97 Mon Sep 17 00:00:00 2001 From: Angel Aviel Domaoan <13580338+tenshiAMD@users.noreply.github.com> Date: Sun, 25 Sep 2022 09:37:54 +0800 Subject: [PATCH] fix: resolve commit message issue --- lib/add-contributor.js | 9 +++----- lib/modules/helpers.js | 15 ++++++++++++- test/unit/helpers.test.js | 47 ++++++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/lib/add-contributor.js b/lib/add-contributor.js index b9757fa8..bdd30d10 100644 --- a/lib/add-contributor.js +++ b/lib/add-contributor.js @@ -4,6 +4,8 @@ const getUserDetails = require("./get-user-details"); const ContentFiles = require("./modules/content-files"); const convertMessage = require("commit-conv"); +const { generatePrTitle } = require("./modules/helpers"); + async function addContributor({ context, commentReply, @@ -53,15 +55,10 @@ async function addContributor({ originalSha: config.getOriginalSha(), }; - const contributionsTitlePartLimit = 3 - const arr = contributions.slice(0, contributionsTitlePartLimit); - if (arr.length > contributionsTitlePartLimit) arr[arr.length - 1] = `${contributions.length - 2} more` - const contributionsTitlePartText = arr.slice(0, arr.length - 1).join(', ') + ", and " + arr.slice(-1); - const convention = config.get().commitConvention; const prTitle = convertMessage({ tag: "docs", - msg: `add ${who} as a contributor for ${contributionsTitlePartText}`, + msg: generatePrTitle(`add ${who} as a contributor`, contributions), convention }); diff --git a/lib/modules/helpers.js b/lib/modules/helpers.js index dc941f52..4a161933 100644 --- a/lib/modules/helpers.js +++ b/lib/modules/helpers.js @@ -1,11 +1,24 @@ function generateValidProfileLink(blog, githubProfileURL) { const validRegexWithScheme = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ const validRegexWithoutScheme = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ - if (validRegexWithScheme.test(blog)) return blog; if (validRegexWithoutScheme.test(blog)) return `http://${blog}`; + if (validRegexWithScheme.test(blog)) return blog; return githubProfileURL || '' } +function generatePrTitle(message, contributions) { + const contributionsTitlePartLimit = 3 + + let contributionsTitlePartText = contributions.join(', ') + + const arr = contributions.slice(0, contributionsTitlePartLimit); + if (contributions.length > contributionsTitlePartLimit) arr[arr.length - 1] = `${contributions.length - 2} more` + if (arr.length > 1) contributionsTitlePartText = arr.slice(0, arr.length - 1).join(', ') + ", and " + arr.slice(-1); + + return [message, `for ${contributionsTitlePartText}`].join(' ') +} + module.exports = { + generatePrTitle, generateValidProfileLink } diff --git a/test/unit/helpers.test.js b/test/unit/helpers.test.js index 0f62116f..bcd8ccf0 100644 --- a/test/unit/helpers.test.js +++ b/test/unit/helpers.test.js @@ -1,4 +1,39 @@ -const { generateValidProfileLink } = require('../../lib/modules/helpers'); +const { + generatePrTitle, + generateValidProfileLink, +} = require('../../lib/modules/helpers'); + +describe('generatePrTitle', () => { + const message = 'add tenshiAMD as a contributor'; + + test('returns valid message - with 1 contribution type/s', async () => { + let contributions = ['code']; + let validText = generatePrTitle(message, contributions); + + expect(validText).toEqual('add tenshiAMD as a contributor for code'); + }); + + test('returns valid message - with 2 contribution type/s', async () => { + let contributions = ['code', 'bug']; + let validText = generatePrTitle(message, contributions); + + expect(validText).toEqual('add tenshiAMD as a contributor for code, and bug'); + }); + + test('returns valid message - with 3 contribution type/s', async () => { + let contributions = ['code', 'bug', 'design']; + let validText = generatePrTitle(message, contributions); + + expect(validText).toEqual('add tenshiAMD as a contributor for code, bug, and design'); + }); + + test('returns valid message - with nth contribution type/s', async () => { + let contributions = ['code', 'bug', 'a11y', 'design', 'review']; + let validText = generatePrTitle(message, contributions); + + expect(validText).toEqual(`add tenshiAMD as a contributor for code, bug, and ${contributions.length - 2} more`); + }); +}); describe('generateValidProfileLink', () => { const githubProfileUrl = 'https://github.com/tenshiAMD' @@ -28,35 +63,35 @@ describe('generateValidProfileLink', () => { let url = 'tenshhttpiamd.com'; let validUrl = generateValidProfileLink(url, githubProfileUrl); - expect(validUrl).toEqual(url); + expect(validUrl).toEqual(`http://${url}`); }); test('returns valid link - valid URL format with `https` in between', async () => { let url = 'tenshhttpsiamd.com'; let validUrl = generateValidProfileLink(url, githubProfileUrl); - expect(validUrl).toEqual(url); + expect(validUrl).toEqual(`http://${url}`); }); test('returns valid link - no protocol', async () => { let url = 'tenshiamd.com'; let validUrl = generateValidProfileLink(url, githubProfileUrl); - expect(validUrl).toEqual(url); + expect(validUrl).toEqual(`http://${url}`); }); test('returns valid link - no protocol and starting with `http`', async () => { let url = 'httptenshiamd.com'; let validUrl = generateValidProfileLink(url, githubProfileUrl); - expect(validUrl).toEqual(url); + expect(validUrl).toEqual(`http://${url}`); }); test('returns valid link - no protocol and starting with `https`', async () => { let url = 'httpstenshiamd.com'; let validUrl = generateValidProfileLink(url, githubProfileUrl); - expect(validUrl).toEqual(url); + expect(validUrl).toEqual(`http://${url}`); }); test('returns valid link - incomplete URL format', async () => {