Skip to content

Commit

Permalink
# This is a combination of 3 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:
fix:all-contributors#6 added common error handling for username not provided or wrong username provided.

# This is the commit message all-contributors#2:

fix:all-contributors#170 added tests for no username case in github getInfo api.

# This is the commit message all-contributors#3:

fix:all-contributors#170 added tests for no username case in github getInfo api.
  • Loading branch information
erdahuja committed Feb 25, 2019
1 parent e0240de commit 28c933e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/contributors/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ function getQuestions(options, username, contributions) {
options.repoType,
)} username?`,
when: !username,
validate: function validate(input) {
if (!input) {
return 'Username not provided'
}
return true
},
},
{
type: 'checkbox',
Expand Down Expand Up @@ -88,7 +94,6 @@ function getValidUserContributions(options, contributions) {
`${invalidUserContributions.toString()} is/are invalid contribution type(s)`,
)
}

return validUserContributions
}

Expand Down
4 changes: 4 additions & 0 deletions src/repo/__tests__/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ test('handle errors', async () => {
await rejects(getUserInfo('nodisplayname'))
})

test('Throw error when no username is provided', () => {
expect(getUserInfo).toThrow('No login when adding a contributor. Please specify a username.')
})

test('handle github errors', async () => {
nock('https://api.github.com')
.get('/users/nodisplayname')
Expand Down
11 changes: 10 additions & 1 deletion src/repo/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
hostname = 'https://github.com'
}

if (!username) {
throw new Error(
`No login when adding a contributor. Please specify a username.`,
)
}

const root = hostname.replace(/:\/\//, '://api.')
return request
.get({
Expand All @@ -68,11 +74,14 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) {
})
.then(res => {
const body = JSON.parse(res.body)

let profile = body.blog || body.html_url

// Github throwing specific errors as 200...
if (!profile && body.message) {
throw new Error(body.message)
throw new Error(
`Login not found when adding a contributor for username - ${username}.`,
)
}

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

0 comments on commit 28c933e

Please sign in to comment.