Skip to content
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
18 changes: 12 additions & 6 deletions src/contributors/__tests__/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
10 changes: 8 additions & 2 deletions src/contributors/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/contributors/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function getQuestions(options, username, contributions) {
return options.contributors
.filter(
entry =>
entry.login &&
entry.login.toLowerCase() === answers.username.toLowerCase(),
)
.reduce(
Expand All @@ -48,6 +49,7 @@ function getQuestions(options, username, contributions) {
const previousContributions = options.contributors
.filter(
entry =>
entry.login &&
entry.login.toLowerCase() === answers.username.toLowerCase(),
)
.reduce(
Expand Down
10 changes: 10 additions & 0 deletions src/generate/__tests__/fixtures/contributors.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
10 changes: 10 additions & 0 deletions src/generate/__tests__/format-contribution-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
})
10 changes: 10 additions & 0 deletions src/generate/__tests__/format-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
16 changes: 12 additions & 4 deletions src/generate/format-contribution-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
)
}

Expand All @@ -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) {
Expand All @@ -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}`
}
}
24 changes: 16 additions & 8 deletions src/generate/format-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>',
)
Expand All @@ -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))
}
Expand Down