UNPKG

deliverybot

Version:

Controls and configures deployments using the GitHub API.

329 lines 16.4 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var util_1 = require("./util"); var deploy_1 = require("./deploy"); var util_2 = require("./util"); var uuid_1 = require("uuid"); function match(auto, ref) { if (!auto) return false; for (var i = 0; i < auto.length; i++) { if (auto[i] === '*') return true; if (auto[i] !== ref[i]) return false; } return auto.length === ref.length; } exports.match = match; /** * Wires up automatic deployments for the `auto_deploy_on` configuration * variable in the deploy.yml. */ function auto(app, lockStore, watchStore) { var _this = this; /** * Add watch adds a watch on a specific ref, sha and repository. */ function addWatch(context, ref, sha, repository) { return __awaiter(this, void 0, void 0, function () { var anyAdded, conf, _a, _b, _i, target, targetVal, watch, error_1; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 8, , 9]); anyAdded = false; return [4 /*yield*/, deploy_1.config(context.github, context.repo())]; case 1: conf = _c.sent(); _a = []; for (_b in conf) _a.push(_b); _i = 0; _c.label = 2; case 2: if (!(_i < _a.length)) return [3 /*break*/, 5]; target = _a[_i]; targetVal = conf[target]; if (!match(targetVal.auto_deploy_on, ref)) { return [3 /*break*/, 4]; } context.log.info(util_1.logCtx(context, { ref: ref, sha: sha, target: target, autoDeployOn: targetVal.auto_deploy_on }), "auto deploy: add watch"); watch = { id: uuid_1.v4(), targetVal: targetVal, ref: ref, sha: sha, repository: repository, target: target }; anyAdded = true; return [4 /*yield*/, watchStore.addWatch(repository.id, watch)]; case 3: _c.sent(); _c.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: if (!anyAdded) return [3 /*break*/, 7]; return [4 /*yield*/, emitWatches(context, repository.id, sha)]; case 6: _c.sent(); _c.label = 7; case 7: return [3 /*break*/, 9]; case 8: error_1 = _c.sent(); // This error block will mostly catch configuration errors and simply // return until the next event comes a long with new configuration. switch (error_1.status) { case 404: context.log.info(util_1.logCtx(context, { error: error_1 }), "auto deploy: no config"); break; case "ConfigError": context.log.info(util_1.logCtx(context, { error: error_1 }), "auto deploy: config err"); break; default: context.log.error(util_1.logCtx(context, { error: error_1 }), "auto deploy: failed"); throw error_1; } return [3 /*break*/, 9]; case 9: return [2 /*return*/]; } }); }); } /** * Process watch determines if given a watch it needs to be re-deployed. It * returns true if this watch is done and can be removed. Watches are done for * a variety of scenarios: * * - The watch is old and should not be processed. * - The watch is already deployed. * - There is a non-retryable configuration error. */ function processWatch(context, watch) { return __awaiter(this, void 0, void 0, function () { var sha, ref, target, targetVal, refreshed, currentSha, deploys, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: sha = watch.sha, ref = watch.ref, target = watch.target, targetVal = watch.targetVal; return [4 /*yield*/, context.github.git.getRef(context.repo({ ref: ref.replace("refs/", "") }))]; case 1: refreshed = _a.sent(); currentSha = refreshed.data.object.sha; if (currentSha !== sha) { // This is an old watch, return true. context.log.info(util_1.logCtx(context, { ref: ref, sha: sha, currentSha: currentSha }), "auto deploy: old watch"); return [2 /*return*/, true]; } context.log.info(util_1.logCtx(context, { ref: ref, sha: sha, target: target, watchId: watch.id }), "auto deploy: processing watch"); return [4 /*yield*/, context.github.repos.listDeployments(context.repo({ sha: sha }))]; case 2: deploys = _a.sent(); if (deploys.data.find(function (d) { return d.environment === targetVal.environment; })) { context.log.info(util_1.logCtx(context, { ref: ref }), "auto deploy: already deployed"); return [2 /*return*/, true]; } context.log.info(util_1.logCtx(context, { ref: ref }), "auto deploy: deploying"); _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); return [4 /*yield*/, deploy_1.deploy(context.github, context.log, lockStore, context.repo({ ref: ref, sha: sha, target: target }))]; case 4: _a.sent(); context.log.info(util_1.logCtx(context, { ref: ref }), "auto deploy: done"); return [2 /*return*/, true]; case 5: error_2 = _a.sent(); // Catch deploy errors and return if this is a normal scenario for an // auto deployment. switch (error_2.status) { case 409: context.log.info(util_1.logCtx(context, { target: target, ref: ref, error: error_2 }), "auto deploy: checks not ready"); return [2 /*return*/, false]; case "LockError": context.log.info(util_1.logCtx(context, { target: target, ref: ref, error: error_2 }), "auto deploy: environment locked"); return [2 /*return*/, true]; // We don't wait for "unlock" events and redeploy. case "ConfigError": context.log.info(util_1.logCtx(context, { target: target, ref: ref, error: error_2 }), "auto deploy: target config error"); return [2 /*return*/, true]; default: context.log.error(util_1.logCtx(context, { target: target, ref: ref, error: error_2 }), "auto deploy: deploy attempt failed"); throw error_2; } return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); } /** * Lock watch will handle receiving a watch and deleting it if it's processed * successfully. */ function lockWatch(context, handle) { var _this = this; // We need to lock by the ref + repository here. Since we want to deploy // by reference and not by the sha. var key = util_2.hash([context.payload.repository.id, context.payload.ref]); context.log.info({ key: key, watchId: context.payload.id }, "auto deploy: locking"); return lockStore.lock(key, function () { return __awaiter(_this, void 0, void 0, function () { var watch, done; return __generator(this, function (_a) { switch (_a.label) { case 0: watch = context.payload; return [4 /*yield*/, handle()]; case 1: done = _a.sent(); if (!done) return [3 /*break*/, 3]; return [4 /*yield*/, watchStore.delWatch(watch.repository.id, watch)]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); }); } /** * Emit watches emits all watches listed by a given sha as a push_watch event * to trigger logic to run the automatic deployments. */ function emitWatches(context, repoId, sha) { return __awaiter(this, void 0, void 0, function () { var watches; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, watchStore.listWatchBySha(repoId, sha)]; case 1: watches = _a.sent(); context.log.info(util_1.logCtx(context, { repoId: repoId, sha: sha, watches: watches.map(function (w) { return ({ id: w.id, ref: w.ref, sha: w.sha, repo: w.repository.id }); }) }), "auto deploy: emitting watches"); return [4 /*yield*/, Promise.all(watches.map(function (watch) { return app.receive({ id: context.id, name: "push_watch", payload: __assign(__assign({}, watch), { installation: context.payload.installation }), protocol: context.protocol, host: context.host, url: context.url }); }))]; case 2: _a.sent(); return [2 /*return*/]; } }); }); } // Status events emit different watches to auto-deploy code. app.on("status", function (context) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, emitWatches(context, context.payload.repository.id, context.payload.sha)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); // Check run events emit different watches to auto-deploy code. app.on("check_run", function (context) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, emitWatches(context, context.payload.repository.id, context.payload.check_run.check_suite.head_sha)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); // Handles a synthetic push watch event. This fires whenever a change happens // to a specific commit where a watch exists on that commit. app.on("push_watch", function (context) { return lockWatch(context, function () { var watch = context.payload; return processWatch(context, watch); }); }); // Push handles creating new watches that need to appear. app.on("push", function (context) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, addWatch(context, context.payload.ref, context.payload.after, context.payload.repository)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); } exports.auto = auto; //# sourceMappingURL=auto.js.map