-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
limits.ts
130 lines (122 loc) · 4.06 KB
/
limits.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { DateTime } from 'luxon';
import { RenovateConfig } from '../../../config';
import { logger } from '../../../logger';
import { Pr, platform } from '../../../platform';
import { PrState } from '../../../types';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { branchExists } from '../../../util/git';
import { BranchConfig } from '../../common';
export async function getPrHourlyRemaining(
config: RenovateConfig
): Promise<number> {
if (config.prHourlyLimit) {
try {
logger.debug('Calculating hourly PRs remaining');
const prList = await platform.getPrList();
const currentHourStart = DateTime.local().startOf('hour');
logger.debug(`currentHourStart=${String(currentHourStart)}`);
const soFarThisHour = prList.filter(
(pr) =>
pr.sourceBranch !== config.onboardingBranch &&
pr.sourceBranch.startsWith(config.branchPrefix) &&
DateTime.fromISO(pr.createdAt) > currentHourStart
);
const prsRemaining = Math.max(
0,
config.prHourlyLimit - soFarThisHour.length
);
logger.debug(`PR hourly limit remaining: ${prsRemaining}`);
return prsRemaining;
} catch (err) {
// istanbul ignore if
if (err instanceof ExternalHostError) {
throw err;
}
logger.error({ err }, 'Error checking PRs created per hour');
return config.prHourlyLimit;
}
}
return 99;
}
export async function getConcurrentPrsRemaining(
config: RenovateConfig,
branches: BranchConfig[]
): Promise<number> {
if (config.prConcurrentLimit) {
logger.debug(`Calculating prConcurrentLimit (${config.prConcurrentLimit})`);
try {
const openPrs: Pr[] = [];
for (const { branchName } of branches) {
try {
const pr = await platform.getBranchPr(branchName);
if (
pr &&
pr.sourceBranch !== config.onboardingBranch &&
pr.state === PrState.Open
) {
openPrs.push(pr);
}
} catch (err) {
// no-op
}
}
logger.debug(`${openPrs.length} PRs are currently open`);
const concurrentRemaining = Math.max(
0,
config.prConcurrentLimit - openPrs.length
);
logger.debug(`PR concurrent limit remaining: ${concurrentRemaining}`);
return concurrentRemaining;
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'Error checking concurrent PRs');
return config.prConcurrentLimit;
}
}
return 99;
}
export async function getPrsRemaining(
config: RenovateConfig,
branches: BranchConfig[]
): Promise<number> {
const hourlyRemaining = await getPrHourlyRemaining(config);
const concurrentRemaining = await getConcurrentPrsRemaining(config, branches);
return Math.min(hourlyRemaining, concurrentRemaining);
}
export function getConcurrentBranchesRemaining(
config: RenovateConfig,
branches: BranchConfig[]
): number {
const { branchConcurrentLimit, prConcurrentLimit } = config;
const limit =
typeof branchConcurrentLimit === 'number'
? branchConcurrentLimit
: prConcurrentLimit;
if (typeof limit === 'number' && limit) {
logger.debug(`Calculating branchConcurrentLimit (${limit})`);
try {
const existingBranches: string[] = [];
for (const branch of branches) {
if (branchExists(branch.branchName)) {
existingBranches.push(branch.branchName);
}
}
const existingCount = existingBranches.length;
logger.debug(
`${existingCount} already existing branches found: ${existingBranches.join()}`
);
const concurrentRemaining = Math.max(0, limit - existingCount);
logger.debug(`Branch concurrent limit remaining: ${concurrentRemaining}`);
return concurrentRemaining;
} catch (err) {
logger.error({ err }, 'Error checking concurrent branches');
return limit;
}
}
return 99;
}
export function getBranchesRemaining(
config: RenovateConfig,
branches: BranchConfig[]
): number {
return getConcurrentBranchesRemaining(config, branches);
}