1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.BitbucketPublisher = void 0;
|
4 | const builder_util_1 = require("builder-util");
|
5 | const nodeHttpExecutor_1 = require("builder-util/out/nodeHttpExecutor");
|
6 | const electron_publish_1 = require("electron-publish");
|
7 | const builder_util_runtime_1 = require("builder-util-runtime");
|
8 | const FormData = require("form-data");
|
9 | const fs_extra_1 = require("fs-extra");
|
10 | class BitbucketPublisher extends electron_publish_1.HttpPublisher {
|
11 | constructor(context, info) {
|
12 | super(context);
|
13 | this.providerName = "bitbucket";
|
14 | this.hostname = "api.bitbucket.org";
|
15 | const token = info.token || process.env.BITBUCKET_TOKEN || null;
|
16 | const username = info.username || process.env.BITBUCKET_USERNAME || null;
|
17 | if ((0, builder_util_1.isEmptyOrSpaces)(token)) {
|
18 | throw new builder_util_1.InvalidConfigurationError(`Bitbucket token is not set using env "BITBUCKET_TOKEN" (see https://www.electron.build/configuration/publish#BitbucketOptions)`);
|
19 | }
|
20 | if ((0, builder_util_1.isEmptyOrSpaces)(username)) {
|
21 | builder_util_1.log.warn('No Bitbucket username provided via "BITBUCKET_USERNAME". Defaulting to use repo owner.');
|
22 | }
|
23 | this.info = info;
|
24 | this.auth = BitbucketPublisher.convertAppPassword(username !== null && username !== void 0 ? username : this.info.owner, token);
|
25 | this.basePath = `/2.0/repositories/${this.info.owner}/${this.info.slug}/downloads`;
|
26 | }
|
27 | doUpload(fileName, _arch, _dataLength, _requestProcessor, file) {
|
28 | return builder_util_runtime_1.HttpExecutor.retryOnServerError(async () => {
|
29 | const fileContent = await (0, fs_extra_1.readFile)(file);
|
30 | const form = new FormData();
|
31 | form.append("files", fileContent, fileName);
|
32 | const upload = {
|
33 | hostname: this.hostname,
|
34 | path: this.basePath,
|
35 | headers: form.getHeaders(),
|
36 | timeout: this.info.timeout || undefined,
|
37 | };
|
38 | await nodeHttpExecutor_1.httpExecutor.doApiRequest((0, builder_util_runtime_1.configureRequestOptions)(upload, this.auth, "POST"), this.context.cancellationToken, it => form.pipe(it));
|
39 | return fileName;
|
40 | });
|
41 | }
|
42 | async deleteRelease(filename) {
|
43 | const req = {
|
44 | hostname: this.hostname,
|
45 | path: `${this.basePath}/${filename}`,
|
46 | timeout: this.info.timeout || undefined,
|
47 | };
|
48 | await nodeHttpExecutor_1.httpExecutor.request((0, builder_util_runtime_1.configureRequestOptions)(req, this.auth, "DELETE"), this.context.cancellationToken);
|
49 | }
|
50 | toString() {
|
51 | const { owner, slug, channel } = this.info;
|
52 | return `Bitbucket (owner: ${owner}, slug: ${slug}, channel: ${channel})`;
|
53 | }
|
54 | static convertAppPassword(username, token) {
|
55 | const base64encodedData = Buffer.from(`${username}:${token.trim()}`).toString("base64");
|
56 | return `Basic ${base64encodedData}`;
|
57 | }
|
58 | }
|
59 | exports.BitbucketPublisher = BitbucketPublisher;
|
60 |
|
\ | No newline at end of file |