Skip to content

Commit

Permalink
refactor: replace enums with union/object (#18903)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Nov 14, 2022
1 parent e4e34ff commit af3b203
Show file tree
Hide file tree
Showing 51 changed files with 288 additions and 335 deletions.
12 changes: 6 additions & 6 deletions lib/modules/platform/azure/index.spec.ts
Expand Up @@ -12,7 +12,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../../constants/error-messages';
import type { logger as _logger } from '../../../logger';
import { BranchStatus, PrState } from '../../../types';
import { BranchStatus } from '../../../types';
import type * as _git from '../../../util/git';
import type * as _hostRules from '../../../util/host-rules';
import type { Platform, RepoParams } from '../types';
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('modules/platform/azure/index', () => {
const res = await azure.findPr({
branchName: 'branch-a',
prTitle: 'branch a pr',
state: PrState.Open,
state: 'open',
});
expect(res).toMatchSnapshot();
});
Expand All @@ -259,7 +259,7 @@ describe('modules/platform/azure/index', () => {
const res = await azure.findPr({
branchName: 'branch-a',
prTitle: 'branch a pr',
state: PrState.NotOpen,
state: '!open',
});
expect(res).toMatchSnapshot();
});
Expand All @@ -286,7 +286,7 @@ describe('modules/platform/azure/index', () => {
const res = await azure.findPr({
branchName: 'branch-a',
prTitle: 'branch a pr',
state: PrState.Closed,
state: 'closed',
});
expect(res).toMatchSnapshot();
});
Expand Down Expand Up @@ -843,7 +843,7 @@ describe('modules/platform/azure/index', () => {
number: 1234,
prTitle: 'The New Title',
prBody: 'Hello world again',
state: PrState.Closed,
state: 'closed',
});
expect(updatePullRequest.mock.calls).toMatchSnapshot();
});
Expand All @@ -861,7 +861,7 @@ describe('modules/platform/azure/index', () => {
number: 1234,
prTitle: 'The New Title',
prBody: 'Hello world again',
state: PrState.Open,
state: 'open',
});
expect(updatePullRequest.mock.calls).toMatchSnapshot();
});
Expand Down
16 changes: 8 additions & 8 deletions lib/modules/platform/azure/index.ts
Expand Up @@ -16,7 +16,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../../types';
import { BranchStatus, VulnerabilityAlert } from '../../../types';
import * as git from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { regEx } from '../../../util/regex';
Expand Down Expand Up @@ -295,7 +295,7 @@ export async function getPr(pullRequestId: number): Promise<Pr | null> {
export async function findPr({
branchName,
prTitle,
state = PrState.All,
state = 'all',
}: FindPRConfig): Promise<Pr | null> {
let prsFiltered: Pr[] = [];
try {
Expand All @@ -310,11 +310,11 @@ export async function findPr({
}

switch (state) {
case PrState.All:
case 'all':
// no more filter needed, we can go further...
break;
case PrState.NotOpen:
prsFiltered = prsFiltered.filter((item) => item.state !== PrState.Open);
case '!open':
prsFiltered = prsFiltered.filter((item) => item.state !== 'open');
break;
default:
prsFiltered = prsFiltered.filter((item) => item.state === state);
Expand All @@ -333,7 +333,7 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({
branchName,
state: PrState.Open,
state: 'open',
});
return existingPr ? getPr(existingPr.number) : null;
}
Expand Down Expand Up @@ -503,13 +503,13 @@ export async function updatePr({
objToUpdate.description = max4000Chars(sanitize(body));
}

if (state === PrState.Open) {
if (state === 'open') {
await azureApiGit.updatePullRequest(
{ status: PullRequestStatus.Active },
config.repoId,
prNo
);
} else if (state === PrState.Closed) {
} else if (state === 'closed') {
objToUpdate.status = PullRequestStatus.Abandoned;
}

Expand Down
15 changes: 7 additions & 8 deletions lib/modules/platform/azure/types.ts
Expand Up @@ -4,11 +4,10 @@ export interface AzurePr extends Pr {
sourceRefName?: string;
}

// eslint-disable-next-line typescript-enum/no-enum
export enum AzurePrVote {
NoVote = 0,
Reject = -10,
WaitingForAuthor = -5,
ApprovedWithSuggestions = 5,
Approved = 10,
}
export const AzurePrVote = {
NoVote: 0,
Reject: -10,
WaitingForAuthor: -5,
ApprovedWithSuggestions: 5,
Approved: 10,
};
8 changes: 4 additions & 4 deletions lib/modules/platform/azure/util.ts
Expand Up @@ -5,7 +5,7 @@ import {
PullRequestStatus,
} from 'azure-devops-node-api/interfaces/GitInterfaces.js';
import { logger } from '../../../logger';
import { HostRule, PrState } from '../../../types';
import type { HostRule, PrState } from '../../../types';
import type { GitOptions } from '../../../types/git';
import { addSecretForSanitizing } from '../../../util/sanitize';
import { toBase64 } from '../../../util/string';
Expand Down Expand Up @@ -78,8 +78,8 @@ export function getBranchNameWithoutRefsPrefix(
}

const stateMap = {
[PullRequestStatus.Abandoned]: PrState.Closed,
[PullRequestStatus.Completed]: PrState.Merged,
[PullRequestStatus.Abandoned]: 'closed',
[PullRequestStatus.Completed]: 'merged',
} as Record<PullRequestStatus, PrState | undefined>;

export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr {
Expand All @@ -98,7 +98,7 @@ export function getRenovatePRFormat(azurePr: GitPullRequest): AzurePr {
const createdAt = azurePr.creationDate?.toISOString();

// TODO #7154
const state = stateMap[azurePr.status!] ?? PrState.Open;
const state = stateMap[azurePr.status!] ?? 'open';

const sourceRefName = azurePr.sourceRefName;

Expand Down
12 changes: 6 additions & 6 deletions lib/modules/platform/bitbucket-server/index.spec.ts
Expand Up @@ -5,7 +5,7 @@ import {
REPOSITORY_EMPTY,
REPOSITORY_NOT_FOUND,
} from '../../../constants/error-messages';
import { BranchStatus, PrState } from '../../../types';
import { BranchStatus } from '../../../types';
import type * as _git from '../../../util/git';
import type { Platform } from '../types';

Expand Down Expand Up @@ -1263,7 +1263,7 @@ describe('modules/platform/bitbucket-server/index', () => {
await bitbucket.findPr({
branchName: 'userName1/pullRequest5',
prTitle: 'title',
state: PrState.Open,
state: 'open',
})
).toMatchSnapshot();
});
Expand All @@ -1283,7 +1283,7 @@ describe('modules/platform/bitbucket-server/index', () => {
await bitbucket.findPr({
branchName: 'userName1/pullRequest5',
prTitle: 'title',
state: PrState.Closed,
state: 'closed',
})
).toBeNull();
});
Expand Down Expand Up @@ -1443,7 +1443,7 @@ describe('modules/platform/bitbucket-server/index', () => {
number: 5,
prTitle: 'title',
prBody: 'body',
state: PrState.Closed,
state: 'closed',
})
).toResolve();
});
Expand All @@ -1469,7 +1469,7 @@ describe('modules/platform/bitbucket-server/index', () => {
number: 5,
prTitle: 'title',
prBody: 'body',
state: PrState.Open,
state: 'open',
})
).toResolve();
});
Expand Down Expand Up @@ -1558,7 +1558,7 @@ describe('modules/platform/bitbucket-server/index', () => {
number: 5,
prTitle: 'title',
prBody: 'body',
state: PrState.Open,
state: 'open',
})
).toResolve();
});
Expand Down
14 changes: 7 additions & 7 deletions lib/modules/platform/bitbucket-server/index.ts
Expand Up @@ -8,7 +8,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../../types';
import { BranchStatus, VulnerabilityAlert } from '../../../types';
import type { FileData } from '../../../types/platform/bitbucket-server';
import * as git from '../../../util/git';
import { deleteBranch } from '../../../util/git';
Expand Down Expand Up @@ -275,7 +275,7 @@ export async function getPr(
// TODO: coverage (#9624)
// istanbul ignore next
function matchesState(state: string, desiredState: string): boolean {
if (desiredState === PrState.All) {
if (desiredState === 'all') {
return true;
}
if (desiredState.startsWith('!')) {
Expand Down Expand Up @@ -323,7 +323,7 @@ export async function getPrList(refreshCache?: boolean): Promise<Pr[]> {
export async function findPr({
branchName,
prTitle,
state = PrState.All,
state = 'all',
refreshCache,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, "${prTitle!}", "${state}")`);
Expand All @@ -342,7 +342,7 @@ export async function getBranchPr(branchName: string): Promise<BbsPr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({
branchName,
state: PrState.Open,
state: 'open',
});
return existingPr ? getPr(existingPr.number) : null;
}
Expand Down Expand Up @@ -893,16 +893,16 @@ export async function updatePr({
const currentState = updatedPr.state;
// TODO #7154
const newState = {
[PrState.Open]: 'OPEN',
[PrState.Closed]: 'DECLINED',
['open']: 'OPEN',
['closed']: 'DECLINED',
}[state!];

if (
newState &&
['OPEN', 'DECLINED'].includes(currentState) &&
currentState !== newState
) {
const command = state === PrState.Open ? 'reopen' : 'decline';
const command = state === 'open' ? 'reopen' : 'decline';
const { body: updatedStatePr } = await bitbucketServerHttp.postJson<{
version: number;
}>(
Expand Down
8 changes: 4 additions & 4 deletions lib/modules/platform/bitbucket-server/utils.ts
Expand Up @@ -3,7 +3,7 @@ import url from 'url';
import is from '@sindresorhus/is';
import { CONFIG_GIT_URL_UNAVAILABLE } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { HostRule, PrState } from '../../../types';
import type { HostRule } from '../../../types';
import type { GitProtocol } from '../../../types/git';
import * as git from '../../../util/git';
import { BitbucketServerHttp } from '../../../util/http/bitbucket-server';
Expand All @@ -20,9 +20,9 @@ const bitbucketServerHttp = new BitbucketServerHttp();

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

export function prInfo(pr: BbsRestPr): BbsPr {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/bitbucket/index.spec.ts
@@ -1,6 +1,6 @@
import * as httpMock from '../../../../test/http-mock';
import type { logger as _logger } from '../../../logger';
import { BranchStatus, PrState } from '../../../types';
import { BranchStatus } from '../../../types';
import type * as _git from '../../../util/git';
import { setBaseUrl } from '../../../util/http/bitbucket';
import type { Platform, PlatformResult, RepoParams } from '../types';
Expand Down Expand Up @@ -1210,7 +1210,7 @@ describe('modules/platform/bitbucket/index', () => {
await bitbucket.updatePr({
number: pr.id,
prTitle: pr.title,
state: PrState.Closed,
state: 'closed',
})
).toBeUndefined();
});
Expand Down
10 changes: 5 additions & 5 deletions lib/modules/platform/bitbucket/index.ts
Expand Up @@ -3,7 +3,7 @@ import is from '@sindresorhus/is';
import JSON5 from 'json5';
import { REPOSITORY_NOT_FOUND } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../../types';
import { BranchStatus, VulnerabilityAlert } from '../../../types';
import * as git from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { BitbucketHttp, setBaseUrl } from '../../../util/http/bitbucket';
Expand Down Expand Up @@ -231,7 +231,7 @@ export function getRepoForceRebase(): Promise<boolean> {

// istanbul ignore next
function matchesState(state: string, desiredState: string): boolean {
if (desiredState === PrState.All) {
if (desiredState === 'all') {
return true;
}
if (desiredState.startsWith('!')) {
Expand Down Expand Up @@ -259,7 +259,7 @@ export async function getPrList(): Promise<Pr[]> {
export async function findPr({
branchName,
prTitle,
state = PrState.All,
state = 'all',
}: FindPRConfig): Promise<Pr | null> {
// TODO: types (#7154)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down Expand Up @@ -333,7 +333,7 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> {
logger.debug(`getBranchPr(${branchName})`);
const existingPr = await findPr({
branchName,
state: PrState.Open,
state: 'open',
});
return existingPr ? getPr(existingPr.number) : null;
}
Expand Down Expand Up @@ -853,7 +853,7 @@ export async function updatePr({
}
}

if (state === PrState.Closed && pr) {
if (state === 'closed' && pr) {
await bitbucketHttp.postJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/decline`
);
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/bitbucket/utils.ts
@@ -1,6 +1,6 @@
import url from 'url';
import type { MergeStrategy } from '../../../config/types';
import { BranchStatus, PrState } from '../../../types';
import type { BranchStatus } from '../../../types';
import { BitbucketHttp } from '../../../util/http/bitbucket';
import type { HttpOptions, HttpResponse } from '../../../util/http/types';
import { getPrBodyStruct } from '../pr-body';
Expand Down Expand Up @@ -185,7 +185,7 @@ export function prInfo(pr: PrResponse): Pr {
targetBranch: pr.destination?.branch?.name,
title: pr.title,
state: prStates.closed?.includes(pr.state)
? /* istanbul ignore next */ PrState.Closed
? /* istanbul ignore next */ 'closed'
: pr.state?.toLowerCase(),
createdAt: pr.created_on,
};
Expand Down

0 comments on commit af3b203

Please sign in to comment.