Skip to content

Commit a064077

Browse files
authored
ref(core): Don't create spans for skipped release steps (#112)
Skip creating spans if optional release pipeline steps are skipped Adjust and add tests
1 parent 8adba64 commit a064077

File tree

2 files changed

+153
-54
lines changed

2 files changed

+153
-54
lines changed

packages/bundler-plugin-core/src/sentry/releasePipeline.ts

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// - huge download
77
// - unnecessary functionality
88

9+
import { logger } from "@sentry/utils";
910
import { InternalOptions } from "../options-mapping";
1011
import { BuildContext } from "../types";
1112
import { addSpanToTransaction } from "./telemetry";
@@ -19,6 +20,20 @@ export async function createNewRelease(options: InternalOptions, ctx: BuildConte
1920
span?.finish();
2021
}
2122

23+
export async function cleanArtifacts(options: InternalOptions, ctx: BuildContext): Promise<void> {
24+
if (!options.cleanArtifacts) {
25+
logger.debug("Skipping artifact cleanup.");
26+
return;
27+
}
28+
29+
const span = addSpanToTransaction(ctx, "function.plugin.clean_artifacts");
30+
31+
await ctx.cli.releases.execute(["releases", "files", options.release, "delete", "--all"], true);
32+
33+
ctx.logger.info("Successfully cleaned previous artifacts.");
34+
span?.finish();
35+
}
36+
2237
export async function uploadSourceMaps(options: InternalOptions, ctx: BuildContext): Promise<void> {
2338
const span = addSpanToTransaction(ctx, "function.plugin.upload_sourcemaps");
2439
ctx.logger.info("Uploading Sourcemaps.");
@@ -31,66 +46,60 @@ export async function uploadSourceMaps(options: InternalOptions, ctx: BuildConte
3146
span?.finish();
3247
}
3348

34-
export async function finalizeRelease(options: InternalOptions, ctx: BuildContext): Promise<void> {
35-
const span = addSpanToTransaction(ctx, "function.plugin.finalize_release");
36-
37-
if (options.finalize) {
38-
await ctx.cli.releases.finalize(options.release);
39-
ctx.logger.info("Successfully finalized release.");
49+
export async function setCommits(options: InternalOptions, ctx: BuildContext): Promise<void> {
50+
if (!options.setCommits) {
51+
logger.debug("Skipping setting commits to release.");
52+
return;
4053
}
4154

42-
span?.finish();
43-
}
44-
45-
export async function cleanArtifacts(options: InternalOptions, ctx: BuildContext): Promise<void> {
46-
const span = addSpanToTransaction(ctx, "function.plugin.clean_artifacts");
47-
48-
if (options.cleanArtifacts) {
49-
await ctx.cli.releases.execute(["releases", "files", options.release, "delete", "--all"], true);
50-
ctx.logger.info("Successfully cleaned previous artifacts.");
51-
}
55+
const span = addSpanToTransaction(ctx, "function.plugin.set_commits");
5256

57+
const { auto, repo, commit, previousCommit, ignoreMissing, ignoreEmpty } = options.setCommits;
58+
await ctx.cli.releases.setCommits(options.release, {
59+
commit,
60+
previousCommit,
61+
repo,
62+
auto,
63+
ignoreMissing,
64+
ignoreEmpty,
65+
});
66+
67+
ctx.logger.info("Successfully set commits.");
5368
span?.finish();
5469
}
5570

56-
export async function setCommits(options: InternalOptions, ctx: BuildContext): Promise<void> {
57-
const span = addSpanToTransaction(ctx, "function.plugin.set_commits");
58-
59-
if (options.setCommits) {
60-
const { auto, repo, commit, previousCommit, ignoreMissing, ignoreEmpty } = options.setCommits;
71+
export async function finalizeRelease(options: InternalOptions, ctx: BuildContext): Promise<void> {
72+
if (!options.finalize) {
73+
logger.debug("Skipping release finalization.");
74+
return;
75+
}
6176

62-
await ctx.cli.releases.setCommits(options.release, {
63-
commit,
64-
previousCommit,
65-
repo,
66-
auto,
67-
ignoreMissing,
68-
ignoreEmpty,
69-
});
77+
const span = addSpanToTransaction(ctx, "function.plugin.finalize_release");
7078

71-
ctx.logger.info("Successfully set commits.");
72-
}
79+
await ctx.cli.releases.finalize(options.release);
7380

81+
ctx.logger.info("Successfully finalized release.");
7482
span?.finish();
7583
}
7684

7785
export async function addDeploy(options: InternalOptions, ctx: BuildContext): Promise<void> {
78-
const span = addSpanToTransaction(ctx, "function.plugin.deploy");
79-
80-
if (options.deploy) {
81-
const { env, started, finished, time, name, url } = options.deploy;
82-
83-
await ctx.cli.releases.newDeploy(options.release, {
84-
env,
85-
started,
86-
finished,
87-
time,
88-
name,
89-
url,
90-
});
91-
92-
ctx.logger.info("Successfully added deploy.");
86+
if (!options.deploy) {
87+
logger.debug("Skipping adding deploy info to release.");
88+
return;
9389
}
9490

91+
const span = addSpanToTransaction(ctx, "function.plugin.deploy");
92+
93+
const { env, started, finished, time, name, url } = options.deploy;
94+
await ctx.cli.releases.newDeploy(options.release, {
95+
env,
96+
started,
97+
finished,
98+
time,
99+
name,
100+
url,
101+
});
102+
103+
ctx.logger.info("Successfully added deploy.");
95104
span?.finish();
96105
}

packages/bundler-plugin-core/test/releasePipeline.test.ts

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { InternalOptions } from "../src/options-mapping";
2-
import { addDeploy, setCommits } from "../src/sentry/releasePipeline";
2+
import {
3+
addDeploy,
4+
cleanArtifacts,
5+
createNewRelease,
6+
finalizeRelease,
7+
setCommits,
8+
uploadSourceMaps,
9+
} from "../src/sentry/releasePipeline";
310
import { BuildContext } from "../src/types";
411

512
const mockedAddSpanToTxn = jest.fn();
@@ -11,9 +18,9 @@ jest.mock("../src/sentry/telemetry", () => {
1118
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
1219
return {
1320
...original,
14-
addSpanToTransaction: () => {
21+
addSpanToTransaction: (ctx: unknown, op: string) => {
1522
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
16-
return mockedAddSpanToTxn();
23+
return mockedAddSpanToTxn(ctx, op);
1724
},
1825
};
1926
});
@@ -28,7 +35,11 @@ describe("Release Pipeline", () => {
2835

2936
const mockedCLI = {
3037
releases: {
38+
new: jest.fn(),
39+
execute: jest.fn(),
40+
uploadSourceMaps: jest.fn(),
3141
setCommits: jest.fn(),
42+
finalize: jest.fn(),
3243
newDeploy: jest.fn(),
3344
},
3445
};
@@ -38,13 +49,71 @@ describe("Release Pipeline", () => {
3849

3950
const ctx = { cli: mockedCLI, logger: mockedLogger };
4051

52+
beforeEach(() => {
53+
jest.clearAllMocks();
54+
});
55+
56+
describe("createNewRelease", () => {
57+
it("makes a call to Sentry CLI's releases creation command", async () => {
58+
await createNewRelease(
59+
{ release: "1.0.0" } as InternalOptions,
60+
ctx as unknown as BuildContext
61+
);
62+
63+
expect(mockedCLI.releases.new).toHaveBeenCalledWith("1.0.0");
64+
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.create_release");
65+
expect(mockedChildSpan.finish).toHaveBeenCalled();
66+
});
67+
});
68+
69+
describe("cleanArtifacts", () => {
70+
it("doest do anything if cleanArtifacts is not true", async () => {
71+
await cleanArtifacts({} as InternalOptions, ctx as unknown as BuildContext);
72+
73+
expect(mockedCLI.releases.execute).not.toHaveBeenCalled();
74+
expect(mockedAddSpanToTxn).not.toHaveBeenCalled();
75+
expect(mockedChildSpan.finish).not.toHaveBeenCalled();
76+
});
77+
78+
it("makes a call to Sentry CLI's artifact removal command if `cleanArtifacts` is set", async () => {
79+
await cleanArtifacts(
80+
{ release: "1.0.0", cleanArtifacts: true } as InternalOptions,
81+
ctx as unknown as BuildContext
82+
);
83+
84+
expect(mockedCLI.releases.execute).toHaveBeenCalledWith(
85+
["releases", "files", "1.0.0", "delete", "--all"],
86+
true
87+
);
88+
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.clean_artifacts");
89+
expect(mockedChildSpan.finish).toHaveBeenCalled();
90+
});
91+
});
92+
93+
describe("uploadSourceMaps", () => {
94+
it("makes a call to Sentry CLI's sourcemaps upload command", async () => {
95+
const options = {
96+
release: "1.0.0",
97+
include: [{ paths: ["dist"] }],
98+
} as InternalOptions;
99+
100+
await uploadSourceMaps(options, ctx as unknown as BuildContext);
101+
102+
expect(mockedCLI.releases.uploadSourceMaps).toHaveBeenCalledWith("1.0.0", {
103+
include: [{ paths: ["dist"] }],
104+
});
105+
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.upload_sourcemaps");
106+
expect(mockedChildSpan.finish).toHaveBeenCalled();
107+
});
108+
});
109+
41110
describe("setCommits", () => {
42111
it("doesn't do anything if `setCommits` option is not specified", async () => {
43112
await setCommits({} as InternalOptions, ctx as unknown as BuildContext);
44113

45114
expect(mockedCLI.releases.setCommits).not.toHaveBeenCalled();
46-
expect(mockedAddSpanToTxn).toHaveBeenCalled();
47-
expect(mockedChildSpan.finish).toHaveBeenCalled();
115+
expect(mockedAddSpanToTxn).not.toHaveBeenCalled();
116+
expect(mockedChildSpan.finish).not.toHaveBeenCalled();
48117
});
49118

50119
it("makes a call to Sentry CLI if the correct options are specified", async () => {
@@ -54,7 +123,28 @@ describe("Release Pipeline", () => {
54123
);
55124

56125
expect(mockedCLI.releases.setCommits).toHaveBeenCalledWith("1.0.0", { auto: true });
57-
expect(mockedAddSpanToTxn).toHaveBeenCalled();
126+
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.set_commits");
127+
expect(mockedChildSpan.finish).toHaveBeenCalled();
128+
});
129+
});
130+
131+
describe("finalizeRelease", () => {
132+
it("doesn't do anything if `finalize` is not set", async () => {
133+
await finalizeRelease({} as InternalOptions, ctx as unknown as BuildContext);
134+
135+
expect(mockedCLI.releases.finalize).not.toHaveBeenCalled();
136+
expect(mockedAddSpanToTxn).not.toHaveBeenCalled();
137+
expect(mockedChildSpan.finish).not.toHaveBeenCalled();
138+
});
139+
140+
it("makes a call to Sentry CLI's release finalization command if `finalize` is true", async () => {
141+
await finalizeRelease(
142+
{ release: "1.0.0", finalize: true } as InternalOptions,
143+
ctx as unknown as BuildContext
144+
);
145+
146+
expect(mockedCLI.releases.finalize).toHaveBeenCalledWith("1.0.0");
147+
expect(mockedAddSpanToTxn).toHaveBeenCalledWith(ctx, "function.plugin.finalize_release");
58148
expect(mockedChildSpan.finish).toHaveBeenCalled();
59149
});
60150
});
@@ -64,8 +154,8 @@ describe("Release Pipeline", () => {
64154
await addDeploy({} as InternalOptions, ctx as unknown as BuildContext);
65155

66156
expect(mockedCLI.releases.newDeploy).not.toHaveBeenCalled();
67-
expect(mockedAddSpanToTxn).toHaveBeenCalled();
68-
expect(mockedChildSpan.finish).toHaveBeenCalled();
157+
expect(mockedAddSpanToTxn).not.toHaveBeenCalled();
158+
expect(mockedChildSpan.finish).not.toHaveBeenCalled();
69159
});
70160

71161
it("makes a call to Sentry CLI if the correct options are specified", async () => {

0 commit comments

Comments
 (0)