From dae0a6243ab23c94f35a61b9ae1a241601870ed3 Mon Sep 17 00:00:00 2001 From: "P.S NIKHILESH" Date: Mon, 16 Nov 2026 08:13:27 +0530 Subject: [PATCH 1/2] fix: initialize missing contributor table --- lib/add-contributor.js | 5 ++- lib/modules/content-files.js | 17 +++++++-- test/integration/issue_comment.test.js | 48 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/add-contributor.js b/lib/add-contributor.js index 8a3d7fb3..f454a5e4 100644 --- a/lib/add-contributor.js +++ b/lib/add-contributor.js @@ -43,9 +43,8 @@ export default async function addContributor({ repository, }); await contentFiles.fetch(config); - if (config.getOriginalSha() === undefined) { - contentFiles.init(); - } + const hasConfig = config.getOriginalSha() !== undefined; + contentFiles.init({ force: !hasConfig, preserveExistingBadge: hasConfig }); contentFiles.generate(config); const filesByPathToUpdate = contentFiles.get(); diff --git a/lib/modules/content-files.js b/lib/modules/content-files.js index b1357867..8ea0b2bb 100644 --- a/lib/modules/content-files.js +++ b/lib/modules/content-files.js @@ -31,12 +31,23 @@ export default class ContentFiles { this.contentFilesByPath = await this.repository.getMultipleFiles(options.files); } - init() { + init({ force = false, preserveExistingBadge = false } = {}) { const newFilesByPath = modifyFiles({ contentFilesByPath: this.contentFilesByPath, fileContentModifierFunction: function (content) { - const contentWithBadge = initBadge(content); - const contentWithList = initContributorsList(contentWithBadge); + if (force) { + const contentWithBadge = initBadge(content); + return initContributorsList(contentWithBadge); + } + + const hasBadge = + content.includes('ALL-CONTRIBUTORS-BADGE:START') || + (preserveExistingBadge && 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( From 210eb1e02e70b4ae4ea3c7c6d85e4e0a4e9bb54e Mon Sep 17 00:00:00 2001 From: "P.S NIKHILESH" Date: Mon, 16 Nov 2026 17:52:58 +0530 Subject: [PATCH 2/2] fix: initialize contributor markers from content --- lib/add-contributor.js | 3 +-- lib/modules/content-files.js | 9 ++------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/add-contributor.js b/lib/add-contributor.js index f454a5e4..bb8c63e7 100644 --- a/lib/add-contributor.js +++ b/lib/add-contributor.js @@ -43,8 +43,7 @@ export default async function addContributor({ repository, }); await contentFiles.fetch(config); - const hasConfig = config.getOriginalSha() !== undefined; - contentFiles.init({ force: !hasConfig, preserveExistingBadge: hasConfig }); + contentFiles.init(); contentFiles.generate(config); const filesByPathToUpdate = contentFiles.get(); diff --git a/lib/modules/content-files.js b/lib/modules/content-files.js index 8ea0b2bb..7a4c00ea 100644 --- a/lib/modules/content-files.js +++ b/lib/modules/content-files.js @@ -31,18 +31,13 @@ export default class ContentFiles { this.contentFilesByPath = await this.repository.getMultipleFiles(options.files); } - init({ force = false, preserveExistingBadge = false } = {}) { + init() { const newFilesByPath = modifyFiles({ contentFilesByPath: this.contentFilesByPath, fileContentModifierFunction: function (content) { - if (force) { - const contentWithBadge = initBadge(content); - return initContributorsList(contentWithBadge); - } - const hasBadge = content.includes('ALL-CONTRIBUTORS-BADGE:START') || - (preserveExistingBadge && content.includes('img.shields.io/badge/all_contributors-')); + 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