UNPKG

@atomist/automation-client

Version:

Atomist API for software low-level client

111 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ActionResult_1 = require("../../action/ActionResult"); const configuration_1 = require("../../configuration"); const base64_1 = require("../../internal/util/base64"); const httpClient_1 = require("../../spi/http/httpClient"); const logger_1 = require("../../util/logger"); const AbstractRemoteRepoRef_1 = require("./AbstractRemoteRepoRef"); const BasicAuthCredentials_1 = require("./BasicAuthCredentials"); const RepoId_1 = require("./RepoId"); exports.BitBucketDotComBase = "https://bitbucket.org/api/2.0"; class BitBucketRepoRef extends AbstractRemoteRepoRef_1.AbstractRemoteRepoRef { constructor(owner, repo, sha, apiBase = exports.BitBucketDotComBase, path, branch, remote) { super(RepoId_1.ProviderType.bitbucket_cloud, remote || "https://bitbucket.org", apiBase, owner, repo, sha, path, branch); this.apiBase = apiBase; this.kind = "bitbucket"; } createRemote(creds, description, visibility) { const url = `${this.scheme}${this.apiBase}/repositories/${this.owner}/${this.repo}`; logger_1.logger.debug("Making request to BitBucket '%s' to create repo", url); return configuration_1.configurationValue("http.client.factory", httpClient_1.DefaultHttpClientFactory).create(url).exchange(url, { method: httpClient_1.HttpMethod.Post, headers: Object.assign({ "Content-Type": "application/json" }, headers(creds)), body: { scm: "git", is_private: visibility === "private", }, }) .then(response => { return { target: this, success: true, response, }; }) .catch(error => { error.message = `Error attempting to create repository ${JSON.stringify(this)}: ${error.message}`; logger_1.logger.error(error.message); throw error; }); } deleteRemote(creds) { const url = `${this.scheme}${this.apiBase}/repositories/${this.owner}/${this.repo}`; logger_1.logger.debug(`Making request to '${url}' to delete repo`); return configuration_1.configurationValue("http.client.factory", httpClient_1.DefaultHttpClientFactory).create(url).exchange(url, { method: httpClient_1.HttpMethod.Delete, headers: Object.assign({}, headers(creds)), }) .then(response => { return { target: this, success: true, response, }; }) .catch(error => { error.message = `Error attempting to delete repository: ${error.message}`; logger_1.logger.error(error.message); throw error; }); } setUserConfig(credentials, project) { return Promise.resolve(ActionResult_1.successOn(this)); } raisePullRequest(creds, title, body, head, base) { const url = `${this.scheme}${this.apiBase}/repositories/${this.owner}/${this.repo}/pullrequests`; logger_1.logger.debug(`Making request to '${url}' to raise PR`); return configuration_1.configurationValue("http.client.factory", httpClient_1.DefaultHttpClientFactory).create(url).exchange(url, { method: httpClient_1.HttpMethod.Post, headers: Object.assign({ "Content-Type": "application/json" }, headers(creds)), body: { title, description: body, source: { branch: { name: head, }, }, destination: { branch: { name: base, }, }, }, }) .then(response => { return { target: this, success: true, response, }; }) .catch(error => { error.message = `Error attempting to raise PR ${url}: ${error}`; logger_1.logger.error(error.message); throw error; }); } } exports.BitBucketRepoRef = BitBucketRepoRef; function headers(creds) { if (!BasicAuthCredentials_1.isBasicAuthCredentials(creds)) { throw new Error("Only Basic auth supported: Had " + JSON.stringify(creds)); } const upwd = `${creds.username}:${creds.password}`; const encoded = base64_1.encode(upwd); return { Authorization: `Basic ${encoded}`, }; } //# sourceMappingURL=BitBucketRepoRef.js.map