-
Notifications
You must be signed in to change notification settings - Fork 981
/
Copy pathgithub_api.ts
254 lines (224 loc) · 7.05 KB
/
github_api.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Url from 'url';
import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios';
import parseLinkHeader from 'parse-link-header';
import { ToolingLog, isAxiosResponseError, isAxiosRequestError } from '@osd/dev-utils';
const BASE_URL = 'https://api.github.com/repos/elastic/kibana/';
export interface GithubIssue {
html_url: string;
number: number;
title: string;
labels: unknown[];
body: string;
}
/**
* Minimal GithubIssue type that can be easily replicated by dry-run helpers
*/
export interface GithubIssueMini {
number: GithubIssue['number'];
body: GithubIssue['body'];
html_url: GithubIssue['html_url'];
}
type RequestOptions = AxiosRequestConfig & {
safeForDryRun?: boolean;
maxAttempts?: number;
attempt?: number;
};
export class GithubApi {
private readonly log: ToolingLog;
private readonly token: string | undefined;
private readonly dryRun: boolean;
private readonly x: AxiosInstance;
/**
* Create a GithubApi helper object, if token is undefined requests won't be
* sent, but will instead be logged.
*/
constructor(options: {
log: GithubApi['log'];
token: GithubApi['token'];
dryRun: GithubApi['dryRun'];
}) {
this.log = options.log;
this.token = options.token;
this.dryRun = options.dryRun;
if (!this.token && !this.dryRun) {
throw new TypeError('token parameter is required');
}
this.x = Axios.create({
headers: {
...(this.token ? { Authorization: `token ${this.token}` } : {}),
'User-Agent': 'elastic/kibana#failed_test_reporter',
},
});
}
private failedTestIssuesPageCache: {
pages: GithubIssue[][];
nextRequest: RequestOptions | undefined;
} = {
pages: [],
nextRequest: {
safeForDryRun: true,
method: 'GET',
url: Url.resolve(BASE_URL, 'issues'),
params: {
state: 'all',
per_page: '100',
labels: 'failed-test',
sort: 'updated',
direction: 'desc',
},
},
};
/**
* Iterate the `failed-test` issues from elastic/kibana, each response
* from Github is cached and subsequent calls to this method will first
* iterate the previous responses from Github, then start requesting
* more pages of issues from github until all pages have been cached.
*
* Aborting the iterator part way through will prevent unnecessary request
* to Github from being issued.
*/
async *iterateCachedFailedTestIssues() {
const cache = this.failedTestIssuesPageCache;
// start from page 0, and progress forward if we have cache or a request that will load that cache page
for (let page = 0; page < cache.pages.length || cache.nextRequest; page++) {
if (page >= cache.pages.length && cache.nextRequest) {
const resp = await this.request<GithubIssue[]>(cache.nextRequest, []);
cache.pages.push(resp.data);
const link =
typeof resp.headers.link === 'string' ? parseLinkHeader(resp.headers.link) : undefined;
cache.nextRequest =
link && link.next && link.next.url
? {
safeForDryRun: true,
method: 'GET',
url: link.next.url,
}
: undefined;
}
for (const issue of cache.pages[page]) {
yield issue;
}
}
}
async findFailedTestIssue(test: (issue: GithubIssue) => boolean) {
for await (const issue of this.iterateCachedFailedTestIssues()) {
if (test(issue)) {
return issue;
}
}
}
async editIssueBodyAndEnsureOpen(issueNumber: number, newBody: string) {
await this.request(
{
method: 'PATCH',
url: Url.resolve(BASE_URL, `issues/${encodeURIComponent(issueNumber)}`),
data: {
state: 'open', // Reopen issue if it was closed.
body: newBody,
},
},
undefined
);
}
async addIssueComment(issueNumber: number, commentBody: string) {
await this.request(
{
method: 'POST',
url: Url.resolve(BASE_URL, `issues/${encodeURIComponent(issueNumber)}/comments`),
data: {
body: commentBody,
},
},
undefined
);
}
async createIssue(title: string, body: string, labels?: string[]) {
const resp = await this.request<GithubIssueMini>(
{
method: 'POST',
url: Url.resolve(BASE_URL, 'issues'),
data: {
title,
body,
labels,
},
},
{
body,
number: 999,
html_url: 'https://dryrun',
}
);
return resp.data;
}
private async request<T>(
options: RequestOptions,
dryRunResponse: T
): Promise<{
status: number;
statusText: string;
headers: Record<string, string | string[] | undefined>;
data: T;
}> {
const executeRequest = !this.dryRun || options.safeForDryRun;
const maxAttempts = options.maxAttempts || 5;
const attempt = options.attempt || 1;
this.log.verbose('Github API', executeRequest ? 'Request' : 'Dry Run', options);
if (!executeRequest) {
return {
status: 200,
statusText: 'OK',
headers: {},
data: dryRunResponse,
};
}
try {
return await this.x.request<T>(options);
} catch (error) {
const unableToReachGithub = isAxiosRequestError(error);
const githubApiFailed = isAxiosResponseError(error) && error.response.status >= 500;
const errorResponseLog =
isAxiosResponseError(error) &&
`[${error.config.method} ${error.config.url}] ${error.response.status} ${error.response.statusText} Error`;
if ((unableToReachGithub || githubApiFailed) && attempt < maxAttempts) {
const waitMs = 1000 * attempt;
if (errorResponseLog) {
this.log.error(`${errorResponseLog}: waiting ${waitMs}ms to retry`);
} else {
this.log.error(`Unable to reach github, waiting ${waitMs}ms to retry`);
}
await new Promise((resolve) => setTimeout(resolve, waitMs));
return await this.request<T>(
{
...options,
maxAttempts,
attempt: attempt + 1,
},
dryRunResponse
);
}
if (errorResponseLog) {
throw new Error(`${errorResponseLog}: ${JSON.stringify(error.response.data)}`);
}
throw error;
}
}
}