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

fix(tag), exit immediately when a pre-release id is invalid #9063

Merged
merged 2 commits into from
Jul 23, 2024
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
17 changes: 17 additions & 0 deletions e2e/harmony/tag-harmony.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@ describe('tag components on Harmony', function () {
expect(tagOutput).to.have.string('[email protected]');
});
});
describe('invalid pre-release after normal tag', () => {
let result: string;
before(() => {
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(1);
helper.command.tagAllWithoutBuild();
result = helper.general.runWithTryCatch(`bit tag --unmodified --pre-release "h?h"`);
});
it('should throw an error', () => {
expect(result).to.have.string('is not a valid semantic version');
});
it('should not create a new version', () => {
const comp = helper.command.catComponent('comp1');
const ver1Hash = comp.versions['0.0.1'];
expect(comp.head).to.equal(ver1Hash);
});
});
describe('soft-tag pre-release', () => {
let tagOutput: string;
before(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/scope/models/model-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ComponentOverrides from '../../consumer/config/component-overrides';
import ValidationError from '../../error/validation-error';
import logger from '../../logger/logger';
import { getStringifyArgs } from '@teambit/legacy.utils';
import { getLatestVersion } from '@teambit/pkg.modules.semver-helper';
import { getLatestVersion, validateVersion } from '@teambit/pkg.modules.semver-helper';
import ComponentObjects from '../component-objects';
import { SnapsDistance } from '../component-ops/snaps-distance';
import { getDivergeData } from '../component-ops/get-diverge-data';
Expand Down Expand Up @@ -602,7 +602,9 @@ export default class Component extends BitObject {
if (exactVersion && this.versions[exactVersion]) {
throw new VersionAlreadyExists(exactVersion, this.id());
}
return exactVersion || this.version(releaseType, incrementBy, preReleaseId);
const version = exactVersion || this.version(releaseType, incrementBy, preReleaseId);
validateVersion(version);
return version;
}

isEqual(component: Component, considerOrphanedVersions = true): boolean {
Expand Down
8 changes: 7 additions & 1 deletion src/scope/version-validator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable complexity */
import { PackageJsonValidator as PJV } from '@teambit/pkg.package-json.validator';
import R from 'ramda';
import { lt, gt } from 'semver';
import packageNameValidate from 'validate-npm-package-name';
import { ComponentID, ComponentIdList } from '@teambit/component-id';
import { BitError } from '@teambit/bit-error';
import { isSnap } from '@teambit/component-version';
import { isSnap, isTag } from '@teambit/component-version';
import { DEPENDENCIES_FIELDS } from '../constants';
import { SchemaName } from '../consumer/component/component-schema';
import { Dependencies } from '../consumer/component/dependencies';
Expand Down Expand Up @@ -316,6 +317,11 @@ ${duplicationStr}`);
});
});
}
const ver = version.componentId?.version;
if (ver && !isSnap(ver) && !isTag(ver)) {
throw new VersionInvalid(`${message}, the version "${ver}" is invalid. it's not a hash (snap) nor a tag`);
}

if (version.isLegacy) {
// mainly to make sure that all Harmony components are saved with schema
// if they don't have schema, they'll fail on this test
Expand Down