Skip to content

Commit

Permalink
isCountReached -> isLimitReached
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Nov 20, 2024
1 parent 0071de2 commit 02d0809
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
15 changes: 7 additions & 8 deletions lib/workers/global/limits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
hasMultipleLimits,
incCountValue,
incLimitedValue,
isCountReached,
isLimitReached,
resetAllLimits,
setCount,
Expand Down Expand Up @@ -217,7 +216,7 @@ describe('workers/global/limits', () => {
});
});

describe('isCountReached', () => {
describe('isLimitReached', () => {
it('returns false based on concurrent limits', () => {
setCount('PullRequests', 1);
setCount('HourlyPullRequests', 1);
Expand All @@ -240,10 +239,10 @@ describe('workers/global/limits', () => {
},
]);
expect(
isCountReached('Branches', partial<BranchConfig>({ upgrades })),
isLimitReached('Branches', partial<BranchConfig>({ upgrades })),
).toBe(false);
expect(
isCountReached('PullRequests', partial<BranchConfig>({ upgrades })),
isLimitReached('PullRequests', partial<BranchConfig>({ upgrades })),
).toBe(false);
});

Expand All @@ -269,10 +268,10 @@ describe('workers/global/limits', () => {
},
]);
expect(
isCountReached('Branches', partial<BranchConfig>({ upgrades })),
isLimitReached('Branches', partial<BranchConfig>({ upgrades })),
).toBe(true);
expect(
isCountReached('PullRequests', partial<BranchConfig>({ upgrades })),
isLimitReached('PullRequests', partial<BranchConfig>({ upgrades })),
).toBe(true);
});

Expand All @@ -298,10 +297,10 @@ describe('workers/global/limits', () => {
},
]);
expect(
isCountReached('Branches', partial<BranchConfig>({ upgrades })),
isLimitReached('Branches', partial<BranchConfig>({ upgrades })),
).toBe(true);
expect(
isCountReached('PullRequests', partial<BranchConfig>({ upgrades })),
isLimitReached('PullRequests', partial<BranchConfig>({ upgrades })),
).toBe(true);
});
});
Expand Down
15 changes: 13 additions & 2 deletions lib/workers/global/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function incLimitedValue(key: Limit, incBy = 1): void {
});
}

export function isLimitReached(key: Limit): boolean {
export function handleCommitsLimit(key: Limit): boolean {
const limit = limits.get(key);
// TODO: fix me?
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
Expand Down Expand Up @@ -64,7 +64,7 @@ export function incCountValue(key: CountName, incBy = 1): void {
});
}

export function isCountReached(
export function handleOtherLimits(
key: Exclude<CountName, 'HourlyPullRequests'>,
config: BranchConfig,
): boolean {
Expand Down Expand Up @@ -176,3 +176,14 @@ export function hasMultipleLimits(

return distinctLimits.size > 1;
}

export function isLimitReached(
limit: 'Branches' | 'PullRequests' | 'Commits',
config?: BranchConfig,
): boolean {
if (limit === 'Commits') {
return handleCommitsLimit(limit);
}

return handleOtherLimits(limit, config!);
}
6 changes: 3 additions & 3 deletions lib/workers/repository/update/branch/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ describe('workers/repository/update/branch/index', () => {
artifactErrors: [],
updatedArtifacts: [],
});
limits.isCountReached.mockReturnValueOnce(true);
limits.isLimitReached.mockReturnValueOnce(true);
limits.isLimitReached.mockReturnValueOnce(false);
expect(await branchWorker.processBranch(config)).toEqual({
branchExists: false,
Expand Down Expand Up @@ -598,7 +598,7 @@ describe('workers/repository/update/branch/index', () => {
type: 'without-pr',
prBlockedBy: 'RateLimited',
});
limits.isCountReached.mockReturnValue(false);
limits.isLimitReached.mockReturnValue(false);
//git.getBranchCommit.mockReturnValue('123test');TODO:not needed?
expect(await branchWorker.processBranch(config)).toEqual({
branchExists: true,
Expand All @@ -617,7 +617,7 @@ describe('workers/repository/update/branch/index', () => {
updatedArtifacts: [],
});
scm.branchExists.mockResolvedValue(false);
limits.isCountReached.mockReturnValueOnce(false);
limits.isLimitReached.mockReturnValueOnce(false);
limits.isLimitReached.mockReturnValueOnce(true);
expect(await branchWorker.processBranch(config)).toEqual({
branchExists: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/update/branch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { coerceNumber } from '../../../../util/number';
import { toMs } from '../../../../util/pretty-time';
import * as template from '../../../../util/template';
import { counts, isCountReached, isLimitReached } from '../../../global/limits';
import { counts, isLimitReached } from '../../../global/limits';
import type { BranchConfig, BranchResult, PrBlockedBy } from '../../../types';
import { embedChangelogs } from '../../changelog';
import { ensurePr, getPlatformPrOptions } from '../pr';
Expand Down Expand Up @@ -216,7 +216,7 @@ export async function processBranch(

if (
!branchExists &&
isCountReached('Branches', branchConfig) &&
isLimitReached('Branches', branchConfig) &&
!dependencyDashboardCheck &&
!config.isVulnerabilityAlert
) {
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/update/pr/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('workers/repository/update/pr/index', () => {

it('aborts PR creation once limit is exceeded', async () => {
platform.createPr.mockResolvedValueOnce(pr);
limits.isCountReached.mockReturnValueOnce(true);
limits.isLimitReached.mockReturnValueOnce(true);

config.fetchChangeLogs = 'pr';

Expand All @@ -114,7 +114,7 @@ describe('workers/repository/update/pr/index', () => {

it('ignores PR limits on vulnerability alert', async () => {
platform.createPr.mockResolvedValueOnce(pr);
limits.isCountReached.mockReturnValueOnce(true);
limits.isLimitReached.mockReturnValueOnce(true);

const prConfig = { ...config, isVulnerabilityAlert: true };
delete prConfig.prTitle; // for coverage
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/update/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { stripEmojis } from '../../../../util/emoji';
import { fingerprint } from '../../../../util/fingerprint';
import { getBranchLastCommitTime } from '../../../../util/git';
import { memoize } from '../../../../util/memoize';
import { incCountValue, isCountReached } from '../../../global/limits';
import { incCountValue, isLimitReached } from '../../../global/limits';
import type {
BranchConfig,
BranchUpgradeConfig,
Expand Down Expand Up @@ -482,7 +482,7 @@ export async function ensurePr(
try {
if (
!dependencyDashboardCheck &&
isCountReached('PullRequests', prConfig) &&
isLimitReached('PullRequests', prConfig) &&
!config.isVulnerabilityAlert
) {
logger.debug('Skipping PR - limit reached');
Expand Down

0 comments on commit 02d0809

Please sign in to comment.