Skip to content

Commit

Permalink
feat(constants): Pull Request Status Constants (#5158)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Mar 5, 2020
1 parent 804a8c0 commit 23a820e
Show file tree
Hide file tree
Showing 28 changed files with 162 additions and 79 deletions.
5 changes: 5 additions & 0 deletions lib/constants/pull-requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PR_STATE_MERGED = 'merged';
export const PR_STATE_OPEN = 'open';
export const PR_STATE_CLOSED = 'closed';
export const PR_STATE_ALL = 'all';
export const PR_STATE_NOT_OPEN = '!open';
11 changes: 8 additions & 3 deletions lib/platform/azure/azure-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
import * as azureApi from './azure-got-wrapper';
import { logger } from '../../logger';
import { Pr } from '../common';
import {
PR_STATE_CLOSED,
PR_STATE_MERGED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';

const mergePolicyGuid = 'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab'; // Magic GUID for merge strategy policy configurations

Expand Down Expand Up @@ -173,11 +178,11 @@ export function getRenovatePRFormat(azurePr: GitPullRequest): Pr {
// All = 4,
// }
if (azurePr.status === 2) {
pr.state = 'closed';
pr.state = PR_STATE_CLOSED;
} else if (azurePr.status === 3) {
pr.state = 'merged';
pr.state = PR_STATE_MERGED;
} else {
pr.state = 'open';
pr.state = PR_STATE_OPEN;
}

// mergeStatus
Expand Down
19 changes: 13 additions & 6 deletions lib/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { sanitize } from '../../util/sanitize';
import { smartTruncate } from '../utils/pr-body';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import { PLATFORM_TYPE_AZURE } from '../../constants/platforms';
import {
PR_STATE_ALL,
PR_STATE_NOT_OPEN,
PR_STATE_OPEN,
} from '../../constants/pull-requests';
import {
BRANCH_STATUS_FAILED,
BRANCH_STATUS_PENDING,
Expand Down Expand Up @@ -291,11 +296,10 @@ export async function getPr(pullRequestId: number): Promise<Pr | null> {

return azurePr;
}

export async function findPr({
branchName,
prTitle,
state = 'all',
state = PR_STATE_ALL,
}: FindPRConfig): Promise<Pr | null> {
let prsFiltered: Pr[] = [];
try {
Expand All @@ -310,11 +314,11 @@ export async function findPr({
}

switch (state) {
case 'all':
case PR_STATE_ALL:
// no more filter needed, we can go further...
break;
case '!open':
prsFiltered = prsFiltered.filter(item => item.state !== 'open');
case PR_STATE_NOT_OPEN:
prsFiltered = prsFiltered.filter(item => item.state !== PR_STATE_OPEN);
break;
default:
prsFiltered = prsFiltered.filter(item => item.state === state);
Expand All @@ -331,7 +335,10 @@ export async function findPr({

export async function getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({ branchName, state: 'open' });
const existingPr = await findPr({
branchName,
state: PR_STATE_OPEN,
});
return existingPr ? getPr(existingPr.pullRequestId) : null;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
REPOSITORY_DISABLED,
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,
Expand Down Expand Up @@ -307,7 +308,7 @@ export async function getPr(

pr.version = updatePrVersion(pr.number, pr.version);

if (pr.state === 'open') {
if (pr.state === PR_STATE_OPEN) {
const mergeRes = await api.get(
`./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/pull-requests/${prNo}/merge`,
{ useCache: !refreshCache }
Expand Down Expand Up @@ -352,7 +353,7 @@ export async function getPr(
// TODO: coverage
// istanbul ignore next
function matchesState(state: string, desiredState: string): boolean {
if (desiredState === 'all') {
if (desiredState === PR_STATE_ALL) {
return true;
}
if (desiredState.startsWith('!')) {
Expand Down Expand Up @@ -400,7 +401,7 @@ export async function getPrList(_args?: any): Promise<Pr[]> {
export async function findPr({
branchName,
prTitle,
state = 'all',
state = PR_STATE_ALL,
refreshCache,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, "${prTitle}", "${state}")`);
Expand All @@ -422,7 +423,7 @@ export async function getBranchPr(
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({
branchName,
state: 'open',
state: PR_STATE_OPEN,
});
return existingPr ? getPr(existingPr.number, refreshCache) : null;
}
Expand Down
11 changes: 8 additions & 3 deletions lib/platform/bitbucket-server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
import url from 'url';
import { api } from './bb-got-wrapper';
import { Pr } from '../common';
import {
PR_STATE_CLOSED,
PR_STATE_MERGED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';

// https://docs.atlassian.com/bitbucket-server/rest/6.0.0/bitbucket-rest.html#idp250
const prStateMapping: any = {
MERGED: 'merged',
DECLINED: 'closed',
OPEN: 'open',
MERGED: PR_STATE_MERGED,
DECLINED: PR_STATE_CLOSED,
OPEN: PR_STATE_OPEN,
};

export function prInfo(pr: any): Pr {
Expand Down
12 changes: 8 additions & 4 deletions lib/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
REPOSITORY_DISABLED,
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests';
import { PLATFORM_TYPE_BITBUCKET } from '../../constants/platforms';
import {
BRANCH_STATUS_FAILED,
Expand Down Expand Up @@ -210,7 +211,7 @@ export function getFile(

// istanbul ignore next
function matchesState(state: string, desiredState: string): boolean {
if (desiredState === 'all') {
if (desiredState === PR_STATE_ALL) {
return true;
}
if (desiredState.startsWith('!')) {
Expand All @@ -235,7 +236,7 @@ export async function getPrList(): Promise<Pr[]> {
export async function findPr({
branchName,
prTitle,
state = 'all',
state = PR_STATE_ALL,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);
const prList = await getPrList();
Expand All @@ -256,7 +257,7 @@ export async function deleteBranch(
closePr?: boolean
): Promise<void> {
if (closePr) {
const pr = await findPr({ branchName, state: 'open' });
const pr = await findPr({ branchName, state: PR_STATE_OPEN });
if (pr) {
await api.post(
`/2.0/repositories/${config.repository}/pullrequests/${pr.number}/decline`
Expand Down Expand Up @@ -387,7 +388,10 @@ async function getBranchCommit(branchName: string): Promise<string | null> {
// Returns the Pull Request for a branch. Null if not exists.
export async function getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({ branchName, state: 'open' });
const existingPr = await findPr({
branchName,
state: PR_STATE_OPEN,
});
return existingPr ? getPr(existingPr.number) : null;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/platform/bitbucket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import url from 'url';
import { api } from './bb-got-wrapper';
import { Storage } from '../git/storage';
import { GotResponse, Pr } from '../common';
import { PR_STATE_CLOSED } from '../../constants/pull-requests';

export interface Config {
baseBranch: string;
Expand Down Expand Up @@ -121,7 +122,7 @@ export function prInfo(pr: any): Pr {
targetBranch: pr.destination.branch.name,
title: pr.title,
state: prStates.closed.includes(pr.state)
? /* istanbul ignore next */ 'closed'
? /* istanbul ignore next */ PR_STATE_CLOSED
: pr.state.toLowerCase(),
createdAt: pr.created_on,
};
Expand Down
3 changes: 2 additions & 1 deletion lib/platform/gitea/gitea-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { URLSearchParams } from 'url';
import { api, GiteaGotOptions } from './gitea-got-wrapper';
import { GotResponse } from '../common';
import { PR_STATE_CLOSED } from '../../constants/pull-requests';

export type PRState = 'open' | 'closed' | 'all';
export type IssueState = 'open' | 'closed' | 'all';
Expand Down Expand Up @@ -287,7 +288,7 @@ export async function closePR(
): Promise<void> {
await updatePR(repoPath, idx, {
...options,
state: 'closed',
state: PR_STATE_CLOSED,
});
}

Expand Down
9 changes: 5 additions & 4 deletions lib/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import * as helper from './gitea-helper';
import { PR_STATE_ALL, PR_STATE_OPEN } from '../../constants/pull-requests';

type GiteaRenovateConfig = {
endpoint: string;
Expand Down Expand Up @@ -112,7 +113,7 @@ function toRenovatePR(data: helper.PR): Pr | null {
}

function matchesState(actual: string, expected: string): boolean {
if (expected === 'all') {
if (expected === PR_STATE_ALL) {
return true;
}
if (expected.startsWith('!')) {
Expand Down Expand Up @@ -484,7 +485,7 @@ const platform: Platform = {
async findPr({
branchName,
prTitle: title,
state = 'all',
state = PR_STATE_ALL,
}: FindPRConfig): Promise<Pr> {
logger.debug(`findPr(${branchName}, ${title}, ${state})`);
const prList = await platform.getPrList();
Expand Down Expand Up @@ -549,7 +550,7 @@ const platform: Platform = {
config.prList = null;
const pr = await platform.findPr({
branchName,
state: 'open',
state: PR_STATE_OPEN,
});

// If a valid PR was found, return and gracefully recover from the error. Otherwise, abort and throw error.
Expand Down Expand Up @@ -810,7 +811,7 @@ const platform: Platform = {

async getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const pr = await platform.findPr({ branchName, state: 'open' });
const pr = await platform.findPr({ branchName, state: PR_STATE_OPEN });
return pr ? platform.getPr(pr.number) : null;
},

Expand Down
20 changes: 14 additions & 6 deletions lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ import {
BRANCH_STATUS_PENDING,
BRANCH_STATUS_SUCCESS,
} from '../../constants/branch-constants';
import {
PR_STATE_ALL,
PR_STATE_CLOSED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';

const defaultConfigFile = configFileNames[0];

Expand Down Expand Up @@ -759,7 +764,7 @@ async function getOpenPrs(): Promise<PrList> {
for (const pr of res.data.repository.pullRequests.nodes) {
// https://developer.github.com/v4/object/pullrequest/
pr.displayNumber = `Pull Request #${pr.number}`;
pr.state = 'open';
pr.state = PR_STATE_OPEN;
pr.branchName = pr.headRefName;
const branchName = pr.branchName;
const prNo = pr.number;
Expand Down Expand Up @@ -882,7 +887,7 @@ export async function getPr(prNo: number): Promise<Pr | null> {
}
// Harmonise PR values
pr.displayNumber = `Pull Request #${pr.number}`;
if (pr.state === 'open') {
if (pr.state === PR_STATE_OPEN) {
pr.isModified = true;
pr.branchName = pr.head ? pr.head.ref : undefined;
pr.sha = pr.head ? pr.head.sha : undefined;
Expand Down Expand Up @@ -975,7 +980,7 @@ export async function getPr(prNo: number): Promise<Pr | null> {
}

function matchesState(state: string, desiredState: string): boolean {
if (desiredState === 'all') {
if (desiredState === PR_STATE_ALL) {
return true;
}
if (desiredState.startsWith('!')) {
Expand Down Expand Up @@ -1014,7 +1019,7 @@ export async function getPrList(): Promise<Pr[]> {
sha: pr.head.sha,
title: pr.title,
state:
pr.state === 'closed' && pr.merged_at && pr.merged_at.length
pr.state === PR_STATE_CLOSED && pr.merged_at && pr.merged_at.length
? /* istanbul ignore next */ 'merged'
: pr.state,
createdAt: pr.created_at,
Expand All @@ -1031,7 +1036,7 @@ export async function getPrList(): Promise<Pr[]> {
export async function findPr({
branchName,
prTitle,
state = 'all',
state = PR_STATE_ALL,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);
const prList = await getPrList();
Expand All @@ -1051,7 +1056,10 @@ export async function findPr({
// Returns the Pull Request for a branch. Null if not exists.
export async function getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({ branchName, state: 'open' });
const existingPr = await findPr({
branchName,
state: PR_STATE_OPEN,
});
return existingPr ? getPr(existingPr.number) : null;
}

Expand Down

0 comments on commit 23a820e

Please sign in to comment.