diff --git a/lib/constants/pull-requests.ts b/lib/constants/pull-requests.ts deleted file mode 100644 index 9f926c4e0d8effb809e3fbaba2007388a3d52346..0000000000000000000000000000000000000000 --- a/lib/constants/pull-requests.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const PR_STATE_MERGED = 'merged'; -export const PR_STATE_OPEN = 'open'; -export const PR_STATE_CLOSED = 'closed'; -export const PR_STATE_ALL = 'all'; -export const PR_STATE_NOT_OPEN = '!open'; diff --git a/lib/platform/azure/azure-helper.ts b/lib/platform/azure/azure-helper.ts index edc153e4d1679625755f338344cf143d5c8254e3..7f9f7dea8efcb469d0ecaf2e7d577e51f76a3429 100644 --- a/lib/platform/azure/azure-helper.ts +++ b/lib/platform/azure/azure-helper.ts @@ -5,14 +5,9 @@ import { GitRef, } from 'azure-devops-node-api/interfaces/GitInterfaces'; import { Options } from 'simple-git'; -import { - PR_STATE_CLOSED, - PR_STATE_MERGED, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { HostRule } from '../../types'; +import { HostRule, PrState } from '../../types'; import * as azureApi from './azure-got-wrapper'; import { AzurePr } from './types'; @@ -199,11 +194,11 @@ export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr { // All = 4, // } if (azurePr.status === 2) { - pr.state = PR_STATE_CLOSED; + pr.state = PrState.Closed; } else if (azurePr.status === 3) { - pr.state = PR_STATE_MERGED; + pr.state = PrState.Merged; } else { - pr.state = PR_STATE_OPEN; + pr.state = PrState.Open; } // mergeStatus diff --git a/lib/platform/azure/index.spec.ts b/lib/platform/azure/index.spec.ts index 878668f1cf779dc0d4963b5655802ee11d8eff79..5827e375ba38a44eb01d0ec6840e82e864450384 100644 --- a/lib/platform/azure/index.spec.ts +++ b/lib/platform/azure/index.spec.ts @@ -1,6 +1,6 @@ import is from '@sindresorhus/is'; import { REPOSITORY_DISABLED } from '../../constants/error-messages'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; import * as _hostRules from '../../util/host-rules'; import { Platform, RepoParams } from '../common'; @@ -190,7 +190,7 @@ describe('platform/azure', () => { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'open', + state: PrState.Open, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), @@ -205,13 +205,13 @@ describe('platform/azure', () => { number: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'open', + state: PrState.Open, } as any) ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', - state: 'open', + state: PrState.Open, }); expect(res).toMatchSnapshot(); }); @@ -227,7 +227,7 @@ describe('platform/azure', () => { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), @@ -242,13 +242,13 @@ describe('platform/azure', () => { number: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, } as any) ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', - state: '!open', + state: PrState.NotOpen, }); expect(res).toMatchSnapshot(); }); @@ -264,7 +264,7 @@ describe('platform/azure', () => { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), @@ -279,13 +279,13 @@ describe('platform/azure', () => { number: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, } as any) ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', - state: 'closed', + state: PrState.Closed, }); expect(res).toMatchSnapshot(); }); @@ -301,7 +301,7 @@ describe('platform/azure', () => { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), @@ -316,7 +316,7 @@ describe('platform/azure', () => { number: 1, sourceRefName: 'refs/heads/branch-a', title: 'branch a pr', - state: 'closed', + state: PrState.Closed, } as any) ); const res = await azure.findPr({ @@ -590,7 +590,11 @@ describe('platform/azure', () => { updatePullRequest, } as any) ); - await azure.updatePr(1234, 'The New Title', 'Hello world again'); + await azure.updatePr({ + number: 1234, + prTitle: 'The New Title', + prBody: 'Hello world again', + }); expect(updatePullRequest.mock.calls).toMatchSnapshot(); }); @@ -603,7 +607,10 @@ describe('platform/azure', () => { updatePullRequest, } as any) ); - await azure.updatePr(1234, 'The New Title - autoclose'); + await azure.updatePr({ + number: 1234, + prTitle: 'The New Title - autoclose', + }); expect(updatePullRequest.mock.calls).toMatchSnapshot(); }); }); diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index 7f9b5c42d4d75d36d5ecdd7445cfc681b7b9a1a0..3539b052428596dcd9775a583ff6b488f22ed255 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -7,13 +7,8 @@ import { import { RenovateConfig } from '../../config/common'; import { REPOSITORY_DISABLED } from '../../constants/error-messages'; import { PLATFORM_TYPE_AZURE } from '../../constants/platforms'; -import { - PR_STATE_ALL, - PR_STATE_NOT_OPEN, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; import { sanitize } from '../../util/sanitize'; @@ -31,6 +26,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -244,7 +240,7 @@ export async function getPr(pullRequestId: number): Promise<Pr | null> { export async function findPr({ branchName, prTitle, - state = PR_STATE_ALL, + state = PrState.All, }: FindPRConfig): Promise<Pr | null> { let prsFiltered: Pr[] = []; try { @@ -259,13 +255,11 @@ export async function findPr({ } switch (state) { - case PR_STATE_ALL: + case PrState.All: // no more filter needed, we can go further... break; - case PR_STATE_NOT_OPEN: - prsFiltered = prsFiltered.filter( - (item) => item.state !== PR_STATE_OPEN - ); + case PrState.NotOpen: + prsFiltered = prsFiltered.filter((item) => item.state !== PrState.Open); break; default: prsFiltered = prsFiltered.filter((item) => item.state === state); @@ -284,7 +278,7 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); return existingPr ? getPr(existingPr.number) : null; } @@ -392,11 +386,11 @@ export async function createPr({ return azureHelper.getRenovatePRFormat(pr); } -export async function updatePr( - prNo: number, - title: string, - body?: string -): Promise<void> { +export async function updatePr({ + number: prNo, + prTitle: title, + prBody: body, +}: UpdatePrConfig): Promise<void> { logger.debug(`updatePr(${prNo}, ${title}, body)`); const azureApiGit = await azureApi.gitApi(); const objToUpdate: GitPullRequest = { diff --git a/lib/platform/bitbucket-server/index.spec.ts b/lib/platform/bitbucket-server/index.spec.ts index 062e4976bb95c72eaf0d82f8ad5f19e96909ffc3..d69c1afb98813c369fcb6373bc2592afd9ff6716 100644 --- a/lib/platform/bitbucket-server/index.spec.ts +++ b/lib/platform/bitbucket-server/index.spec.ts @@ -6,8 +6,7 @@ import { REPOSITORY_DISABLED, REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; -import { PR_STATE_CLOSED, PR_STATE_OPEN } from '../../constants/pull-requests'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; import { Platform } from '../common'; @@ -1075,7 +1074,7 @@ describe(getName(__filename), () => { await bitbucket.findPr({ branchName: 'userName1/pullRequest5', prTitle: 'title', - state: PR_STATE_OPEN, + state: PrState.Open, }) ).toMatchSnapshot(); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -1095,7 +1094,7 @@ describe(getName(__filename), () => { await bitbucket.findPr({ branchName: 'userName1/pullRequest5', prTitle: 'title', - state: PR_STATE_CLOSED, + state: PrState.Closed, }) ).toBeUndefined(); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -1248,14 +1247,22 @@ describe(getName(__filename), () => { ) .reply(200); - await bitbucket.updatePr(5, 'title', 'body'); + await bitbucket.updatePr({ + number: 5, + prTitle: 'title', + prBody: 'body', + }); expect(httpMock.getTrace()).toMatchSnapshot(); }); it('throws not-found 1', async () => { await initRepo(); await expect( - bitbucket.updatePr(null as any, 'title', 'body') + bitbucket.updatePr({ + number: null as any, + prTitle: 'title', + prBody: 'body', + }) ).rejects.toThrow(REPOSITORY_NOT_FOUND); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -1268,9 +1275,9 @@ describe(getName(__filename), () => { `${urlPath}/rest/api/1.0/projects/SOME/repos/repo/pull-requests/4` ) .reply(404); - await expect(bitbucket.updatePr(4, 'title', 'body')).rejects.toThrow( - REPOSITORY_NOT_FOUND - ); + await expect( + bitbucket.updatePr({ number: 4, prTitle: 'title', prBody: 'body' }) + ).rejects.toThrow(REPOSITORY_NOT_FOUND); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -1291,9 +1298,9 @@ describe(getName(__filename), () => { ) .reply(404); - await expect(bitbucket.updatePr(5, 'title', 'body')).rejects.toThrow( - REPOSITORY_NOT_FOUND - ); + await expect( + bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }) + ).rejects.toThrow(REPOSITORY_NOT_FOUND); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -1314,9 +1321,9 @@ describe(getName(__filename), () => { ) .reply(409); - await expect(bitbucket.updatePr(5, 'title', 'body')).rejects.toThrow( - REPOSITORY_CHANGED - ); + await expect( + bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }) + ).rejects.toThrow(REPOSITORY_CHANGED); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -1337,7 +1344,7 @@ describe(getName(__filename), () => { .reply(405); await expect( - bitbucket.updatePr(5, 'title', 'body') + bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }) ).rejects.toThrowErrorMatchingSnapshot(); expect(httpMock.getTrace()).toMatchSnapshot(); }); diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts index f3ff7b8669bd8d1b1c434311aab69c328d6e8b95..a8fc57246f749b6eb61d2571a21b16a7a31d7427 100644 --- a/lib/platform/bitbucket-server/index.ts +++ b/lib/platform/bitbucket-server/index.ts @@ -9,9 +9,8 @@ import { REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; import { PLATFORM_TYPE_BITBUCKET_SERVER } from '../../constants/platforms'; -import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; import { HttpResponse } from '../../util/http'; @@ -35,6 +34,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -269,7 +269,7 @@ export async function getPr( pr.hasReviewers = is.nonEmptyArray(pr.reviewers); pr.version = updatePrVersion(pr.number, pr.version); - if (pr.state === PR_STATE_OPEN) { + if (pr.state === PrState.Open) { const mergeRes = await bitbucketServerHttp.getJson<{ conflicted: string; canMerge: string; @@ -287,7 +287,7 @@ export async function getPr( // TODO: coverage // istanbul ignore next function matchesState(state: string, desiredState: string): boolean { - if (desiredState === PR_STATE_ALL) { + if (desiredState === PrState.All) { return true; } if (desiredState.startsWith('!')) { @@ -334,7 +334,7 @@ export async function getPrList(refreshCache?: boolean): Promise<Pr[]> { export async function findPr({ branchName, prTitle, - state = PR_STATE_ALL, + state = PrState.All, refreshCache, }: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, "${prTitle}", "${state}")`); @@ -353,7 +353,7 @@ export async function getBranchPr(branchName: string): Promise<BbsPr | null> { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); return existingPr ? getPr(existingPr.number) : null; } @@ -875,11 +875,11 @@ export async function createPr({ return pr; } -export async function updatePr( - prNo: number, - title: string, - rawDescription: string -): Promise<void> { +export async function updatePr({ + number: prNo, + prTitle: title, + prBody: rawDescription, +}: UpdatePrConfig): Promise<void> { const description = sanitize(rawDescription); logger.debug(`updatePr(${prNo}, title=${title})`); diff --git a/lib/platform/bitbucket-server/utils.ts b/lib/platform/bitbucket-server/utils.ts index 6eae318a320c261d76d73300c8160b90c22c2a0a..b236d67970a9cb7ba65c51f2e21a44f76eb4ca08 100644 --- a/lib/platform/bitbucket-server/utils.ts +++ b/lib/platform/bitbucket-server/utils.ts @@ -1,10 +1,6 @@ // SEE for the reference https://github.com/renovatebot/renovate/blob/c3e9e572b225085448d94aa121c7ec81c14d3955/lib/platform/bitbucket/utils.js import url from 'url'; -import { - PR_STATE_CLOSED, - PR_STATE_MERGED, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; +import { PrState } from '../../types'; import { HttpResponse } from '../../util/http'; import { BitbucketServerHttp } from '../../util/http/bitbucket-server'; import { BbbsRestPr, BbsPr } from './types'; @@ -13,9 +9,9 @@ const bitbucketServerHttp = new BitbucketServerHttp(); // https://docs.atlassian.com/bitbucket-server/rest/6.0.0/bitbucket-rest.html#idp250 const prStateMapping: any = { - MERGED: PR_STATE_MERGED, - DECLINED: PR_STATE_CLOSED, - OPEN: PR_STATE_OPEN, + MERGED: PrState.Merged, + DECLINED: PrState.Closed, + OPEN: PrState.Open, }; export function prInfo(pr: BbbsRestPr): BbsPr { diff --git a/lib/platform/bitbucket/index.spec.ts b/lib/platform/bitbucket/index.spec.ts index 04a7878af3fa6c62e9e13f66072d1e996c4a060f..38b07cf2b1b71274299afd4324eb53ce130b88ef 100644 --- a/lib/platform/bitbucket/index.spec.ts +++ b/lib/platform/bitbucket/index.spec.ts @@ -766,7 +766,7 @@ describe('platform/bitbucket', () => { .reply(200, { reviewers: [reviewer] }) .put('/2.0/repositories/some/repo/pullrequests/5') .reply(200); - await bitbucket.updatePr(5, 'title', 'body'); + await bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }); expect(httpMock.getTrace()).toMatchSnapshot(); }); it('throws an error on failure to get current list of reviewers', async () => { @@ -775,7 +775,7 @@ describe('platform/bitbucket', () => { .get('/2.0/repositories/some/repo/pullrequests/5') .reply(500, undefined); await expect(() => - bitbucket.updatePr(5, 'title', 'body') + bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }) ).rejects.toThrowErrorMatchingSnapshot(); expect(httpMock.getTrace()).toMatchSnapshot(); }); diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 30ad14f102ec3c1612c9be236d98788833f59233..ff5aafcdf546cf5f550eab7bdca8480baf31d036 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -7,9 +7,8 @@ import { REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; import { PLATFORM_TYPE_BITBUCKET } from '../../constants/platforms'; -import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; import { BitbucketHttp, setBaseUrl } from '../../util/http/bitbucket'; @@ -28,6 +27,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -180,7 +180,7 @@ export async function setBaseBranch(branchName: string): Promise<string> { // istanbul ignore next function matchesState(state: string, desiredState: string): boolean { - if (desiredState === PR_STATE_ALL) { + if (desiredState === PrState.All) { return true; } if (desiredState.startsWith('!')) { @@ -205,7 +205,7 @@ export async function getPrList(): Promise<Pr[]> { export async function findPr({ branchName, prTitle, - state = PR_STATE_ALL, + state = PrState.All, }: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); @@ -226,7 +226,7 @@ export async function deleteBranch( closePr?: boolean ): Promise<void> { if (closePr) { - const pr = await findPr({ branchName, state: PR_STATE_OPEN }); + const pr = await findPr({ branchName, state: PrState.Open }); if (pr) { await bitbucketHttp.postJson( `/2.0/repositories/${config.repository}/pullrequests/${pr.number}/decline` @@ -322,7 +322,7 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); return existingPr ? getPr(existingPr.number) : null; } @@ -731,11 +731,11 @@ interface Commit { author: { raw: string }; } -export async function updatePr( - prNo: number, - title: string, - description: string -): Promise<void> { +export async function updatePr({ + number: prNo, + prTitle: title, + prBody: description, +}: UpdatePrConfig): Promise<void> { logger.debug(`updatePr(${prNo}, ${title}, body)`); // Updating a PR in Bitbucket will clear the reviewers if reviewers is not present const pr = ( diff --git a/lib/platform/bitbucket/utils.ts b/lib/platform/bitbucket/utils.ts index 188bbbed0c58a772af2796a226c9c3ee2e289d6d..818389e3c47ec37550dd7f8f54d0db619c1b9577 100644 --- a/lib/platform/bitbucket/utils.ts +++ b/lib/platform/bitbucket/utils.ts @@ -1,6 +1,5 @@ import url from 'url'; -import { PR_STATE_CLOSED } from '../../constants/pull-requests'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import { HttpResponse } from '../../util/http'; import { BitbucketHttp } from '../../util/http/bitbucket'; import { Pr } from '../common'; @@ -139,7 +138,7 @@ export function prInfo(pr: any): Pr { targetBranch: pr.destination.branch.name, title: pr.title, state: prStates.closed.includes(pr.state) - ? /* istanbul ignore next */ PR_STATE_CLOSED + ? /* istanbul ignore next */ PrState.Closed : pr.state.toLowerCase(), createdAt: pr.created_on, }; diff --git a/lib/platform/common.ts b/lib/platform/common.ts index a3d687fc364655417c47d003c36ecf067ac95c8c..9af5e72777a3b04411a09858f12edab0972e1a23 100644 --- a/lib/platform/common.ts +++ b/lib/platform/common.ts @@ -1,5 +1,6 @@ import { BranchStatus, + PrState, VulnerabilityAlert as _VulnerabilityAlert, } from '../types'; @@ -83,6 +84,12 @@ export interface CreatePRConfig { platformOptions?: PlatformPrOptions; draftPR?: boolean; } +export interface UpdatePrConfig { + number: number; + prTitle: string; + prBody?: string; + state?: PrState.Open | PrState.Closed; +} export interface EnsureIssueConfig { title: string; reuseTitle?: string; @@ -100,7 +107,7 @@ export interface BranchStatusConfig { export interface FindPRConfig { branchName: string; prTitle?: string | null; - state?: 'open' | 'closed' | '!open' | 'all'; + state?: PrState.Open | PrState.Closed | PrState.NotOpen | PrState.All; refreshCache?: boolean; } export interface EnsureCommentConfig { @@ -136,7 +143,7 @@ export interface Platform { issueConfig: EnsureIssueConfig ): Promise<EnsureIssueResult | null>; getPrBody(prBody: string): string; - updatePr(number: number, prTitle: string, prBody?: string): Promise<void>; + updatePr(prConfig: UpdatePrConfig): Promise<void>; mergePr(number: number, branchName: string): Promise<boolean>; addReviewers(number: number, reviewers: string[]): Promise<void>; addAssignees(number: number, assignees: string[]): Promise<void>; diff --git a/lib/platform/gitea/gitea-helper.spec.ts b/lib/platform/gitea/gitea-helper.spec.ts index 2117b298377355ffa358a99c5b1639dcd08f556b..edf77fc755d19ecc132f2a90661b9344c43297f1 100644 --- a/lib/platform/gitea/gitea-helper.spec.ts +++ b/lib/platform/gitea/gitea-helper.spec.ts @@ -1,5 +1,5 @@ import * as httpMock from '../../../test/httpMock'; -import { PR_STATE_CLOSED } from '../../constants/pull-requests'; +import { PrState } from '../../types'; import { setBaseUrl } from '../../util/http/gitea'; import * as ght from './gitea-helper'; @@ -63,7 +63,7 @@ describe('platform/gitea/gitea-helper', () => { const mockPR: ght.PR = { number: 13, - state: 'open', + state: PrState.Open, title: 'Some PR', body: 'Lorem ipsum dolor sit amet', mergeable: true, @@ -306,7 +306,7 @@ describe('platform/gitea/gitea-helper', () => { it('should call /api/v1/repos/[repo]/pulls/[pull] endpoint', async () => { const updatedMockPR: ght.PR = { ...mockPR, - state: 'closed', + state: PrState.Closed, title: 'new-title', body: 'new-body', }; @@ -317,7 +317,7 @@ describe('platform/gitea/gitea-helper', () => { .reply(200, updatedMockPR); const res = await ght.updatePR(mockRepo.full_name, mockPR.number, { - state: PR_STATE_CLOSED, + state: PrState.Closed, title: 'new-title', body: 'new-body', assignees: [otherMockUser.username], @@ -392,7 +392,7 @@ describe('platform/gitea/gitea-helper', () => { .reply(200, [mockPR]); const res = await ght.searchPRs(mockRepo.full_name, { - state: 'open', + state: PrState.Open, labels: [mockLabel.id, otherMockLabel.id], }); expect(res).toEqual([mockPR]); diff --git a/lib/platform/gitea/gitea-helper.ts b/lib/platform/gitea/gitea-helper.ts index 39da6d66e199e03d8cf2e21dc509edc86a208aa0..a9436f2ae5d9a397ee424d4d3c381f4ebe6e129f 100644 --- a/lib/platform/gitea/gitea-helper.ts +++ b/lib/platform/gitea/gitea-helper.ts @@ -1,11 +1,10 @@ import { URLSearchParams } from 'url'; -import { PR_STATE_CLOSED } from '../../constants/pull-requests'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import { GiteaHttp, GiteaHttpOptions } from '../../util/http/gitea'; const giteaHttp = new GiteaHttp(); -export type PRState = 'open' | 'closed' | 'all'; +export type PRState = PrState.Open | PrState.Closed | PrState.All; export type IssueState = 'open' | 'closed' | 'all'; export type CommitStatusType = | 'pending' @@ -295,7 +294,7 @@ export async function closePR( ): Promise<void> { await updatePR(repoPath, idx, { ...options, - state: PR_STATE_CLOSED, + state: PrState.Closed, }); } diff --git a/lib/platform/gitea/index.spec.ts b/lib/platform/gitea/index.spec.ts index 72b6ee87cc5c8bd62b9dcaac89db1024a39bdfc1..cc7bd248f8b1606792ecedb12b4609724079827f 100644 --- a/lib/platform/gitea/index.spec.ts +++ b/lib/platform/gitea/index.spec.ts @@ -10,7 +10,7 @@ import { REPOSITORY_MIRRORED, } from '../../constants/error-messages'; import { logger as _logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; import { setBaseUrl } from '../../util/http/gitea'; import * as ght from './gitea-helper'; @@ -52,7 +52,7 @@ describe('platform/gitea', () => { number: 1, title: 'Some PR', body: 'some random pull request', - state: 'open', + state: PrState.Open, diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/1.diff', created_at: '2015-03-22T20:36:16Z', closed_at: null, @@ -68,7 +68,7 @@ describe('platform/gitea', () => { number: 2, title: 'Other PR', body: 'other random pull request', - state: 'closed', + state: PrState.Closed, diff_url: 'https://gitea.renovatebot.com/some/repo/pulls/2.diff', created_at: '2011-08-18T22:30:38Z', closed_at: '2016-01-09T10:03:21Z', @@ -664,7 +664,7 @@ describe('platform/gitea', () => { describe('createPr', () => { const mockNewPR: ght.PR = { number: 42, - state: 'open', + state: PrState.Open, head: { label: 'pr-branch', sha: mockCommitHash, @@ -832,7 +832,7 @@ describe('platform/gitea', () => { describe('updatePr', () => { it('should update pull request with title', async () => { await initFakeRepo(); - await gitea.updatePr(1, 'New Title'); + await gitea.updatePr({ number: 1, prTitle: 'New Title' }); expect(helper.updatePR).toHaveBeenCalledTimes(1); expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, { @@ -842,7 +842,11 @@ describe('platform/gitea', () => { it('should update pull request with title and body', async () => { await initFakeRepo(); - await gitea.updatePr(1, 'New Title', 'New Body'); + await gitea.updatePr({ + number: 1, + prTitle: 'New Title', + prBody: 'New Body', + }); expect(helper.updatePR).toHaveBeenCalledTimes(1); expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, { diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts index fa4ab36f413515c440166961aae189dd253a5955..e636adf75e93bc7366d817e41c4c9f85c6c9f120 100644 --- a/lib/platform/gitea/index.ts +++ b/lib/platform/gitea/index.ts @@ -12,9 +12,8 @@ import { REPOSITORY_MIRRORED, } from '../../constants/error-messages'; import { PLATFORM_TYPE_GITEA } from '../../constants/platforms'; -import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; import { setBaseUrl } from '../../util/http/gitea'; @@ -34,6 +33,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -107,7 +107,7 @@ function toRenovatePR(data: helper.PR): Pr | null { } function matchesState(actual: string, expected: string): boolean { - if (expected === PR_STATE_ALL) { + if (expected === PrState.All) { return true; } if (expected.startsWith('!')) { @@ -427,7 +427,11 @@ const platform: Platform = { getPrList(): Promise<Pr[]> { if (config.prList === null) { config.prList = helper - .searchPRs(config.repository, { state: 'all' }, { useCache: false }) + .searchPRs( + config.repository, + { state: PrState.All }, + { useCache: false } + ) .then((prs) => { const prList = prs.map(toRenovatePR).filter(Boolean); logger.debug(`Retrieved ${prList.length} Pull Requests`); @@ -466,7 +470,7 @@ const platform: Platform = { async findPr({ branchName, prTitle: title, - state = PR_STATE_ALL, + state = PrState.All, }: FindPRConfig): Promise<Pr> { logger.debug(`findPr(${branchName}, ${title}, ${state})`); const prList = await platform.getPrList(); @@ -531,7 +535,7 @@ const platform: Platform = { config.prList = null; const pr = await platform.findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); // If a valid PR was found, return and gracefully recover from the error. Otherwise, abort and throw error. @@ -540,7 +544,11 @@ const platform: Platform = { logger.debug( `Recovered from 409 Conflict, but PR for ${branchName} is outdated. Updating...` ); - await platform.updatePr(pr.number, title, body); + await platform.updatePr({ + number: pr.number, + prTitle: title, + prBody: body, + }); pr.title = title; pr.body = body; } else { @@ -557,7 +565,11 @@ const platform: Platform = { } }, - async updatePr(number: number, title: string, body?: string): Promise<void> { + async updatePr({ + number, + prTitle: title, + prBody: body, + }: UpdatePrConfig): Promise<void> { await helper.updatePR(config.repository, number, { title, ...(body && { body }), @@ -782,7 +794,7 @@ const platform: Platform = { async getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); - const pr = await platform.findPr({ branchName, state: PR_STATE_OPEN }); + const pr = await platform.findPr({ branchName, state: PrState.Open }); return pr ? platform.getPr(pr.number) : null; }, diff --git a/lib/platform/github/index.spec.ts b/lib/platform/github/index.spec.ts index 333d8078fc9275dac7be798468bc396af81163fb..0a718a1f76997b22768f6d567c4d8958101a3fd3 100644 --- a/lib/platform/github/index.spec.ts +++ b/lib/platform/github/index.spec.ts @@ -6,7 +6,7 @@ import { REPOSITORY_NOT_FOUND, REPOSITORY_RENAMED, } from '../../constants/error-messages'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; import { Platform } from '../common'; @@ -484,12 +484,12 @@ describe('platform/github', () => { { number: 90, head: { ref: 'somebranch', repo: { full_name: 'other/repo' } }, - state: 'open', + state: PrState.Open, }, { number: 91, head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, - state: 'open', + state: PrState.Open, }, ]) .get('/repos/some/repo/pulls/91') @@ -502,7 +502,7 @@ describe('platform/github', () => { sha: '1234', }, head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, - state: 'open', + state: PrState.Open, }); await github.initRepo({ @@ -524,12 +524,12 @@ describe('platform/github', () => { { number: 90, head: { ref: 'somebranch', repo: { full_name: 'other/repo' } }, - state: 'open', + state: PrState.Open, }, { number: 91, head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, - state: 'open', + state: PrState.Open, }, ]) .get('/repos/some/repo/pulls/90') @@ -542,7 +542,7 @@ describe('platform/github', () => { sha: '1234', }, head: { ref: 'somebranch', repo: { full_name: 'other/repo' } }, - state: 'open', + state: PrState.Open, }) .patch('/repos/forked/repo/git/refs/heads/master') .reply(200); @@ -1510,7 +1510,7 @@ describe('platform/github', () => { number: 1, head: { ref: 'branch-a' }, title: 'branch a pr', - state: 'open', + state: PrState.Open, }, ]); @@ -1529,13 +1529,13 @@ describe('platform/github', () => { number: 1, head: { ref: 'branch-a' }, title: 'branch a pr', - state: 'closed', + state: PrState.Closed, }, ]); const res = await github.findPr({ branchName: 'branch-a', - state: '!open', + state: PrState.NotOpen, }); expect(res).toBeDefined(); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -1549,7 +1549,7 @@ describe('platform/github', () => { number: 1, head: { ref: 'branch-a' }, title: 'branch a pr', - state: 'open', + state: PrState.Open, }, ]); let res = await github.findPr({ branchName: 'branch-a' }); @@ -1562,7 +1562,7 @@ describe('platform/github', () => { res = await github.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', - state: 'open', + state: PrState.Open, }); expect(res).toBeDefined(); res = await github.findPr({ branchName: 'branch-b' }); @@ -1703,7 +1703,7 @@ describe('platform/github', () => { .get('/repos/some/repo/pulls/1234') .reply(200, { number: 1, - state: 'closed', + state: PrState.Closed, base: { sha: '1234' }, mergeable: true, merged_at: 'sometime', @@ -1726,7 +1726,7 @@ describe('platform/github', () => { .get('/repos/some/repo/pulls/1234') .reply(200, { number: 1, - state: 'open', + state: PrState.Open, mergeable_state: 'dirty', base: { sha: '1234' }, commits: 1, @@ -1749,7 +1749,7 @@ describe('platform/github', () => { .get('/repos/some/repo/pulls/1234') .reply(200, { number: 1, - state: 'open', + state: PrState.Open, base: { sha: '5678' }, commits: 1, mergeable: true, @@ -1772,7 +1772,11 @@ describe('platform/github', () => { initRepoMock(scope, 'some/repo'); scope.patch('/repos/some/repo/pulls/1234').reply(200); await github.initRepo({ repository: 'some/repo', token: 'token' } as any); - await github.updatePr(1234, 'The New Title', 'Hello world again'); + await github.updatePr({ + number: 1234, + prTitle: 'The New Title', + prBody: 'Hello world again', + }); expect(httpMock.getTrace()).toMatchSnapshot(); }); }); diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index f03bc7dbcd97a4ea79c645f94a20ec734a603116..2b4d0d0c6578f5a65fee1499b17d39a024f24230 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -16,13 +16,8 @@ import { REPOSITORY_RENAMED, } from '../../constants/error-messages'; import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; -import { - PR_STATE_ALL, - PR_STATE_CLOSED, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import { ExternalHostError } from '../../types/errors/external-host-error'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; @@ -42,6 +37,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -624,7 +620,7 @@ async function getOpenPrs(): Promise<PrList> { for (const pr of nodes) { // https://developer.github.com/v4/object/pullrequest/ pr.displayNumber = `Pull Request #${pr.number}`; - pr.state = PR_STATE_OPEN; + pr.state = PrState.Open; pr.branchName = pr.headRefName; delete pr.headRefName; pr.targetBranch = pr.baseRefName; @@ -703,7 +699,7 @@ export async function getPr(prNo: number): Promise<Pr | null> { } // Harmonise PR values pr.displayNumber = `Pull Request #${pr.number}`; - if (pr.state === PR_STATE_OPEN) { + if (pr.state === PrState.Open) { pr.branchName = pr.head ? pr.head.ref : undefined; pr.sha = pr.head ? pr.head.sha : undefined; if (pr.mergeable === true) { @@ -721,7 +717,7 @@ export async function getPr(prNo: number): Promise<Pr | null> { } function matchesState(state: string, desiredState: string): boolean { - if (desiredState === PR_STATE_ALL) { + if (desiredState === PrState.All) { return true; } if (desiredState.startsWith('!')) { @@ -760,8 +756,8 @@ export async function getPrList(): Promise<Pr[]> { sha: pr.head.sha, title: pr.title, state: - pr.state === PR_STATE_CLOSED && pr.merged_at?.length - ? /* istanbul ignore next */ 'merged' + pr.state === PrState.Closed && pr.merged_at?.length + ? /* istanbul ignore next */ PrState.Merged : pr.state, createdAt: pr.created_at, closed_at: pr.closed_at, @@ -775,7 +771,7 @@ export async function getPrList(): Promise<Pr[]> { export async function findPr({ branchName, prTitle, - state = PR_STATE_ALL, + state = PrState.All, }: FindPRConfig): Promise<Pr | null> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); @@ -797,7 +793,7 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> { logger.debug(`getBranchPr(${branchName})`); const existingPr = await findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); return existingPr ? getPr(existingPr.number) : null; } @@ -1453,11 +1449,11 @@ export async function createPr({ return pr; } -export async function updatePr( - prNo: number, - title: string, - rawBody?: string -): Promise<void> { +export async function updatePr({ + number: prNo, + prTitle: title, + prBody: rawBody, +}: UpdatePrConfig): Promise<void> { logger.debug(`updatePr(${prNo}, ${title}, body)`); const body = sanitize(rawBody); const patchBody: any = { title }; diff --git a/lib/platform/gitlab/index.spec.ts b/lib/platform/gitlab/index.spec.ts index ca818f05aead3738d7bf0ea1f647ba7fcdd61a06..4003e7bef37761bb71dbf72739110c1760e5efa4 100644 --- a/lib/platform/gitlab/index.spec.ts +++ b/lib/platform/gitlab/index.spec.ts @@ -9,11 +9,7 @@ import { REPOSITORY_EMPTY, REPOSITORY_MIRRORED, } from '../../constants/error-messages'; -import { - PR_STATE_NOT_OPEN, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; import * as _hostRules from '../../util/host-rules'; @@ -907,12 +903,12 @@ describe('platform/gitlab', () => { iid: 1, source_branch: 'branch-a', title: 'branch a pr', - state: 'merged', + state: PrState.Merged, }, ]); const res = await gitlab.findPr({ branchName: 'branch-a', - state: PR_STATE_NOT_OPEN, + state: PrState.NotOpen, }); expect(res).toBeDefined(); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -935,7 +931,7 @@ describe('platform/gitlab', () => { const res = await gitlab.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', - state: PR_STATE_OPEN, + state: PrState.Open, }); expect(res).toBeDefined(); expect(httpMock.getTrace()).toMatchSnapshot(); @@ -1048,7 +1044,7 @@ describe('platform/gitlab', () => { id: 1, iid: 12345, description: 'a merge request', - state: 'merged', + state: PrState.Merged, merge_status: 'cannot_be_merged', diverged_commits_count: 5, source_branch: 'some-branch', @@ -1070,7 +1066,7 @@ describe('platform/gitlab', () => { id: 1, iid: 12345, description: 'a merge request', - state: 'open', + state: PrState.Open, diverged_commits_count: 5, source_branch: 'some-branch', target_branch: 'master', @@ -1097,7 +1093,7 @@ describe('platform/gitlab', () => { id: 1, iid: 12345, description: 'a merge request', - state: 'open', + state: PrState.Open, merge_status: 'cannot_be_merged', diverged_commits_count: 2, source_branch: 'some-branch', @@ -1121,7 +1117,7 @@ describe('platform/gitlab', () => { .scope(gitlabApiHost) .put('/api/v4/projects/undefined/merge_requests/1') .reply(200); - await gitlab.updatePr(1, 'title', 'body'); + await gitlab.updatePr({ number: 1, prTitle: 'title', prBody: 'body' }); expect(httpMock.getTrace()).toMatchSnapshot(); }); }); @@ -1168,7 +1164,7 @@ These updates have all been created already. Click a checkbox below to force a r id: 1, iid: 12345, description: 'a merge request', - state: 'merged', + state: PrState.Merged, merge_status: 'cannot_be_merged', diverged_commits_count: 5, source_branch: 'some-branch', diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index 19e511d6de334324b1c73c05fe80aac4b58c3ed2..182b7da93e4169f7d6f91f8792f0213c3f3521d5 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -15,9 +15,8 @@ import { REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms'; -import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import * as git from '../../util/git'; import * as hostRules from '../../util/host-rules'; import { HttpResponse } from '../../util/http'; @@ -37,6 +36,7 @@ import { Pr, RepoParams, RepoResult, + UpdatePrConfig, VulnerabilityAlert, } from '../common'; import { smartTruncate } from '../utils/pr-body'; @@ -441,7 +441,7 @@ export async function getPr(iid: number): Promise<Pr> { pr.number = pr.iid; pr.displayNumber = `Merge Request #${pr.iid}`; pr.body = pr.description; - pr.state = pr.state === 'opened' ? PR_STATE_OPEN : pr.state; + pr.state = pr.state === 'opened' ? PrState.Open : pr.state; pr.hasAssignees = !!(pr.assignee?.id || pr.assignees?.[0]?.id); delete pr.assignee; delete pr.assignees; @@ -450,7 +450,7 @@ export async function getPr(iid: number): Promise<Pr> { logger.debug('pr cannot be merged'); pr.canMerge = false; pr.isConflicted = true; - } else if (pr.state === PR_STATE_OPEN) { + } else if (pr.state === PrState.Open) { const branchStatus = await getBranchStatus(pr.branchName, []); if (branchStatus === BranchStatus.green) { pr.canMerge = true; @@ -471,11 +471,11 @@ async function closePr(iid: number): Promise<void> { ); } -export async function updatePr( - iid: number, - title: string, - description: string -): Promise<void> { +export async function updatePr({ + number: iid, + prTitle: title, + prBody: description, +}: UpdatePrConfig): Promise<void> { await gitlabApi.putJson( `projects/${config.repository}/merge_requests/${iid}`, { @@ -957,7 +957,7 @@ async function fetchPrList(): Promise<Pr[]> { number: pr.iid, branchName: pr.source_branch, title: pr.title, - state: pr.state === 'opened' ? PR_STATE_OPEN : pr.state, + state: pr.state === 'opened' ? PrState.Open : pr.state, createdAt: pr.created_at, })); } catch (err) /* istanbul ignore next */ { @@ -977,7 +977,7 @@ export async function getPrList(): Promise<Pr[]> { } function matchesState(state: string, desiredState: string): boolean { - if (desiredState === PR_STATE_ALL) { + if (desiredState === PrState.All) { return true; } if (desiredState.startsWith('!')) { @@ -989,7 +989,7 @@ function matchesState(state: string, desiredState: string): boolean { export async function findPr({ branchName, prTitle, - state = PR_STATE_ALL, + state = PrState.All, }: FindPRConfig): Promise<Pr> { logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`); const prList = await getPrList(); diff --git a/lib/types/index.ts b/lib/types/index.ts index 05321e3056e3bbe0afd07eb4d9a090ffcf451bef..e1e0fa9d4271991258fb868c06e04e77c5541fe6 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -3,3 +3,4 @@ export * from './skip-reason'; export * from './versioning'; export * from './branch-status'; export * from './vulnerability-alert'; +export * from './pr-state'; diff --git a/lib/types/pr-state.ts b/lib/types/pr-state.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a3812fcdff2ae30eb8cd267ef874a7c454ac517 --- /dev/null +++ b/lib/types/pr-state.ts @@ -0,0 +1,7 @@ +export enum PrState { + Merged = 'merged', + Open = 'open', + Closed = 'closed', + All = 'all', + NotOpen = '!open', +} diff --git a/lib/workers/branch/check-existing.spec.ts b/lib/workers/branch/check-existing.spec.ts index 0d7d64bd1531cee091e962f4df11b63274378e89..569dbdfbf6f6defc5c6c34fcd936c0ed936f2de6 100644 --- a/lib/workers/branch/check-existing.spec.ts +++ b/lib/workers/branch/check-existing.spec.ts @@ -1,5 +1,5 @@ import { defaultConfig, partial, platform } from '../../../test/util'; -import { PR_STATE_CLOSED } from '../../constants/pull-requests'; +import { PrState } from '../../types'; import { BranchConfig } from '../common'; import { prAlreadyExisted } from './check-existing'; @@ -28,7 +28,7 @@ describe('workers/branch/check-existing', () => { platform.findPr.mockResolvedValueOnce({ number: 12 } as never); platform.getPr.mockResolvedValueOnce({ number: 12, - state: PR_STATE_CLOSED, + state: PrState.Closed, } as never); expect(await prAlreadyExisted(config)).toEqual({ number: 12 }); expect(platform.findPr).toHaveBeenCalledTimes(1); diff --git a/lib/workers/branch/check-existing.ts b/lib/workers/branch/check-existing.ts index 4b855229fdbd8861cc51f6d0949b0298c1dae215..731455b362285518da04d63d963380e4fb1aefaa 100644 --- a/lib/workers/branch/check-existing.ts +++ b/lib/workers/branch/check-existing.ts @@ -1,10 +1,7 @@ import { REPOSITORY_CHANGED } from '../../constants/error-messages'; -import { - PR_STATE_NOT_OPEN, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import { logger } from '../../logger'; import { platform } from '../../platform'; +import { PrState } from '../../types'; import { BranchConfig } from '../common'; /** TODO: Proper return type */ @@ -21,13 +18,13 @@ export async function prAlreadyExisted( const pr = await platform.findPr({ branchName: config.branchName, prTitle: config.prTitle, - state: PR_STATE_NOT_OPEN, + state: PrState.NotOpen, }); if (pr) { logger.debug('Found closed PR with current title'); const prDetails = await platform.getPr(pr.number); // istanbul ignore if - if (prDetails.state === PR_STATE_OPEN) { + if (prDetails.state === PrState.Open) { logger.debug('PR reopened'); throw new Error(REPOSITORY_CHANGED); } diff --git a/lib/workers/branch/index.spec.ts b/lib/workers/branch/index.spec.ts index e8b19bdcda42f3bf69daff976ef01a257393bedd..13f94011ed3ff4fa6f3ce7e4c0a70e2d6e5e1c26 100644 --- a/lib/workers/branch/index.spec.ts +++ b/lib/workers/branch/index.spec.ts @@ -4,12 +4,8 @@ import { MANAGER_LOCKFILE_ERROR, REPOSITORY_CHANGED, } from '../../constants/error-messages'; -import { - PR_STATE_CLOSED, - PR_STATE_MERGED, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import * as _npmPostExtract from '../../manager/npm/post-update'; +import { PrState } from '../../types'; import * as _exec from '../../util/exec'; import { File, StatusResult } from '../../util/git'; import { BranchConfig, PrResult } from '../common'; @@ -113,7 +109,7 @@ describe('workers/branch', () => { config.updateNotScheduled = true; git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_OPEN, + state: PrState.Open, } as never); git.isBranchModified.mockResolvedValueOnce(false); await branchWorker.processBranch(config); @@ -125,7 +121,7 @@ describe('workers/branch', () => { config.updateType = 'major'; checkExisting.prAlreadyExisted.mockResolvedValueOnce({ number: 13, - state: PR_STATE_CLOSED, + state: PrState.Closed, } as never); await branchWorker.processBranch(config); expect(reuse.shouldReuseExistingBranch).toHaveBeenCalledTimes(0); @@ -136,7 +132,7 @@ describe('workers/branch', () => { config.updateType = 'digest'; checkExisting.prAlreadyExisted.mockResolvedValueOnce({ number: 13, - state: PR_STATE_CLOSED, + state: PrState.Closed, }); await branchWorker.processBranch(config); expect(reuse.shouldReuseExistingBranch).toHaveBeenCalledTimes(0); @@ -146,7 +142,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); checkExisting.prAlreadyExisted.mockResolvedValueOnce({ number: 13, - state: PR_STATE_CLOSED, + state: PrState.Closed, }); await branchWorker.processBranch(config); expect(reuse.shouldReuseExistingBranch).toHaveBeenCalledTimes(0); @@ -156,7 +152,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); checkExisting.prAlreadyExisted.mockResolvedValueOnce({ number: 13, - state: PR_STATE_MERGED, + state: PrState.Merged, }); await branchWorker.processBranch(config); expect(reuse.shouldReuseExistingBranch).toHaveBeenCalledTimes(0); @@ -165,7 +161,7 @@ describe('workers/branch', () => { schedule.isScheduledNow.mockReturnValueOnce(false); git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_MERGED, + state: PrState.Merged, } as never); git.isBranchModified.mockResolvedValueOnce(true); await expect(branchWorker.processBranch(config)).rejects.toThrow( @@ -176,7 +172,7 @@ describe('workers/branch', () => { schedule.isScheduledNow.mockReturnValueOnce(false); git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_OPEN, + state: PrState.Open, labels: ['rebase'], } as never); git.isBranchModified.mockResolvedValueOnce(true); @@ -187,7 +183,7 @@ describe('workers/branch', () => { schedule.isScheduledNow.mockReturnValueOnce(false); git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_OPEN, + state: PrState.Open, body: '**Rebasing**: something', } as never); git.isBranchModified.mockResolvedValueOnce(true); @@ -198,7 +194,7 @@ describe('workers/branch', () => { schedule.isScheduledNow.mockReturnValueOnce(false); git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_OPEN, + state: PrState.Open, targetBranch: 'v6', } as never); git.isBranchModified.mockResolvedValueOnce(false); @@ -517,7 +513,7 @@ describe('workers/branch', () => { it('closed pr (dry run)', async () => { git.branchExists.mockResolvedValueOnce(true); checkExisting.prAlreadyExisted.mockResolvedValueOnce({ - state: PR_STATE_CLOSED, + state: PrState.Closed, }); expect( await branchWorker.processBranch({ ...config, dryRun: true }) @@ -527,7 +523,7 @@ describe('workers/branch', () => { it('branch pr no rebase (dry run)', async () => { git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ - state: PR_STATE_OPEN, + state: PrState.Open, } as never); git.isBranchModified.mockResolvedValueOnce(true); expect( @@ -547,7 +543,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ title: 'rebase!', - state: PR_STATE_OPEN, + state: PrState.Open, body: `- [x] <!-- rebase-check -->`, } as never); git.isBranchModified.mockResolvedValueOnce(true); @@ -577,7 +573,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ title: 'rebase!', - state: PR_STATE_OPEN, + state: PrState.Open, body: `- [x] <!-- rebase-check -->`, } as never); git.isBranchModified.mockResolvedValueOnce(true); @@ -608,7 +604,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ title: 'rebase!', - state: PR_STATE_OPEN, + state: PrState.Open, body: `- [x] <!-- rebase-check -->`, } as never); git.isBranchModified.mockResolvedValueOnce(true); @@ -645,7 +641,7 @@ describe('workers/branch', () => { git.branchExists.mockResolvedValueOnce(true); platform.getBranchPr.mockResolvedValueOnce({ title: 'rebase!', - state: 'open', + state: PrState.Open, body: `- [x] <!-- rebase-check -->`, } as never); git.isBranchModified.mockResolvedValueOnce(true); diff --git a/lib/workers/branch/index.ts b/lib/workers/branch/index.ts index 464d08e5d29c6fd2aefa84a346500ee1391066f3..2f9a81c170d4f10e3b426fe8e05fcd6273ebf0a5 100644 --- a/lib/workers/branch/index.ts +++ b/lib/workers/branch/index.ts @@ -12,15 +12,10 @@ import { SYSTEM_INSUFFICIENT_DISK_SPACE, WORKER_FILE_UPDATE_FAILED, } from '../../constants/error-messages'; -import { - PR_STATE_CLOSED, - PR_STATE_MERGED, - PR_STATE_OPEN, -} from '../../constants/pull-requests'; import { logger } from '../../logger'; import { getAdditionalFiles } from '../../manager/npm/post-update'; import { platform } from '../../platform'; -import { BranchStatus } from '../../types'; +import { BranchStatus, PrState } from '../../types'; import { ExternalHostError } from '../../types/errors/external-host-error'; import { emojify } from '../../util/emoji'; import { exec } from '../../util/exec'; @@ -97,7 +92,7 @@ export async function processBranch( { prTitle: config.prTitle }, 'Closed PR already exists. Skipping branch.' ); - if (existingPr.state === PR_STATE_CLOSED) { + if (existingPr.state === PrState.Closed) { const topic = `Renovate Ignore Notification`; let content; if (config.updateType === 'major') { @@ -130,7 +125,7 @@ export async function processBranch( await platform.deleteBranch(config.branchName); } } - } else if (existingPr.state === PR_STATE_MERGED) { + } else if (existingPr.state === PrState.Merged) { logger.debug( { pr: existingPr.number }, 'Merged PR is blocking this branch' @@ -168,7 +163,7 @@ export async function processBranch( logger.debug('Checking if PR has been edited'); if (branchPr) { logger.debug('Found existing branch PR'); - if (branchPr.state !== PR_STATE_OPEN) { + if (branchPr.state !== PrState.Open) { logger.debug( 'PR has been closed or merged since this run started - aborting' ); @@ -192,7 +187,11 @@ export async function processBranch( logger.debug( 'Updating existing PR to indicate that rebasing is not possible' ); - await platform.updatePr(branchPr.number, branchPr.title, newBody); + await platform.updatePr({ + number: branchPr.number, + prTitle: branchPr.title, + prBody: newBody, + }); } return 'pr-edited'; } diff --git a/lib/workers/branch/reuse.spec.ts b/lib/workers/branch/reuse.spec.ts index 5cef523337fb52a47b2c78b512e86464070385bf..d8fd9a5da5e6d5a6886f25b38759f6e16a701771 100644 --- a/lib/workers/branch/reuse.spec.ts +++ b/lib/workers/branch/reuse.spec.ts @@ -1,7 +1,7 @@ import { git, platform } from '../../../test/util'; import { RenovateConfig } from '../../config'; -import { PR_STATE_OPEN } from '../../constants/pull-requests'; import { Pr } from '../../platform'; +import { PrState } from '../../types'; import { shouldReuseExistingBranch } from './reuse'; jest.mock('../../util/git'); @@ -10,7 +10,7 @@ describe('workers/branch/parent', () => { describe('getParentBranch(config)', () => { const pr: Pr = { branchName: 'master', - state: PR_STATE_OPEN, + state: PrState.Open, title: 'any', }; let config: RenovateConfig; diff --git a/lib/workers/pr/index.ts b/lib/workers/pr/index.ts index 65aba8ac2a22b32b2e6617735c3c24379c895e59..f2b8add5c88e46a2e8686c16142b05d7ca202e7a 100644 --- a/lib/workers/pr/index.ts +++ b/lib/workers/pr/index.ts @@ -331,7 +331,7 @@ export async function ensurePr( if (config.dryRun) { logger.info('DRY-RUN: Would update PR #' + existingPr.number); } else { - await platform.updatePr(existingPr.number, prTitle, prBody); + await platform.updatePr({ number: existingPr.number, prTitle, prBody }); logger.info({ pr: existingPr.number, prTitle }, `PR updated`); } return { prResult: PrResult.Updated, pr: existingPr }; diff --git a/lib/workers/repository/dependency-dashboard.spec.ts b/lib/workers/repository/dependency-dashboard.spec.ts index 47b80760267d6ac4527055ad876edd53d79f95e5..909d5ad9d5c1c2f0e008d9ed9bae3d46cd87f038 100644 --- a/lib/workers/repository/dependency-dashboard.spec.ts +++ b/lib/workers/repository/dependency-dashboard.spec.ts @@ -2,8 +2,8 @@ import fs from 'fs'; import { mock } from 'jest-mock-extended'; import { RenovateConfig, getConfig, platform } from '../../../test/util'; import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; -import { PR_STATE_NOT_OPEN } from '../../constants/pull-requests'; import { Platform, Pr } from '../../platform'; +import { PrState } from '../../types'; import { BranchConfig, BranchUpgradeConfig } from '../common'; import * as dependencyDashboard from './dependency-dashboard'; @@ -354,10 +354,10 @@ describe('workers/repository/master-issue', () => { expect(platform.findPr).toHaveBeenCalledTimes(2); 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(PR_STATE_NOT_OPEN); + expect(platform.findPr.mock.calls[0][0].state).toBe(PrState.NotOpen); 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(PR_STATE_NOT_OPEN); + expect(platform.findPr.mock.calls[1][0].state).toBe(PrState.NotOpen); // same with dry run await dryRun(branches, platform, 0, 0, 0, 2); diff --git a/lib/workers/repository/dependency-dashboard.ts b/lib/workers/repository/dependency-dashboard.ts index 3ece53276e019fb4739591b0b75d19ce852bba89..c687327704ca2e0b401a86cf19317c3928338ef4 100644 --- a/lib/workers/repository/dependency-dashboard.ts +++ b/lib/workers/repository/dependency-dashboard.ts @@ -1,7 +1,7 @@ import { RenovateConfig } from '../../config'; -import { PR_STATE_NOT_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; import { Pr, platform } from '../../platform'; +import { PrState } from '../../types'; import { BranchConfig } from '../common'; function getListItem(branch: BranchConfig, type: string, pr?: Pr): string { @@ -197,7 +197,7 @@ export async function ensureMasterIssue( const pr = await platform.findPr({ branchName: branch.branchName, prTitle: branch.prTitle, - state: PR_STATE_NOT_OPEN, + state: PrState.NotOpen, }); issueBody += getListItem(branch, 'recreate', pr); } diff --git a/lib/workers/repository/error-config.spec.ts b/lib/workers/repository/error-config.spec.ts index 922ef19d202abe8d6bbcd216167e2341ddaf8c1a..d6cf860ba4309ac9d1cb1a28f809d39088786dc2 100644 --- a/lib/workers/repository/error-config.spec.ts +++ b/lib/workers/repository/error-config.spec.ts @@ -1,8 +1,8 @@ import { mock } from 'jest-mock-extended'; import { RenovateConfig, getConfig, platform } from '../../../test/util'; import { CONFIG_VALIDATION } from '../../constants/error-messages'; -import { PR_STATE_OPEN } from '../../constants/pull-requests'; import { Pr } from '../../platform'; +import { PrState } from '../../types'; import { raiseConfigWarningIssue } from './error-config'; jest.mock('../../platform'); @@ -41,7 +41,7 @@ describe('workers/repository/error-config', () => { platform.getBranchPr.mockResolvedValue({ ...mock<Pr>(), number: 1, - state: PR_STATE_OPEN, + state: PrState.Open, }); const res = await raiseConfigWarningIssue(config, error); expect(res).toBeUndefined(); @@ -53,7 +53,7 @@ describe('workers/repository/error-config', () => { platform.getBranchPr.mockResolvedValue({ ...mock<Pr>(), number: 1, - state: PR_STATE_OPEN, + state: PrState.Open, }); const res = await raiseConfigWarningIssue( { ...config, dryRun: true }, diff --git a/lib/workers/repository/error-config.ts b/lib/workers/repository/error-config.ts index d34054322d190253c7154817747d5742c44e0cc3..95a1cdaef21059ea57f0ed3fbd4ef57818354330 100644 --- a/lib/workers/repository/error-config.ts +++ b/lib/workers/repository/error-config.ts @@ -1,7 +1,7 @@ import { RenovateConfig } from '../../config'; -import { PR_STATE_OPEN } from '../../constants/pull-requests'; import { logger } from '../../logger'; import { platform } from '../../platform'; +import { PrState } from '../../types'; export async function raiseConfigWarningIssue( config: RenovateConfig, @@ -17,14 +17,18 @@ export async function raiseConfigWarningIssue( body += `Message: \`${error.validationMessage}\`\n`; } const pr = await platform.getBranchPr(config.onboardingBranch); - if (pr?.state === PR_STATE_OPEN) { + if (pr?.state === PrState.Open) { logger.debug('Updating onboarding PR with config error notice'); body = `## Action Required: Fix Renovate Configuration\n\n${body}`; body += `\n\nOnce you have resolved this problem (in this onboarding branch), Renovate will return to providing you with a preview of your repository's configuration.`; if (config.dryRun) { logger.info(`DRY-RUN: Would update PR #${pr.number}`); } else { - await platform.updatePr(pr.number, config.onboardingPrTitle, body); + await platform.updatePr({ + number: pr.number, + prTitle: config.onboardingPrTitle, + prBody: body, + }); } } else if (config.dryRun) { logger.info('DRY-RUN: Would ensure config error issue'); diff --git a/lib/workers/repository/finalise/prune.ts b/lib/workers/repository/finalise/prune.ts index dd534c04b7bb96a0dc82a4fb12bbb7f7bd0fd790..f907faa8465a1fee3be767a4a200592b996facac 100644 --- a/lib/workers/repository/finalise/prune.ts +++ b/lib/workers/repository/finalise/prune.ts @@ -1,8 +1,8 @@ import { RenovateConfig } from '../../../config'; import { REPOSITORY_CHANGED } from '../../../constants/error-messages'; -import { PR_STATE_OPEN } from '../../../constants/pull-requests'; import { logger } from '../../../logger'; import { platform } from '../../../platform'; +import { PrState } from '../../../types'; import { getAllRenovateBranches, isBranchModified } from '../../../util/git'; async function cleanUpBranches( @@ -13,7 +13,7 @@ async function cleanUpBranches( try { const pr = await platform.findPr({ branchName, - state: PR_STATE_OPEN, + state: PrState.Open, }); const branchIsModified = await isBranchModified(branchName); if (pr && !branchIsModified) { @@ -27,7 +27,10 @@ async function cleanUpBranches( `PRUNING-DISABLED: Would update pr ${pr.number} to ${pr.title} - autoclosed` ); } else { - await platform.updatePr(pr.number, `${pr.title} - autoclosed`); + await platform.updatePr({ + number: pr.number, + prTitle: `${pr.title} - autoclosed`, + }); } } } diff --git a/lib/workers/repository/onboarding/branch/check.ts b/lib/workers/repository/onboarding/branch/check.ts index e74f623e000d482afb2ae0f9e25311f75c19a7db..f70b806db1800baec88805f5d208480937e9c7bd 100644 --- a/lib/workers/repository/onboarding/branch/check.ts +++ b/lib/workers/repository/onboarding/branch/check.ts @@ -1,9 +1,9 @@ import { RenovateConfig } from '../../../../config'; import { configFileNames } from '../../../../config/app-strings'; import { REPOSITORY_DISABLED } from '../../../../constants/error-messages'; -import { PR_STATE_NOT_OPEN } from '../../../../constants/pull-requests'; import { logger } from '../../../../logger'; import { platform } from '../../../../platform'; +import { PrState } from '../../../../types'; import { readLocalFile } from '../../../../util/fs'; import { getFileList } from '../../../../util/git'; @@ -41,7 +41,7 @@ const closedPrExists = (config: RenovateConfig): Promise<Pr> => platform.findPr({ branchName: config.onboardingBranch, prTitle: config.onboardingPrTitle, - state: PR_STATE_NOT_OPEN, + state: PrState.NotOpen, }); export const isOnboarded = async (config: RenovateConfig): Promise<boolean> => { diff --git a/lib/workers/repository/onboarding/branch/index.spec.ts b/lib/workers/repository/onboarding/branch/index.spec.ts index 639c7ecba068935e64c532a26d2650bfacb4a0bf..19e5994e538cff03c7d52158e4d0335aed456556 100644 --- a/lib/workers/repository/onboarding/branch/index.spec.ts +++ b/lib/workers/repository/onboarding/branch/index.spec.ts @@ -6,8 +6,8 @@ import { git, platform, } from '../../../../../test/util'; -import { PR_STATE_OPEN } from '../../../../constants/pull-requests'; import { Pr } from '../../../../platform'; +import { PrState } from '../../../../types'; import * as _rebase from './rebase'; import { checkOnboardingBranch } from '.'; @@ -85,7 +85,7 @@ describe('workers/repository/onboarding/branch', () => { { ...mock<Pr>(), branchName: 'renovate/something', - state: PR_STATE_OPEN, + state: PrState.Open, }, ]); await expect(checkOnboardingBranch(config)).rejects.toThrow(); diff --git a/lib/workers/repository/onboarding/pr/index.ts b/lib/workers/repository/onboarding/pr/index.ts index 25c005ade5340e5fbd99a310b0352c76b20815a6..829bc4db405e482ad0021ea0031ef047e5871d33 100644 --- a/lib/workers/repository/onboarding/pr/index.ts +++ b/lib/workers/repository/onboarding/pr/index.ts @@ -113,7 +113,11 @@ If you need any further assistance then you can also [request help here](${confi if (config.dryRun) { logger.info('DRY-RUN: Would update onboarding PR'); } else { - await platform.updatePr(existingPr.number, existingPr.title, prBody); + await platform.updatePr({ + number: existingPr.number, + prTitle: existingPr.title, + prBody, + }); logger.info({ pr: existingPr.number }, 'Onboarding PR updated'); } return;