UNPKG

6.44 kBJavaScriptView Raw
1"use strict";
2/*
3 * MIT License
4 *
5 * Copyright (c) 2018 Choko (choko@curioswitch.org)
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26 return new (P || (P = Promise))(function (resolve, reject) {
27 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
30 step((generator = generator.apply(thisArg, _arguments || [])).next());
31 });
32};
33var __importDefault = (this && this.__importDefault) || function (mod) {
34 return (mod && mod.__esModule) ? mod : { "default": mod };
35};
36Object.defineProperty(exports, "__esModule", { value: true });
37const process_1 = __importDefault(require("process"));
38const axios_1 = __importDefault(require("axios"));
39const child_process_promise_1 = require("child-process-promise");
40const commander_1 = __importDefault(require("commander"));
41const inquirer_1 = __importDefault(require("inquirer"));
42const package_json_1 = __importDefault(require("../../package.json"));
43const config_1 = __importDefault(require("../config"));
44const gcloud_1 = __importDefault(require("../gcloud"));
45const keymanager_1 = require("../keymanager");
46const GITHUB_API_BASE = 'https://api.github.com';
47function makeGithubRequest(uri, body, token, method = 'POST') {
48 return __awaiter(this, void 0, void 0, function* () {
49 return axios_1.default({
50 method,
51 url: uri,
52 data: body,
53 headers: {
54 'User-Agent': 'cloudbuild-github',
55 },
56 auth: {
57 username: 'token',
58 password: token,
59 },
60 });
61 });
62}
63function deploy() {
64 return __awaiter(this, void 0, void 0, function* () {
65 const ui = new inquirer_1.default.ui.BottomBar();
66 const webhookSecret = yield keymanager_1.keyManager.getWebhookSecret();
67 const google = yield gcloud_1.default();
68 const projectId = yield google.auth.getProjectId();
69 const memory = commander_1.default.memory;
70 ui.log.write(`Deploying cloud functions for project ${projectId}.`);
71 if (commander_1.default.delete) {
72 try {
73 yield child_process_promise_1.spawn('gcloud', ['--quiet', 'beta', 'functions', 'delete', 'cloudbuildGithubWebhook'], { stdio: 'inherit' });
74 }
75 catch (err) {
76 ui.log.write('Could not delete cloudbuildGithubWebhook, skipping.');
77 }
78 try {
79 yield child_process_promise_1.spawn('gcloud', ['--quiet', 'beta', 'functions', 'delete', 'cloudbuildGithubNotifier'], { stdio: 'inherit' });
80 }
81 catch (err) {
82 ui.log.write('Could not delete cloudbuildGithubNotifier, skipping.');
83 }
84 }
85 yield child_process_promise_1.spawn('gcloud', [
86 'beta',
87 'functions',
88 'deploy',
89 'cloudbuildGithubWebhook',
90 '--trigger-http',
91 '--runtime=nodejs10',
92 `--memory=${memory}`,
93 ], { stdio: 'inherit' });
94 yield child_process_promise_1.spawn('gcloud', [
95 'beta',
96 'functions',
97 'deploy',
98 'cloudbuildGithubNotifier',
99 '--trigger-topic',
100 'cloud-builds',
101 '--runtime=nodejs10',
102 `--memory=${memory}`,
103 ], { stdio: 'inherit' });
104 const webhookUrl = `https://us-central1-${projectId}.cloudfunctions.net/cloudbuildGithubWebhook`;
105 yield Promise.all(Object.keys(config_1.default.repos).map((repoName) => __awaiter(this, void 0, void 0, function* () {
106 const repoToken = yield keymanager_1.keyManager.getGithubToken(repoName);
107 ui.log.write(`Setting up repository webhook for ${repoName}`);
108 const hooksUri = `${GITHUB_API_BASE}/repos/${repoName}/hooks`;
109 const existingHooks = yield makeGithubRequest(hooksUri, null, repoToken, 'GET');
110 yield Promise.all(existingHooks.data
111 .filter((h) => h.config.url === webhookUrl)
112 .map((h) => __awaiter(this, void 0, void 0, function* () { return makeGithubRequest(`${hooksUri}/${h.id}`, null, repoToken, 'DELETE'); })));
113 const hook = {
114 name: 'web',
115 config: {
116 url: webhookUrl,
117 // eslint-disable-next-line @typescript-eslint/camelcase
118 content_type: 'json',
119 secret: webhookSecret,
120 },
121 events: ['pull_request'],
122 };
123 return makeGithubRequest(hooksUri, hook, repoToken);
124 })));
125 ui.log.write('Done!');
126 });
127}
128commander_1.default
129 .version(package_json_1.default.version)
130 .option('--delete', 'Delete existing functions first')
131 .option('--memory [size]', 'Memory usage for function', '256MB')
132 .parse(process_1.default.argv);
133deploy().then(() => process_1.default.exit(), (err) => {
134 console.error('Unexpected error.', err);
135 process_1.default.exit(1);
136});
137//# sourceMappingURL=cloudbuild-cli-deploy.js.map
\No newline at end of file