-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathrelease-management.ts
More file actions
110 lines (101 loc) · 3.77 KB
/
release-management.ts
File metadata and controls
110 lines (101 loc) · 3.77 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
import SentryCli, { SentryCliCommitsOptions, SentryCliNewDeployOptions } from "@sentry/cli";
import { Scope } from "@sentry/core";
import { UnpluginOptions } from "unplugin";
import { Logger } from "../sentry/logger";
import { safeFlushTelemetry } from "../sentry/telemetry";
import { HandleRecoverableErrorFn, IncludeEntry } from "../types";
import { arrayify } from "../utils";
import { Client } from "@sentry/types";
interface ReleaseManagementPluginOptions {
logger: Logger;
releaseName: string;
shouldCreateRelease: boolean;
shouldFinalizeRelease: boolean;
include?: string | IncludeEntry | Array<string | IncludeEntry>;
setCommitsOption?: SentryCliCommitsOptions;
deployOptions?: SentryCliNewDeployOptions;
dist?: string;
handleRecoverableError: HandleRecoverableErrorFn;
sentryScope: Scope;
sentryClient: Client;
sentryCliOptions: {
url: string;
authToken: string;
org?: string;
project: string;
vcsRemote: string;
silent: boolean;
headers?: Record<string, string>;
};
createDependencyOnSourcemapFiles: () => () => void;
}
/**
* Creates a plugin that creates releases, sets commits, deploys and finalizes releases.
*
* Additionally, if legacy upload options are set, it uploads source maps in the legacy (non-debugId) way.
*/
export function releaseManagementPlugin({
releaseName,
include,
dist,
setCommitsOption,
shouldCreateRelease,
shouldFinalizeRelease,
deployOptions,
handleRecoverableError,
sentryScope,
sentryClient,
sentryCliOptions,
createDependencyOnSourcemapFiles,
}: ReleaseManagementPluginOptions): UnpluginOptions {
const freeGlobalDependencyOnSourcemapFiles = createDependencyOnSourcemapFiles();
return {
name: "sentry-release-management-plugin",
async writeBundle() {
// It is possible that this writeBundle hook is called multiple times in one build (for example when reusing the plugin, or when using build tooling like `@vitejs/plugin-legacy`)
// Therefore we need to actually register the execution of this hook as dependency on the sourcemap files.
const freeWriteBundleInvocationDependencyOnSourcemapFiles =
createDependencyOnSourcemapFiles();
try {
const cliInstance = new SentryCli(null, sentryCliOptions);
if (shouldCreateRelease) {
await cliInstance.releases.new(releaseName);
}
if (include) {
const normalizedInclude = arrayify(include)
.map((includeItem) =>
typeof includeItem === "string" ? { paths: [includeItem] } : includeItem
)
.map((includeEntry) => ({
...includeEntry,
validate: includeEntry.validate ?? false,
ext: includeEntry.ext
? includeEntry.ext.map((extension) => `.${extension.replace(/^\./, "")}`)
: [".js", ".map", ".jsbundle", ".bundle"],
ignore: includeEntry.ignore ? arrayify(includeEntry.ignore) : undefined,
}));
await cliInstance.releases.uploadSourceMaps(releaseName, {
include: normalizedInclude,
dist,
});
}
if (setCommitsOption) {
await cliInstance.releases.setCommits(releaseName, setCommitsOption);
}
if (shouldFinalizeRelease) {
await cliInstance.releases.finalize(releaseName);
}
if (deployOptions) {
await cliInstance.releases.newDeploy(releaseName, deployOptions);
}
} catch (e) {
sentryScope.captureException('Error in "releaseManagementPlugin" writeBundle hook');
await safeFlushTelemetry(sentryClient);
handleRecoverableError(e, false);
} finally {
freeGlobalDependencyOnSourcemapFiles();
freeWriteBundleInvocationDependencyOnSourcemapFiles();
}
},
};
}