Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: separate update generation #9788

Merged
merged 6 commits into from Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -170,12 +170,10 @@ Object {

exports[`workers/repository/process/lookup/index .lookupUpdates() handles git submodule update 1`] = `
Object {
"currentVersion": undefined,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check this isn't a mistake

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because you moved the versioning.valueToVersion call to inside the isValid condition and git submodules don't have a current value.

@JamieMagee Maybe we should pass the tracking branch as current value and add a git versioning, which allows branches / tags as version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we call valueToVersion now only on valid currentValue. Maybe we should mark this pr as feature, as it causes a bahavior change here.

"updates": Array [
Object {
"newDigest": "4b825dc642cb6eb9a060e54bf8d69288fbee4904",
"newValue": undefined,
"newVersion": undefined,
"updateType": "digest",
},
],
Expand Down
68 changes: 68 additions & 0 deletions lib/workers/repository/process/lookup/generate.ts
@@ -0,0 +1,68 @@
import { Release } from '../../../../datasource';
rarkins marked this conversation as resolved.
Show resolved Hide resolved
import { logger } from '../../../../logger';
import type { LookupUpdate } from '../../../../manager/types';
import { VersioningApi } from '../../../../versioning';
rarkins marked this conversation as resolved.
Show resolved Hide resolved
import { LookupUpdateConfig } from './types';
rarkins marked this conversation as resolved.
Show resolved Hide resolved
import { getUpdateType } from './update-type';

export function generateUpdate(
config: LookupUpdateConfig,
versioning: VersioningApi,
currentVersion: string,
bucket: string,
release: Release
): LookupUpdate {
const newVersion = release.version;
const update: LookupUpdate = {
bucket,
newVersion,
newValue: null,
};
const releaseFields = [
'checksumUrl',
'downloadUrl',
'newDigest',
'releaseTimestamp',
];
for (const field of releaseFields) {
if (release[field] !== undefined) {
update[field] = release[field];
}
}
const { currentValue, rangeStrategy } = config;
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;
rarkins marked this conversation as resolved.
Show resolved Hide resolved
}
update.newMajor = versioning.getMajor(newVersion);
update.newMinor = versioning.getMinor(newVersion);
update.updateType =
update.updateType ||
getUpdateType(config, versioning, currentVersion, newVersion);
if (!versioning.isVersion(update.newValue)) {
update.isRange = true;
}
if (rangeStrategy === 'update-lockfile' && currentValue === update.newValue) {
update.isLockfileUpdate = true;
}
if (
rangeStrategy === 'bump' &&
versioning.matches(newVersion, currentValue)
) {
update.isBump = true;
}
if (versioning.valueToVersion) {
update.newVersion = versioning.valueToVersion(update.newVersion);
}
return update;
}
70 changes: 12 additions & 58 deletions lib/workers/repository/process/lookup/index.ts
Expand Up @@ -10,17 +10,16 @@ import {
} 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 { generateUpdate } from './generate';
import { getRollbackUpdate } from './rollback';
import type { LookupUpdateConfig, UpdateResult } from './types';
import { getUpdateType } from './update-type';

export async function lookupUpdates(
inconfig: LookupUpdateConfig
Expand Down Expand Up @@ -153,6 +152,9 @@ export async function lookupUpdates(
allVersions.map((v) => v.version)
);
res.currentVersion = currentVersion;
if (versioning.valueToVersion) {
res.currentVersion = versioning.valueToVersion(res.currentVersion);
}
if (
currentVersion &&
rangeStrategy === 'pin' &&
Expand Down Expand Up @@ -208,25 +210,13 @@ export async function lookupUpdates(
);
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;
}
const update = generateUpdate(
config,
versioning,
currentVersion,
bucket,
release
);
if (!update.newValue || update.newValue === currentValue) {
if (!lockedVersion) {
continue; // eslint-disable-line no-continue
Expand All @@ -241,39 +231,9 @@ export async function lookupUpdates(
}
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 (
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) {
Expand Down Expand Up @@ -314,12 +274,6 @@ export async function lookupUpdates(
});
}
}
if (versioning.valueToVersion) {
res.currentVersion = versioning.valueToVersion(res.currentVersion);
for (const update of res.updates || []) {
update.newVersion = versioning.valueToVersion(update.newVersion);
}
}
// update digest for all
for (const update of res.updates) {
if (pinDigests || currentDigest) {
Expand Down