Skip to content

Commit

Permalink
fix: be consistently strict when parsing semvers
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Dec 27, 2023
1 parent 0005b83 commit bb05385
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/domain/metadata-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ describe("constructor", () => {

expect(metadata.versions).toEqual(["12.4.2.1.1", "20210324.2", "55"]);
});

test("sorts non-semver versions that look like semver as non-semver", () => {
// https://github.com/bazel-contrib/publish-to-bcr/issues/97
mockMetadataFile(`\
{
"homepage": "https://foo.bar",
"maintainers": [],
"repository": [
"github:bar/rules_foo"
],
"versions": [
"1.0.0-rc0",
"1.0.0-rc1",
"1.0.0rc1"
],
"yanked_versions": {}
}
`);
const metadata = new MetadataFile("metadata.json");

expect(metadata.versions).toEqual(["1.0.0rc1", "1.0.0-rc0", "1.0.0-rc1"]);
});
});

describe("save", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/domain/metadata-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,18 @@ export class MetadataFile {
}

private sortVersions(): void {
const semver = this.metadata.versions.filter(validSemver);
const semver = this.metadata.versions.filter(
(v: string) => !!validSemver(v, { loose: false })
);
const nonSemver = this.metadata.versions.filter(
(v: string) => !validSemver(v)
);

this.metadata.versions = [
...nonSemver.sort(),
...semver.sort(semverCompare),
...semver.sort((a: string, b: string) =>
semverCompare(a, b, { loose: false })
),
];
}
}

0 comments on commit bb05385

Please sign in to comment.