-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.ts
181 lines (154 loc) · 5.53 KB
/
webhook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Application } from 'probot';
import axios from 'axios';
import stream from 'stream';
import archiver from 'archiver';
import { asyncFind, asyncFlatMap } from 'iter-tools';
// Type imports, not declared because we don't want to diverge from the versions used by Probot
import Octokit from '@octokit/rest'; // eslint-disable-line import/no-extraneous-dependencies
import { WebhookPayloadCheckSuiteCheckSuite } from '@octokit/webhooks'; // eslint-disable-line import/no-extraneous-dependencies
import CircleCi from './CircleCi';
const WEBSITE_JOB_NAME = 'website';
const NETLIFY_BASE_URL = 'https://api.netlify.com/api/v1';
const SITE_ID = process.env.NETLIFY_SITE_ID!;
const APP_USER = Number(process.env.BOT_USER_ID!);
const GITHUB_REPO = 'nusmodifications/nusmods';
const GITHUB_REPO_PARAMS = {
owner: 'nusmodifications',
repo: 'nusmods',
};
const circleCi = new CircleCi({
vcsType: 'github',
username: 'nusmodifications',
project: 'nusmods',
});
/* eslint-disable @typescript-eslint/camelcase */
async function getComment(
github: Octokit,
issue: number,
): Promise<Octokit.IssuesListCommentsResponseItem | undefined> {
// Find the comment posted by this bot
const comments = asyncFlatMap(
(response: Octokit.Response<Octokit.IssuesListCommentsResponse>) =>
response.data,
github.paginate.iterator({
url: `/repos/${GITHUB_REPO}/issues/${issue}/comments`,
}),
);
return asyncFind(comment => comment.user.id === APP_USER, comments);
}
function getCommentBody(
pull: number,
commitSha: string,
deploymentUrl: string,
): string {
const url = `https://github.com/${GITHUB_REPO}/pull/${pull}/commits/${commitSha}`;
return `Deployment preview for [\`${commitSha.slice(
0,
8,
)}\`](${url}) ready at <${deploymentUrl}>`;
}
function isCircleCiBuild(check: WebhookPayloadCheckSuiteCheckSuite): boolean {
return Boolean(check.app.name.match(/circleci/i));
}
// export = ApplicationFn compiles into Probot compatible JS export
export = (app: Application) => {
axios.interceptors.request.use(config => {
// Log all outgoing axios requests
app.log.trace('Making request', { url: config.url });
return config;
});
app.on('check_suite.completed', async context => {
const { check_suite } = context.payload;
if (!isCircleCiBuild(check_suite) || check_suite.conclusion !== 'success') {
context.log.debug('Check is not a successful CircleCI build - skipping');
return;
}
// For some reason GitHub doesn't include pull requests in success check webhook payloads, so we need
// to go and look for it in all check runs
let pulls = check_suite.pull_requests.map(pull => pull.number);
if (pulls.length === 0) {
const searchResult = await context.github.search.issuesAndPullRequests({
q: `repo:${GITHUB_REPO} type:pr is:open ${check_suite.head_sha}`,
});
pulls = searchResult.data.items.map(item => item.number);
}
// Only continue if there's a pull request we can comment on
if (pulls.length === 0) {
context.log.warn('Pull request cannot be found for check suite', {
check_suite,
});
return;
}
context.log.trace('Getting builds from CircleCI');
const builds = await circleCi.getBuilds();
const checkBuild = builds.find(
build =>
build.vcs_revision === check_suite.head_sha &&
build.workflows.job_name === WEBSITE_JOB_NAME,
);
if (checkBuild == null) {
context.log.warn('Could not find matching CircleCI build');
return;
}
const artifacts = await circleCi.getArtifacts(checkBuild.build_num);
context.log.debug('Found CircleCI artifacts', { artifacts });
// Deploy artifacts from CircleCI by uploading a zip archive
// https://docs.netlify.com/api/get-started/#zip-file-method
// We're not using Netlify's OpenAPI wrapper because the wrapper
// doesn't seem to support this
const archive = archiver('zip');
const request = axios.post(
`${NETLIFY_BASE_URL}/sites/${SITE_ID}/deploys`,
archive,
{
headers: {
'content-type': 'application/zip',
authorization: `Bearer ${process.env.NETLIFY_TOKEN}`,
},
},
);
await Promise.all(
artifacts.map(({ url, path }) =>
axios
.get<stream.Readable>(url, {
responseType: 'stream',
})
.then(response => {
context.log.trace('Appending file to archive', { path });
archive.append(response.data, { name: path });
}),
),
);
context.log.trace('Finalizing archive');
archive.finalize();
context.log.trace('Making Netlify request');
const response = await request;
context.log.debug('Got response from Netlify', { data: response.data });
// Get the bot comment on the pull request
await Promise.all(
pulls.map(async issue => {
const body = getCommentBody(
issue,
check_suite.head_sha,
response.data.deploy_url,
);
const comment = await getComment(context.github, issue);
if (comment == null) {
context.log.debug('No existing comment - creating');
await context.github.issues.createComment({
...GITHUB_REPO_PARAMS,
issue_number: issue,
body,
});
} else {
context.log.debug('Updating existing comment');
await context.github.issues.updateComment({
...GITHUB_REPO_PARAMS,
comment_id: comment.id,
body,
});
}
}),
);
});
};