1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var __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 | };
|
33 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
34 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
35 | };
|
36 | Object.defineProperty(exports, "__esModule", { value: true });
|
37 | const process_1 = __importDefault(require("process"));
|
38 | const axios_1 = __importDefault(require("axios"));
|
39 | const child_process_promise_1 = require("child-process-promise");
|
40 | const commander_1 = __importDefault(require("commander"));
|
41 | const inquirer_1 = __importDefault(require("inquirer"));
|
42 | const package_json_1 = __importDefault(require("../../package.json"));
|
43 | const config_1 = __importDefault(require("../config"));
|
44 | const gcloud_1 = __importDefault(require("../gcloud"));
|
45 | const keymanager_1 = require("../keymanager");
|
46 | const GITHUB_API_BASE = 'https://api.github.com';
|
47 | function 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 | }
|
63 | function 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 |
|
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 | }
|
128 | commander_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);
|
133 | deploy().then(() => process_1.default.exit(), (err) => {
|
134 | console.error('Unexpected error.', err);
|
135 | process_1.default.exit(1);
|
136 | });
|
137 |
|
\ | No newline at end of file |