Skip to content

add a min oc version checker for templates if defined #1394

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

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/registry/domain/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ocCliVersionValidator from './oc-cli-version';
import packageJsonValidator from './package-json-validator';
import pluginsRequirementsValidator from './plugins-requirements';
import registryConfigurationValidator from './registry-configuration';
import templateOcVersionValidator from './templace-oc-version';

import nodeVersionValidator from './node-version';
import uploadedPackageValidator from './uploaded-package';
Expand All @@ -19,6 +20,8 @@ export const validatePackage = uploadedPackageValidator;
export const validatePackageJson = packageJsonValidator;
export const validatePluginsRequirements = pluginsRequirementsValidator;
export const validateRegistryConfiguration = registryConfigurationValidator;
export const validateTemplateOcVersion = templateOcVersionValidator;

export function validateVersion(version: string): boolean {
return !!semver.valid(version);
}
33 changes: 33 additions & 0 deletions src/registry/domain/validators/templace-oc-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'node:path';
import fs from 'fs-extra';
import semver from 'semver';

const packageInfo = fs.readJsonSync(
path.join(__dirname, '..', '..', '..', '..', 'package.json')
);

type OkResult = { isValid: true };
type ErrorResult = {
isValid: false;
error: {
registryVersion: string;
minOcVersion: string;
code: string;
};
};
type Result = OkResult | ErrorResult;

export default function templateOcVersion(minOcVersion: string): Result {
if (semver.lt(packageInfo.version, minOcVersion)) {
return {
isValid: false,
error: {
registryVersion: packageInfo.version,
minOcVersion,
code: 'old_version'
}
};
}

return { isValid: true };
}
19 changes: 19 additions & 0 deletions src/registry/routes/helpers/get-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import RequireWrapper from '../../domain/require-wrapper';
import * as sanitiser from '../../domain/sanitiser';
import * as urlBuilder from '../../domain/url-builder';
import * as validator from '../../domain/validators';
import { validateTemplateOcVersion } from '../../domain/validators';
import applyDefaultValues from './apply-default-values';
import * as getComponentFallback from './get-component-fallback';
import GetComponentRetrievingInfo from './get-component-retrieving-info';
Expand Down Expand Up @@ -203,6 +204,24 @@ export default function getComponent(conf: Config, repository: Repository) {
});
}

if (component.oc.files.template.minOcVersion) {
const templateOcVersionResult = validateTemplateOcVersion(
component.oc.files.template.minOcVersion
);
if (!templateOcVersionResult.isValid) {
return callback({
status: 400,
response: {
code: 'TEMPLATE_REQUIRES_HIGHER_OC_VERSION',
error: strings.errors.cli.TEMPLATE_OC_VERSION_NOT_VALID(
templateOcVersionResult.error.minOcVersion,
templateOcVersionResult.error.registryVersion
)
}
});
}
}

// Support legacy templates
let templateType = component.oc.files.template.type;
const isLegacyTemplate = isTemplateLegacy(templateType);
Expand Down
19 changes: 19 additions & 0 deletions src/registry/routes/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import strings from '../../resources/index';
import extractPackage from '../domain/extract-package';
import type { Repository } from '../domain/repository';
import * as validator from '../domain/validators';
import { validateTemplateOcVersion } from '../domain/validators';

export default function publish(repository: Repository) {
return async (req: Request, res: Response): Promise<void> => {
Expand Down Expand Up @@ -56,6 +57,24 @@ export default function publish(repository: Repository) {
try {
const pkgDetails = await extractPackage(files, res.conf.tarExtractMode);

if (pkgDetails.packageJson.oc.files.template.minOcVersion) {
const templateOcVersionResult = validateTemplateOcVersion(
pkgDetails.packageJson.oc.files.template.minOcVersion
);
if (!templateOcVersionResult.isValid) {
res.errorDetails = `Your template requires a version of OC higher than ${templateOcVersionResult.error.minOcVersion}`;
res.status(409).json({
code: 'template_oc_version_not_valid',
error: strings.errors.cli.TEMPLATE_OC_VERSION_NOT_VALID(
templateOcVersionResult.error.minOcVersion,
templateOcVersionResult.error.registryVersion
),
details: templateOcVersionResult.error
});
return;
}
}

try {
await repository.publishComponent({
pkgDetails,
Expand Down
5 changes: 5 additions & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ export default {
`Error requiring oc-template: "${template}" not found`,
TEMPLATE_TYPE_NOT_VALID: (template: string): string =>
`Error requiring oc-template: "${template}" is not a valid oc-template`,
TEMPLATE_OC_VERSION_NOT_VALID: (
template: string,
version: string
): string =>
`Your template requires a version of OC higher than ${template} but your OC version is ${version}`,
TEMPLATE_DEP_MISSING: (template: string, path: string): string =>
`Template dependency missing. To fix it run:\n\nnpm install --save-dev ${template}-compiler --prefix ${path}\n\n`
},
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface OcConfiguration {
src: string;
type: string;
version: string;
minOcVersion?: string;
size?: number;
};
env?: string;
Expand Down