diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index 44ffff1b2b2e6065169837920b26d1b7e15bb830..4c4e11c45ca7b2412b40407c02aeee34f9207ac3 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -17,6 +17,7 @@ import { VulnerabilityAlert, CreatePRConfig, BranchStatusConfig, + FindPRConfig, EnsureCommentConfig, } from '../common'; import { sanitize } from '../../util/sanitize'; @@ -275,11 +276,11 @@ export async function getPr(pullRequestId: number): Promise<Pr | null> { return azurePr; } -export async function findPr( - branchName: string, - prTitle: string | null, - state = 'all' -): Promise<Pr | null> { +export async function findPr({ + branchName, + prTitle, + state = 'all', +}: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); // TODO: fix typing let prsFiltered: any[] = []; @@ -316,7 +317,7 @@ export async function findPr( export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); - const existingPr = await findPr(branchName, null, 'open'); + const existingPr = await findPr({ branchName, state: 'open' }); return existingPr ? getPr(existingPr.pullRequestId) : null; } diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts index 9a1f57c45206a4e801f15ab020ddf86d377f3990..3f46fa1149a44dcf49a29e5d8925bd6862e799e4 100644 --- a/lib/platform/bitbucket-server/index.ts +++ b/lib/platform/bitbucket-server/index.ts @@ -16,6 +16,7 @@ import { GotResponse, CreatePRConfig, BranchStatusConfig, + FindPRConfig, EnsureCommentConfig, } from '../common'; import { sanitize } from '../../util/sanitize'; @@ -382,12 +383,12 @@ export async function getPrList(_args?: any): Promise<Pr[]> { // TODO: coverage // istanbul ignore next -export async function findPr( - branchName: string, - prTitle?: string, +export async function findPr({ + branchName, + prTitle, state = 'all', - refreshCache?: boolean -): Promise<Pr | null> { + refreshCache, +}: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, "${prTitle}", "${state}")`); const prList = await getPrList({ refreshCache }); const pr = prList.find(isRelevantPr(branchName, prTitle, state)); @@ -405,7 +406,10 @@ export async function getBranchPr( refreshCache?: boolean ): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); - const existingPr = await findPr(branchName, undefined, 'open'); + const existingPr = await findPr({ + branchName, + state: 'open', + }); return existingPr ? getPr(existingPr.number, refreshCache) : null; } diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 79bd2ce4fa83986b4c2897bb4c3d74a1f2ad13a5..bc3551a59eff84e7c7841fb67776892b90eb0475 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -17,6 +17,7 @@ import { CreatePRConfig, EnsureIssueConfig, BranchStatusConfig, + FindPRConfig, EnsureCommentConfig, } from '../common'; import { sanitize } from '../../util/sanitize'; @@ -224,11 +225,11 @@ export async function getPrList(): Promise<Pr[]> { return config.prList; } -export async function findPr( - branchName: string, - prTitle?: string | null, - state = 'all' -): Promise<Pr | null> { +export async function findPr({ + branchName, + prTitle, + state = 'all', +}: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); const pr = prList.find( @@ -248,7 +249,7 @@ export async function deleteBranch( closePr?: boolean ): Promise<void> { if (closePr) { - const pr = await findPr(branchName, null, 'open'); + const pr = await findPr({ branchName, state: 'open' }); if (pr) { await api.post( `/2.0/repositories/${config.repository}/pullrequests/${pr.number}/decline` @@ -375,7 +376,7 @@ async function getBranchCommit(branchName: string): Promise<string | null> { // Returns the Pull Request for a branch. Null if not exists. export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); - const existingPr = await findPr(branchName, null, 'open'); + const existingPr = await findPr({ branchName, state: 'open' }); return existingPr ? getPr(existingPr.number) : null; } diff --git a/lib/platform/common.ts b/lib/platform/common.ts index 93e8cc07e77585f36719766366545788a08b7c37..e70e17fdbe7ead8a35f121ccb1b1938ba44202f0 100644 --- a/lib/platform/common.ts +++ b/lib/platform/common.ts @@ -129,6 +129,12 @@ export interface BranchStatusConfig { state: string | null; url?: string; } +export interface FindPRConfig { + branchName: string; + prTitle?: string | null; + state?: 'open' | 'closed' | '!open' | 'all'; + refreshCache?: boolean; +} export interface EnsureCommentConfig { number: number; topic: string; @@ -175,11 +181,7 @@ export interface Platform { setBaseBranch(baseBranch: string): Promise<void>; commitFilesToBranch(commitFile: CommitFilesConfig): Promise<void>; getPr(number: number): Promise<Pr>; - findPr( - branchName: string, - prTitle: string | null, - state?: string - ): Promise<Pr>; + findPr(findPRConfig: FindPRConfig): Promise<Pr>; mergeBranch(branchName: string): Promise<void>; getBranchStatus( branchName: string, diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 1d4d9ece810f604bc26a354d7c7f088df9b2ec83..6de0bf12cfda6495be1174afb859812007157f25 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -16,6 +16,7 @@ import { CreatePRConfig, EnsureIssueConfig, BranchStatusConfig, + FindPRConfig, EnsureCommentConfig, } from '../common'; @@ -981,11 +982,11 @@ export async function getPrList(): Promise<Pr[]> { return config.prList!; } -export async function findPr( - branchName: string, - prTitle?: string | null, - state = 'all' -): Promise<Pr | null> { +export async function findPr({ + branchName, + prTitle, + state = 'all', +}: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); const pr = prList.find( @@ -1003,7 +1004,7 @@ export async function findPr( // Returns the Pull Request for a branch. Null if not exists. export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); - const existingPr = await findPr(branchName, null, 'open'); + const existingPr = await findPr({ branchName, state: 'open' }); return existingPr ? getPr(existingPr.number) : null; } diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index b76614abb847f9227c8421c57ec585bda50760c7..59c8c5c56a9c388ae28a7f0642cf2b1792b7bcdf 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -15,6 +15,7 @@ import { CreatePRConfig, EnsureIssueConfig, BranchStatusConfig, + FindPRConfig, EnsureCommentConfig, } from '../common'; import { configFileNames } from '../../config/app-strings'; @@ -959,11 +960,11 @@ function matchesState(state: string, desiredState: string): boolean { return state === desiredState; } -export async function findPr( - branchName: string, - prTitle?: string | null, - state = 'all' -): Promise<Pr> { +export async function findPr({ + branchName, + prTitle, + state = 'all', +}: FindPRConfig): Promise<Pr> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); return prList.find( diff --git a/lib/workers/branch/check-existing.ts b/lib/workers/branch/check-existing.ts index 8badbb1e413c448486418643d0061c8c8fdaea06..9f6236090d6e47f7ab6dbcbed4c0a3d31c2ceaf8 100644 --- a/lib/workers/branch/check-existing.ts +++ b/lib/workers/branch/check-existing.ts @@ -14,7 +14,11 @@ export async function prAlreadyExisted( } logger.debug('recreateClosed is false'); // Return if same PR already existed - const pr = await platform.findPr(config.branchName, config.prTitle, '!open'); + const pr = await platform.findPr({ + branchName: config.branchName, + prTitle: config.prTitle, + state: '!open', + }); if (pr) { logger.debug('Found closed PR with current title'); const prDetails = await platform.getPr(pr.number); diff --git a/lib/workers/repository/finalise/prune.ts b/lib/workers/repository/finalise/prune.ts index d78e4022e8f2277ff36289d227b397fb11722dc5..8736fdfe08987a283cf9e77775b79c05d4b89c9e 100644 --- a/lib/workers/repository/finalise/prune.ts +++ b/lib/workers/repository/finalise/prune.ts @@ -8,7 +8,10 @@ async function cleanUpBranches( ): Promise<void> { for (const branchName of remainingBranches) { try { - const pr = await platform.findPr(branchName, null, 'open'); + const pr = await platform.findPr({ + branchName, + state: 'open', + }); const branchPr = await platform.getBranchPr(branchName); const skipAutoclose = branchPr && branchPr.isModified; if (pr && !skipAutoclose) { diff --git a/lib/workers/repository/master-issue.ts b/lib/workers/repository/master-issue.ts index 82296b31f493c3ba983f0a0a1bb47ab581c94331..e299813e8615e01dc62fb8ece7768e6aa929b16c 100644 --- a/lib/workers/repository/master-issue.ts +++ b/lib/workers/repository/master-issue.ts @@ -179,11 +179,11 @@ export async function ensureMasterIssue( issueBody += 'These updates were closed unmerged and will not be recreated unless you click a checkbox below.\n\n'; for (const branch of alreadyExisted) { - const pr = await platform.findPr( - branch.branchName, - branch.prTitle, - '!open' - ); + const pr = await platform.findPr({ + branchName: branch.branchName, + prTitle: branch.prTitle, + state: '!open', + }); issueBody += getListItem(branch, 'recreate', pr); } issueBody += '\n'; diff --git a/lib/workers/repository/onboarding/branch/check.ts b/lib/workers/repository/onboarding/branch/check.ts index 8b047417d82158101c8aa63427f59693b821264d..ce6f8a3554e93b708d60a39fdfa80da4fd709205 100644 --- a/lib/workers/repository/onboarding/branch/check.ts +++ b/lib/workers/repository/onboarding/branch/check.ts @@ -35,7 +35,11 @@ const packageJsonConfigExists = async (): Promise<boolean> => { export type Pr = any; const closedPrExists = (config: RenovateConfig): Promise<Pr> => - platform.findPr(config.onboardingBranch, config.onboardingPrTitle, '!open'); + platform.findPr({ + branchName: config.onboardingBranch, + prTitle: config.onboardingPrTitle, + state: '!open', + }); export const isOnboarded = async (config: RenovateConfig): Promise<boolean> => { logger.debug('isOnboarded()'); diff --git a/test/platform/azure/index.spec.ts b/test/platform/azure/index.spec.ts index c75c41c0deb6d63b7654be592e980164ee8d86d9..2e54f5dd893aa66e7670c7ec686c5550373f5208 100644 --- a/test/platform/azure/index.spec.ts +++ b/test/platform/azure/index.spec.ts @@ -228,7 +228,11 @@ describe('platform/azure', () => { state: 'open', } as any) ); - const res = await azure.findPr('branch-a', 'branch a pr', 'open'); + const res = await azure.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + state: 'open', + }); expect(res).toMatchSnapshot(); }); it('returns pr if found not open', async () => { @@ -260,7 +264,11 @@ describe('platform/azure', () => { state: 'closed', } as any) ); - const res = await azure.findPr('branch-a', 'branch a pr', '!open'); + const res = await azure.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + state: '!open', + }); expect(res).toMatchSnapshot(); }); it('returns pr if found it close', async () => { @@ -292,7 +300,11 @@ describe('platform/azure', () => { state: 'closed', } as any) ); - const res = await azure.findPr('branch-a', 'branch a pr', 'closed'); + const res = await azure.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + state: 'closed', + }); expect(res).toMatchSnapshot(); }); it('returns pr if found it all state', async () => { @@ -324,7 +336,10 @@ describe('platform/azure', () => { state: 'closed', } as any) ); - const res = await azure.findPr('branch-a', 'branch a pr'); + const res = await azure.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + }); expect(res).toMatchSnapshot(); }); }); diff --git a/test/platform/bitbucket-server/index.spec.ts b/test/platform/bitbucket-server/index.spec.ts index 454f0092efb30feabc708878b66bc7ce29dc43c0..3d7aafa44973bf9b984e94b4c1c099a424045ec7 100644 --- a/test/platform/bitbucket-server/index.spec.ts +++ b/test/platform/bitbucket-server/index.spec.ts @@ -481,7 +481,7 @@ describe('platform/bitbucket-server', () => { expect.assertions(2); await initRepo(); expect( - await bitbucket.findPr('userName1/pullRequest1') + await bitbucket.findPr({ branchName: 'userName1/pullRequest1' }) ).toBeUndefined(); expect(api.get.mock.calls).toMatchSnapshot(); }); @@ -492,7 +492,11 @@ describe('platform/bitbucket-server', () => { expect.assertions(2); await initRepo(); expect( - await bitbucket.findPr('userName1/pullRequest5', 'title', 'open') + await bitbucket.findPr({ + branchName: 'userName1/pullRequest5', + prTitle: 'title', + state: 'open', + }) ).toMatchSnapshot(); expect(api.get.mock.calls).toMatchSnapshot(); }); @@ -500,7 +504,11 @@ describe('platform/bitbucket-server', () => { expect.assertions(2); await initRepo(); expect( - await bitbucket.findPr('userName1/pullRequest5', 'title', 'closed') + await bitbucket.findPr({ + branchName: 'userName1/pullRequest5', + prTitle: 'title', + state: 'closed', + }) ).toBeUndefined(); expect(api.get.mock.calls).toMatchSnapshot(); }); diff --git a/test/platform/bitbucket/index.spec.ts b/test/platform/bitbucket/index.spec.ts index 029ff0f820fbafb635d0e811d8072a490a68100b..62cd49fe927736ad47d0aff73da357fb9f6a5b10 100644 --- a/test/platform/bitbucket/index.spec.ts +++ b/test/platform/bitbucket/index.spec.ts @@ -389,7 +389,9 @@ describe('platform/bitbucket', () => { it('finds pr', async () => { await initRepo(); await mocked(async () => { - expect(await bitbucket.findPr('branch', 'title')).toMatchSnapshot(); + expect( + await bitbucket.findPr({ branchName: 'branch', prTitle: 'title' }) + ).toMatchSnapshot(); }); }); }); diff --git a/test/platform/github/index.spec.ts b/test/platform/github/index.spec.ts index 0ccc9bf7fb3fd7b855e67175e34b422c794f98a9..dab774cb20305dc6ac279e81ebc896fd246859c9 100644 --- a/test/platform/github/index.spec.ts +++ b/test/platform/github/index.spec.ts @@ -1415,7 +1415,9 @@ describe('platform/github', () => { }, ], } as any); - const res = await github.findPr('branch-a', null); + const res = await github.findPr({ + branchName: 'branch-a', + }); expect(res).toBeDefined(); }); it('returns true if not open', async () => { @@ -1429,7 +1431,10 @@ describe('platform/github', () => { }, ], } as any); - const res = await github.findPr('branch-a', null, '!open'); + const res = await github.findPr({ + branchName: 'branch-a', + state: '!open', + }); expect(res).toBeDefined(); }); it('caches pr list', async () => { @@ -1443,13 +1448,20 @@ describe('platform/github', () => { }, ], } as any); - let res = await github.findPr('branch-a', null); + let res = await github.findPr({ branchName: 'branch-a' }); expect(res).toBeDefined(); - res = await github.findPr('branch-a', 'branch a pr'); + res = await github.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + }); expect(res).toBeDefined(); - res = await github.findPr('branch-a', 'branch a pr', 'open'); + res = await github.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + state: 'open', + }); expect(res).toBeDefined(); - res = await github.findPr('branch-b'); + res = await github.findPr({ branchName: 'branch-b' }); expect(res).not.toBeDefined(); }); }); diff --git a/test/platform/gitlab/index.spec.ts b/test/platform/gitlab/index.spec.ts index d547309b4b97321f1c176067e643a9f7806a296b..1209c40c9006b0af81845d02c09873eb05b9858a 100644 --- a/test/platform/gitlab/index.spec.ts +++ b/test/platform/gitlab/index.spec.ts @@ -778,7 +778,9 @@ describe('platform/gitlab', () => { }, ], } as any); - const res = await gitlab.findPr('branch-a', null); + const res = await gitlab.findPr({ + branchName: 'branch-a', + }); expect(res).toBeDefined(); }); it('returns true if not open', async () => { @@ -792,7 +794,10 @@ describe('platform/gitlab', () => { }, ], } as any); - const res = await gitlab.findPr('branch-a', null, '!open'); + const res = await gitlab.findPr({ + branchName: 'branch-a', + state: '!open', + }); expect(res).toBeDefined(); }); @@ -807,7 +812,11 @@ describe('platform/gitlab', () => { }, ], } as any); - const res = await gitlab.findPr('branch-a', 'branch a pr', 'open'); + const res = await gitlab.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + state: 'open', + }); expect(res).toBeDefined(); }); @@ -822,7 +831,10 @@ describe('platform/gitlab', () => { }, ], } as any); - const res = await gitlab.findPr('branch-a', 'branch a pr'); + const res = await gitlab.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + }); expect(res).toBeDefined(); }); }); diff --git a/test/workers/repository/master-issue.spec.ts b/test/workers/repository/master-issue.spec.ts index cde7b50edf57ff72a598c3d59ae5ebdf094b21a0..e40ac92f5238a0d0532b5abd93d5e6646a7a3e19 100644 --- a/test/workers/repository/master-issue.spec.ts +++ b/test/workers/repository/master-issue.spec.ts @@ -349,12 +349,12 @@ describe('workers/repository/master-issue', () => { ); expect(platform.getBranchPr).toHaveBeenCalledTimes(0); expect(platform.findPr).toHaveBeenCalledTimes(2); - expect(platform.findPr.mock.calls[0][0]).toBe('branchName1'); - expect(platform.findPr.mock.calls[0][1]).toBe('pr1'); - expect(platform.findPr.mock.calls[0][2]).toBe('!open'); - expect(platform.findPr.mock.calls[1][0]).toBe('branchName2'); - expect(platform.findPr.mock.calls[1][1]).toBe('pr2'); - expect(platform.findPr.mock.calls[1][2]).toBe('!open'); + expect(platform.findPr.mock.calls[0][0].branchName).toBe('branchName1'); + expect(platform.findPr.mock.calls[0][0].prTitle).toBe('pr1'); + expect(platform.findPr.mock.calls[0][0].state).toBe('!open'); + expect(platform.findPr.mock.calls[1][0].branchName).toBe('branchName2'); + expect(platform.findPr.mock.calls[1][0].prTitle).toBe('pr2'); + expect(platform.findPr.mock.calls[1][0].state).toBe('!open'); // same with dry run await dryRun(branches, platform, 0, 0, 0, 2);