@atomist/automation-client
Version:
Atomist API for software low-level client
93 lines • 4.37 kB
JavaScript
;
/*
* Copyright © 2019 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 BasicAuthCredentials_1 = require("./BasicAuthCredentials");
const GitlabPrivateTokenCredentials_1 = require("./GitlabPrivateTokenCredentials");
const ProjectOperationCredentials_1 = require("./ProjectOperationCredentials");
/**
* Superclass for RemoteRepoRef implementations.
* Handles parsing remote base
*
* This should ultimately move down to automation-client-ts and replace AbstractRepoRef.
*/
class AbstractRemoteRepoRef {
/**
* Construct a new RemoteRepoRef
* @param {ProviderType} providerType
* @param {string} rawRemote remote url, like for cloning or linking into the repo. Should start with a scheme.
* May have a trailing slash, which will be stripped
* @param rawApiBase API base url. Should start with a scheme. May have a trailing slash, which will be stripped.
* @param {string} owner
* @param {string} repo
* @param {string} sha
* @param {string} path
*/
constructor(providerType, rawRemote, rawApiBase, owner, repo, sha, path, branch) {
this.providerType = providerType;
this.owner = owner;
this.repo = repo;
this.sha = sha;
this.path = path;
this.branch = branch;
const [remoteScheme, remoteBase] = splitSchemeFromUrl(rawRemote);
const [apiScheme, apiBase] = splitSchemeFromUrl(rawApiBase);
if (apiScheme !== remoteScheme) { // that's confusing, don't handle it
throw new Error(`Scheme is different between remoteBase ${rawRemote} and apiBase ${rawApiBase}`);
}
this.apiBase = apiBase;
this.scheme = remoteScheme;
this.remoteBase = remoteBase;
}
get url() {
return `${this.scheme}${this.remoteBase}/${this.owner}/${this.repo}`;
}
cloneUrl(creds) {
if (!!creds && BasicAuthCredentials_1.isBasicAuthCredentials(creds)) {
return `${this.scheme}${encodeURIComponent(creds.username)}:${encodeURIComponent(creds.password)}@` +
`${this.remoteBase}/${this.pathComponent}.git`;
}
if (!!creds && GitlabPrivateTokenCredentials_1.isGitlabPrivateTokenCredentials(creds)) {
return `${this.scheme}gitlab-ci-token:${creds.privateToken}@` +
`${this.remoteBase}/${this.pathComponent}.git`;
}
if (!!creds && ProjectOperationCredentials_1.isTokenCredentials(creds)) {
const token = creds.token;
// GitHub App tokens start with v1. and are expected in the password field
if (!!token && token.startsWith("v1.")) {
return `${this.scheme}atomist:${token}@${this.remoteBase}/${this.pathComponent}.git`;
}
else {
return `${this.scheme}${token}:x-oauth-basic@${this.remoteBase}/${this.pathComponent}.git`;
}
}
return `${this.scheme}${this.remoteBase}/${this.pathComponent}.git`;
}
get pathComponent() {
return this.owner + "/" + this.repo;
}
}
exports.AbstractRemoteRepoRef = AbstractRemoteRepoRef;
function splitSchemeFromUrl(urlWithSchemeAndPossibleTrailingSlash) {
if (!urlWithSchemeAndPossibleTrailingSlash.startsWith("http")) {
throw new Error(`This URL needs to start with http or https: '${urlWithSchemeAndPossibleTrailingSlash}'`);
}
const scheme = urlWithSchemeAndPossibleTrailingSlash.startsWith("http://") ? "http://" : "https://";
const base = urlWithSchemeAndPossibleTrailingSlash.substr(scheme.length).replace(/\/$/, "");
return [scheme, base];
}
//# sourceMappingURL=AbstractRemoteRepoRef.js.map