-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathoptions-mapping.ts
More file actions
236 lines (226 loc) · 7.45 KB
/
options-mapping.ts
File metadata and controls
236 lines (226 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { Logger } from "./logger";
import {
Options as UserOptions,
SetCommitsOptions,
RewriteSourcesHook,
ResolveSourceMapHook,
IncludeEntry,
ModuleMetadata,
ModuleMetadataCallback,
} from "./types";
import { determineReleaseName } from "./utils";
export type NormalizedOptions = {
org: string | undefined;
project: string | string[] | undefined;
authToken: string | undefined;
url: string;
headers: Record<string, string> | undefined;
debug: boolean;
silent: boolean;
errorHandler: ((err: Error) => void) | undefined;
telemetry: boolean;
disable: boolean;
sourcemaps:
| {
disable?: boolean | "disable-upload";
assets?: string | string[];
ignore?: string | string[];
rewriteSources?: RewriteSourcesHook;
resolveSourceMap?: ResolveSourceMapHook;
filesToDeleteAfterUpload?: string | string[] | Promise<string | string[] | undefined>;
}
| undefined;
release: {
name: string | undefined;
inject: boolean;
create: boolean;
finalize: boolean;
vcsRemote: string;
setCommits:
| (SetCommitsOptions & {
shouldNotThrowOnFailure?: boolean;
})
| false
| undefined;
dist?: string;
deploy?:
| {
env: string;
started?: number | string;
finished?: number | string;
time?: number;
name?: string;
url?: string;
}
| false;
uploadLegacySourcemaps?: string | IncludeEntry | Array<string | IncludeEntry>;
};
bundleSizeOptimizations:
| {
excludeDebugStatements?: boolean;
excludeTracing?: boolean;
excludeReplayCanvas?: boolean;
excludeReplayShadowDom?: boolean;
excludeReplayIframe?: boolean;
excludeReplayWorker?: boolean;
}
| undefined;
reactComponentAnnotation:
| {
enabled?: boolean;
ignoredComponents?: string[];
}
| undefined;
_metaOptions: {
telemetry: {
metaFramework: string | undefined;
};
};
applicationKey: string | undefined;
moduleMetadata: ModuleMetadata | ModuleMetadataCallback | undefined;
_experiments: {
injectBuildInformation?: boolean;
} & Record<string, unknown>;
};
export const SENTRY_SAAS_URL = "https://sentry.io";
export function normalizeUserOptions(userOptions: UserOptions): NormalizedOptions {
const options = {
org: userOptions.org ?? process.env["SENTRY_ORG"],
project:
userOptions.project ??
(process.env["SENTRY_PROJECT"]?.includes(",")
? process.env["SENTRY_PROJECT"].split(",").map((p) => p.trim())
: process.env["SENTRY_PROJECT"]),
authToken: userOptions.authToken ?? process.env["SENTRY_AUTH_TOKEN"],
url: userOptions.url ?? process.env["SENTRY_URL"] ?? SENTRY_SAAS_URL,
headers: userOptions.headers,
debug: userOptions.debug ?? false,
silent: userOptions.silent ?? false,
errorHandler: userOptions.errorHandler,
telemetry: userOptions.telemetry ?? true,
disable: userOptions.disable ?? false,
sourcemaps: userOptions.sourcemaps,
release: {
...userOptions.release,
name: userOptions.release?.name ?? process.env["SENTRY_RELEASE"] ?? determineReleaseName(),
inject: userOptions.release?.inject ?? true,
create: userOptions.release?.create ?? true,
finalize: userOptions.release?.finalize ?? true,
vcsRemote: userOptions.release?.vcsRemote ?? process.env["SENTRY_VSC_REMOTE"] ?? "origin",
setCommits: userOptions.release?.setCommits as
| (SetCommitsOptions & { shouldNotThrowOnFailure?: boolean })
| false
| undefined,
},
bundleSizeOptimizations: userOptions.bundleSizeOptimizations,
reactComponentAnnotation: userOptions.reactComponentAnnotation,
_metaOptions: {
telemetry: {
metaFramework: userOptions._metaOptions?.telemetry?.metaFramework,
},
},
applicationKey: userOptions.applicationKey,
moduleMetadata: userOptions.moduleMetadata,
_experiments: userOptions._experiments ?? {},
};
if (options.release.setCommits === undefined) {
if (
process.env["VERCEL"] &&
process.env["VERCEL_GIT_COMMIT_SHA"] &&
process.env["VERCEL_GIT_REPO_SLUG"] &&
process.env["VERCEL_GIT_REPO_OWNER"] &&
// We only want to set commits for the production env because Sentry becomes extremely noisy (eg on slack) for
// preview environments because the previous commit is always the "stem" commit of the preview/PR causing Sentry
// to notify you for other people creating PRs.
process.env["VERCEL_TARGET_ENV"] === "production"
) {
options.release.setCommits = {
shouldNotThrowOnFailure: true,
commit: process.env["VERCEL_GIT_COMMIT_SHA"],
previousCommit: process.env["VERCEL_GIT_PREVIOUS_SHA"],
repo: `${process.env["VERCEL_GIT_REPO_OWNER"]}/${process.env["VERCEL_GIT_REPO_SLUG"]}`,
ignoreEmpty: true,
ignoreMissing: true,
};
} else {
options.release.setCommits = {
shouldNotThrowOnFailure: true,
auto: true,
ignoreEmpty: true,
ignoreMissing: true,
};
}
}
if (
options.release.deploy === undefined &&
process.env["VERCEL"] &&
process.env["VERCEL_TARGET_ENV"]
) {
options.release.deploy = {
env: `vercel-${process.env["VERCEL_TARGET_ENV"]}`,
url: process.env["VERCEL_URL"] ? `https://${process.env["VERCEL_URL"]}` : undefined,
};
}
return options;
}
/**
* Validates a few combinations of options that are not checked by Sentry CLI.
*
* For all other options, we can rely on Sentry CLI to validate them. In fact,
* we can't validate them in the plugin because Sentry CLI might pick up options from
* its config file.
*
* @param options the internal options
* @param logger the logger
*
* @returns `true` if the options are valid, `false` otherwise
*/
export function validateOptions(options: NormalizedOptions, logger: Logger): boolean {
const setCommits = options.release?.setCommits;
if (setCommits) {
if (!setCommits.auto && !(setCommits.repo && setCommits.commit)) {
logger.error(
"The `setCommits` option was specified but is missing required properties.",
"Please set either `auto` or both, `repo` and `commit`."
);
return false;
}
if (setCommits.auto && setCommits.repo && setCommits) {
logger.warn(
"The `setCommits` options includes `auto` but also `repo` and `commit`.",
"Ignoring `repo` and `commit`.",
"Please only set either `auto` or both, `repo` and `commit`."
);
}
}
if (
options.release?.deploy &&
typeof options.release.deploy === "object" &&
!options.release.deploy.env
) {
logger.error(
"The `deploy` option was specified but is missing the required `env` property.",
"Please set the `env` property."
);
return false;
}
if (options.project && Array.isArray(options.project)) {
if (options.project.length === 0) {
logger.error(
"The `project` option was specified as an array but is empty.",
"Please provide at least one project slug."
);
return false;
}
// Check each project is a non-empty string
const invalidProjects = options.project.filter((p) => typeof p !== "string" || p.trim() === "");
if (invalidProjects.length > 0) {
logger.error(
"The `project` option contains invalid project slugs.",
"All projects must be non-empty strings."
);
return false;
}
}
return true;
}