mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
fix: support contributors without github accounts (#77)
This commit is contained in:
parent
6fa55d5bd5
commit
5f9fa20346
8 changed files with 80 additions and 20 deletions
|
@ -26,6 +26,12 @@ function fixtures() {
|
|||
profile: 'www.profile.url',
|
||||
contributions: [{type: 'blog', url: 'www.blog.url/path'}, 'code'],
|
||||
},
|
||||
{
|
||||
name: 'Missing Login',
|
||||
avatar_url: 'www.avatar.url',
|
||||
profile: 'www.profile.url',
|
||||
contributions: ['code'],
|
||||
},
|
||||
],
|
||||
}
|
||||
return {options}
|
||||
|
@ -71,8 +77,8 @@ test('add new contributor at the end of the list of contributors', () => {
|
|||
|
||||
return addContributor(options, username, contributions, mockInfoFetcher).then(
|
||||
contributors => {
|
||||
expect(contributors.length).toBe(3)
|
||||
expect(contributors[2]).toEqual({
|
||||
expect(contributors.length).toBe(options.contributors.length + 1)
|
||||
expect(contributors[options.contributors.length]).toEqual({
|
||||
login: 'login3',
|
||||
name: 'Some name',
|
||||
avatar_url: 'www.avatar.url',
|
||||
|
@ -91,8 +97,8 @@ test('add new contributor at the end of the list of contributors with a url link
|
|||
|
||||
return addContributor(options, username, contributions, mockInfoFetcher).then(
|
||||
contributors => {
|
||||
expect(contributors.length).toBe(3)
|
||||
expect(contributors[2]).toEqual({
|
||||
expect(contributors.length).toBe(options.contributors.length + 1)
|
||||
expect(contributors[options.contributors.length]).toEqual({
|
||||
login: 'login3',
|
||||
name: 'Some name',
|
||||
avatar_url: 'www.avatar.url',
|
||||
|
@ -133,7 +139,7 @@ test(`should update an existing contributor's contributions if a new type is add
|
|||
const contributions = ['bug']
|
||||
return addContributor(options, username, contributions, mockInfoFetcher).then(
|
||||
contributors => {
|
||||
expect(contributors.length).toBe(2)
|
||||
expect(contributors.length).toBe(options.contributors.length)
|
||||
expect(contributors[0]).toEqual({
|
||||
login: 'login1',
|
||||
name: 'Some name',
|
||||
|
@ -171,7 +177,7 @@ test(`should update an existing contributor's contributions if a new type is add
|
|||
|
||||
return addContributor(options, username, contributions, mockInfoFetcher).then(
|
||||
contributors => {
|
||||
expect(contributors.length).toBe(2)
|
||||
expect(contributors.length).toBe(options.contributors.length)
|
||||
expect(contributors[0]).toEqual({
|
||||
login: 'login1',
|
||||
name: 'Some name',
|
||||
|
|
|
@ -27,7 +27,10 @@ function updateContributor(options, contributor, contributions) {
|
|||
|
||||
function updateExistingContributor(options, username, contributions) {
|
||||
return options.contributors.map(contributor => {
|
||||
if (username.toLowerCase() !== contributor.login.toLowerCase()) {
|
||||
if (
|
||||
!contributor.login ||
|
||||
username.toLowerCase() !== contributor.login.toLowerCase()
|
||||
) {
|
||||
return contributor
|
||||
}
|
||||
return updateContributor(options, contributor, contributions)
|
||||
|
@ -51,7 +54,10 @@ module.exports = function addContributor(
|
|||
) {
|
||||
// case insensitive find
|
||||
const exists = _.find(contributor => {
|
||||
return contributor.login.toLowerCase() === username.toLowerCase()
|
||||
return (
|
||||
contributor.login &&
|
||||
contributor.login.toLowerCase() === username.toLowerCase()
|
||||
)
|
||||
}, options.contributors)
|
||||
|
||||
if (exists) {
|
||||
|
|
|
@ -35,6 +35,7 @@ function getQuestions(options, username, contributions) {
|
|||
return options.contributors
|
||||
.filter(
|
||||
entry =>
|
||||
entry.login &&
|
||||
entry.login.toLowerCase() === answers.username.toLowerCase(),
|
||||
)
|
||||
.reduce(
|
||||
|
@ -48,6 +49,7 @@ function getQuestions(options, username, contributions) {
|
|||
const previousContributions = options.contributors
|
||||
.filter(
|
||||
entry =>
|
||||
entry.login &&
|
||||
entry.login.toLowerCase() === answers.username.toLowerCase(),
|
||||
)
|
||||
.reduce(
|
||||
|
|
|
@ -19,5 +19,15 @@
|
|||
"profile": "http://github.com/chrisinajar",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/1500684",
|
||||
"contributions": ["doc"]
|
||||
},
|
||||
"nologin": {
|
||||
"name": "No Github Account",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/1500684",
|
||||
"contributions": ["translation"]
|
||||
},
|
||||
"nologin_badrole": {
|
||||
"name": "Wildly Misconfigured",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/1500684",
|
||||
"contributions": ["plumbis"]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -153,3 +153,13 @@ test('throw a helpful error on unknown type', () => {
|
|||
formatContributionType(options, contributor, 'docs'),
|
||||
).toThrowError('Unknown contribution type docs for contributor kentcdodds')
|
||||
})
|
||||
|
||||
test('throw a helpful error on unknown type and no login', () => {
|
||||
const contributor = contributors.nologin_badrole
|
||||
const {options} = fixtures()
|
||||
expect(() =>
|
||||
formatContributionType(options, contributor, 'docs'),
|
||||
).toThrowError(
|
||||
'Unknown contribution type docs for contributor Wildly Misconfigured',
|
||||
)
|
||||
})
|
||||
|
|
|
@ -65,3 +65,13 @@ test('format contributor with pipes in their name', () => {
|
|||
|
||||
expect(formatContributor(options, contributor)).toBe(expected)
|
||||
})
|
||||
|
||||
test('format contributor with no github account', () => {
|
||||
const contributor = contributors.nologin
|
||||
const {options} = fixtures()
|
||||
|
||||
const expected =
|
||||
'<img src="https://avatars1.githubusercontent.com/u/1500684" width="150px;"/><br /><sub><b>No Github Account</b></sub><br />[🌍](#translation "Translation")'
|
||||
|
||||
expect(formatContributor(options, contributor)).toBe(expected)
|
||||
})
|
||||
|
|
|
@ -19,9 +19,8 @@ module.exports = function formatContribution(
|
|||
|
||||
if (!type) {
|
||||
throw new Error(
|
||||
`Unknown contribution type ${contribution} for contributor ${
|
||||
contributor.login
|
||||
}`,
|
||||
`Unknown contribution type ${contribution} for contributor ${contributor.login ||
|
||||
contributor.name}`,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -32,7 +31,8 @@ module.exports = function formatContribution(
|
|||
options,
|
||||
}
|
||||
|
||||
let url = `#${contribution}-${contributor.login}`
|
||||
let url = getUrl(contribution, contributor)
|
||||
|
||||
if (contribution.url) {
|
||||
url = contribution.url
|
||||
} else if (type.link) {
|
||||
|
@ -41,3 +41,11 @@ module.exports = function formatContribution(
|
|||
|
||||
return linkTemplate(_.assign({url}, templateData))
|
||||
}
|
||||
|
||||
function getUrl(contribution, contributor) {
|
||||
if (contributor.login) {
|
||||
return `#${contribution}-${contributor.login}`
|
||||
} else {
|
||||
return `#${contribution}`
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,9 @@ const avatarTemplate = _.template(
|
|||
const avatarBlockTemplate = _.template(
|
||||
'[<%= avatar %><br /><sub><b><%= name %></b></sub>](<%= contributor.profile %>)',
|
||||
)
|
||||
const avatarBlockTemplateNoProfile = _.template(
|
||||
'<%= avatar %><br /><sub><b><%= name %></b></sub>',
|
||||
)
|
||||
const contributorTemplate = _.template(
|
||||
'<%= avatarBlock %><br /><%= contributions %>',
|
||||
)
|
||||
|
@ -15,15 +18,20 @@ const defaultImageSize = 100
|
|||
|
||||
function defaultTemplate(templateData) {
|
||||
const avatar = avatarTemplate(templateData)
|
||||
const avatarBlock = avatarBlockTemplate(
|
||||
_.assign(
|
||||
{
|
||||
name: escapeName(templateData.contributor.name),
|
||||
avatar,
|
||||
},
|
||||
templateData,
|
||||
),
|
||||
const avatarBlockTemplateData = _.assign(
|
||||
{
|
||||
name: escapeName(templateData.contributor.name),
|
||||
avatar,
|
||||
},
|
||||
templateData,
|
||||
)
|
||||
let avatarBlock = null
|
||||
|
||||
if (templateData.contributor.profile) {
|
||||
avatarBlock = avatarBlockTemplate(avatarBlockTemplateData)
|
||||
} else {
|
||||
avatarBlock = avatarBlockTemplateNoProfile(avatarBlockTemplateData)
|
||||
}
|
||||
|
||||
return contributorTemplate(_.assign({avatarBlock}, templateData))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue