/
index.ts
358 lines (356 loc) · 11.2 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
import type { ValidationMessage } from '../../../../config/types';
import {
Release,
getDatasourceList,
getDefaultVersioning,
getDigest,
getPkgReleases,
isGetPkgReleasesConfig,
supportsDigests,
} from '../../../../datasource';
import { logger } from '../../../../logger';
import { getRangeStrategy } from '../../../../manager';
import type { LookupUpdate } from '../../../../manager/types';
import { SkipReason } from '../../../../types';
import { clone } from '../../../../util/clone';
import { applyPackageRules } from '../../../../util/package-rules';
import * as allVersioning from '../../../../versioning';
import { getBucket } from './bucket';
import { getCurrentVersion } from './current';
import { filterVersions } from './filter';
import { getRollbackUpdate } from './rollback';
import type { LookupUpdateConfig, UpdateResult } from './types';
import { getUpdateType } from './update-type';
export async function lookupUpdates(
inconfig: LookupUpdateConfig
): Promise<UpdateResult> {
let config: LookupUpdateConfig = { ...inconfig };
const {
currentDigest,
currentValue,
datasource,
depName,
digestOneAndOnly,
followTag,
lockedVersion,
packageFile,
pinDigests,
rollbackPrs,
isVulnerabilityAlert,
} = config;
logger.trace({ dependency: depName, currentValue }, 'lookupUpdates');
// Use the datasource's default versioning if none is configured
const versioning = allVersioning.get(
config.versioning || getDefaultVersioning(datasource)
);
const res: UpdateResult = { updates: [], warnings: [] } as any;
// istanbul ignore if
if (
!isGetPkgReleasesConfig(config) ||
!getDatasourceList().includes(datasource)
) {
res.skipReason = SkipReason.InvalidConfig;
return res;
}
const isValid = currentValue && versioning.isValid(currentValue);
if (isValid) {
const dependency = clone(await getPkgReleases(config));
if (!dependency) {
// If dependency lookup fails then warn and return
const warning: ValidationMessage = {
topic: depName,
message: `Failed to look up dependency ${depName}`,
};
logger.debug({ dependency: depName, packageFile }, warning.message);
// TODO: return warnings in own field
res.warnings.push(warning);
return res;
}
if (dependency.deprecationMessage) {
logger.debug({ dependency: depName }, 'Found deprecationMessage');
res.deprecationMessage = dependency.deprecationMessage;
}
res.sourceUrl = dependency?.sourceUrl;
if (dependency.sourceDirectory) {
res.sourceDirectory = dependency.sourceDirectory;
}
res.homepage = dependency.homepage;
res.changelogUrl = dependency.changelogUrl;
res.dependencyUrl = dependency?.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: depName, result: dependency }, message);
if (!currentDigest) {
return res;
}
}
// Reapply package rules in case we missed something from sourceUrl
config = applyPackageRules({ ...config, sourceUrl: res.sourceUrl });
if (followTag) {
const taggedVersion = dependency.tags[followTag];
if (!taggedVersion) {
res.warnings.push({
topic: depName,
message: `Can't find version with tag ${followTag} for ${depName}`,
});
return res;
}
allVersions = allVersions.filter(
(v) =>
v.version === taggedVersion ||
(v.version === currentValue &&
versioning.isGreaterThan(taggedVersion, currentValue))
);
}
// Check that existing constraint can be satisfied
const allSatisfyingVersions = allVersions.filter((v) =>
versioning.matches(v.version, currentValue)
);
if (rollbackPrs && !allSatisfyingVersions.length) {
const rollback = getRollbackUpdate(config, allVersions);
// istanbul ignore if
if (!rollback) {
res.warnings.push({
topic: depName,
message: `Can't find version matching ${currentValue} for ${depName}`,
});
return res;
}
res.updates.push(rollback);
}
let rangeStrategy = getRangeStrategy(config);
// istanbul ignore next
if (
isVulnerabilityAlert &&
rangeStrategy === 'update-lockfile' &&
!lockedVersion
) {
rangeStrategy = 'bump';
}
const nonDeprecatedVersions = dependency.releases
.filter((release) => !release.isDeprecated)
.map((release) => release.version);
const currentVersion =
lockedVersion ||
getCurrentVersion(
config,
versioning,
rangeStrategy,
latestVersion,
nonDeprecatedVersions
) ||
getCurrentVersion(
config,
versioning,
rangeStrategy,
latestVersion,
allVersions.map((v) => v.version)
);
res.currentVersion = currentVersion;
if (
currentVersion &&
rangeStrategy === 'pin' &&
!versioning.isSingleVersion(currentValue)
) {
res.updates.push({
updateType: 'pin',
isPin: true,
newValue: versioning.getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion: currentVersion,
}),
newMajor: versioning.getMajor(currentVersion),
});
}
let filterStart = currentVersion;
if (lockedVersion && rangeStrategy === 'update-lockfile') {
// Look for versions greater than the current locked version that still satisfy the package.json range
filterStart = lockedVersion;
}
// Filter latest, unstable, etc
let filteredReleases = filterVersions(
config,
filterStart,
latestVersion,
allVersions
).filter((v) =>
// Leave only compatible versions
versioning.isCompatible(v.version, currentValue)
);
if (isVulnerabilityAlert) {
filteredReleases = filteredReleases.slice(0, 1);
}
const buckets: Record<string, [Release]> = {};
for (const release of filteredReleases) {
const bucket = getBucket(
config,
currentVersion,
release.version,
versioning
);
if (buckets[bucket]) {
buckets[bucket].push(release);
} else {
buckets[bucket] = [release];
}
}
for (const [bucket, releases] of Object.entries(buckets)) {
const sortedReleases = releases.sort((r1, r2) =>
versioning.sortVersions(r1.version, r2.version)
);
const release = sortedReleases.pop();
const newVersion = release.version;
const update: LookupUpdate = {
newVersion,
newValue: null,
};
update.bucket = bucket;
try {
update.newValue = versioning.getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
});
} catch (err) /* istanbul ignore next */ {
logger.warn(
{ err, currentValue, rangeStrategy, currentVersion, newVersion },
'getNewValue error'
);
update.newValue = currentValue;
}
if (!update.newValue || update.newValue === currentValue) {
if (!lockedVersion) {
continue; // eslint-disable-line no-continue
}
// istanbul ignore if
if (rangeStrategy === 'bump') {
logger.trace(
{ depName, currentValue, lockedVersion, newVersion },
'Skipping bump because newValue is the same'
);
continue; // eslint-disable-line no-continue
}
res.isSingleVersion = true;
}
update.newMajor = versioning.getMajor(newVersion);
update.newMinor = versioning.getMinor(newVersion);
update.updateType =
update.updateType ||
getUpdateType(config, versioning, currentVersion, newVersion);
res.isSingleVersion =
res.isSingleVersion || !!versioning.isSingleVersion(update.newValue);
if (!versioning.isVersion(update.newValue)) {
update.isRange = true;
}
const releaseFields = [
'checksumUrl',
'downloadUrl',
'newDigest',
'releaseTimestamp',
];
releaseFields.forEach((field) => {
if (release[field] !== undefined) {
update[field] = release[field];
}
});
if (sortedReleases.length) {
update.skippedOverVersions = sortedReleases.map((r) => r.version);
}
if (
rangeStrategy === 'update-lockfile' &&
currentValue === update.newValue
) {
update.isLockfileUpdate = true;
}
if (
rangeStrategy === 'bump' &&
versioning.matches(newVersion, currentValue)
) {
update.isBump = true;
}
res.updates.push(update);
}
} else if (currentValue) {
logger.debug(`Dependency ${depName} has unsupported value ${currentValue}`);
if (!pinDigests && !currentDigest) {
res.skipReason = SkipReason.InvalidValue;
} else {
delete res.skipReason;
}
} else {
res.skipReason = SkipReason.InvalidValue;
}
// Record if the dep is fixed to a version
if (lockedVersion) {
res.fixedVersion = lockedVersion;
} else if (currentValue && versioning.isSingleVersion(currentValue)) {
res.fixedVersion = currentValue.replace(/^=+/, '');
}
// Add digests if necessary
if (supportsDigests(config)) {
if (currentDigest) {
if (!digestOneAndOnly || !res.updates.length) {
// digest update
res.updates.push({
updateType: 'digest',
newValue: currentValue,
});
}
} else if (pinDigests) {
// Create a pin only if one doesn't already exists
if (!res.updates.some((update) => update.updateType === 'pin')) {
// pin digest
res.updates.push({
updateType: 'pin',
newValue: currentValue,
});
}
}
if (versioning.valueToVersion) {
for (const update of res.updates || []) {
res.currentVersion = versioning.valueToVersion(res.currentVersion);
update.newVersion = versioning.valueToVersion(update.newVersion);
}
}
// update digest for all
for (const update of res.updates) {
if (pinDigests || currentDigest) {
update.newDigest =
update.newDigest || (await getDigest(config, update.newValue));
}
}
}
if (res.updates.length) {
delete res.skipReason;
}
// Strip out any non-changed ones
res.updates = res.updates
.filter((update) => update.newDigest !== null)
.filter(
(update) =>
update.newValue !== currentValue ||
update.isLockfileUpdate ||
(update.newDigest && !update.newDigest.startsWith(currentDigest))
);
if (res.updates.some((update) => update.updateType === 'pin')) {
for (const update of res.updates) {
if (
update.updateType !== 'pin' &&
update.updateType !== 'rollback' &&
!isVulnerabilityAlert
) {
update.blockedByPin = true;
}
}
}
return res;
}