Skip to content

Commit

Permalink
fix(azure): add default branch policy support (#12806)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
Kevenvz and viceice committed Nov 24, 2021
1 parent 2ec5c54 commit f6454ba
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
78 changes: 72 additions & 6 deletions lib/platform/azure/azure-helper.spec.ts
Expand Up @@ -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(
() =>
({
Expand Down Expand Up @@ -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,
Expand All @@ -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(
() =>
({
Expand All @@ -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,
Expand All @@ -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);
});
});
});
11 changes: 9 additions & 2 deletions lib/platform/azure/azure-helper.ts
Expand Up @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/platform/azure/index.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit f6454ba

Please sign in to comment.