diff --git a/lib/platform/bitbucket/index.js b/lib/platform/bitbucket/index.js index 2e964c3f73698fe31de06f12d22b767a23fb2223..aaadba6f33fbec2ae28b2230875dd3c4b72e1eff 100644 --- a/lib/platform/bitbucket/index.js +++ b/lib/platform/bitbucket/index.js @@ -27,6 +27,7 @@ module.exports = { mergeBranch, getBranchLastCommitTime, // Issue + findIssue, ensureIssue, ensureIssueClosing, addAssignees, @@ -291,6 +292,10 @@ function mergeBranch() { return Promise.reject(new Error('Branch automerge not supported')); } +function findIssue() { + logger.warn('findIssue not implemented yet'); + return Promise.resolve(); +} function ensureIssue() { logger.warn('ensureIssue not implemented yet'); return Promise.resolve(); diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index a786e3999a818f17cb49dff8a635a7e6d41623e0..6ce7ef36337a7928bd411d378ac00289fe1d56f1 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -32,6 +32,7 @@ module.exports = { mergeBranch, getBranchLastCommitTime, // issue + findIssue, ensureIssue, ensureIssueClosing, addAssignees, @@ -540,6 +541,26 @@ async function getIssueList() { return config.issueList; } +async function findIssue(title) { + logger.debug(`findIssue(${title})`); + try { + const [issue] = (await getIssueList()).filter(i => i.title === title); + if (!issue) { + return null; + } + const issueBody = (await get( + `repos/${config.parentRepo || config.repository}/issues/${issue.number}` + )).body.body; + return { + number: issue.number, + body: issueBody, + }; + } catch (err) /* istanbul ignore next */ { + logger.warn('Error finding issue'); + return null; + } +} + async function ensureIssue(title, body) { logger.debug(`ensureIssue()`); try { diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 51afaad3ffab57ba5fb79ea8a8f408f657b40bb4..5b8beb3546d6658337d60129d5a7805717e17780 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -27,6 +27,7 @@ module.exports = { mergeBranch, getBranchLastCommitTime, // issue + findIssue, ensureIssue, ensureIssueClosing, addAssignees, @@ -414,6 +415,27 @@ async function getIssueList() { return config.issueList; } +async function findIssue(title) { + logger.debug(`findIssue(${title})`); + try { + const issueList = await getIssueList(); + const issue = issueList.find(i => i.title === title); + if (!issue) { + return null; + } + const issueBody = (await get( + `projects/${config.repository}/issues/${issue.iid}` + )).body.body; + return { + number: issue.iid, + body: issueBody, + }; + } catch (err) /* istanbul ignore next */ { + logger.warn('Error finding issue'); + return null; + } +} + async function ensureIssue(title, body) { logger.debug(`ensureIssue()`); try { diff --git a/lib/platform/vsts/index.js b/lib/platform/vsts/index.js index fc4725535babc27066dbe2874f802d129189902d..25c79e0703f054fbfed3be2ddcb286e27fd54404 100644 --- a/lib/platform/vsts/index.js +++ b/lib/platform/vsts/index.js @@ -27,6 +27,7 @@ module.exports = { mergeBranch, getBranchLastCommitTime, // issue + findIssue, ensureIssue, ensureIssueClosing, addAssignees, @@ -524,6 +525,11 @@ function getPrBody(input) { .replace('</details>', ''); } +function findIssue() { + // istanbul ignore next + logger.warn(`findIssue() is not implemented`); +} + function ensureIssue() { // istanbul ignore next logger.warn(`ensureIssue() is not implemented`); diff --git a/test/platform/__snapshots__/index.spec.js.snap b/test/platform/__snapshots__/index.spec.js.snap index e6870bffdadcb5da65b534130ba54760acba4222..7e3561009a32cbae1d9b23355ce7456a7ea3ed13 100644 --- a/test/platform/__snapshots__/index.spec.js.snap +++ b/test/platform/__snapshots__/index.spec.js.snap @@ -19,6 +19,7 @@ Array [ "deleteBranch", "mergeBranch", "getBranchLastCommitTime", + "findIssue", "ensureIssue", "ensureIssueClosing", "addAssignees", @@ -60,6 +61,7 @@ Array [ "deleteBranch", "mergeBranch", "getBranchLastCommitTime", + "findIssue", "ensureIssue", "ensureIssueClosing", "addAssignees", @@ -101,6 +103,7 @@ Array [ "deleteBranch", "mergeBranch", "getBranchLastCommitTime", + "findIssue", "ensureIssue", "ensureIssueClosing", "addAssignees", diff --git a/test/platform/bitbucket/index.spec.js b/test/platform/bitbucket/index.spec.js index 2c3053c8186d15835321a6d597d532a16b2d55f3..307903054a110e3167686941c66615457e120028 100644 --- a/test/platform/bitbucket/index.spec.js +++ b/test/platform/bitbucket/index.spec.js @@ -230,6 +230,11 @@ describe('platform/bitbucket', () => { }); }); + describe('findIssue()', () => { + it('does not throw', async () => { + await bitbucket.findIssue('title'); + }); + }); describe('ensureIssue()', () => { it('does not throw', async () => { await bitbucket.ensureIssue('title', 'body'); diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 1328a4deebe0d7614156b57901511d128a403de8..e34982a299789ee956b7f2bc117d660af492dbb2 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -863,6 +863,41 @@ describe('platform/github', () => { expect(res).toBeDefined(); }); }); + describe('findIssue()', () => { + it('returns null if no issue', async () => { + get.mockReturnValueOnce({ + body: [ + { + number: 1, + title: 'title-1', + }, + { + number: 2, + title: 'title-2', + }, + ], + }); + const res = await github.findIssue('title-3'); + expect(res).toBeNull(); + }); + it('finds issue', async () => { + get.mockReturnValueOnce({ + body: [ + { + number: 1, + title: 'title-1', + }, + { + number: 2, + title: 'title-2', + }, + ], + }); + get.mockReturnValueOnce({ body: { body: 'new-content' } }); + const res = await github.findIssue('title-2'); + expect(res).not.toBeNull(); + }); + }); describe('ensureIssue()', () => { it('creates issue', async () => { get.mockImplementationOnce(() => ({ diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index c7b76469f1ec42c2fc876fc85335e3177341ec64..c94484841067bc90b8fc0b2712e36be73fb25bdf 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -561,16 +561,51 @@ describe('platform/gitlab', () => { expect(res).toBeDefined(); }); }); + describe('findIssue()', () => { + it('returns null if no issue', async () => { + get.mockReturnValueOnce({ + body: [ + { + iid: 1, + title: 'title-1', + }, + { + iid: 2, + title: 'title-2', + }, + ], + }); + const res = await gitlab.findIssue('title-3'); + expect(res).toBeNull(); + }); + it('finds issue', async () => { + get.mockReturnValueOnce({ + body: [ + { + iid: 1, + title: 'title-1', + }, + { + iid: 2, + title: 'title-2', + }, + ], + }); + get.mockReturnValueOnce({ body: { body: 'new-content' } }); + const res = await gitlab.findIssue('title-2'); + expect(res).not.toBeNull(); + }); + }); describe('ensureIssue()', () => { it('creates issue', async () => { get.mockImplementationOnce(() => ({ body: [ { - number: 1, + iid: 1, title: 'title-1', }, { - number: 2, + iid: 2, title: 'title-2', }, ], @@ -582,11 +617,11 @@ describe('platform/gitlab', () => { get.mockReturnValueOnce({ body: [ { - number: 1, + iid: 1, title: 'title-1', }, { - number: 2, + iid: 2, title: 'title-2', }, ],