From f6454ba1a77f995cd6e60ec9025718e8eeba863e Mon Sep 17 00:00:00 2001 From: Keven van Zuijlen <kevenvz1@gmail.com> Date: Wed, 24 Nov 2021 12:33:22 +0100 Subject: [PATCH] fix(azure): add default branch policy support (#12806) Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- lib/platform/azure/azure-helper.spec.ts | 78 +++++++++++++++++++++++-- lib/platform/azure/azure-helper.ts | 11 +++- lib/platform/azure/index.ts | 7 ++- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/lib/platform/azure/azure-helper.spec.ts b/lib/platform/azure/azure-helper.spec.ts index 18d3d23e23..e4e2776a77 100644 --- a/lib/platform/azure/azure-helper.spec.ts +++ b/lib/platform/azure/azure-helper.spec.ts @@ -232,8 +232,47 @@ describe('platform/azure/azure-helper', () => { GitPullRequestMergeStrategy.Squash ); }); + it('should return default branch policy', async () => { + azureApi.policyApi.mockImplementationOnce( + () => + ({ + getPolicyConfigurations: jest.fn(() => [ + { + settings: { + allowSquash: true, + scope: [ + { + repositoryId: 'doo-dee-doo-repository-id', + }, + ], + }, + type: { + id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', + }, + }, + { + settings: { + allowRebase: true, + scope: [ + { + matchKind: 'DefaultBranch', + }, + ], + }, + type: { + id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', + }, + }, + ]), + } as any) + ); + expect(await azureHelper.getMergeMethod('', '')).toEqual( + GitPullRequestMergeStrategy.Rebase + ); + }); it('should return most specific exact branch policy', async () => { const refMock = 'refs/heads/ding'; + const defaultBranchMock = 'dong'; azureApi.policyApi.mockImplementationOnce( () => ({ @@ -264,6 +303,19 @@ describe('platform/azure/azure-helper', () => { id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', }, }, + { + settings: { + allowSquash: true, + scope: [ + { + matchKind: 'DefaultBranch', + }, + ], + }, + type: { + id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', + }, + }, { settings: { allowRebase: true, @@ -282,12 +334,13 @@ describe('platform/azure/azure-helper', () => { ]), } as any) ); - expect(await azureHelper.getMergeMethod('', '', refMock)).toEqual( - GitPullRequestMergeStrategy.Rebase - ); + expect( + await azureHelper.getMergeMethod('', '', refMock, defaultBranchMock) + ).toEqual(GitPullRequestMergeStrategy.Rebase); }); it('should return most specific prefix branch policy', async () => { const refMock = 'refs/heads/ding-wow'; + const defaultBranchMock = 'dong-wow'; azureApi.policyApi.mockImplementationOnce( () => ({ @@ -305,6 +358,19 @@ describe('platform/azure/azure-helper', () => { id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', }, }, + { + settings: { + allowSquash: true, + scope: [ + { + matchKind: 'DefaultBranch', + }, + ], + }, + type: { + id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', + }, + }, { settings: { allowRebase: true, @@ -323,9 +389,9 @@ describe('platform/azure/azure-helper', () => { ]), } as any) ); - expect(await azureHelper.getMergeMethod('', '', refMock)).toEqual( - GitPullRequestMergeStrategy.Rebase - ); + expect( + await azureHelper.getMergeMethod('', '', refMock, defaultBranchMock) + ).toEqual(GitPullRequestMergeStrategy.Rebase); }); }); }); diff --git a/lib/platform/azure/azure-helper.ts b/lib/platform/azure/azure-helper.ts index ce6b6089f6..43540132a8 100644 --- a/lib/platform/azure/azure-helper.ts +++ b/lib/platform/azure/azure-helper.ts @@ -110,14 +110,21 @@ export async function getCommitDetails( export async function getMergeMethod( repoId: string, project: string, - branchRef?: string + branchRef?: string, + defaultBranch?: string ): Promise<GitPullRequestMergeStrategy> { type Scope = { repositoryId: string; refName?: string; - matchKind: 'Prefix' | 'Exact'; + matchKind: 'Prefix' | 'Exact' | 'DefaultBranch'; }; const isRelevantScope = (scope: Scope): boolean => { + if ( + scope.matchKind === 'DefaultBranch' && + (!branchRef || branchRef === `refs/heads/${defaultBranch}`) + ) { + return true; + } if (scope.repositoryId !== repoId) { return false; } diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index cccec641fd..1b340395f2 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -169,7 +169,9 @@ export async function initRepo({ const names = getProjectAndRepo(repository); config.defaultMergeMethod = await azureHelper.getMergeMethod( repo.id, - names.project + names.project, + null, + defaultBranch ); config.mergeMethods = {}; config.repoForceRebase = false; @@ -618,7 +620,8 @@ export async function mergePr({ (config.mergeMethods[pr.targetRefName] = await azureHelper.getMergeMethod( config.repoId, config.project, - pr.targetRefName + pr.targetRefName, + config.defaultBranch )); const objToUpdate: GitPullRequest = { -- GitLab