@atomist/automation-client
Version:
Atomist API for software low-level client
176 lines • 6.45 kB
JavaScript
"use strict";
/*
* Copyright © 2018 Atomist, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const ActionResult_1 = require("../../action/ActionResult");
const base64_1 = require("../../internal/util/base64");
const logger_1 = require("../../internal/util/logger");
const spawned_1 = require("../../util/spawned");
const AbstractRemoteRepoRef_1 = require("./AbstractRemoteRepoRef");
const BasicAuthCredentials_1 = require("./BasicAuthCredentials");
const RepoId_1 = require("./RepoId");
/**
* RemoteRepoRef implementation for BitBucket server (not BitBucket Cloud)
*/
class BitBucketServerRepoRef extends AbstractRemoteRepoRef_1.AbstractRemoteRepoRef {
/**
* Construct a new BitBucketServerRepoRef
* @param {string} remoteBase remote base, including scheme
* @param {string} owner
* @param {string} repo
* @param {boolean} isProject
* @param {string} sha
* @param {string} path
*/
constructor(remoteBase, owner, repo, isProject = true, sha = "master", path) {
super(RepoId_1.ProviderType.bitbucket, remoteBase, noTrailingSlash(remoteBase) + "/rest/api/1.0/", owner, repo, sha, path);
this.isProject = isProject;
this.httpStrategy = process.env.ATOMIST_CURL_FOR_BITBUCKET ? CurlHttpStrategy : AxiosHttpStrategy;
this.ownerType = isProject ? "projects" : "users";
logger_1.logger.info("Constructed BitBucketServerRepoRef: %j", this);
}
createRemote(creds, description, visibility) {
const url = `${this.scheme}${this.apiBase}/${this.apiBasePathComponent}`;
const data = {
name: this.repo,
scmId: "git",
forkable: "true",
};
logger_1.logger.info("Making request to BitBucket '%s' to create repo, data=%j", url, data);
return this.httpStrategy.doPost(this, creds, url, data).catch(error => {
logger_1.logger.error("Error attempting to create repository %j: %s", this, error);
return Promise.reject(error);
});
}
deleteRemote(creds) {
const url = `${this.scheme}${this.apiBase}/${this.apiPathComponent}`;
logger_1.logger.debug(`Making request to '${url}' to delete repo`);
return this.httpStrategy.doDelete(this, creds, url).catch(err => {
logger_1.logger.error(`Error attempting to delete repository: ${err}`);
return Promise.reject(err);
});
}
setUserConfig(credentials, project) {
return Promise.resolve(ActionResult_1.successOn(this));
}
raisePullRequest(credentials, title, body, head, base) {
const url = `${this.scheme}${this.apiBase}/${this.apiPathComponent}/pull-requests`;
logger_1.logger.debug(`Making request to '${url}' to raise PR`);
return this.httpStrategy.doPost(this, credentials, url, {
title,
description: body,
fromRef: {
id: head,
},
toRef: {
id: base,
},
}).catch(err => {
logger_1.logger.error(`Error attempting to raise PR. url: ${url} ${err}`);
return Promise.reject(err);
});
}
get url() {
let url = `projects/${this.owner}/repos`;
if (!this.isProject) {
url = `users/${this.owner}/repos`;
}
return `${this.scheme}${this.remoteBase}/${url}/${this.repo}`;
}
get pathComponent() {
return `scm/${this.maybeTilde}${this.owner}/${this.repo}`;
}
get maybeTilde() {
return this.isProject ? "" : "~";
}
get apiBasePathComponent() {
return `projects/${this.maybeTilde}${this.owner}/repos/`;
}
get apiPathComponent() {
return this.apiBasePathComponent + this.repo;
}
}
exports.BitBucketServerRepoRef = BitBucketServerRepoRef;
const AxiosHttpStrategy = {
doPost(target, creds, url, data) {
return axios_1.default.post(url, data, headers(creds))
.then(fullResponse => {
return {
target,
success: true,
fullResponse,
};
});
},
doDelete(target, creds, url) {
return axios_1.default.delete(url, headers(creds))
.then(fullResponse => {
return {
target,
success: true,
fullResponse,
};
});
},
};
const CurlHttpStrategy = {
doPost(target, creds, url, data) {
const passthroughLog = {
write(str) {
logger_1.logger.info(str);
},
};
return spawned_1.spawnAndWatch({
command: "curl", args: [
"-u", usernameColonPassword(creds),
"-X", "POST",
"-H", "Content-Type: application/json",
"-d", JSON.stringify(data),
url,
],
}, {}, passthroughLog)
.then(fullResponse => {
return {
target,
success: true,
fullResponse,
};
});
},
doDelete(target, creds, url) {
throw new Error("Not implemented");
},
};
function usernameColonPassword(creds) {
if (!BasicAuthCredentials_1.isBasicAuthCredentials(creds)) {
throw new Error("Only Basic auth supported: Had " + JSON.stringify(creds));
}
return `${creds.username}:${creds.password}`;
}
function headers(creds) {
const encoded = base64_1.encode(usernameColonPassword(creds));
return {
headers: {
Authorization: `Basic ${encoded}`,
},
};
}
function noTrailingSlash(s) {
return s.replace(/\/$/, "");
}
//# sourceMappingURL=BitBucketServerRepoRef.js.map