Skip to content

Commit

Permalink
fix: resolve commit message issue (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenshiAMD authored Sep 25, 2022
1 parent 629c5b8 commit 89953af
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
9 changes: 3 additions & 6 deletions lib/add-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
});

Expand Down
15 changes: 14 additions & 1 deletion lib/modules/helpers.js
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 41 additions & 6 deletions test/unit/helpers.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 () => {
Expand Down

1 comment on commit 89953af

@vercel
Copy link

@vercel vercel bot commented on 89953af Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.