diff --git a/lib/add-contributor.js b/lib/add-contributor.js index 8a3d7fb3..bb8c63e7 100644 --- a/lib/add-contributor.js +++ b/lib/add-contributor.js @@ -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(); diff --git a/lib/modules/content-files.js b/lib/modules/content-files.js index b1357867..7a4c00ea 100644 --- a/lib/modules/content-files.js +++ b/lib/modules/content-files.js @@ -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); return contentWithList; }, }); diff --git a/test/integration/issue_comment.test.js b/test/integration/issue_comment.test.js index 2eb02e9b..2eba5fa3 100644 --- a/test/integration/issue_comment.test.js +++ b/test/integration/issue_comment.test.js @@ -117,6 +117,54 @@ describe('issue_comment event', () => { 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(