Skip to content

Commit

Permalink
feat(azure): support different merge strategies for autocomplete (#4584)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored and rarkins committed Oct 18, 2019
1 parent 661cb95 commit 2784016
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
6 changes: 5 additions & 1 deletion lib/platform/azure/azure-got-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ export function gitApi() {
return azureObj().getGitApi();
}

export function getCoreApi() {
export function coreApi() {
return azureObj().getCoreApi();
}

export function policyApi() {
return azureObj().getPolicyApi();
}

export function setEndpoint(e: string) {
endpoint = e;
}
63 changes: 26 additions & 37 deletions lib/platform/azure/azure-helper.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { GitPullRequestMergeStrategy } from 'azure-devops-node-api/interfaces/GitInterfaces';

import * as azureApi from './azure-got-wrapper';
import { logger } from '../../logger';

/**
*
* @param {string} branchName
*/
const mergePolicyGuid = 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab'; // Magic GUID for merge strategy policy configurations

export function getNewBranchName(branchName?: string) {
if (branchName && !branchName.startsWith('refs/heads/')) {
return `refs/heads/${branchName}`;
}
return branchName;
}

/**
*
* @param {string} branchPath
*/
export function getBranchNameWithoutRefsheadsPrefix(branchPath: string) {
if (!branchPath) {
logger.error(`getBranchNameWithoutRefsheadsPrefix(${branchPath})`);
Expand All @@ -30,10 +26,6 @@ export function getBranchNameWithoutRefsheadsPrefix(branchPath: string) {
return branchPath.substring(11, branchPath.length);
}

/**
*
* @param {string} branchPath
*/
function getBranchNameWithoutRefsPrefix(branchPath?: string) {
if (!branchPath) {
logger.error(`getBranchNameWithoutRefsPrefix(${branchPath})`);
Expand All @@ -48,11 +40,6 @@ function getBranchNameWithoutRefsPrefix(branchPath?: string) {
return branchPath.substring(5, branchPath.length);
}

/**
*
* @param {string} repoId
* @param {string} branchName
*/
export async function getRefs(repoId: string, branchName?: string) {
logger.debug(`getRefs(${repoId}, ${branchName})`);
const azureApiGit = await azureApi.gitApi();
Expand All @@ -64,12 +51,6 @@ export async function getRefs(repoId: string, branchName?: string) {
return refs;
}

/**
*
* @param repoId
* @param branchName
* @param from
*/
export async function getAzureBranchObj(
repoId: string,
branchName: string,
Expand Down Expand Up @@ -119,12 +100,7 @@ export async function getChanges(
return changes;
}

/**
* if no branchName, look globaly
* @param {string} repoId
* @param {string} filePath
* @param {string} branchName
*/
// if no branchName, look globaly
export async function getFile(
repoId: string,
filePath: string,
Expand Down Expand Up @@ -182,10 +158,6 @@ async function streamToString(stream: NodeJS.ReadableStream) {
return p;
}

/**
*
* @param {string} str
*/
export function max4000Chars(str: string) {
if (str && str.length >= 4000) {
return str.substring(0, 3999);
Expand Down Expand Up @@ -248,10 +220,6 @@ export async function getCommitDetails(commit: string, repoId: string) {
return results;
}

/**
*
* @param {string} str
*/
export function getProjectAndRepo(str: string) {
logger.trace(`getProjectAndRepo(${str})`);
const strSplited = str.split(`/`);
Expand All @@ -271,3 +239,24 @@ export function getProjectAndRepo(str: string) {
logger.error(msg);
throw new Error(msg);
}

export async function getMergeMethod(
repoId: string,
project: string
): Promise<GitPullRequestMergeStrategy> {
const policyConfigurations = (await (await azureApi.policyApi()).getPolicyConfigurations(
project
))
.filter(
p =>
p.settings.scope.some(s => s.repositoryId === repoId) &&
p.type.id === mergePolicyGuid
)
.map(p => p.settings)[0];

return (
Object.keys(policyConfigurations)
.map(p => GitPullRequestMergeStrategy[p.slice(5)])
.find(p => p) || GitPullRequestMergeStrategy.NoFastForward
);
}
10 changes: 6 additions & 4 deletions lib/platform/azure/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GitPullRequestMergeStrategy } from 'azure-devops-node-api/interfaces/GitInterfaces';

import * as azureHelper from './azure-helper';
import * as azureApi from './azure-got-wrapper';
import * as hostRules from '../../util/host-rules';
Expand All @@ -11,7 +13,7 @@ import { smartTruncate } from '../utils/pr-body';
interface Config {
storage: GitStorage;
repoForceRebase: boolean;
mergeMethod: string;
mergeMethod: GitPullRequestMergeStrategy;
baseCommitSHA: string | undefined;
baseBranch: string;
defaultBranch: string;
Expand Down Expand Up @@ -86,7 +88,7 @@ export async function initRepo({
config.baseBranch = config.defaultBranch;
logger.debug(`${repository} default branch = ${config.defaultBranch}`);
config.baseCommitSHA = await getBranchCommit(config.baseBranch);
config.mergeMethod = 'merge';
config.mergeMethod = await azureHelper.getMergeMethod(repo.id, names.project);
config.repoForceRebase = false;

if (optimizeForDisabled) {
Expand Down Expand Up @@ -379,7 +381,7 @@ export async function createPr(
id: pr.createdBy!.id,
},
completionOptions: {
squashMerge: true,
mergeStrategy: config.mergeMethod,
deleteSourceBranch: true,
},
},
Expand Down Expand Up @@ -535,7 +537,7 @@ export async function addAssignees(issueNo: number, assignees: string[]) {
export async function addReviewers(prNo: number, reviewers: string[]) {
logger.trace(`addReviewers(${prNo}, ${reviewers})`);
const azureApiGit = await azureApi.gitApi();
const azureApiCore = await azureApi.getCoreApi();
const azureApiCore = await azureApi.coreApi();
const repos = await azureApiGit.getRepositories();
const repo = repos.filter(c => c.id === config.repoId)[0];
const teams = await azureApiCore.getTeams(repo!.project!.id!);
Expand Down
3 changes: 2 additions & 1 deletion test/platform/azure/azure-got-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('platform/azure/azure-got-wrapper', () => {
describe('gitApi', () => {
it('should throw an error if no token is provided', () => {
expect(azure.gitApi).toThrow('No token found for azure');
expect(azure.getCoreApi).toThrow('No token found for azure');
expect(azure.coreApi).toThrow('No token found for azure');
expect(azure.policyApi).toThrow('No token found for azure');
});
it('should set token and endpoint', async () => {
hostRules.add({
Expand Down
53 changes: 53 additions & 0 deletions test/platform/azure/azure-helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Readable } from 'stream';
import { GitPullRequestMergeStrategy } from 'azure-devops-node-api/interfaces/GitInterfaces';

describe('platform/azure/helpers', () => {
let azureHelper: typeof import('../../../lib/platform/azure/azure-helper');
Expand Down Expand Up @@ -342,4 +343,56 @@ describe('platform/azure/helpers', () => {
);
});
});

describe('getMergeMethod', () => {
it('should default to NoFastForward', async () => {
azureApi.policyApi.mockImplementationOnce(
() =>
({
getPolicyConfigurations: jest.fn(() => [
{
settings: {
scope: [
{
repositoryId: '',
},
],
},
type: {
id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab',
},
},
]),
} as any)
);
expect(await azureHelper.getMergeMethod('', '')).toEqual(
GitPullRequestMergeStrategy.NoFastForward
);
});
it('should return Squash', async () => {
azureApi.policyApi.mockImplementationOnce(
() =>
({
getPolicyConfigurations: jest.fn(() => [
{
settings: {
allowSquash: true,
scope: [
{
repositoryId: '',
},
],
},
type: {
id: 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab',
},
},
]),
} as any)
);
expect(await azureHelper.getMergeMethod('', '')).toEqual(
GitPullRequestMergeStrategy.Squash
);
});
});
});
2 changes: 1 addition & 1 deletion test/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ describe('platform/azure', () => {
createPullRequestReviewer: jest.fn(),
} as any)
);
azureApi.getCoreApi.mockImplementation(
azureApi.coreApi.mockImplementation(
() =>
({
getTeams: jest.fn(() => [
Expand Down

0 comments on commit 2784016

Please sign in to comment.