diff --git a/lib/platform/azure/__snapshots__/azure-helper.spec.ts.snap b/lib/platform/azure/__snapshots__/azure-helper.spec.ts.snap index 24b2fead865c70ce65ecec9f8454983af9d390b2..962fae67fba520d462a46071ce8941291756252e 100644 --- a/lib/platform/azure/__snapshots__/azure-helper.spec.ts.snap +++ b/lib/platform/azure/__snapshots__/azure-helper.spec.ts.snap @@ -54,59 +54,6 @@ Array [ ] `; -exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed v2) 1`] = ` -Object { - "body": undefined, - "branchName": undefined, - "createdAt": undefined, - "displayNumber": "Pull Request #undefined", - "number": undefined, - "state": "merged", - "status": 3, - "targetBranch": undefined, -} -`; - -exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed) 1`] = ` -Object { - "body": undefined, - "branchName": undefined, - "createdAt": undefined, - "displayNumber": "Pull Request #undefined", - "number": undefined, - "state": "closed", - "status": 2, - "targetBranch": undefined, -} -`; - -exports[`platform/azure/helpers getRenovatePRFormat should be formated (isConflicted) 1`] = ` -Object { - "body": undefined, - "branchName": undefined, - "createdAt": undefined, - "displayNumber": "Pull Request #undefined", - "isConflicted": true, - "mergeStatus": 2, - "number": undefined, - "state": "open", - "targetBranch": undefined, -} -`; - -exports[`platform/azure/helpers getRenovatePRFormat should be formated (not closed) 1`] = ` -Object { - "body": undefined, - "branchName": undefined, - "createdAt": undefined, - "displayNumber": "Pull Request #undefined", - "number": undefined, - "state": "open", - "status": 1, - "targetBranch": undefined, -} -`; - exports[`platform/azure/helpers getStorageExtraCloneOpts should configure basic auth 1`] = ` Object { "-c": "http.extraheader=AUTHORIZATION: basic dXNlcjpwYXNz", diff --git a/lib/platform/azure/__snapshots__/index.spec.ts.snap b/lib/platform/azure/__snapshots__/index.spec.ts.snap index 8e3ca180284bfff0b286a76f3286f876f4016b1e..707811098531a5ea7d103bb26c425b5d5684623b 100644 --- a/lib/platform/azure/__snapshots__/index.spec.ts.snap +++ b/lib/platform/azure/__snapshots__/index.spec.ts.snap @@ -2,17 +2,29 @@ exports[`platform/azure createPr() should create and return a PR object 1`] = ` Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, "displayNumber": "Pull Request #456", "number": 456, "pullRequestId": 456, + "sourceRefName": undefined, + "state": "open", + "targetBranch": undefined, } `; exports[`platform/azure createPr() should create and return a PR object from base branch 1`] = ` Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, "displayNumber": "Pull Request #456", "number": 456, "pullRequestId": 456, + "sourceRefName": undefined, + "state": "open", + "targetBranch": undefined, } `; @@ -21,15 +33,22 @@ Object { "autoCompleteSetBy": Object { "id": 123, }, + "body": undefined, + "branchName": undefined, "completionOptions": Object { "deleteSourceBranch": true, "squashMerge": true, }, + "createdAt": undefined, "createdBy": Object { "id": 123, }, "displayNumber": "Pull Request #456", + "number": 456, "pullRequestId": 456, + "sourceRefName": undefined, + "state": "open", + "targetBranch": undefined, } `; @@ -94,36 +113,68 @@ content", exports[`platform/azure findPr(branchName, prTitle, state) returns pr if found it all state 1`] = ` Object { + "body": undefined, + "branchName": "branch-a", + "createdAt": undefined, + "displayNumber": "Pull Request #1", "number": 1, + "pullRequestId": 1, "sourceRefName": "refs/heads/branch-a", "state": "closed", + "status": 2, + "targetBranch": "branch-b", + "targetRefName": "refs/heads/branch-b", "title": "branch a pr", } `; exports[`platform/azure findPr(branchName, prTitle, state) returns pr if found it close 1`] = ` Object { + "body": undefined, + "branchName": "branch-a", + "createdAt": undefined, + "displayNumber": "Pull Request #1", "number": 1, + "pullRequestId": 1, "sourceRefName": "refs/heads/branch-a", "state": "closed", + "status": 2, + "targetBranch": "branch-b", + "targetRefName": "refs/heads/branch-b", "title": "branch a pr", } `; exports[`platform/azure findPr(branchName, prTitle, state) returns pr if found it open 1`] = ` Object { + "body": undefined, + "branchName": "branch-a", + "createdAt": undefined, + "displayNumber": "Pull Request #1", "number": 1, + "pullRequestId": 1, "sourceRefName": "refs/heads/branch-a", "state": "open", + "status": 1, + "targetBranch": "branch-b", + "targetRefName": "refs/heads/branch-b", "title": "branch a pr", } `; exports[`platform/azure findPr(branchName, prTitle, state) returns pr if found not open 1`] = ` Object { + "body": undefined, + "branchName": "branch-a", + "createdAt": undefined, + "displayNumber": "Pull Request #1", "number": 1, + "pullRequestId": 1, "sourceRefName": "refs/heads/branch-a", - "state": "closed", + "state": "merged", + "status": 3, + "targetBranch": "branch-b", + "targetRefName": "refs/heads/branch-b", "title": "branch a pr", } `; @@ -132,11 +183,19 @@ exports[`platform/azure getBranchPr(branchName) should return the pr 1`] = `null exports[`platform/azure getPr(prNo) should return a pr in the right format 1`] = ` Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, + "displayNumber": "Pull Request #1234", "hasReviewers": false, "labels": Array [ "renovate", ], "number": 1234, + "pullRequestId": 1234, + "sourceRefName": undefined, + "state": "open", + "targetBranch": undefined, } `; diff --git a/lib/platform/azure/__snapshots__/util.spec.ts.snap b/lib/platform/azure/__snapshots__/util.spec.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..6c6623c895bfba5502fc03846b6d573a18d17b81 --- /dev/null +++ b/lib/platform/azure/__snapshots__/util.spec.ts.snap @@ -0,0 +1,58 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed v2) 1`] = ` +Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, + "displayNumber": "Pull Request #undefined", + "number": undefined, + "sourceRefName": undefined, + "state": "merged", + "status": 3, + "targetBranch": undefined, +} +`; + +exports[`platform/azure/helpers getRenovatePRFormat should be formated (closed) 1`] = ` +Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, + "displayNumber": "Pull Request #undefined", + "number": undefined, + "sourceRefName": undefined, + "state": "closed", + "status": 2, + "targetBranch": undefined, +} +`; + +exports[`platform/azure/helpers getRenovatePRFormat should be formated (isConflicted) 1`] = ` +Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, + "displayNumber": "Pull Request #undefined", + "isConflicted": true, + "mergeStatus": 2, + "number": undefined, + "sourceRefName": undefined, + "state": "open", + "targetBranch": undefined, +} +`; + +exports[`platform/azure/helpers getRenovatePRFormat should be formated (not closed) 1`] = ` +Object { + "body": undefined, + "branchName": undefined, + "createdAt": undefined, + "displayNumber": "Pull Request #undefined", + "number": undefined, + "sourceRefName": undefined, + "state": "open", + "status": 1, + "targetBranch": undefined, +} +`; diff --git a/lib/platform/azure/azure-helper.spec.ts b/lib/platform/azure/azure-helper.spec.ts index 0888fbb4ed4bff0c9c41d74bd87d1e74bed25907..0f37e64f4e7472bc5be1d2006ca42fbcaf221d92 100644 --- a/lib/platform/azure/azure-helper.spec.ts +++ b/lib/platform/azure/azure-helper.spec.ts @@ -33,36 +33,6 @@ describe('platform/azure/helpers', () => { }); }); - describe('getNewBranchName', () => { - it('should add refs/heads', () => { - const res = azureHelper.getNewBranchName('testBB'); - expect(res).toBe(`refs/heads/testBB`); - }); - it('should be the same', () => { - const res = azureHelper.getNewBranchName('refs/heads/testBB'); - expect(res).toBe(`refs/heads/testBB`); - }); - }); - - describe('getBranchNameWithoutRefsheadsPrefix', () => { - it('should be renamed', () => { - const res = azureHelper.getBranchNameWithoutRefsheadsPrefix( - 'refs/heads/testBB' - ); - expect(res).toBe(`testBB`); - }); - it('should log error and return undefined', () => { - const res = azureHelper.getBranchNameWithoutRefsheadsPrefix( - undefined as any - ); - expect(res).toBeUndefined(); - }); - it('should return the input', () => { - const res = azureHelper.getBranchNameWithoutRefsheadsPrefix('testBB'); - expect(res).toBe('testBB'); - }); - }); - describe('getRef', () => { it('should get the ref with short ref name', async () => { azureApi.gitApi.mockImplementationOnce( @@ -248,28 +218,6 @@ describe('platform/azure/helpers', () => { }); }); - describe('getRenovatePRFormat', () => { - it('should be formated (closed)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 2 } as any); - expect(res).toMatchSnapshot(); - }); - - it('should be formated (closed v2)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 3 } as any); - expect(res).toMatchSnapshot(); - }); - - it('should be formated (not closed)', () => { - const res = azureHelper.getRenovatePRFormat({ status: 1 } as any); - expect(res).toMatchSnapshot(); - }); - - it('should be formated (isConflicted)', () => { - const res = azureHelper.getRenovatePRFormat({ mergeStatus: 2 } as any); - expect(res).toMatchSnapshot(); - }); - }); - describe('getCommitDetails', () => { it('should get commit details', async () => { azureApi.gitApi.mockImplementationOnce( diff --git a/lib/platform/azure/azure-helper.ts b/lib/platform/azure/azure-helper.ts index 32b9f18ae829b5d2d263513f9025729356d19287..2ff0573082f070bee278282a4cddfe920f1d8b71 100644 --- a/lib/platform/azure/azure-helper.ts +++ b/lib/platform/azure/azure-helper.ts @@ -1,15 +1,18 @@ import { GitCommit, - GitPullRequest, GitPullRequestMergeStrategy, GitRef, } from 'azure-devops-node-api/interfaces/GitInterfaces'; import { logger } from '../../logger'; -import { HostRule, PrState } from '../../types'; +import { HostRule } from '../../types'; import { GitOptions } from '../../types/git'; import * as azureApi from './azure-got-wrapper'; -import { AzurePr } from './types'; +import { + getBranchNameWithoutRefsPrefix, + getBranchNameWithoutRefsheadsPrefix, + getNewBranchName, +} from './util'; const mergePolicyGuid = 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab'; // Magic GUID for merge strategy policy configurations @@ -32,45 +35,6 @@ export function getStorageExtraCloneOpts(config: HostRule): GitOptions { return { '-c': `http.extraheader=${header}` }; } -export function getNewBranchName(branchName?: string): string { - if (branchName && !branchName.startsWith('refs/heads/')) { - return `refs/heads/${branchName}`; - } - return branchName; -} - -export function getBranchNameWithoutRefsheadsPrefix( - branchPath: string -): string | undefined { - if (!branchPath) { - logger.error(`getBranchNameWithoutRefsheadsPrefix(${branchPath})`); - return undefined; - } - if (!branchPath.startsWith('refs/heads/')) { - logger.trace( - `The refs/heads/ name should have started with 'refs/heads/' but it didn't. (${branchPath})` - ); - return branchPath; - } - return branchPath.substring(11, branchPath.length); -} - -function getBranchNameWithoutRefsPrefix( - branchPath?: string -): string | undefined { - if (!branchPath) { - logger.error(`getBranchNameWithoutRefsPrefix(${branchPath})`); - return undefined; - } - if (!branchPath.startsWith('refs/')) { - logger.trace( - `The ref name should have started with 'refs/' but it didn't. (${branchPath})` - ); - return branchPath; - } - return branchPath.substring(5, branchPath.length); -} - export async function getRefs( repoId: string, branchName?: string @@ -175,48 +139,6 @@ export function max4000Chars(str: string): string { return str; } -export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr { - const pr: AzurePr = azurePr as any; - - pr.displayNumber = `Pull Request #${azurePr.pullRequestId}`; - pr.number = azurePr.pullRequestId; - pr.body = azurePr.description; - pr.targetBranch = getBranchNameWithoutRefsheadsPrefix(azurePr.targetRefName); - pr.branchName = pr.targetBranch; - pr.createdAt = azurePr.creationDate?.toISOString(); - - // status - // export declare enum PullRequestStatus { - // NotSet = 0, - // Active = 1, - // Abandoned = 2, - // Completed = 3, - // All = 4, - // } - if (azurePr.status === 2) { - pr.state = PrState.Closed; - } else if (azurePr.status === 3) { - pr.state = PrState.Merged; - } else { - pr.state = PrState.Open; - } - - // mergeStatus - // export declare enum PullRequestAsyncStatus { - // NotSet = 0, - // Queued = 1, - // Conflicts = 2, - // Succeeded = 3, - // RejectedByPolicy = 4, - // Failure = 5, - // } - if (azurePr.mergeStatus === 2) { - pr.isConflicted = true; - } - - return pr; -} - export async function getCommitDetails( commit: string, repoId: string diff --git a/lib/platform/azure/index.spec.ts b/lib/platform/azure/index.spec.ts index 0ad83f30b10456f0fd3cda2bb2ecd35a37fbce3d..597de4701c5cf850018e19e7b10c0e4cfc337430 100644 --- a/lib/platform/azure/index.spec.ts +++ b/lib/platform/azure/index.spec.ts @@ -1,4 +1,5 @@ import is from '@sindresorhus/is'; +import { PullRequestStatus } from 'azure-devops-node-api/interfaces/GitInterfaces'; import { REPOSITORY_DISABLED } from '../../constants/error-messages'; import { BranchStatus, PrState } from '../../types'; import * as _git from '../../util/git'; @@ -189,25 +190,14 @@ describe('platform/azure', () => { { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', + targetRefName: 'refs/heads/branch-b', title: 'branch a pr', - state: PrState.Open, + status: PullRequestStatus.Active, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), } as any) ); - azureHelper.getNewBranchName.mockImplementationOnce( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementationOnce( - () => - ({ - number: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: PrState.Open, - } as any) - ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', @@ -226,25 +216,14 @@ describe('platform/azure', () => { { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', + targetRefName: 'refs/heads/branch-b', title: 'branch a pr', - state: PrState.Closed, + status: PullRequestStatus.Completed, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), } as any) ); - azureHelper.getNewBranchName.mockImplementationOnce( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementationOnce( - () => - ({ - number: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: PrState.Closed, - } as any) - ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', @@ -263,25 +242,14 @@ describe('platform/azure', () => { { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', + targetRefName: 'refs/heads/branch-b', title: 'branch a pr', - state: PrState.Closed, + status: PullRequestStatus.Abandoned, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), } as any) ); - azureHelper.getNewBranchName.mockImplementationOnce( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementationOnce( - () => - ({ - number: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: PrState.Closed, - } as any) - ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', @@ -300,25 +268,14 @@ describe('platform/azure', () => { { pullRequestId: 1, sourceRefName: 'refs/heads/branch-a', + targetRefName: 'refs/heads/branch-b', title: 'branch a pr', - state: PrState.Closed, + status: PullRequestStatus.Abandoned, }, ]), getPullRequestCommits: jest.fn().mockReturnValue([]), } as any) ); - azureHelper.getNewBranchName.mockImplementationOnce( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementationOnce( - () => - ({ - number: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - state: PrState.Closed, - } as any) - ); const res = await azure.findPr({ branchName: 'branch-a', prTitle: 'branch a pr', @@ -372,19 +329,6 @@ describe('platform/azure', () => { getPullRequestCommits: jest.fn().mockReturnValue([]), } as any) ); - azureHelper.getNewBranchName.mockImplementation( - () => 'refs/heads/branch-a' - ); - azureHelper.getRenovatePRFormat.mockImplementation( - () => - ({ - pullRequestId: 1, - number: 1, - head: { ref: 'branch-a' }, - title: 'branch a pr', - isClosed: false, - } as any) - ); const pr = await azure.getBranchPr('somebranch'); expect(pr).toMatchSnapshot(); }); @@ -466,12 +410,6 @@ describe('platform/azure', () => { ]), } as any) ); - azureHelper.getRenovatePRFormat.mockImplementation( - () => - ({ - number: 1234, - } as any) - ); const pr = await azure.getPr(1234); expect(pr).toMatchSnapshot(); }); @@ -490,14 +428,6 @@ describe('platform/azure', () => { createPullRequestLabel: jest.fn(() => ({})), } as any) ); - azureHelper.getRenovatePRFormat.mockImplementation( - () => - ({ - displayNumber: 'Pull Request #456', - number: 456, - pullRequestId: 456, - } as any) - ); const pr = await azure.createPr({ branchName: 'some-branch', targetBranch: 'master', @@ -519,14 +449,6 @@ describe('platform/azure', () => { createPullRequestLabel: jest.fn(() => ({})), } as any) ); - azureHelper.getRenovatePRFormat.mockImplementation( - () => - ({ - displayNumber: 'Pull Request #456', - number: 456, - pullRequestId: 456, - } as any) - ); const pr = await azure.createPr({ branchName: 'some-branch', targetBranch: 'master', @@ -566,7 +488,6 @@ describe('platform/azure', () => { updatePullRequest: updateFn, } as any) ); - azureHelper.getRenovatePRFormat.mockImplementation((x) => x as any); const pr = await azure.createPr({ branchName: 'some-branch', targetBranch: 'dev', diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index f726d8550e518c397eb539575975781b13a543bf..464b209a664efd6992f025cd4583470f02733759 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -36,6 +36,11 @@ import { smartTruncate } from '../utils/pr-body'; import * as azureApi from './azure-got-wrapper'; import * as azureHelper from './azure-helper'; import { AzurePr } from './types'; +import { + getBranchNameWithoutRefsheadsPrefix, + getNewBranchName, + getRenovatePRFormat, +} from './util'; interface Config { repoForceRebase: boolean; @@ -194,7 +199,7 @@ export async function getPrList(): Promise<AzurePr[]> { skip += 100; } while (fetchedPrs.length > 0); - config.prList = prs.map(azureHelper.getRenovatePRFormat); + config.prList = prs.map(getRenovatePRFormat); logger.debug({ length: config.prList.length }, 'Retrieved Pull Requests'); } return config.prList; @@ -236,7 +241,7 @@ export async function findPr({ const prs = await getPrList(); prsFiltered = prs.filter( - (item) => item.sourceRefName === azureHelper.getNewBranchName(branchName) + (item) => item.sourceRefName === getNewBranchName(branchName) ); if (prTitle) { @@ -280,7 +285,7 @@ export async function getBranchStatusCheck( const azureApiGit = await azureApi.gitApi(); const branch = await azureApiGit.getBranch( config.repoId, - azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName)! + getBranchNameWithoutRefsheadsPrefix(branchName)! ); if (branch.aheadCount === 0) { return BranchStatus.green; @@ -315,8 +320,8 @@ export async function createPr({ draftPR = false, platformOptions = {}, }: CreatePRConfig): Promise<Pr> { - const sourceRefName = azureHelper.getNewBranchName(branchName); - const targetRefName = azureHelper.getNewBranchName(targetBranch); + const sourceRefName = getNewBranchName(branchName); + const targetRefName = getNewBranchName(targetBranch); const description = azureHelper.max4000Chars(sanitize(body)); const azureApiGit = await azureApi.gitApi(); const workItemRefs = [ @@ -361,7 +366,7 @@ export async function createPr({ ) ) ); - return azureHelper.getRenovatePRFormat(pr); + return getRenovatePRFormat(pr); } export async function updatePr({ diff --git a/lib/platform/azure/util.spec.ts b/lib/platform/azure/util.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fc2c8dc1974a2148ceb955df6c97e6e955a8cb3 --- /dev/null +++ b/lib/platform/azure/util.spec.ts @@ -0,0 +1,55 @@ +import { + getBranchNameWithoutRefsheadsPrefix, + getNewBranchName, + getRenovatePRFormat, +} from './util'; + +describe('platform/azure/helpers', () => { + describe('getNewBranchName', () => { + it('should add refs/heads', () => { + const res = getNewBranchName('testBB'); + expect(res).toBe(`refs/heads/testBB`); + }); + it('should be the same', () => { + const res = getNewBranchName('refs/heads/testBB'); + expect(res).toBe(`refs/heads/testBB`); + }); + }); + + describe('getBranchNameWithoutRefsheadsPrefix', () => { + it('should be renamed', () => { + const res = getBranchNameWithoutRefsheadsPrefix('refs/heads/testBB'); + expect(res).toBe(`testBB`); + }); + it('should log error and return undefined', () => { + const res = getBranchNameWithoutRefsheadsPrefix(undefined as any); + expect(res).toBeUndefined(); + }); + it('should return the input', () => { + const res = getBranchNameWithoutRefsheadsPrefix('testBB'); + expect(res).toBe('testBB'); + }); + }); + + describe('getRenovatePRFormat', () => { + it('should be formated (closed)', () => { + const res = getRenovatePRFormat({ status: 2 } as any); + expect(res).toMatchSnapshot(); + }); + + it('should be formated (closed v2)', () => { + const res = getRenovatePRFormat({ status: 3 } as any); + expect(res).toMatchSnapshot(); + }); + + it('should be formated (not closed)', () => { + const res = getRenovatePRFormat({ status: 1 } as any); + expect(res).toMatchSnapshot(); + }); + + it('should be formated (isConflicted)', () => { + const res = getRenovatePRFormat({ mergeStatus: 2 } as any); + expect(res).toMatchSnapshot(); + }); + }); +}); diff --git a/lib/platform/azure/util.ts b/lib/platform/azure/util.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f1fc3ec0bc0394d5191790f93ec68f4560a4dde --- /dev/null +++ b/lib/platform/azure/util.ts @@ -0,0 +1,82 @@ +import { + GitPullRequest, + PullRequestAsyncStatus, + PullRequestStatus, +} from 'azure-devops-node-api/interfaces/GitInterfaces'; +import { logger } from '../../logger'; +import { PrState } from '../../types'; +import { AzurePr } from './types'; + +export function getNewBranchName(branchName?: string): string { + if (branchName && !branchName.startsWith('refs/heads/')) { + return `refs/heads/${branchName}`; + } + return branchName; +} + +export function getBranchNameWithoutRefsheadsPrefix( + branchPath: string +): string | undefined { + if (!branchPath) { + logger.error(`getBranchNameWithoutRefsheadsPrefix(${branchPath})`); + return undefined; + } + if (!branchPath.startsWith('refs/heads/')) { + logger.trace( + `The refs/heads/ name should have started with 'refs/heads/' but it didn't. (${branchPath})` + ); + return branchPath; + } + return branchPath.substring(11, branchPath.length); +} + +export function getBranchNameWithoutRefsPrefix( + branchPath?: string +): string | undefined { + if (!branchPath) { + logger.error(`getBranchNameWithoutRefsPrefix(${branchPath})`); + return undefined; + } + if (!branchPath.startsWith('refs/')) { + logger.trace( + `The ref name should have started with 'refs/' but it didn't. (${branchPath})` + ); + return branchPath; + } + return branchPath.substring(5, branchPath.length); +} + +export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr { + const number = azurePr.pullRequestId; + const displayNumber = `Pull Request #${number}`; + + const branchName = getBranchNameWithoutRefsheadsPrefix(azurePr.sourceRefName); + const targetBranch = getBranchNameWithoutRefsheadsPrefix( + azurePr.targetRefName + ); + const body = azurePr.description; + + const createdAt = azurePr.creationDate?.toISOString(); + const state = + { + [PullRequestStatus.Abandoned]: PrState.Closed, + [PullRequestStatus.Completed]: PrState.Merged, + }[azurePr.status] || PrState.Open; + + const sourceRefName = azurePr.sourceRefName; + + const isConflicted = azurePr.mergeStatus === PullRequestAsyncStatus.Conflicts; + + return { + ...azurePr, + branchName, + state, + number, + displayNumber, + body, + sourceRefName, + targetBranch, + createdAt, + ...(isConflicted && { isConflicted }), + } as AzurePr; +}