Skip to content

Commit 8a7fa93

Browse files
authored
feat(core): Add deploy command (#97)
Add the implementation of the `deploy` option and its step in the release pipeline. The implementation is mostly taken from the [webpack plugin](https://github.com/getsentry/sentry-webpack-plugin/blob/137503f3ac6fe423b16c5c50379859c86e689017/src/index.js#L524-L536) and relies on Sentry CLI. Add tests for deploy
1 parent 4e7802f commit 8a7fa93

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

packages/bundler-plugin-core/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,8 @@ const unplugin = createUnplugin<Options>((options, unpluginMetaContext) => {
289289
.then(() => uploadSourceMaps(internalOptions, ctx))
290290
.then(() => setCommits(internalOptions, ctx))
291291
.then(() => finalizeRelease(internalOptions, ctx))
292-
.then(() => addDeploy(ctx)) // this is a noop for now
293-
.then(() => {
294-
transaction?.setStatus("ok");
295-
})
292+
.then(() => addDeploy(internalOptions, ctx))
293+
.then(() => transaction?.setStatus("ok"))
296294
.catch((e: Error) => {
297295
captureMinimalError(e, sentryHub);
298296
transaction?.setStatus("cancelled");

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,29 @@ export async function setCommits(options: InternalOptions, ctx: BuildContext): P
127127
span?.finish();
128128
}
129129

130-
export async function addDeploy(
131-
/* version: string, */
132-
ctx: BuildContext
133-
): Promise<string> {
134-
const span = addSpanToTransaction(ctx, "function.plugin.add_deploy");
130+
export async function addDeploy(options: InternalOptions, ctx: BuildContext): Promise<void> {
131+
const span = addSpanToTransaction(ctx, "function.plugin.deploy");
132+
133+
if (options.deploy) {
134+
const { env, started, finished, time, name, url } = options.deploy;
135+
136+
if (env) {
137+
await ctx.cli.releases.newDeploy(options.release, {
138+
env,
139+
started,
140+
finished,
141+
time,
142+
name,
143+
url,
144+
});
145+
ctx.logger.info("Successfully added deploy.");
146+
} else {
147+
ctx.logger.error(
148+
"Couldn't add deploy - the `env` option was not specified!",
149+
"Make sure to set `deploy.env` (e.g. to 'production')."
150+
);
151+
}
152+
}
135153

136154
span?.finish();
137-
return Promise.resolve("Noop");
138155
}

packages/bundler-plugin-core/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ type DeployOptions = {
326326
/**
327327
* Deployment start time in Unix timestamp (in seconds) or ISO 8601 format.
328328
*/
329-
started?: number;
329+
started?: number | string;
330330

331331
/**
332332
* Deployment finish time in Unix timestamp (in seconds) or ISO 8601 format.
333333
*/
334-
finished?: number;
334+
finished?: number | string;
335335

336336
/**
337337
* Deployment duration (in seconds). Can be used instead of started and finished.

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { InternalOptions } from "../src/options-mapping";
2-
import { setCommits } from "../src/sentry/releasePipeline";
2+
import { addDeploy, setCommits } from "../src/sentry/releasePipeline";
33
import { BuildContext } from "../src/types";
44

55
const mockedAddSpanToTxn = jest.fn();
@@ -29,6 +29,7 @@ describe("Release Pipeline", () => {
2929
const mockedCLI = {
3030
releases: {
3131
setCommits: jest.fn(),
32+
newDeploy: jest.fn(),
3233
},
3334
};
3435

@@ -38,7 +39,7 @@ describe("Release Pipeline", () => {
3839
const ctx = { cli: mockedCLI, logger: mockedLogger };
3940

4041
describe("setCommits", () => {
41-
it("doesn't do anything if setCommits option is not specified", async () => {
42+
it("doesn't do anything if `setCommits` option is not specified", async () => {
4243
await setCommits({} as InternalOptions, ctx as unknown as BuildContext);
4344

4445
expect(mockedCLI.releases.setCommits).not.toHaveBeenCalled();
@@ -68,4 +69,44 @@ describe("Release Pipeline", () => {
6869
expect(mockedChildSpan.finish).toHaveBeenCalled();
6970
});
7071
});
72+
73+
describe("addDeploy", () => {
74+
it("doesn't do anything if `deploy` option is not specified", async () => {
75+
await addDeploy({} as InternalOptions, ctx as unknown as BuildContext);
76+
77+
expect(mockedCLI.releases.newDeploy).not.toHaveBeenCalled();
78+
expect(mockedAddSpanToTxn).toHaveBeenCalled();
79+
expect(mockedChildSpan.finish).toHaveBeenCalled();
80+
});
81+
82+
it("logs an error and does nothing if `env` isn't specified", async () => {
83+
await addDeploy({ deploy: {} } as InternalOptions, ctx as unknown as BuildContext);
84+
expect(mockedCLI.releases.newDeploy).not.toHaveBeenCalled();
85+
expect(mockedLogger.error).toHaveBeenLastCalledWith(
86+
expect.stringMatching(/Couldn't add deploy.*env/),
87+
expect.stringMatching(/env/)
88+
);
89+
expect(mockedAddSpanToTxn).toHaveBeenCalled();
90+
expect(mockedChildSpan.finish).toHaveBeenCalled();
91+
});
92+
93+
it("makes a call to Sentry CLI if the correct options are specified", async () => {
94+
const deployOptions = {
95+
env: "production",
96+
started: 0,
97+
finished: 10,
98+
name: "myDeployment",
99+
url: "https://my-deploy-server.com",
100+
};
101+
102+
await addDeploy(
103+
{ deploy: deployOptions, release: "1.0.0" } as InternalOptions,
104+
ctx as unknown as BuildContext
105+
);
106+
107+
expect(mockedCLI.releases.newDeploy).toHaveBeenCalledWith("1.0.0", deployOptions);
108+
expect(mockedAddSpanToTxn).toHaveBeenCalled();
109+
expect(mockedChildSpan.finish).toHaveBeenCalled();
110+
});
111+
});
71112
});

packages/playground/vite.config.smallNodeApp.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export default defineConfig({
3131
auto: true,
3232
ignoreMissing: true,
3333
},
34+
deploy: {
35+
env: "myEnv",
36+
time: 10,
37+
},
3438
}),
3539
],
3640
});

0 commit comments

Comments
 (0)