15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitbucketPublisher = void 0;
const builder_util_1 = require("builder-util");
const nodeHttpExecutor_1 = require("builder-util/out/nodeHttpExecutor");
const electron_publish_1 = require("electron-publish");
const builder_util_runtime_1 = require("builder-util-runtime");
const FormData = require("form-data");
const fs_extra_1 = require("fs-extra");
class BitbucketPublisher extends electron_publish_1.HttpPublisher {
    constructor(context, info) {
        super(context);
        this.providerName = "bitbucket";
        this.hostname = "api.bitbucket.org";
        const token = info.token || process.env.BITBUCKET_TOKEN || null;
        const username = info.username || process.env.BITBUCKET_USERNAME || null;
        if (builder_util_1.isEmptyOrSpaces(token)) {
            throw new builder_util_1.InvalidConfigurationError(`Bitbucket token is not set using env "BITBUCKET_TOKEN" (see https://www.electron.build/configuration/publish#BitbucketOptions)`);
        }
        if (builder_util_1.isEmptyOrSpaces(username)) {
            builder_util_1.log.warn('No Bitbucket username provided via "BITBUCKET_USERNAME". Defaulting to use repo owner.');
        }
        this.info = info;
        this.auth = BitbucketPublisher.convertAppPassword(username !== null && username !== void 0 ? username : this.info.owner, token);
        this.basePath = `/2.0/repositories/${this.info.owner}/${this.info.slug}/downloads`;
    }
    doUpload(fileName, _arch, _dataLength, _requestProcessor, file) {
        return builder_util_runtime_1.HttpExecutor.retryOnServerError(async () => {
            const fileContent = await fs_extra_1.readFile(file);
            const form = new FormData();
            form.append("files", fileContent, fileName);
            const upload = {
                hostname: this.hostname,
                path: this.basePath,
                headers: form.getHeaders(),
                timeout: this.info.timeout || undefined,
            };
            await nodeHttpExecutor_1.httpExecutor.doApiRequest(builder_util_runtime_1.configureRequestOptions(upload, this.auth, "POST"), this.context.cancellationToken, it => form.pipe(it));
            return fileName;
        });
    }
    async deleteRelease(filename) {
        const req = {
            hostname: this.hostname,
            path: `${this.basePath}/${filename}`,
            timeout: this.info.timeout || undefined,
        };
        await nodeHttpExecutor_1.httpExecutor.request(builder_util_runtime_1.configureRequestOptions(req, this.auth, "DELETE"), this.context.cancellationToken);
    }
    toString() {
        const { owner, slug, channel } = this.info;
        return `Bitbucket (owner: ${owner}, slug: ${slug}, channel: ${channel})`;
    }
    static convertAppPassword(username, token) {
        const base64encodedData = Buffer.from(`${username}:${token.trim()}`).toString("base64");
        return `Basic ${base64encodedData}`;
    }
}
exports.BitbucketPublisher = BitbucketPublisher;
//# sourceMappingURL=BitbucketPublisher.js.map