From a19421fff6e50a24f4e3d8c8f843f2d6191f4c89 Mon Sep 17 00:00:00 2001 From: Manuel Rueda <ManRueda@users.noreply.github.com> Date: Tue, 14 Nov 2017 05:55:05 -0300 Subject: [PATCH] fix: normalize GitLab findPr with GitHub and add PR cache (#1165) Closes #1164 --- lib/platform/gitlab/index.js | 40 ++++++++++++++------------ test/platform/gitlab/index.spec.js | 46 +++++++++++++----------------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 330293f416..004352d4e6 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 920b2033a7..74e6f8657d 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)', () => { -- GitLab