-
Notifications
You must be signed in to change notification settings - Fork 148
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: add stricter checks in link for profile
#422
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
2f5ef0b
to
a1e728c
Compare
Codecov ReportBase: 100.00% // Head: 100.00% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #422 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 16 16
Lines 391 390 -1
Branches 45 44 -1
=========================================
- Hits 391 390 -1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
a1e728c
to
c1201a6
Compare
c1201a6
to
08c21b8
Compare
lib/modules/helpers.js
Outdated
const validRegexWithoutProtocol = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ | ||
|
||
if (validLink.match(validRegexWithoutProtocol)) validLink = `http://${url}/` | ||
if (!validLink.match(validRegex)) validLink = `https://github.com/${validUsername}/` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One suggestion here, this is basically creating html_url
that was already in the calling function. If how we call this function is changed like
getUserLink(blog, html_url)
This function can be changed into
function getUserLink(blog, githubProfileURL) {
const validRegexWithScheme = /same as now/
const validRegexWithoutScheme = /same as now/
if (validRegexWithProtocol.test(blog)) return blog;
if (validRegexWithoutProtocol.test(blog)) return `http://${blog}`;
return githubProfileURL
}
This way, someone reading the code in the future only have to think about which return
would be triggered.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Roshanjossey I somehow agree however we dp have tests that somehow act as the documentation for that method. I do not think this is necessary to add more lines. I like to simplify things rather than make them complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rephrase what I commented earlier. I'm suggesting to change
function generateValidLink(url, username) {
let validLink = url
let validUsername = username || 'all-contributors'
const validRegex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
const validRegexWithoutProtocol = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
if (validLink.match(validRegexWithoutProtocol)) validLink = `http://${url}/`
if (!validLink.match(validRegex)) validLink = `https://github.com/${validUsername}/`
return validLink
}
to
function getUserLink(blog, githubProfileURL) {
const validRegexWithScheme = /same as now/
const validRegexWithoutScheme = /same as now/
if (validRegexWithSchema.test(blog)) return blog;
if (validRegexWithoutSchema.test(blog)) return `http://${blog}`;
return githubProfileURL
}
Isn't this simpler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rephrase what I commented earlier. I'm suggesting to change
function generateValidLink(url, username) { let validLink = url let validUsername = username || 'all-contributors' const validRegex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ const validRegexWithoutProtocol = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ if (validLink.match(validRegexWithoutProtocol)) validLink = `http://${url}/` if (!validLink.match(validRegex)) validLink = `https://github.com/${validUsername}/` return validLink }to
function getUserLink(blog, githubProfileURL) { const validRegexWithScheme = /same as now/ const validRegexWithoutScheme = /same as now/ if (validRegexWithSchema.test(blog)) return blog; if (validRegexWithoutSchema.test(blog)) return `http://${blog}`; return githubProfileURL }Isn't this simpler?
@Roshanjossey I tried this earlier and 7 tests I made are failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you change the params for calling the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FAIL test/unit/helpers.test.js
● generateValidLink › returns valid link - valid URL format with http
in between
expect(received).toEqual(expected) // deep equality
Expected: "http://tenshhttpiamd.com/"
Received: "tenshhttpiamd.com"
29 | let validUrl = generateValidLink(url, username);
30 |
> 31 | expect(validUrl).toEqual(`http://${url}/`);
| ^
32 | });
33 |
34 | test('returns valid link - valid URL format with `https` in between', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:31:22)
● generateValidLink › returns valid link - valid URL format with https
in between
expect(received).toEqual(expected) // deep equality
Expected: "http://tenshhttpsiamd.com/"
Received: "tenshhttpsiamd.com"
36 | let validUrl = generateValidLink(url, username);
37 |
> 38 | expect(validUrl).toEqual(`http://${url}/`);
| ^
39 | });
40 |
41 | test('returns valid link - no protocol', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:38:22)
● generateValidLink › returns valid link - no protocol
expect(received).toEqual(expected) // deep equality
Expected: "http://tenshiamd.com/"
Received: "tenshiamd.com"
43 | let validUrl = generateValidLink(url, username);
44 |
> 45 | expect(validUrl).toEqual(`http://${url}/`);
| ^
46 | });
47 |
48 | test('returns valid link - no protocol and starting with `http`', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:45:22)
● generateValidLink › returns valid link - no protocol and starting with http
expect(received).toEqual(expected) // deep equality
Expected: "http://httptenshiamd.com/"
Received: "httptenshiamd.com"
50 | let validUrl = generateValidLink(url, username);
51 |
> 52 | expect(validUrl).toEqual(`http://${url}/`);
| ^
53 | });
54 |
55 | test('returns valid link - no protocol and starting with `https`', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:52:22)
● generateValidLink › returns valid link - no protocol and starting with https
expect(received).toEqual(expected) // deep equality
Expected: "http://httpstenshiamd.com/"
Received: "httpstenshiamd.com"
57 | let validUrl = generateValidLink(url, username);
58 |
> 59 | expect(validUrl).toEqual(`http://${url}/`);
| ^
60 | });
61 |
62 | test('returns valid link - incomplete URL format', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:59:22)
● generateValidLink › returns valid link - incomplete URL format
expect(received).toEqual(expected) // deep equality
Expected: "https://github.com/tenshiAMD/"
Received: "tenshiAMD"
64 | let validUrl = generateValidLink(url, username);
65 |
> 66 | expect(validUrl).toEqual(`https://github.com/${username}/`);
| ^
67 | });
68 |
69 | test('returns valid link - incomplete URL format with `null` username', async () => {
at Object.<anonymous> (test/unit/helpers.test.js:66:22)
● generateValidLink › returns valid link - incomplete URL format with null
username
expect(received).toEqual(expected) // deep equality
Expected: "https://github.com/all-contributors/"
Received: null
71 | let validUrl = generateValidLink(url, null);
72 |
> 73 | expect(validUrl).toEqual(`https://github.com/all-contributors/`);
| ^
74 | });
75 | });
76 |
at Object.<anonymous> (test/unit/helpers.test.js:73:22)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let validUrl = generateValidLink(url, username);
This is still the same parameters, the function I suggested takes in html_url
as second param.
I think there's a communication problem here. Do you wanna get on a call and sort this out?
https://meet.jit.si/open
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Roshanjossey hmm I see I still need to change my tests 😕 though it's the same implementation, this just takes my bandwidth. :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could create a PR if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could create a PR if you want
@Roshanjossey No need 😄 it will just take your time and I added some tweaks. LGTM now. I will just add you as a co-author.
🎉 This PR is included in version 1.16.3 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
What:
profile
CC @Roshanjossey
Why:
How:
Checklist:
Bot Usage