diff --git a/lib/platform/gitlab/__snapshots__/index.spec.ts.snap b/lib/platform/gitlab/__snapshots__/index.spec.ts.snap index 8db3acf5879060ed3f12fbe05714e33f808dd5b7..5f12ade0c59813e8f1acc993037e866db3a2107e 100644 --- a/lib/platform/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/platform/gitlab/__snapshots__/index.spec.ts.snap @@ -961,7 +961,7 @@ Array [ "user-agent": "https://github.com/renovatebot/renovate", }, "method": "GET", - "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100", }, ] `; @@ -1011,7 +1011,7 @@ Array [ "user-agent": "https://github.com/renovatebot/renovate", }, "method": "GET", - "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100", }, Object { "headers": Object { @@ -1084,7 +1084,7 @@ Array [ "user-agent": "https://github.com/renovatebot/renovate", }, "method": "GET", - "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100", }, Object { "headers": Object { @@ -1157,7 +1157,7 @@ Array [ "user-agent": "https://github.com/renovatebot/renovate", }, "method": "GET", - "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/merge_requests?per_page=100", }, Object { "headers": Object { diff --git a/lib/platform/gitlab/index.spec.ts b/lib/platform/gitlab/index.spec.ts index 9357d5c8beb1c44a9306a27414e5eedbbf371b81..48f2f371d6d6c4026fc92402fbff18ed50b588e7 100644 --- a/lib/platform/gitlab/index.spec.ts +++ b/lib/platform/gitlab/index.spec.ts @@ -311,9 +311,7 @@ describe('platform/gitlab', () => { it('should return null if no PR exists', async () => { const scope = await initRepo(); scope - .get( - '/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch' - ) + .get('/api/v4/projects/some%2Frepo/merge_requests?per_page=100') .reply(200, []); const pr = await gitlab.getBranchPr('some-branch'); expect(pr).toBeNull(); @@ -322,12 +320,11 @@ describe('platform/gitlab', () => { it('should return the PR object', async () => { const scope = await initRepo(); scope - .get( - '/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch' - ) + .get('/api/v4/projects/some%2Frepo/merge_requests?per_page=100') .reply(200, [ { iid: 91, + title: 'some change', source_branch: 'some-branch', target_branch: 'master', state: 'opened', @@ -360,12 +357,11 @@ describe('platform/gitlab', () => { it('should strip draft prefix from title', async () => { const scope = await initRepo(); scope - .get( - '/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch' - ) + .get('/api/v4/projects/some%2Frepo/merge_requests?per_page=100') .reply(200, [ { iid: 91, + title: 'Draft: some change', source_branch: 'some-branch', target_branch: 'master', state: 'opened', @@ -398,12 +394,11 @@ describe('platform/gitlab', () => { it('should strip deprecated draft prefix from title', async () => { const scope = await initRepo(); scope - .get( - '/api/v4/projects/some%2Frepo/merge_requests?per_page=100&state=opened&source_branch=some-branch' - ) + .get('/api/v4/projects/some%2Frepo/merge_requests?per_page=100') .reply(200, [ { iid: 91, + title: 'WIP: some change', source_branch: 'some-branch', target_branch: 'master', state: 'opened', diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index b2704f464c70f6c6bc2076e8adc694a44a3866e4..fc2df2f1db8659a51c1c3fb934de4adef3769694 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -608,33 +608,39 @@ export function getPrBody(input: string): string { // Branch +function matchesState(state: string, desiredState: string): boolean { + if (desiredState === PrState.All) { + return true; + } + if (desiredState.startsWith('!')) { + return state !== desiredState.substring(1); + } + return state === desiredState; +} + +export async function findPr({ + branchName, + prTitle, + state = PrState.All, +}: FindPRConfig): Promise<Pr> { + logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); + const prList = await getPrList(); + return prList.find( + (p: { sourceBranch: string; title: string; state: string }) => + p.sourceBranch === branchName && + (!prTitle || p.title === prTitle) && + matchesState(p.state, state) + ); +} + // Returns the Pull Request for a branch. Null if not exists. export async function getBranchPr(branchName: string): Promise<Pr> { logger.debug(`getBranchPr(${branchName})`); - // istanbul ignore if - if (!git.branchExists(branchName)) { - return null; - } - const query = new URLSearchParams({ - per_page: '100', - state: 'opened', - source_branch: branchName, - }).toString(); - const urlString = `projects/${config.repository}/merge_requests?${query}`; - const res = await gitlabApi.getJson<{ source_branch: string }[]>(urlString, { - paginate: true, - }); - logger.debug(`Got res with ${res.body.length} results`); - let pr: any = null; - res.body.forEach((result) => { - if (result.source_branch === branchName) { - pr = result; - } + const existingPr = await findPr({ + branchName, + state: PrState.Open, }); - if (!pr) { - return null; - } - return getPr(pr.iid); + return existingPr ? getPr(existingPr.number) : null; } export async function getBranchStatusCheck( @@ -1000,31 +1006,6 @@ export async function ensureCommentRemoval({ } } -function matchesState(state: string, desiredState: string): boolean { - if (desiredState === PrState.All) { - return true; - } - if (desiredState.startsWith('!')) { - return state !== desiredState.substring(1); - } - return state === desiredState; -} - -export async function findPr({ - branchName, - prTitle, - state = PrState.All, -}: FindPRConfig): Promise<Pr> { - logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); - const prList = await getPrList(); - return prList.find( - (p: { sourceBranch: string; title: string; state: string }) => - p.sourceBranch === branchName && - (!prTitle || p.title === prTitle) && - matchesState(p.state, state) - ); -} - export function getVulnerabilityAlerts(): Promise<VulnerabilityAlert[]> { return Promise.resolve([]); }