Skip to content
Open
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
4 changes: 1 addition & 3 deletions lib/add-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export default async function addContributor({
repository,
});
await contentFiles.fetch(config);
if (config.getOriginalSha() === undefined) {
contentFiles.init();
}
contentFiles.init();
contentFiles.generate(config);
const filesByPathToUpdate = contentFiles.get();

Expand Down
10 changes: 8 additions & 2 deletions lib/modules/content-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ export default class ContentFiles {
const newFilesByPath = modifyFiles({
contentFilesByPath: this.contentFilesByPath,
fileContentModifierFunction: function (content) {
const contentWithBadge = initBadge(content);
const contentWithList = initContributorsList(contentWithBadge);
const hasBadge =
content.includes('ALL-CONTRIBUTORS-BADGE:START') ||
content.includes('img.shields.io/badge/all_contributors-');
const hasList = content.includes('ALL-CONTRIBUTORS-LIST:START');
const contentWithBadge = hasBadge ? content : initBadge(content);
const contentWithList = hasList
? contentWithBadge
: initContributorsList(contentWithBadge);
Comment on lines +42 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if the badge isn't there but the list is, then it won't attempt to add the badge. I think that's probably ok, since the person may have intentionally removed the badge 👍, but just calling that out.

return contentWithList;
},
});
Expand Down
48 changes: 48 additions & 0 deletions test/integration/issue_comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,54 @@
expect(output).toMatchSnapshot('logs');
});

test('Initializes contributor markers when config exists but README is not set up', async () => {
const readmeWithoutContributorMarkers = {
...reposGetContentsREADMEMDdata,
content: Buffer.from(
'# Example project\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg)](#contributors-)\n',
).toString('base64'),
sha: 'readme-sha',
};

const mock = nock('https://api.github.com')
.get(
`/repos/all-contributors/all-contributors-bot/git/ref/heads%2Fall-contributors%2Fadd-jakebolam`,
)
.reply(404)
.get('/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master')
.reply(200, reposGetContentsAllContributorsRCdata)
.get('/users/jakebolam')
.reply(200, usersGetByUsernameJakeBolamdata)
.get('/repos/all-contributors/all-contributors-bot/contents/README.md?ref=master')
.reply(200, readmeWithoutContributorMarkers)
.get(`/repos/all-contributors/all-contributors-bot/git/ref/heads%2Fmaster`)
.reply(200, gitGetRefdata)
.post(`/repos/all-contributors/all-contributors-bot/git/refs`)
.reply(201, gitCreateRefdata)
.put(`/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc`)
.reply(200, reposUpdateFiledata)
.put(`/repos/all-contributors/all-contributors-bot/contents/README.md`)
.reply(200, async (request) => {
const updatedReadme = Buffer.from((await request.json()).content, 'base64').toString();
expect(updatedReadme).toContain('ALL-CONTRIBUTORS-LIST:START');
expect(updatedReadme.match(/ALL-CONTRIBUTORS-LIST:START/g)).toHaveLength(1);
expect(updatedReadme.match(/img\.shields\.io\/badge\/all_contributors-/g)).toHaveLength(1);
return reposUpdateFiledata;
})
.post(`/repos/all-contributors/all-contributors-bot/pulls`)
.reply(201, pullsCreatedata)
.post('/repos/all-contributors/all-contributors-bot/issues/1/comments')
.reply(200);

await probot.receive({
name: 'issue_comment',
id: '1',
payload: issueCommentCreatedPayload,
});

expect(mock.activeMocks()).toStrictEqual([]);
});

test('Happy path, add correct new contributor, no allcontributors file (repo needs init first)', async () => {
const mock = nock('https://api.github.com')
.get(
Expand Down Expand Up @@ -335,7 +383,7 @@
payload: issueCommentCreatedPayload,
});

expect(mock.activeMocks()).toStrictEqual([]);

Check failure on line 386 in test/integration/issue_comment.test.js

View workflow job for this annotation

GitHub Actions / Test

test/integration/issue_comment.test.js > issue_comment event > Happy path, add correct new contributor, but branch exists

AssertionError: expected [ Array(1) ] to strictly equal [] - Expected + Received - [] + [ + "POST https://api.github.com:443/repos/all-contributors/all-contributors-bot/pulls", + ] ❯ test/integration/issue_comment.test.js:386:32
expect(output).toMatchSnapshot('logs');
});

Expand Down Expand Up @@ -593,7 +641,7 @@
payload: issueCommentCreatedPayload,
});

expect(mock.activeMocks()).toStrictEqual([]);

Check failure on line 644 in test/integration/issue_comment.test.js

View workflow job for this annotation

GitHub Actions / Test

test/integration/issue_comment.test.js > issue_comment event > skipCi=false

AssertionError: expected [ Array(1) ] to strictly equal [] - Expected + Received - [] + [ + "POST https://api.github.com:443/repos/all-contributors/all-contributors-bot/pulls", + ] ❯ test/integration/issue_comment.test.js:644:32
expect(output).toMatchSnapshot('logs');
});

Expand Down Expand Up @@ -699,7 +747,7 @@
payload: issueCommentCreatedMultipleContributorsPayload,
});

expect(mock.activeMocks()).toStrictEqual([]);

Check failure on line 750 in test/integration/issue_comment.test.js

View workflow job for this annotation

GitHub Actions / Test

test/integration/issue_comment.test.js > issue_comment event > Happy path, add correct new multiple contributors

AssertionError: expected [ Array(1) ] to strictly equal [] - Expected + Received - [] + [ + "POST https://api.github.com:443/repos/all-contributors/all-contributors-bot/pulls", + ] ❯ test/integration/issue_comment.test.js:750:32
expect(output).toMatchSnapshot('logs');
});
});
Loading