Skip to content

Commit

Permalink
feat(constants): Branch Status (#5166)
Browse files Browse the repository at this point in the history
1. Branch related statuses are moved to lib/branch-constants.ts
2. Updated all the references

Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
souravdasslg and rarkins committed Jan 17, 2020
1 parent 6dc76f2 commit 9024eda
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 120 deletions.
8 changes: 8 additions & 0 deletions lib/constants/branch-constants.ts
@@ -0,0 +1,8 @@
// Branch Status
export const BRANCH_STATUS_SUCCESS = 'success';
export const BRANCH_STATUS_FAILURE = 'failure';
export const BRANCH_STATUS_ERROR = 'error';
export const BRANCH_STATUS_PENDING = 'pending';
export const BRANCH_STATUS_CREATED = 'created';
export const BRANCH_STATUS_RUNNING = 'running';
export const BRANCH_STATUS_FAILED = 'failed';
13 changes: 9 additions & 4 deletions lib/platform/azure/index.ts
Expand Up @@ -23,6 +23,11 @@ import {
import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';

interface Config {
storage: GitStorage;
Expand Down Expand Up @@ -381,9 +386,9 @@ export async function getBranchStatusCheck(
azureHelper.getBranchNameWithoutRefsheadsPrefix(branchName)!
);
if (branch.aheadCount === 0) {
return 'success';
return BRANCH_STATUS_SUCCESS;
}
return 'pending';
return BRANCH_STATUS_PENDING;
}

export async function getBranchStatus(
Expand All @@ -393,12 +398,12 @@ export async function getBranchStatus(
logger.debug(`getBranchStatus(${branchName})`);
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
return 'success';
return BRANCH_STATUS_SUCCESS;
}
if (requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
return BRANCH_STATUS_FAILED;
}
const branchStatusCheck = await getBranchStatusCheck(branchName);
return branchStatusCheck;
Expand Down
30 changes: 19 additions & 11 deletions lib/platform/bitbucket-server/index.ts
Expand Up @@ -26,6 +26,12 @@ import {
REPOSITORY_DISABLED,
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_FAILURE,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
/*
* Version: 5.3 (EOL Date: 15 Aug 2019)
* See following docs for api information:
Expand Down Expand Up @@ -526,7 +532,7 @@ export async function getBranchStatus(
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
logger.debug('Status checks disabled = returning "success"');
return 'success';
return BRANCH_STATUS_SUCCESS;
}

if (!(await branchExists(branchName))) {
Expand All @@ -538,12 +544,14 @@ export async function getBranchStatus(

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

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

Expand Down Expand Up @@ -574,12 +582,12 @@ export async function getBranchStatusCheck(
if (state.key === context) {
switch (state.state) {
case 'SUCCESSFUL':
return 'success';
return BRANCH_STATUS_SUCCESS;
case 'INPROGRESS':
return 'pending';
return BRANCH_STATUS_PENDING;
case 'FAILED':
default:
return 'failure';
return BRANCH_STATUS_FAILURE;
}
}
}
Expand Down Expand Up @@ -614,13 +622,13 @@ export async function setBranchStatus({
};

switch (state) {
case 'success':
case BRANCH_STATUS_SUCCESS:
body.state = 'SUCCESSFUL';
break;
case 'pending':
case BRANCH_STATUS_PENDING:
body.state = 'INPROGRESS';
break;
case 'failure':
case BRANCH_STATUS_FAILURE:
default:
body.state = 'FAILED';
break;
Expand Down
17 changes: 11 additions & 6 deletions lib/platform/bitbucket/index.ts
Expand Up @@ -26,6 +26,11 @@ import {
REPOSITORY_DISABLED,
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';

let config: utils.Config = {} as any;

Expand Down Expand Up @@ -406,32 +411,32 @@ export async function getBranchStatus(
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
logger.debug('Status checks disabled = returning "success"');
return 'success';
return BRANCH_STATUS_SUCCESS;
}
if (requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
return BRANCH_STATUS_FAILED;
}
const statuses = await getStatus(branchName);
logger.debug({ branch: branchName, statuses }, 'branch status check result');
if (!statuses.length) {
logger.debug('empty branch status check result = returning "pending"');
return 'pending';
return BRANCH_STATUS_PENDING;
}
const noOfFailures = statuses.filter(
(status: { state: string }) => status.state === 'FAILED'
).length;
if (noOfFailures) {
return 'failed';
return BRANCH_STATUS_FAILED;
}
const noOfPending = statuses.filter(
(status: { state: string }) => status.state === 'INPROGRESS'
).length;
if (noOfPending) {
return 'pending';
return BRANCH_STATUS_PENDING;
}
return 'success';
return BRANCH_STATUS_SUCCESS;
}

export async function getBranchStatusCheck(
Expand Down
17 changes: 11 additions & 6 deletions lib/platform/github/index.ts
Expand Up @@ -38,6 +38,11 @@ import {
REPOSITORY_NOT_FOUND,
REPOSITORY_RENAMED,
} from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';

const defaultConfigFile = configFileNames[0];

Expand Down Expand Up @@ -1057,12 +1062,12 @@ export async function getBranchStatus(
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
logger.debug('Status checks disabled = returning "success"');
return 'success';
return BRANCH_STATUS_SUCCESS;
}
if (requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
return BRANCH_STATUS_FAILED;
}
let commitStatus;
try {
Expand Down Expand Up @@ -1127,15 +1132,15 @@ export async function getBranchStatus(
commitStatus.state === 'failed' ||
checkRuns.some(run => run.conclusion === 'failed')
) {
return 'failed';
return BRANCH_STATUS_FAILED;
}
if (
(commitStatus.state === 'success' || commitStatus.statuses.length === 0) &&
checkRuns.every(run => ['neutral', 'success'].includes(run.conclusion))
) {
return 'success';
return BRANCH_STATUS_SUCCESS;
}
return 'pending';
return BRANCH_STATUS_PENDING;
}

async function getStatusCheck(
Expand Down Expand Up @@ -1658,7 +1663,7 @@ export async function createPr({
branchName,
context: `renovate/verify`,
description: `Renovate verified pull request`,
state: 'success',
state: BRANCH_STATUS_SUCCESS,
url: 'https://github.com/renovatebot/renovate',
});
}
Expand Down
18 changes: 12 additions & 6 deletions lib/platform/gitlab/index.ts
Expand Up @@ -33,6 +33,12 @@ import {
REPOSITORY_MIRRORED,
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_FAILURE,
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';

const defaultConfigFile = configFileNames[0];
let config: {
Expand Down Expand Up @@ -305,12 +311,12 @@ export async function getBranchStatus(
logger.debug(`getBranchStatus(${branchName})`);
if (!requiredStatusChecks) {
// null means disable status checks, so it always succeeds
return 'success';
return BRANCH_STATUS_SUCCESS;
}
if (Array.isArray(requiredStatusChecks) && requiredStatusChecks.length) {
// This is Unsupported
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
return BRANCH_STATUS_FAILED;
}

if (!(await branchExists(branchName))) {
Expand All @@ -321,16 +327,16 @@ export async function getBranchStatus(
logger.debug(`Got res with ${res.length} results`);
if (res.length === 0) {
// Return 'pending' if we have no status checks
return 'pending';
return BRANCH_STATUS_PENDING;
}
let status = 'success';
let status = BRANCH_STATUS_SUCCESS;
// Return 'success' if all are success
res.forEach(check => {
// If one is failed then don't overwrite that
if (status !== 'failure') {
if (!check.allow_failure) {
if (check.status === 'failed') {
status = 'failure';
status = BRANCH_STATUS_FAILURE;
} else if (check.status !== 'success') {
({ status } = check);
}
Expand Down Expand Up @@ -412,7 +418,7 @@ export async function getPr(iid: number): Promise<Pr> {
pr.isConflicted = true;
} else if (pr.state === 'open') {
const branchStatus = await getBranchStatus(pr.branchName, []);
if (branchStatus === 'success') {
if (branchStatus === BRANCH_STATUS_SUCCESS) {
pr.canMerge = true;
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/workers/branch/automerge.ts
@@ -1,6 +1,11 @@
import { logger } from '../../logger';
import { RenovateConfig } from '../../config';
import { platform } from '../../platform';
import {
BRANCH_STATUS_ERROR,
BRANCH_STATUS_FAILURE,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';

export type AutomergeResult =
| 'automerged'
Expand All @@ -25,7 +30,7 @@ export async function tryBranchAutomerge(
config.branchName,
config.requiredStatusChecks
);
if (branchStatus === 'success') {
if (branchStatus === BRANCH_STATUS_SUCCESS) {
logger.debug(`Automerging branch`);
try {
if (config.dryRun)
Expand All @@ -42,7 +47,9 @@ export async function tryBranchAutomerge(
logger.info({ err }, `Failed to automerge branch`);
return 'failed';
}
} else if (['failure', 'error'].includes(branchStatus)) {
} else if (
[BRANCH_STATUS_FAILURE, BRANCH_STATUS_ERROR].includes(branchStatus)
) {
return 'branch status error';
} else {
logger.debug(`Branch status is "${branchStatus}" - skipping automerge`);
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/branch/index.ts
Expand Up @@ -29,6 +29,7 @@ import {
DATASOURCE_FAILURE,
PLATFORM_FAILURE,
} from '../../constants/error-messages';
import { BRANCH_STATUS_FAILURE } from '../../constants/branch-constants';

export type ProcessBranchResult =
| 'already-existed'
Expand Down Expand Up @@ -518,7 +519,7 @@ export async function processBranch(
}
const context = `renovate/artifacts`;
const description = 'Artifact file update failure';
const state = 'failure';
const state = BRANCH_STATUS_FAILURE;
const existingState = await platform.getBranchStatusCheck(
config.branchName,
context
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/pr/body/config-description.ts
@@ -1,6 +1,7 @@
import { platform } from '../../../platform';
import { emojify } from '../../../util/emoji';
import { PrBodyConfig } from './common';
import { BRANCH_STATUS_FAILED } from '../../../constants/branch-constants';
import { PLATFORM_TYPE_GITHUB } from '../../../constants/platfroms';

export async function getPrConfigDescription(
Expand Down Expand Up @@ -31,7 +32,7 @@ export async function getPrConfigDescription(
config.requiredStatusChecks
);
// istanbul ignore if
if (branchStatus === 'failed') {
if (branchStatus === BRANCH_STATUS_FAILED) {
prBody += 'Disabled due to failing status checks.';
} else {
prBody += 'Enabled.';
Expand Down

0 comments on commit 9024eda

Please sign in to comment.