Skip to content

Commit a82aa37

Browse files
authored
fix: resolve invalid link for profile (#421)
1 parent 0839201 commit a82aa37

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

lib/get-user-details.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = getUserDetails;
22

33
const { UserNotFoundError } = require("./modules/errors");
4+
const { generateValidLink } = require("./modules/helpers");
45

56
async function getUserDetails({ octokit, username }) {
67
// TODO: optimization, if commenting user is the user we're adding we can avoid an api call
@@ -32,6 +33,6 @@ async function getUserDetails({ octokit, username }) {
3233
login,
3334
name: name || username,
3435
avatar_url,
35-
profile: blog || html_url,
36+
profile: generateValidLink(blog || html_url, username),
3637
};
3738
}

lib/modules/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function generateValidLink(url, username = '') {
2+
let validLink = url
3+
const validRegex = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/
4+
5+
if (!validLink.startsWith("http")) validLink = `http://${url}/`
6+
if (!validLink.match(validRegex)) validLink = `https://github.com/${username}/`
7+
8+
return validLink
9+
}
10+
11+
module.exports = {
12+
generateValidLink
13+
}

test/unit/helpers.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { generateValidLink } = require('../../lib/modules/helpers');
2+
3+
describe('generateValidLink', () => {
4+
const username = 'tenshiAMD'
5+
6+
test('return valid link - no protocol', async () => {
7+
let url = 'tenshiamd.com';
8+
let validUrl = generateValidLink(url, username);
9+
10+
expect(validUrl).toEqual(`http://${url}/`);
11+
});
12+
13+
test('return valid link - incomplete URL format', async () => {
14+
let url = 'contributor';
15+
let validUrl = generateValidLink(url, username);
16+
17+
expect(validUrl).toEqual(`https://github.com/${username}/`);
18+
});
19+
});

0 commit comments

Comments
 (0)