Skip to content

Commit d55ca96

Browse files
author
Luca Forstner
authored
feat: Detect Vercel commits and env (#694)
1 parent 186a0d5 commit d55ca96

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

packages/bundler-plugin-core/src/options-mapping.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Logger } from "./sentry/logger";
2-
import { Options as UserOptions } from "./types";
2+
import { Options as UserOptions, SetCommitsOptions } from "./types";
33
import { determineReleaseName } from "./utils";
44

55
export type NormalizedOptions = ReturnType<typeof normalizeUserOptions>;
@@ -26,7 +26,10 @@ export function normalizeUserOptions(userOptions: UserOptions) {
2626
create: userOptions.release?.create ?? true,
2727
finalize: userOptions.release?.finalize ?? true,
2828
vcsRemote: userOptions.release?.vcsRemote ?? process.env["SENTRY_VSC_REMOTE"] ?? "origin",
29-
setCommits: userOptions.release?.setCommits,
29+
setCommits: userOptions.release?.setCommits as
30+
| (SetCommitsOptions & { shouldNotThrowOnFailure?: boolean })
31+
| false
32+
| undefined,
3033
},
3134
bundleSizeOptimizations: userOptions.bundleSizeOptimizations,
3235
reactComponentAnnotation: userOptions.reactComponentAnnotation,
@@ -41,10 +44,38 @@ export function normalizeUserOptions(userOptions: UserOptions) {
4144
};
4245

4346
if (options.release.setCommits === undefined) {
44-
options.release.setCommits = {
45-
// @ts-expect-error This is fine
46-
auto: true,
47-
isDefault: true,
47+
if (
48+
process.env["VERCEL"] &&
49+
process.env["VERCEL_GIT_COMMIT_SHA"] &&
50+
process.env["VERCEL_GIT_REPO_SLUG"] &&
51+
process.env["VERCEL_GIT_REPO_OWNER"]
52+
) {
53+
options.release.setCommits = {
54+
shouldNotThrowOnFailure: true,
55+
commit: process.env["VERCEL_GIT_COMMIT_SHA"],
56+
previousCommit: process.env["VERCEL_GIT_PREVIOUS_SHA"],
57+
repo: `${process.env["VERCEL_GIT_REPO_OWNER"]}/${process.env["VERCEL_GIT_REPO_SLUG"]}`,
58+
ignoreEmpty: true,
59+
ignoreMissing: true,
60+
};
61+
} else {
62+
options.release.setCommits = {
63+
shouldNotThrowOnFailure: true,
64+
auto: true,
65+
ignoreEmpty: true,
66+
ignoreMissing: true,
67+
};
68+
}
69+
}
70+
71+
if (
72+
options.release.deploy === undefined &&
73+
process.env["VERCEL"] &&
74+
process.env["VERCEL_TARGET_ENV"]
75+
) {
76+
options.release.deploy = {
77+
env: `vercel-${process.env["VERCEL_TARGET_ENV"]}`,
78+
url: process.env["VERCEL_URL"] ? `https://${process.env["VERCEL_URL"]}` : undefined,
4879
};
4980
}
5081

packages/bundler-plugin-core/src/plugins/release-management.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ export function releaseManagementPlugin({
9191
try {
9292
await cliInstance.releases.setCommits(releaseName, setCommitsOption);
9393
} catch (e) {
94-
// isDefault being present means that the plugin defaulted to `{ auto: true }` for the setCommitsOptions, meaning that wee should not throw when CLI throws because there is no repo
95-
if (!("isDefault" in setCommitsOption)) {
96-
throw e;
97-
} else {
94+
// shouldNotThrowOnFailure being present means that the plugin defaulted to `{ auto: true }` for the setCommitsOptions, meaning that wee should not throw when CLI throws because there is no repo
95+
if (
96+
"shouldNotThrowOnFailure" in setCommitsOption &&
97+
setCommitsOption.shouldNotThrowOnFailure
98+
) {
9899
logger.debug(
99-
"An error occurred setting commits on release (this message can be ignored unless your commits on release are desired):",
100+
"An error occurred setting commits on release (this message can be ignored unless you commits on release are desired):",
100101
e
101102
);
103+
} else {
104+
throw e;
102105
}
103106
}
104107
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ export interface SentrySDKBuildFlags extends Record<string, boolean | undefined>
460460
__SENTRY_EXCLUDE_REPLAY_WORKER__?: boolean;
461461
}
462462

463-
type SetCommitsOptions = (AutoSetCommitsOptions | ManualSetCommitsOptions) & {
463+
export type SetCommitsOptions = (AutoSetCommitsOptions | ManualSetCommitsOptions) & {
464464
/**
465465
* The commit before the beginning of this release (in other words,
466466
* the last commit of the previous release).

packages/bundler-plugin-core/test/option-mappings.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ describe("normalizeUserOptions()", () => {
2525
uploadLegacySourcemaps: "./out",
2626
setCommits: {
2727
auto: true,
28-
isDefault: true,
28+
shouldNotThrowOnFailure: true,
29+
ignoreEmpty: true,
30+
ignoreMissing: true,
2931
},
3032
},
3133
silent: false,
@@ -80,7 +82,9 @@ describe("normalizeUserOptions()", () => {
8082
},
8183
setCommits: {
8284
auto: true,
83-
isDefault: true,
85+
shouldNotThrowOnFailure: true,
86+
ignoreEmpty: true,
87+
ignoreMissing: true,
8488
},
8589
},
8690
silent: false,

0 commit comments

Comments
 (0)