Skip to content

Commit 3c2a3c9

Browse files
authored
fix(version): truncate release body based on maximum size allowed by VCS client (lerna#4041)
1 parent 89de0eb commit 3c2a3c9

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

libs/commands/version/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import pReduce from "p-reduce";
3636
import pWaterfall from "p-waterfall";
3737
import path from "path";
3838
import semver, { ReleaseType } from "semver";
39-
import { createRelease, createReleaseClient } from "./lib/create-release";
39+
import { createRelease, createReleaseClient, ReleaseClientType } from "./lib/create-release";
4040
import { getCurrentBranch } from "./lib/get-current-branch";
4141
import { gitAdd } from "./lib/git-add";
4242
import { gitCommit } from "./lib/git-commit";
@@ -76,7 +76,7 @@ interface VersionCommandConfigOptions extends CommandConfigOptions {
7676
forceGitTag?: boolean;
7777
tagVersionPrefix?: string;
7878
tagVersionSeparator?: string;
79-
createRelease?: "github" | "gitlab";
79+
createRelease?: ReleaseClientType;
8080
changelog?: boolean;
8181
exact?: boolean;
8282
conventionalPrerelease?: string;
@@ -390,6 +390,7 @@ class VersionCommand extends Command {
390390
createRelease(
391391
this.releaseClient,
392392
{
393+
type: this.options.createRelease,
393394
tags: this.tags,
394395
tagVersionSeparator: this.options.tagVersionSeparator || "@",
395396
releaseNotes: this.releaseNotes,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { truncateReleaseBody } from "./create-release";
2+
3+
describe("truncateReleaseBody", () => {
4+
const generateRandomString = (length: number): string => {
5+
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
6+
let result = "";
7+
for (let i = 0; i < length; i++) {
8+
result += letters.charAt(Math.floor(Math.random() * letters.length));
9+
}
10+
return result;
11+
};
12+
13+
it("should not truncate when body length is within limit for GitHub", () => {
14+
const body = generateRandomString(124999);
15+
const truncatedBody = truncateReleaseBody(body, "github");
16+
expect(truncatedBody).toBe(body);
17+
});
18+
19+
it("should not truncate when body length is within limit for GitLab", () => {
20+
const body = generateRandomString(999999);
21+
const truncatedBody = truncateReleaseBody(body, "gitlab");
22+
expect(truncatedBody).toBe(body);
23+
});
24+
25+
it("should truncate when body length exceeds limit for GitHub", () => {
26+
const body = generateRandomString(125001);
27+
const truncatedBody = truncateReleaseBody(body, "github");
28+
expect(truncatedBody.length).toBe(125000);
29+
expect(truncatedBody.endsWith("...")).toBe(true);
30+
});
31+
32+
it("should truncate when body length exceeds limit for GitLab", () => {
33+
const body = generateRandomString(1000001);
34+
const truncatedBody = truncateReleaseBody(body, "gitlab");
35+
expect(truncatedBody.length).toBe(1000000);
36+
expect(truncatedBody.endsWith("...")).toBe(true);
37+
});
38+
39+
it("should return the body as is when type is undefined", () => {
40+
const body = generateRandomString(150000);
41+
const truncatedBody = truncateReleaseBody(body);
42+
expect(truncatedBody).toBe(body);
43+
});
44+
});

libs/commands/version/src/lib/create-release.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,30 @@ import { createGitHubClient, createGitLabClient, parseGitRepo, ValidationError }
22
import { ExecOptions } from "child_process";
33
import semver from "semver";
44

5-
export function createReleaseClient(type: "github" | "gitlab") {
5+
export type ReleaseClientType = "gitlab" | "github";
6+
7+
export function truncateReleaseBody(body: string, type?: ReleaseClientType) {
8+
let maxReleaseBodyLength: number | undefined;
9+
10+
switch (type) {
11+
case "gitlab":
12+
maxReleaseBodyLength = 1000000;
13+
break;
14+
case "github":
15+
maxReleaseBodyLength = 125000;
16+
break;
17+
default:
18+
return body;
19+
}
20+
21+
if (body.length > maxReleaseBodyLength) {
22+
const ellipsis = "...";
23+
return body.slice(0, maxReleaseBodyLength - ellipsis.length) + ellipsis;
24+
}
25+
return body;
26+
}
27+
28+
export function createReleaseClient(type?: ReleaseClientType) {
629
switch (type) {
730
case "gitlab":
831
return createGitLabClient();
@@ -17,10 +40,16 @@ export function createReleaseClient(type: "github" | "gitlab") {
1740
export function createRelease(
1841
client: ReturnType<typeof createReleaseClient>,
1942
{
43+
type,
2044
tags,
2145
releaseNotes,
2246
tagVersionSeparator,
23-
}: { tags: string[]; tagVersionSeparator: string; releaseNotes: { name: string; notes: string }[] },
47+
}: {
48+
type?: ReleaseClientType;
49+
tags: string[];
50+
tagVersionSeparator: string;
51+
releaseNotes: { name: string; notes: string }[];
52+
},
2453
{ gitRemote, execOpts }: { gitRemote: string; execOpts: ExecOptions }
2554
) {
2655
const repo = parseGitRepo(gitRemote, execOpts);
@@ -37,12 +66,14 @@ export function createRelease(
3766

3867
const prereleaseParts = semver.prerelease(tag.replace(`${name}${tagVersionSeparator}`, "")) || [];
3968

69+
const body = truncateReleaseBody(notes, type);
70+
4071
return client.repos.createRelease({
4172
owner: repo.owner,
4273
repo: repo.name,
4374
tag_name: tag,
4475
name: tag,
45-
body: notes,
76+
body,
4677
draft: false,
4778
prerelease: prereleaseParts.length > 0,
4879
});

0 commit comments

Comments
 (0)