1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.handleWebhook = undefined;
|
7 |
|
8 | let handlePullRequest = (() => {
|
9 | var _ref = _asyncToGenerator(function* (event) {
|
10 | if (event.action !== 'opened' && event.action !== 'synchronize') {
|
11 | console.log('Unhandled event action: ', event.action);
|
12 | return;
|
13 | }
|
14 |
|
15 | const pull = event.pull_request;
|
16 | const otherBranchRemoteRef = `refs/pull/${pull.number}/head`;
|
17 | const otherBranchLocalRef = `refs/remotes/origin/PR-${pull.number}`;
|
18 | const baseBranchRemoteRef = `refs/heads/${pull.base.ref}`;
|
19 | const baseBranchLocalRef = `refs/remotes/origin/${pull.base.ref}`;
|
20 | const repo = event.repository.full_name;
|
21 |
|
22 | const substitutions = {
|
23 | _REPOSITORY_URL: pull.base.repo.clone_url,
|
24 | _OTHER_BRANCH_REMOTE_REF: otherBranchRemoteRef,
|
25 | _OTHER_BRANCH_LOCAL_REF: otherBranchLocalRef,
|
26 | _BASE_BRANCH_REMOTE_REF: baseBranchRemoteRef,
|
27 | _BASE_BRANCH_LOCAL_REF: baseBranchLocalRef,
|
28 | [_constants.STATUSES_URL_KEY]: pull.statuses_url,
|
29 | [_constants.COMMENTS_URL_KEY]: pull.comments_url
|
30 | };
|
31 |
|
32 | const prTag = `pr.${pull.number}`;
|
33 | const tags = [`repo.${event.repository.name.replace('_', '-')}`, `sender.${event.sender.login}`, prTag];
|
34 |
|
35 | const existingBuilds = yield _gcloud.googleApis.listCloudbuilds(`tags="${prTag}"`);
|
36 | existingBuilds.filter(function (build) {
|
37 | return build.status === 'QUEUED' || build.status === 'WORKING';
|
38 | }).forEach(function (build) {
|
39 | console.log(`Found existing build ${build.id}. Cancelling.`);
|
40 | _gcloud.googleApis.cancelCloudbuild(build.id);
|
41 | });
|
42 | console.log(`Starting cloud build for pull request ${pull.number}.`);
|
43 | yield _gcloud.googleApis.startCloudbuild(_config2.default.repos[repo].cloudbuild, substitutions, tags);
|
44 | });
|
45 |
|
46 | return function handlePullRequest(_x) {
|
47 | return _ref.apply(this, arguments);
|
48 | };
|
49 | })();
|
50 |
|
51 | let handleWebhook = exports.handleWebhook = (() => {
|
52 | var _ref2 = _asyncToGenerator(function* (req, res) {
|
53 | if (!(0, _verify2.default)((yield _keymanager.keyManager.getWebhookSecret()), req.rawBody.toString(), req.get('X-Hub-Signature'))) {
|
54 | console.error('Invalid signature.');
|
55 | res.status(400).end();
|
56 | return;
|
57 | }
|
58 |
|
59 | const event = req.body;
|
60 | const eventType = req.get('X-GitHub-Event');
|
61 | switch (eventType) {
|
62 | case 'pull_request':
|
63 | yield handlePullRequest(event);
|
64 | break;
|
65 | default:
|
66 | console.log('Unhandled event type: ', eventType);
|
67 | break;
|
68 | }
|
69 | res.status(200).end();
|
70 | });
|
71 |
|
72 | return function handleWebhook(_x2, _x3) {
|
73 | return _ref2.apply(this, arguments);
|
74 | };
|
75 | })();
|
76 |
|
77 | var _verify = require('@octokit/webhooks/verify');
|
78 |
|
79 | var _verify2 = _interopRequireDefault(_verify);
|
80 |
|
81 | var _keymanager = require('./keymanager');
|
82 |
|
83 | var _gcloud = require('./gcloud');
|
84 |
|
85 | var _config = require('./config');
|
86 |
|
87 | var _config2 = _interopRequireDefault(_config);
|
88 |
|
89 | var _constants = require('./constants');
|
90 |
|
91 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
92 |
|
93 | function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /*
|
94 | * MIT License
|
95 | *
|
96 | * Copyright (c) 2017 Choko (choko@curioswitch.org)
|
97 | *
|
98 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
99 | * of this software and associated documentation files (the "Software"), to deal
|
100 | * in the Software without restriction, including without limitation the rights
|
101 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
102 | * copies of the Software, and to permit persons to whom the Software is
|
103 | * furnished to do so, subject to the following conditions:
|
104 | *
|
105 | * The above copyright notice and this permission notice shall be included in all
|
106 | * copies or substantial portions of the Software.
|
107 | *
|
108 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
109 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
110 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
111 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
112 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
113 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
114 | * SOFTWARE.
|
115 | */ |
\ | No newline at end of file |