Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve commit message issue #423

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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