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

feat(repo): github profile url should be valid #268

Merged
merged 2 commits into from
Jun 7, 2020
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
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,17 @@
"contributions": [
"platform"
]
},
{
"login": "k3nsei",
"name": "Piotr Stępniewski",
"avatar_url": "https://avatars2.githubusercontent.com/u/190422?v=4",
"profile": "https://github.com/k3nsei",
"contributions": [
"bug",
"code",
"test"
]
}
],
"skipCi": true
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ Thanks goes to these wonderful people
<td align="center"><a href="https://anandchowdhary.com/?utm_source=github&utm_campaign=about-link"><img src="https://avatars3.githubusercontent.com/u/2841780?v=4" width="100px;" alt=""/><br /><sub><b>Anand Chowdhary</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/commits?author=AnandChowdhary" title="Tests">⚠️</a> <a href="https://github.com/all-contributors/all-contributors-cli/issues?q=author%3AAnandChowdhary" title="Bug reports">🐛</a> <a href="https://github.com/all-contributors/all-contributors-cli/commits?author=AnandChowdhary" title="Code">💻</a></td>
<td align="center"><a href="https://phacks.dev/"><img src="https://avatars1.githubusercontent.com/u/2587348?v=4" width="100px;" alt=""/><br /><sub><b>Nicolas Goutay</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/commits?author=phacks" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/tylerkrupicka"><img src="https://avatars1.githubusercontent.com/u/5761061?s=460&v=4" width="100px;" alt=""/><br /><sub><b>Tyler Krupicka</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/commits?author=tylerkrupicka" title="Code">💻</a> <a href="https://github.com/all-contributors/all-contributors-cli/commits?author=tylerkrupicka" title="Tests">⚠️</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/smoia"><img src="https://avatars3.githubusercontent.com/u/35300580?v=4" width="100px;" alt=""/><br /><sub><b>Stefano Moia</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/commits?author=smoia" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ilai-deutel"><img src="https://avatars0.githubusercontent.com/u/10098207?v=4" width="100px;" alt=""/><br /><sub><b>Ilaï Deutel</b></sub></a><br /><a href="#platform-ilai-deutel" title="Packaging/porting to new platform">📦</a></td>
<td align="center"><a href="https://github.com/k3nsei"><img src="https://avatars2.githubusercontent.com/u/190422?v=4" width="100px;" alt=""/><br /><sub><b>Piotr Stępniewski</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/issues?q=author%3Ak3nsei" title="Bug reports">🐛</a> <a href="https://github.com/all-contributors/all-contributors-cli/commits?author=k3nsei" title="Code">💻</a> <a href="https://github.com/all-contributors/all-contributors-cli/commits?author=k3nsei" title="Tests">⚠️</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the
Expand Down
5 changes: 3 additions & 2 deletions src/repo/github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const url = require('url')
const pify = require('pify')
const request = pify(require('request'))
const { parseHttpUrl, isValidHttpUrl } = require('../util/url')

/**
* Get the host based on public or enterprise GitHub.
Expand Down Expand Up @@ -93,7 +94,7 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
.then(res => {
const body = JSON.parse(res.body)

let profile = body.blog || body.html_url
let profile = isValidHttpUrl(body.blog) ? body.blog : body.html_url

// Check for authentication required
if (
Expand All @@ -112,7 +113,7 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
)
}

profile = profile.startsWith('http') ? profile : `http://${profile}`
profile = parseHttpUrl(profile)

return {
login: body.login,
Expand Down
49 changes: 49 additions & 0 deletions src/util/__tests__/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import url from '../url';

test(`Result of protocol validation should be true`, () => {
expect(url.isHttpProtocol('http:')).toBe(true)
expect(url.isHttpProtocol('https:')).toBe(true)
})

test(`Result of protocol validation should be false`, () => {
expect(url.isHttpProtocol('ftp:')).toBe(false)
})

test(`Result of url validation should be true`, () => {
expect(url.isValidHttpUrl('https://api.github.com/users/octocat')).toBe(true)
})

test(`Result of url validation should be false when url uses wrong protocol`, () => {
expect(url.isValidHttpUrl('git://git@github.com:all-contributors/all-contributors-cli.git')).toBe(false)
})

test(`Result of url validation should be false when input isn't url`, () => {
expect(url.isValidHttpUrl('github-octocat')).toBe(false)
})

test(`Result of parsed url should be equal`, () => {
const input = 'https://api.github.com/users/octocat'
const expected = 'https://api.github.com/users/octocat'
expect(url.parseHttpUrl(input)).toBe(expected)
})

test(`Result of parsed url without protocol should be equal`, () => {
const input = 'example.com'
const expected = 'http://example.com/'
expect(url.parseHttpUrl(input)).toBe(expected)
})

test(`Throw an error when parsed input isn't a string`, () => {
const input = 123
expect(url.parseHttpUrl.bind(null, input)).toThrowError('input must be a string')
})

test(`Throw an error when parsed url has wrong protocol`, () => {
const input = 'ftp://domain.xyz'
expect(url.parseHttpUrl.bind(null, input)).toThrowError('Provided URL has an invalid protocol')
})

test(`Throw an error when parsed input isn't a URL`, () => {
const input = 'some string'
expect(url.parseHttpUrl.bind(null, input)).toThrowError('Invalid URL: http://some string')
})
1 change: 1 addition & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
contributionTypes: require('./contribution-types'),
git: require('./git'),
markdown: require('./markdown'),
url: require('./url'),
}
33 changes: 33 additions & 0 deletions src/util/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function isHttpProtocol(input) {
return new RegExp('^https?\\:?$').test(input)
}

function isValidHttpUrl(input) {
try {
const url = new URL(input)

return isHttpProtocol(url.protocol)
} catch (e) {
return false
}
}

function parseHttpUrl(input) {
if (typeof input !== 'string') {
throw new TypeError('input must be a string')
}

const url = new URL(new RegExp('^\\w+\\:\\/\\/').test(input) ? input : `http://${input}`)

if (!isHttpProtocol(url.protocol)) {
throw new TypeError('Provided URL has an invalid protocol')
}

return url.toString()
}

module.exports = {
isHttpProtocol,
isValidHttpUrl,
parseHttpUrl
}