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

* feat(repo): github profile url should be valid

* docs: add k3nsei as a contributor
This commit is contained in:
Piotr Stępniewski 2020-06-07 22:38:12 +02:00 committed by GitHub
parent 649f6ceebc
commit b77f86bf7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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.
@ -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 (
@ -112,7 +113,7 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
)
}
profile = profile.startsWith('http') ? profile : `http://${profile}`
profile = parseHttpUrl(profile)
return {
login: body.login,

49
src/util/__tests__/url.js Normal file
View file

@ -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')
})

View file

@ -3,4 +3,5 @@ module.exports = {
contributionTypes: require('./contribution-types'),
git: require('./git'),
markdown: require('./markdown'),
url: require('./url'),
}

33
src/util/url.js Normal file
View file

@ -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
}