Skip to content

Commit

Permalink
feat: branch status unification (#5658)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Mar 8, 2020
1 parent 51a89a3 commit 588616f
Show file tree
Hide file tree
Showing 30 changed files with 359 additions and 383 deletions.
8 changes: 0 additions & 8 deletions lib/constants/branch-constants.ts

This file was deleted.

16 changes: 6 additions & 10 deletions lib/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import is from '@sindresorhus/is';
import * as _hostRules from '../../util/host-rules';
import { RepoParams, Platform } from '../common';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import { BranchStatus } from '../../types';

describe('platform/azure', () => {
let hostRules: jest.Mocked<typeof _hostRules>;
Expand Down Expand Up @@ -416,12 +412,12 @@ describe('platform/azure', () => {
it('return success if requiredStatusChecks null', async () => {
await initRepo('some-repo');
const res = await azure.getBranchStatus('somebranch', null);
expect(res).toEqual(BRANCH_STATUS_SUCCESS);
expect(res).toEqual(BranchStatus.green);
});
it('return failed if unsupported requiredStatusChecks', async () => {
await initRepo('some-repo');
const res = await azure.getBranchStatus('somebranch', ['foo']);
expect(res).toEqual(BRANCH_STATUS_FAILED);
expect(res).toEqual(BranchStatus.red);
});
it('should pass through success', async () => {
await initRepo({ repository: 'some/repo' });
Expand All @@ -432,7 +428,7 @@ describe('platform/azure', () => {
} as any)
);
const res = await azure.getBranchStatus('somebranch', []);
expect(res).toEqual(BRANCH_STATUS_SUCCESS);
expect(res).toEqual(BranchStatus.green);
});
it('should pass through failed', async () => {
await initRepo({ repository: 'some/repo' });
Expand All @@ -443,7 +439,7 @@ describe('platform/azure', () => {
} as any)
);
const res = await azure.getBranchStatus('somebranch', []);
expect(res).toEqual(BRANCH_STATUS_PENDING);
expect(res).toEqual(BranchStatus.yellow);
});
});

Expand Down Expand Up @@ -856,7 +852,7 @@ describe('platform/azure', () => {
branchName: 'test',
context: 'test',
description: 'test',
state: 'test',
state: BranchStatus.yellow,
url: 'test',
});
expect(res).toBeUndefined();
Expand Down
19 changes: 7 additions & 12 deletions lib/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
FindPRConfig,
EnsureCommentConfig,
EnsureIssueResult,
BranchStatus,
} from '../common';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
Expand All @@ -31,11 +30,7 @@ import {
PR_STATE_NOT_OPEN,
PR_STATE_OPEN,
} from '../../constants/pull-requests';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import { BranchStatus } from '../../types';
import { RenovateConfig } from '../../config';

interface Config {
Expand Down Expand Up @@ -393,7 +388,7 @@ export /* istanbul ignore next */ function getCommitMessages(): Promise<

export async function getBranchStatusCheck(
branchName: string,
context?: string
context: string
): Promise<BranchStatus> {
logger.trace(`getBranchStatusCheck(${branchName}, ${context})`);
const azureApiGit = await azureApi.gitApi();
Expand All @@ -402,9 +397,9 @@ export async function getBranchStatusCheck(
azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName)!
);
if (branch.aheadCount === 0) {
return BRANCH_STATUS_SUCCESS;
return BranchStatus.green;
}
return BRANCH_STATUS_PENDING;
return BranchStatus.yellow;
}

export async function getBranchStatus(
Expand All @@ -414,14 +409,14 @@ export async function getBranchStatus(
logger.debug(`getBranchStatus(${branchName})`);
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
return BRANCH_STATUS_SUCCESS;
return BranchStatus.green;
}
if (requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return BRANCH_STATUS_FAILED;
return BranchStatus.red;
}
const branchStatusCheck = await getBranchStatusCheck(branchName);
const branchStatusCheck = await getBranchStatusCheck(branchName, null);
return branchStatusCheck;
}

Expand Down
37 changes: 16 additions & 21 deletions lib/platform/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import { PR_STATE_CLOSED, PR_STATE_OPEN } from '../../constants/pull-requests';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_FAILURE,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import { BranchStatus } from '../../types';

describe('platform/bitbucket-server', () => {
Object.entries(responses).forEach(([scenarioName, mockResponses]) => {
Expand Down Expand Up @@ -787,11 +782,11 @@ Followed by some information.

await expect(
bitbucket.getBranchStatus('somebranch', [])
).resolves.toEqual(BRANCH_STATUS_SUCCESS);
).resolves.toEqual(BranchStatus.green);

await expect(
bitbucket.getBranchStatus('somebranch')
).resolves.toEqual(BRANCH_STATUS_SUCCESS);
).resolves.toEqual(BranchStatus.green);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand All @@ -809,7 +804,7 @@ Followed by some information.

await expect(
bitbucket.getBranchStatus('somebranch', [])
).resolves.toEqual(BRANCH_STATUS_PENDING);
).resolves.toEqual(BranchStatus.yellow);

api.get.mockReturnValueOnce({
body: {
Expand All @@ -821,7 +816,7 @@ Followed by some information.

await expect(
bitbucket.getBranchStatus('somebranch', [])
).resolves.toEqual(BRANCH_STATUS_PENDING);
).resolves.toEqual(BranchStatus.yellow);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand All @@ -840,15 +835,15 @@ Followed by some information.

await expect(
bitbucket.getBranchStatus('somebranch', [])
).resolves.toEqual(BRANCH_STATUS_FAILED);
).resolves.toEqual(BranchStatus.red);

api.get.mockImplementationOnce(() => {
throw new Error('requst-failed');
});

await expect(
bitbucket.getBranchStatus('somebranch', [])
).resolves.toEqual(BRANCH_STATUS_FAILED);
).resolves.toEqual(BranchStatus.red);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand Down Expand Up @@ -889,7 +884,7 @@ Followed by some information.

await expect(
bitbucket.getBranchStatusCheck('somebranch', 'context-2')
).resolves.toEqual(BRANCH_STATUS_SUCCESS);
).resolves.toEqual(BranchStatus.green);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand All @@ -912,7 +907,7 @@ Followed by some information.

