Skip to content

Commit

Permalink
fix: resolve invalid link for profile (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenshiAMD authored Sep 23, 2022
1 parent 0839201 commit a82aa37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/get-user-details.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = getUserDetails;

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

async function getUserDetails({ octokit, username }) {
// TODO: optimization, if commenting user is the user we're adding we can avoid an api call
Expand Down Expand Up @@ -32,6 +33,6 @@ async function getUserDetails({ octokit, username }) {
login,
name: name || username,
avatar_url,
profile: blog || html_url,
profile: generateValidLink(blog || html_url, username),
};
}
13 changes: 13 additions & 0 deletions lib/modules/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function generateValidLink(url, username = '') {
let validLink = url
const validRegex = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/

if (!validLink.startsWith("http")) validLink = `http://${url}/`
if (!validLink.match(validRegex)) validLink = `https://github.com/${username}/`

return validLink
}

module.exports = {
generateValidLink
}
19 changes: 19 additions & 0 deletions test/unit/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { generateValidLink } = require('../../lib/modules/helpers');

describe('generateValidLink', () => {
const username = 'tenshiAMD'

test('return valid link - no protocol', async () => {
let url = 'tenshiamd.com';
let validUrl = generateValidLink(url, username);

expect(validUrl).toEqual(`http://${url}/`);
});

test('return valid link - incomplete URL format', async () => {
let url = 'contributor';
let validUrl = generateValidLink(url, username);

expect(validUrl).toEqual(`https://github.com/${username}/`);
});
});

1 comment on commit a82aa37

@vercel
Copy link

@vercel vercel bot commented on a82aa37 Sep 23, 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.