diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 330293f416e4a1f262d1d39de80ba0f27f151bef..004352d4e6cda68e70beacb52ea70c25e392307b 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -79,6 +79,7 @@ async function initRepo(repoName, token, endpoint) { } config.repoName = repoName.replace('/', '%2F'); config.fileList = null; + config.prList = null; try { const res = await get(`projects/${config.repoName}`); config.defaultBranch = res.body.default_branch; @@ -323,26 +324,29 @@ async function ensureCommentRemoval() { // Todo: implement. See GitHub API for example } +async function getPrList() { + if (!config.prList) { + const urlString = `projects/${config.repoName}/merge_requests?per_page=100`; + const res = await get(urlString, { paginate: true }); + config.prList = res.body.map(pr => ({ + number: pr.iid, + branchName: pr.source_branch, + title: pr.title, + state: pr.state === 'opened' ? 'open' : 'closed', + })); + } + return config.prList; +} + async function findPr(branchName, prTitle, state = 'all') { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); - const urlString = `projects/${config.repoName}/merge_requests?state=${ - state - }&per_page=100`; - const res = await get(urlString, { paginated: true }); - let pr = null; - res.body.forEach(result => { - if ( - (!prTitle || result.title === prTitle) && - result.source_branch === branchName - ) { - pr = result; - // GitHub uses number, GitLab uses iid - pr.number = pr.iid; - pr.body = pr.description; - pr.displayNumber = `Merge Request #${pr.iid}`; - } - }); - return pr; + const prList = await getPrList(); + return prList.find( + p => + p.branchName === branchName && + (!prTitle || p.title === prTitle) && + (state === 'all' || p.state === state) + ); } // Pull Request diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 920b2033a754d097a5ab01ed8d46399e200ac9ea..74e6f8657d8241f26e6bf2b5af13ded133d8c375 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -475,45 +475,39 @@ describe('platform/gitlab', () => { }); }); describe('findPr(branchName, prTitle, state)', () => { - it('returns null if no results', async () => { - get.mockReturnValueOnce({ - body: [], - }); - const pr = await gitlab.findPr('some-branch'); - expect(pr).toBe(null); - }); - it('returns null if no matching titles', async () => { + it('returns true if no title and all state', async () => { get.mockReturnValueOnce({ body: [ { - source_branch: 'some-branch', - iid: 1, - }, - { - source_branch: 'some-branch', - iid: 2, - title: 'foo', + number: 1, + source_branch: 'branch-a', + title: 'branch a pr', + state: 'opened', }, ], }); - const pr = await gitlab.findPr('some-branch', 'some-title'); - expect(pr).toBe(null); + const res = await gitlab.findPr('branch-a', null); + expect(res).toBeDefined(); }); - it('returns last result if multiple match', async () => { + it('caches pr list', async () => { get.mockReturnValueOnce({ body: [ { - source_branch: 'some-branch', - iid: 1, - }, - { - source_branch: 'some-branch', - iid: 2, + number: 1, + source_branch: 'branch-a', + title: 'branch a pr', + state: 'opened', }, ], }); - const pr = await gitlab.findPr('some-branch'); - expect(pr.number).toBe(2); + let res = await gitlab.findPr('branch-a', null); + expect(res).toBeDefined(); + res = await gitlab.findPr('branch-a', 'branch a pr'); + expect(res).toBeDefined(); + res = await gitlab.findPr('branch-a', 'branch a pr', 'open'); + expect(res).toBeDefined(); + res = await gitlab.findPr('branch-b'); + expect(res).not.toBeDefined(); }); }); describe('createPr(branchName, title, body)', () => {