diff --git a/src/contributors/prompt.js b/src/contributors/prompt.js index 89555b8..f3bdbe7 100644 --- a/src/contributors/prompt.js +++ b/src/contributors/prompt.js @@ -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', @@ -88,7 +94,6 @@ function getValidUserContributions(options, contributions) { `${invalidUserContributions.toString()} is/are invalid contribution type(s)`, ) } - return validUserContributions } diff --git a/src/repo/__tests__/github.js b/src/repo/__tests__/github.js index e291b4e..478d50a 100644 --- a/src/repo/__tests__/github.js +++ b/src/repo/__tests__/github.js @@ -65,6 +65,30 @@ 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('Throw error when non existent username is provided', async () => { + const username = 'thisusernamedoesntexist' + nock('https://api.github.com') + .get(`/users/${username}`) + .reply(404, { + message: 'Not Found', + documentation_url: + 'https://developer.github.com/v3/users/#get-a-single-user', + }) + try { + await getUserInfo(username) + } catch (error) { + expect(error.message).toEqual( + `Login not found when adding a contributor for username - ${username}.`, + ) + } +}) + test('handle github errors', async () => { nock('https://api.github.com') .get('/users/nodisplayname') diff --git a/src/repo/github.js b/src/repo/github.js index dd59b49..b306965 100644 --- a/src/repo/github.js +++ b/src/repo/github.js @@ -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({ @@ -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}`