diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index 3cfc4852419dc3e2fa4e3428ce9f556bbac6e77a..e27e4dbe8933b2e4fdf585aa9f9d99d3390be80b 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -609,13 +609,18 @@ async function ensureIssue(title, body) { logger.debug(`ensureIssue()`); try { const issueList = await getIssueList(); - const issue = issueList.find(i => i.title === title); - if (issue) { + const issues = issueList.filter(i => i.title === title).reverse(); + if (issues.length) { + // Close any duplicates + for (const issue of issues.slice(1)) { + await closeIssue(issue.number); + } + const [issue] = issues; const issueBody = (await get( `repos/${config.parentRepo || config.repository}/issues/${issue.number}` )).body.body; if (issueBody !== body) { - logger.debug('Updating issue body'); + logger.info('Updating issue body'); await get.patch( `repos/${config.parentRepo || config.repository}/issues/${ issue.number @@ -627,6 +632,7 @@ async function ensureIssue(title, body) { return 'updated'; } } else { + logger.info('Creating issue'); await get.post(`repos/${config.parentRepo || config.repository}/issues`, { body: { title, @@ -647,20 +653,22 @@ async function ensureIssue(title, body) { return null; } +async function closeIssue(issueNumber) { + await get.patch( + `repos/${config.parentRepo || config.repository}/issues/${issueNumber}`, + { + body: { state: 'closed' }, + } + ); +} + async function ensureIssueClosing(title) { logger.debug(`ensureIssueClosing()`); const issueList = await getIssueList(); for (const issue of issueList) { if (issue.title === title) { logger.info({ issue }, 'Closing issue'); - await get.patch( - `repos/${config.parentRepo || config.repository}/issues/${ - issue.number - }`, - { - body: { state: 'closed' }, - } - ); + await closeIssue(issue.number); } } } diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 888442d47769944fbc3dc1558f653dca176c5c40..7a238f598cfb3153dc53e7b94639def5a3258cf6 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -973,6 +973,23 @@ describe('platform/github', () => { const res = await github.ensureIssue('title-2', 'newer-content'); expect(res).toBe(null); }); + it('deletes if duplicate', async () => { + get.mockReturnValueOnce({ + body: [ + { + number: 1, + title: 'title-1', + }, + { + number: 2, + title: 'title-1', + }, + ], + }); + get.mockReturnValueOnce({ body: { body: 'newer-content' } }); + const res = await github.ensureIssue('title-1', 'newer-content'); + expect(res).toBe(null); + }); }); describe('ensureIssueClosing()', () => { it('closes issue', async () => {