await expect(
bitbucket.getBranchStatusCheck('somebranch', 'context-2')
).resolves.toEqual(BRANCH_STATUS_PENDING);
).resolves.toEqual(BranchStatus.yellow);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand All @@ -935,7 +930,7 @@ Followed by some information.

await expect(
bitbucket.getBranchStatusCheck('somebranch', 'context-2')
).resolves.toEqual(BRANCH_STATUS_FAILURE);
).resolves.toEqual(BranchStatus.red);

expect(api.get.mock.calls).toMatchSnapshot();
});
Expand Down Expand Up @@ -976,28 +971,28 @@ Followed by some information.
branchName: 'somebranch',
context: 'context-2',
description: null as any,
state: BRANCH_STATUS_SUCCESS,
state: BranchStatus.green,
});

await bitbucket.setBranchStatus({
branchName: 'somebranch',
context: 'context-2',
description: null as any,
state: BRANCH_STATUS_FAILED,
state: BranchStatus.red,
});

await bitbucket.setBranchStatus({
branchName: 'somebranch',
context: 'context-2',
description: null as any,
state: BRANCH_STATUS_FAILURE,
state: BranchStatus.red,
});

await bitbucket.setBranchStatus({
branchName: 'somebranch',
context: 'context-2',
description: null as any,
state: BRANCH_STATUS_PENDING,
state: BranchStatus.yellow,
});

api.post.mockImplementationOnce(() => {
Expand All @@ -1008,14 +1003,14 @@ Followed by some information.
branchName: 'somebranch',
context: 'context-2',
description: null as any,
state: BRANCH_STATUS_SUCCESS,
state: BranchStatus.green,
});

await bitbucket.setBranchStatus({
branchName: 'somebranch',
context: 'context-1',
description: null as any,
state: BRANCH_STATUS_SUCCESS,
state: BranchStatus.green,
});

expect(api.get.mock.calls).toMatchSnapshot();
Expand Down
34 changes: 14 additions & 20 deletions lib/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
EnsureCommentConfig,
EnsureIssueResult,
EnsureIssueConfig,
BranchStatus,
} from '../common';
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
Expand All @@ -31,12 +30,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_FAILURE,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import { BranchStatus } from '../../types';
import { RenovateConfig } from '../../config';
/*
* Version: 5.3 (EOL Date: 15 Aug 2019)
Expand Down Expand Up @@ -536,7 +530,7 @@ export async function getBranchStatus(
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
logger.debug('Status checks disabled = returning "success"');
return BRANCH_STATUS_SUCCESS;
return BranchStatus.green;
}

if (!(await branchExists(branchName))) {
Expand All @@ -548,14 +542,14 @@ export async function getBranchStatus(

logger.debug({ commitStatus }, 'branch status check result');

if (commitStatus.failed > 0) return BRANCH_STATUS_FAILED;
if (commitStatus.inProgress > 0) return BRANCH_STATUS_PENDING;
if (commitStatus.failed > 0) return BranchStatus.red;
if (commitStatus.inProgress > 0) return BranchStatus.yellow;
return commitStatus.successful > 0
? BRANCH_STATUS_SUCCESS
: BRANCH_STATUS_PENDING;
? BranchStatus.green
: BranchStatus.yellow;
} catch (err) {
logger.warn({ err }, `Failed to get branch status`);
return BRANCH_STATUS_FAILED;
return BranchStatus.red;
}
}

Expand All @@ -576,7 +570,7 @@ async function getStatusCheck(
export async function getBranchStatusCheck(
branchName: string,
context: string
): Promise<string | null> {
): Promise<BranchStatus | null> {
logger.debug(`getBranchStatusCheck(${branchName}, context=${context})`);

try {
Expand All @@ -586,12 +580,12 @@ export async function getBranchStatusCheck(
if (state.key === context) {
switch (state.state) {
case 'SUCCESSFUL':
return BRANCH_STATUS_SUCCESS;
return BranchStatus.green;
case 'INPROGRESS':
return BRANCH_STATUS_PENDING;
return BranchStatus.yellow;
case 'FAILED':
default:
return BRANCH_STATUS_FAILURE;
return BranchStatus.red;
}
}
}
Expand Down Expand Up @@ -626,13 +620,13 @@ export async function setBranchStatus({
};

switch (state) {
case BRANCH_STATUS_SUCCESS:
case BranchStatus.green:
body.state = 'SUCCESSFUL';
break;
case BRANCH_STATUS_PENDING:
case BranchStatus.yellow:
body.state = 'INPROGRESS';
break;
case BRANCH_STATUS_FAILURE:
case BranchStatus.red:
default:
body.state = 'FAILED';
break;
Expand Down

0 comments on commit 588616f

Please sign in to comment.