@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
144 lines • 6.23 kB
JavaScript
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const sdm_1 = require("@atomist/sdm");
const GitHubApi = require("@octokit/rest");
// tslint:disable-next-line:import-blacklist
const axios_1 = require("axios");
const fs = require("fs-extra");
const p = require("path");
const tmp = require("tmp-promise");
const ghub_1 = require("../../../util/github/ghub");
/* tslint:disable:deprecation */
/**
* Implement ArtifactStore interface to store artifacts as GitHub releases
* @deprecated Artifact storage should be done using project listeners
*/
class GitHubReleaseArtifactStore {
storeFile(appInfo, localFile, creds) {
return __awaiter(this, void 0, void 0, function* () {
const token = sdm_1.toToken(creds);
const tagName = appInfo.version + new Date().getMilliseconds();
const tag = {
tag: tagName,
message: appInfo.version + " for release",
object: appInfo.id.sha,
type: "commit",
tagger: {
name: "Atomist",
email: "info@atomist.com",
date: new Date().toISOString(),
},
};
const grr = appInfo.id;
yield ghub_1.createTag(token, grr, tag);
const release = {
name: appInfo.version,
tag_name: tag.tag,
};
yield ghub_1.createRelease(token, grr, release);
const asset = yield uploadAsset(token, grr.owner, grr.repo, tag.tag, localFile);
automation_client_1.logger.info("Uploaded artifact with url [%s] for %j", asset.browser_download_url, appInfo);
return asset.browser_download_url;
});
}
// TODO this is Maven specific
// Name is of format fintan-0.1.0-SNAPSHOT.jar
checkout(url, id, creds) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.info("Attempting to download artifact [%s] for %j", url, id);
const tmpDir = yield tmp.dir({ prefix: "GitHubReleaseArtifactStore-", unsafeCleanup: true });
const cwd = tmpDir.path;
const lastSlash = url.lastIndexOf("/");
const filename = url.substring(lastSlash + 1);
const re = /([a-zA-Z0-9_]+)-(.*)/;
const match = re.exec(filename);
const name = match[1];
const version = match[2].replace(/.jar$/, "");
const outputPath = cwd + "/" + filename;
automation_client_1.logger.info("Attempting to download url %s to %s", url, outputPath);
yield downloadFileAs(creds, url, outputPath);
automation_client_1.logger.info("Successfully downloaded url %s to %s", url, outputPath);
return {
cwd,
filename,
name,
version,
id,
};
});
}
}
exports.GitHubReleaseArtifactStore = GitHubReleaseArtifactStore;
/**
* Download the file to local disk. This only works on public repos, see
* https://stackoverflow.com/questions/20396329/how-to-download-github-release-from-private-repo-using-command-line
* @param creds ignored
* @param {string} url release asset URL from public repo
* @param {string} outputFilename
* @return {Promise<any>}
*/
function downloadFileAs(creds, url, outputFilename) {
return automation_client_1.doWithRetry(() => axios_1.default.get(url, {
headers: { Accept: "application/octet-stream" },
responseType: "arraybuffer",
}), `Download ${url} to ${outputFilename}`, {
minTimeout: 1000,
maxTimeout: 10000,
retries: 10,
})
.then(result => {
return fs.writeFile(outputFilename, result.data);
});
}
function uploadAsset(token, owner, repo, tag, path, contentType = "application/zip") {
return __awaiter(this, void 0, void 0, function* () {
const github = githubApi(token);
const result = yield github.repos.getReleaseByTag({ owner, repo, tag });
const file = (yield fs.readFile(path)).buffer;
const contentLength = (yield fs.stat(path)).size;
const r = yield github.repos.uploadReleaseAsset({
url: result.data.upload_url,
file,
headers: {
"content-length": contentLength,
"content-type": contentType,
},
name: p.basename(path),
});
return r.data;
});
}
exports.uploadAsset = uploadAsset;
function githubApi(token, apiUrl = "https://api.github.com/") {
return new GitHubApi({
auth: !!token ? `token ${token}` : undefined,
baseUrl: apiUrl.endsWith("/") ? apiUrl.slice(0, -1) : apiUrl,
});
}
exports.githubApi = githubApi;
//# sourceMappingURL=GitHubReleaseArtifactStore.js.map
;