UNPKG

deliverybot

Version:

Controls and configures deployments using the GitHub API.

208 lines 10.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var types_1 = require("./types"); var js_yaml_1 = __importDefault(require("js-yaml")); var schema_json_1 = __importDefault(require("./schema.json")); var jsonschema_1 = require("jsonschema"); var render_1 = require("./render"); var util_1 = require("./util"); function config(github, _a) { var owner = _a.owner, repo = _a.repo, ref = _a.ref; return __awaiter(this, void 0, void 0, function () { var params, content, conf, fields, _loop_1, key, validation, err, key; return __generator(this, function (_b) { switch (_b.label) { case 0: params = { owner: owner, repo: repo, path: ".github/deploy.yml" }; if (ref) params.ref = ref; return [4 /*yield*/, github.repos.getContents(params)]; case 1: content = _b.sent(); if (Array.isArray(content.data)) { throw new types_1.ConfigError(".github/deploy.yml is a folder"); } if (!content.data.content) { throw new types_1.ConfigError("content not found"); } conf = js_yaml_1.default.safeLoad(Buffer.from(content.data.content, "base64").toString()) || {}; fields = [ "task", "auto_merge", "payload", "environment", "description" ]; _loop_1 = function (key) { if (conf[key].deployments && conf[key].deployments.length > 0) { var dep_1 = conf[key].deployments[0]; var tar_1 = conf[key]; fields.forEach(function (field) { tar_1[field] = tar_1[field] || dep_1[field]; }); delete conf[key].deployments; } }; for (key in conf) { _loop_1(key); } validation = jsonschema_1.validate(conf, schema_json_1.default, { propertyName: "config", allowUnknownAttributes: true }); if (validation.errors.length > 0) { err = validation.errors[0]; throw new types_1.ConfigError(err.property + " " + err.message); } for (key in conf) { conf[key].name = key; } return [2 /*return*/, conf]; } }); }); } exports.config = config; function getDeployBody(target, data) { return util_1.withPreview({ task: target.task || "deploy", transient_environment: target.transient_environment || false, production_environment: target.production_environment || false, environment: render_1.render(target.environment || "production", data), auto_merge: target.auto_merge || false, required_contexts: target.required_contexts || [], description: render_1.render(target.description, data), payload: __assign({ target: target.name }, render_1.render(target.payload, data)) }); } /** * Deploy commit handles all the necessities of creating a conformant deployment * including templating and more. All deploys should go through this function. * We need to deploy always using the ref of a branch so that finding * deployments later we can query using the branch ref. * * Throws ConfigError, LockError. */ function deploy(github, log, kv, _a) { var owner = _a.owner, repo = _a.repo, target = _a.target, ref = _a.ref, sha = _a.sha, pr = _a.pr; return __awaiter(this, void 0, void 0, function () { var logCtx, commit, repository, params, conf, targetVal, body, deploy_1, error_1; return __generator(this, function (_b) { switch (_b.label) { case 0: logCtx = { deploy: { target: target, ref: ref, pr: pr }, context: { repo: { owner: owner, repo: repo } } }; return [4 /*yield*/, github.git.getCommit({ owner: owner, repo: repo, commit_sha: sha })]; case 1: commit = _b.sent(); return [4 /*yield*/, github.repos.get({ owner: owner, repo: repo })]; case 2: repository = _b.sent(); params = { ref: ref, target: target, owner: owner, repo: repo, short_sha: sha.substr(0, 7), commit: commit.data, pr: pr ? pr.number : undefined, pull_request: pr }; return [4 /*yield*/, config(github, { owner: owner, repo: repo, ref: ref })]; case 3: conf = _b.sent(); targetVal = conf[target]; if (!targetVal) { log.info(logCtx, "deploy: halted - no target"); throw new types_1.ConfigError("Deployment target \"" + target + "\" does not exist"); } body = __assign({ owner: owner, repo: repo, ref: ref }, getDeployBody(targetVal, params)); return [4 /*yield*/, kv.isLockedEnv(repository.data.id, body.environment)]; case 4: if (_b.sent()) { log.info(logCtx, "deploy: halted - environment locked"); throw new types_1.LockError("Deployment environment locked"); } _b.label = 5; case 5: _b.trys.push([5, 7, , 8]); log.info(__assign(__assign({}, logCtx), { body: body }), "deploy: deploying"); return [4 /*yield*/, github.repos.createDeployment(body)]; case 6: deploy_1 = _b.sent(); log.info(__assign(__assign({}, logCtx), { body: body }), "deploy: successful"); return [2 /*return*/, deploy_1.data]; case 7: error_1 = _b.sent(); if (error_1.status === 409) { log.info(__assign(__assign({}, logCtx), { error: error_1, body: body }), "deploy: checks not ready"); } else { log.error(__assign(__assign({}, logCtx), { error: error_1, body: body }), "deploy: unexpected failure"); } throw error_1; case 8: return [2 /*return*/]; } }); }); } exports.deploy = deploy; //# sourceMappingURL=deploy.js.map