-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
605 lines (575 loc) · 19.3 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import is from '@sindresorhus/is';
import { mergeChildConfig } from '../../../../config';
import type { ValidationMessage } from '../../../../config/types';
import { CONFIG_VALIDATION } from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import {
Release,
ReleaseResult,
applyDatasourceFilters,
getDigest,
getRawPkgReleases,
isGetPkgReleasesConfig,
supportsDigests,
} from '../../../../modules/datasource';
import {
getDatasourceFor,
getDefaultVersioning,
} from '../../../../modules/datasource/common';
import { getRangeStrategy } from '../../../../modules/manager';
import * as allVersioning from '../../../../modules/versioning';
import { ExternalHostError } from '../../../../types/errors/external-host-error';
import { assignKeys } from '../../../../util/assign-keys';
import { applyPackageRules } from '../../../../util/package-rules';
import { regEx } from '../../../../util/regex';
import { Result } from '../../../../util/result';
import { getBucket } from './bucket';
import { getCurrentVersion } from './current';
import { filterVersions } from './filter';
import { filterInternalChecks } from './filter-checks';
import { generateUpdate } from './generate';
import { getRollbackUpdate } from './rollback';
import type { LookupUpdateConfig, UpdateResult } from './types';
import {
addReplacementUpdateIfValid,
isReplacementRulesConfigured,
} from './utils';
export async function lookupUpdates(
inconfig: LookupUpdateConfig,
): Promise<Result<UpdateResult, Error>> {
let config: LookupUpdateConfig = { ...inconfig };
config.versioning ??= getDefaultVersioning(config.datasource);
const versioning = allVersioning.get(config.versioning);
const unconstrainedValue =
!!config.lockedVersion && is.undefined(config.currentValue);
let dependency: ReleaseResult | null = null;
const res: UpdateResult = {
versioning: config.versioning,
updates: [],
warnings: [],
};
try {
logger.trace(
{
dependency: config.packageName,
currentValue: config.currentValue,
},
'lookupUpdates',
);
if (config.currentValue && !is.string(config.currentValue)) {
res.skipReason = 'invalid-value';
return Result.ok(res);
}
if (
!isGetPkgReleasesConfig(config) ||
!getDatasourceFor(config.datasource)
) {
res.skipReason = 'invalid-config';
return Result.ok(res);
}
let compareValue = config.currentValue;
if (
is.string(config.currentValue) &&
is.string(config.versionCompatibility)
) {
const versionCompatbilityRegEx = regEx(config.versionCompatibility);
const regexMatch = versionCompatbilityRegEx.exec(config.currentValue);
if (regexMatch?.groups) {
logger.debug(
{
versionCompatibility: config.versionCompatibility,
currentValue: config.currentValue,
packageName: config.packageName,
groups: regexMatch.groups,
},
'version compatibility regex match',
);
config.currentCompatibility = regexMatch.groups.compatibility;
compareValue = regexMatch.groups.version;
} else {
logger.debug(
{
versionCompatibility: config.versionCompatibility,
currentValue: config.currentValue,
packageName: config.packageName,
},
'version compatibility regex mismatch',
);
}
}
const isValid = is.string(compareValue) && versioning.isValid(compareValue);
if (unconstrainedValue || isValid) {
if (
!config.updatePinnedDependencies &&
// TODO #22198
versioning.isSingleVersion(compareValue!)
) {
res.skipReason = 'is-pinned';
return Result.ok(res);
}
const { val: releaseResult, err: lookupError } = await getRawPkgReleases(
config,
)
.transform((res) => applyDatasourceFilters(res, config))
.unwrap();
if (lookupError instanceof Error) {
throw lookupError;
}
if (lookupError) {
// If dependency lookup fails then warn and return
const warning: ValidationMessage = {
topic: config.packageName,
message: `Failed to look up ${config.datasource} package ${config.packageName}`,
};
logger.debug(
{
dependency: config.packageName,
packageFile: config.packageFile,
},
warning.message,
);
// TODO: return warnings in own field
res.warnings.push(warning);
return Result.ok(res);
}
dependency = releaseResult;
if (dependency.deprecationMessage) {
logger.debug(
`Found deprecationMessage for ${config.datasource} package ${config.packageName}`,
);
}
assignKeys(res, dependency, [
'deprecationMessage',
'sourceUrl',
'registryUrl',
'sourceDirectory',
'homepage',
'changelogUrl',
'dependencyUrl',
]);
const latestVersion = dependency.tags?.latest;
// Filter out any results from datasource that don't comply with our versioning
let allVersions = dependency.releases.filter((release) =>
versioning.isVersion(release.version),
);
// istanbul ignore if
if (allVersions.length === 0) {
const message = `Found no results from datasource that look like a version`;
logger.debug(
{
dependency: config.packageName,
result: dependency,
},
message,
);
if (!config.currentDigest) {
return Result.ok(res);
}
}
// Reapply package rules in case we missed something from sourceUrl
config = applyPackageRules({ ...config, sourceUrl: res.sourceUrl });
if (config.followTag) {
const taggedVersion = dependency.tags?.[config.followTag];
if (!taggedVersion) {
res.warnings.push({
topic: config.packageName,
message: `Can't find version with tag ${config.followTag} for ${config.datasource} package ${config.packageName}`,
});
return Result.ok(res);
}
allVersions = allVersions.filter(
(v) =>
v.version === taggedVersion ||
(v.version === compareValue &&
versioning.isGreaterThan(taggedVersion, compareValue)),
);
}
// Check that existing constraint can be satisfied
const allSatisfyingVersions = allVersions.filter(
(v) =>
// TODO #22198
unconstrainedValue || versioning.matches(v.version, compareValue!),
);
if (!allSatisfyingVersions.length) {
logger.debug(
`Found no satisfying versions with '${config.versioning}' versioning`,
);
}
if (config.rollbackPrs && !allSatisfyingVersions.length) {
const rollback = getRollbackUpdate(config, allVersions, versioning);
// istanbul ignore if
if (!rollback) {
res.warnings.push({
topic: config.packageName,
// TODO: types (#22198)
message: `Can't find version matching ${compareValue!} for ${
config.datasource
} package ${config.packageName}`,
});
return Result.ok(res);
}
res.updates.push(rollback);
}
let rangeStrategy = getRangeStrategy(config);
// istanbul ignore next
if (
config.isVulnerabilityAlert &&
rangeStrategy === 'update-lockfile' &&
!config.lockedVersion
) {
rangeStrategy = 'bump';
}
// unconstrained deps with lockedVersion
if (
config.isVulnerabilityAlert &&
!config.currentValue &&
config.lockedVersion
) {
rangeStrategy = 'update-lockfile';
}
const nonDeprecatedVersions = dependency.releases
.filter((release) => !release.isDeprecated)
.map((release) => release.version);
let currentVersion: string;
if (rangeStrategy === 'update-lockfile') {
currentVersion = config.lockedVersion!;
}
// TODO #22198
currentVersion ??=
getCurrentVersion(
compareValue!,
config.lockedVersion!,
versioning,
rangeStrategy!,
latestVersion!,
nonDeprecatedVersions,
) ??
getCurrentVersion(
compareValue!,
config.lockedVersion!,
versioning,
rangeStrategy!,
latestVersion!,
allVersions.map((v) => v.version),
)!;
if (!currentVersion) {
if (!config.lockedVersion) {
res.skipReason = 'invalid-value';
}
return Result.ok(res);
}
res.currentVersion = currentVersion!;
const currentVersionTimestamp = allVersions.find(
(v) =>
versioning.isValid(v.version) &&
versioning.equals(v.version, currentVersion),
)?.releaseTimestamp;
if (
is.nonEmptyString(currentVersionTimestamp) &&
config.packageRules?.some((rules) =>
is.nonEmptyString(rules.matchCurrentAge),
)
) {
res.currentVersionTimestamp = currentVersionTimestamp;
// Reapply package rules to check matches for matchCurrentAge
config = applyPackageRules({ ...config, currentVersionTimestamp });
}
if (
compareValue &&
currentVersion &&
rangeStrategy === 'pin' &&
!versioning.isSingleVersion(compareValue)
) {
res.updates.push({
updateType: 'pin',
isPin: true,
// TODO: newValue can be null! (#22198)
newValue: versioning.getNewValue({
currentValue: compareValue,
rangeStrategy,
currentVersion,
newVersion: currentVersion,
})!,
newVersion: currentVersion,
newMajor: versioning.getMajor(currentVersion)!,
});
}
if (rangeStrategy === 'pin') {
// Fall back to replace once pinning logic is done
rangeStrategy = 'replace';
}
// istanbul ignore if
if (!versioning.isVersion(currentVersion!)) {
res.skipReason = 'invalid-version';
return Result.ok(res);
}
// Filter latest, unstable, etc
// TODO #22198
let filteredReleases = filterVersions(
config,
currentVersion!,
latestVersion!,
config.rangeStrategy === 'in-range-only'
? allSatisfyingVersions
: allVersions,
versioning,
).filter(
(v) =>
// Leave only compatible versions
unconstrainedValue ||
versioning.isCompatible(v.version, compareValue),
);
if (config.isVulnerabilityAlert && !config.osvVulnerabilityAlerts) {
filteredReleases = filteredReleases.slice(0, 1);
}
const buckets: Record<string, [Release]> = {};
for (const release of filteredReleases) {
const bucket = getBucket(
config,
// TODO #22198
currentVersion!,
release.version,
versioning,
);
if (is.string(bucket)) {
if (buckets[bucket]) {
buckets[bucket].push(release);
} else {
buckets[bucket] = [release];
}
}
}
const depResultConfig = mergeChildConfig(config, res);
for (const [bucket, releases] of Object.entries(buckets)) {
const sortedReleases = releases.sort((r1, r2) =>
versioning.sortVersions(r1.version, r2.version),
);
const { release, pendingChecks, pendingReleases } =
await filterInternalChecks(
depResultConfig,
versioning,
bucket,
sortedReleases,
);
// istanbul ignore next
if (!release) {
return Result.ok(res);
}
const newVersion = release.version;
const update = await generateUpdate(
config,
compareValue,
versioning,
// TODO #22198
rangeStrategy!,
config.lockedVersion ?? currentVersion!,
bucket,
release,
);
if (pendingChecks) {
update.pendingChecks = pendingChecks;
}
// TODO #22198
if (pendingReleases!.length) {
update.pendingVersions = pendingReleases!.map((r) => r.version);
}
if (!update.newValue || update.newValue === compareValue) {
if (!config.lockedVersion) {
continue;
}
// istanbul ignore if
if (rangeStrategy === 'bump') {
logger.trace(
{
packageName: config.packageName,
currentValue: config.currentValue,
lockedVersion: config.lockedVersion,
newVersion,
},
'Skipping bump because newValue is the same',
);
continue;
}
res.isSingleVersion = true;
}
res.isSingleVersion ??=
is.string(update.newValue) &&
versioning.isSingleVersion(update.newValue);
res.updates.push(update);
}
} else if (compareValue) {
logger.debug(
`Dependency ${config.packageName} has unsupported/unversioned value ${compareValue} (versioning=${config.versioning})`,
);
if (!config.pinDigests && !config.currentDigest) {
res.skipReason = 'invalid-value';
} else {
delete res.skipReason;
}
} else {
res.skipReason = 'invalid-value';
}
if (isReplacementRulesConfigured(config)) {
addReplacementUpdateIfValid(res.updates, config);
}
// Record if the dep is fixed to a version
if (config.lockedVersion) {
res.currentVersion = config.lockedVersion;
res.fixedVersion = config.lockedVersion;
} else if (compareValue && versioning.isSingleVersion(compareValue)) {
res.fixedVersion = compareValue.replace(regEx(/^=+/), '');
}
// Add digests if necessary
if (supportsDigests(config.datasource)) {
if (config.currentDigest) {
if (!config.digestOneAndOnly || !res.updates.length) {
// digest update
res.updates.push({
updateType: 'digest',
newValue: compareValue,
});
}
} else if (config.pinDigests) {
// Create a pin only if one doesn't already exists
if (!res.updates.some((update) => update.updateType === 'pin')) {
// pin digest
res.updates.push({
isPinDigest: true,
updateType: 'pinDigest',
newValue: compareValue,
});
}
}
if (versioning.valueToVersion) {
// TODO #22198
res.currentVersion = versioning.valueToVersion(res.currentVersion!);
for (const update of res.updates || /* istanbul ignore next*/ []) {
// TODO #22198
update.newVersion = versioning.valueToVersion(update.newVersion!);
}
}
if (res.registryUrl) {
config.registryUrls = [res.registryUrl];
}
// update digest for all
for (const update of res.updates) {
if (config.pinDigests === true || config.currentDigest) {
// TODO #22198
update.newDigest ??=
dependency?.releases.find((r) => r.version === update.newValue)
?.newDigest ?? (await getDigest(config, update.newValue))!;
// If the digest could not be determined, report this as otherwise the
// update will be omitted later on without notice.
if (update.newDigest === null) {
logger.debug(
{
packageName: config.packageName,
currentValue: config.currentValue,
datasource: config.datasource,
newValue: update.newValue,
bucket: update.bucket,
},
'Could not determine new digest for update.',
);
// Only report a warning if there is a current digest.
// Context: https://github.com/renovatebot/renovate/pull/20175#discussion_r1102615059.
if (config.currentDigest) {
res.warnings.push({
message: `Could not determine new digest for update (${config.datasource} package ${config.packageName})`,
topic: config.packageName,
});
}
}
} else {
delete update.newDigest;
}
if (update.newVersion) {
const registryUrl = dependency?.releases?.find(
(release) => release.version === update.newVersion,
)?.registryUrl;
if (registryUrl && registryUrl !== res.registryUrl) {
update.registryUrl = registryUrl;
}
}
}
}
// massage versionCompatibility
if (
is.string(config.currentValue) &&
is.string(compareValue) &&
is.string(config.versionCompatibility)
) {
for (const update of res.updates) {
logger.debug({ update });
if (is.string(config.currentValue) && is.string(update.newValue)) {
update.newValue = config.currentValue.replace(
compareValue,
update.newValue,
);
}
}
}
if (res.updates.length) {
delete res.skipReason;
}
// Strip out any non-changed ones
res.updates = res.updates
.filter(
(update) => update.newValue !== null || config.currentValue === null,
)
.filter((update) => update.newDigest !== null)
.filter(
(update) =>
(is.string(update.newName) &&
update.newName !== config.packageName) ||
update.isReplacement === true ||
update.newValue !== config.currentValue ||
update.isLockfileUpdate === true ||
// TODO #22198
(update.newDigest &&
!update.newDigest.startsWith(config.currentDigest!)),
);
// If range strategy specified in config is 'in-range-only', also strip out updates where currentValue !== newValue
if (config.rangeStrategy === 'in-range-only') {
res.updates = res.updates.filter(
(update) => update.newValue === config.currentValue,
);
}
// Handle a weird edge case involving followTag and fallbacks
if (config.rollbackPrs && config.followTag) {
res.updates = res.updates.filter(
(update) =>
res.updates.length === 1 ||
/* istanbul ignore next */ update.updateType !== 'rollback',
);
}
} catch (err) /* istanbul ignore next */ {
if (err instanceof ExternalHostError) {
return Result.err(err);
}
if (err instanceof Error && err.message === CONFIG_VALIDATION) {
return Result.err(err);
}
logger.error(
{
currentDigest: config.currentDigest,
currentValue: config.currentValue,
datasource: config.datasource,
packageName: config.packageName,
digestOneAndOnly: config.digestOneAndOnly,
followTag: config.followTag,
lockedVersion: config.lockedVersion,
packageFile: config.packageFile,
pinDigests: config.pinDigests,
rollbackPrs: config.rollbackPrs,
isVulnerabilityAlert: config.isVulnerabilityAlert,
updatePinnedDependencies: config.updatePinnedDependencies,
unconstrainedValue,
err,
},
'lookupUpdates error',
);
res.skipReason = 'internal-error';
}
return Result.ok(res);
}