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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// file : mod/mod-ci-github-gh.hxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef MOD_MOD_CI_GITHUB_GH_HXX
#define MOD_MOD_CI_GITHUB_GH_HXX
#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>
#include <libbrep/build.hxx>
#include <mod/tenant-service.hxx> // build_hints
namespace butl
{
namespace json
{
class parser;
}
}
namespace brep
{
using build_queued_hints = tenant_service_build_queued::build_queued_hints;
// GitHub request/response types (all start with gh_).
//
// Note that the GitHub REST and GraphQL APIs use different id types and
// values. In the REST API they are usually integers (but check the API
// reference for the object in question) whereas in GraphQL they are always
// strings (note: base64-encoded and opaque, not just the REST id value as a
// string).
//
// In both APIs the id field is called `id`, but REST responses and webhook
// events also contain the corresponding GraphQL object's id in the
// `node_id` field.
//
// The GraphQL API's ids are called "global node ids" by GitHub. We refer to
// them simply as node ids and we use them almost exclusively (over the
// REST/webhook ids).
//
// In the structures below, `id` always refers to the REST/webhook id and
// `node_id` always refers to the node id.
//
namespace json = butl::json;
// The check_suite member of a check_run webhook event (gh_check_run_event).
//
struct gh_check_suite
{
string node_id;
optional<string> head_branch;
string head_sha;
explicit
gh_check_suite (json::parser&);
gh_check_suite () = default;
};
// The check_suite member of a check_suite webhook event
// (gh_check_suite_event).
//
struct gh_check_suite_ex: gh_check_suite
{
size_t check_runs_count;
optional<string> conclusion;
string app_id;
explicit
gh_check_suite_ex (json::parser&);
gh_check_suite_ex () = default;
};
// The check_run object returned in response to GraphQL requests
// (e.g. create or update check run). Note that we specifiy the set of
// members to return in the GraphQL request.
//
struct gh_check_run
{
string node_id;
string name;
string status;
explicit
gh_check_run (json::parser&);
gh_check_run () = default;
};
// The check_run member of a check_run webhook event (gh_check_run_event).
//
struct gh_check_run_ex: gh_check_run
{
string details_url;
gh_check_suite check_suite;
string app_id;
explicit
gh_check_run_ex (json::parser&);
gh_check_run_ex () = default;
};
// The pull_request member of a pull_request webhook event
// (gh_pull_request_event).
//
struct gh_pull_request
{
string node_id;
unsigned int number;
string base_path; // Repository path (<org>/<repo>) under github.com.
string base_ref;
string base_sha;
string head_path;
string head_ref;
string head_sha;
// Note: not received from GitHub but set from the app-id webhook query
// parameter instead.
//
// For some reason, unlike the check_suite and check_run webhooks, the
// pull_request webhook does not contain the app id. For the sake of
// simplicity we emulate check_suite and check_run by storing the app-id
// webhook query parameter here.
//
string app_id;
explicit
gh_pull_request (json::parser&);
gh_pull_request () = default;
};
// The repository member of various webhook events.
//
struct gh_repository
{
string node_id;
string path; // Repository path (<org>/<repo>) under github.com.
string clone_url;
explicit
gh_repository (json::parser&);
gh_repository () = default;
};
// The installation member of various webhook events.
//
struct gh_installation
{
string id; // Note: used for installation access token (REST API).
explicit
gh_installation (json::parser&);
gh_installation () = default;
};
// The check_suite webhook event.
//
struct gh_check_suite_event
{
string action;
gh_check_suite_ex check_suite;
gh_repository repository;
gh_installation installation;
explicit
gh_check_suite_event (json::parser&);
gh_check_suite_event () = default;
};
// The check_run webhook event.
//
struct gh_check_run_event
{
string action;
gh_check_run_ex check_run;
gh_repository repository;
gh_installation installation;
explicit
gh_check_run_event (json::parser&);
gh_check_run_event () = default;
};
// The pull_request webhook event.
//
struct gh_pull_request_event
{
string action;
gh_pull_request pull_request;
// The SHA of the previous commit on the head branch before the current
// one. Only present if action is "synchronize".
//
optional<string> before;
gh_repository repository;
gh_installation installation;
explicit
gh_pull_request_event (json::parser&);
gh_pull_request_event () = default;
};
// The push webhook event.
//
struct gh_push_event
{
// The full git ref that was pushed. Example: refs/heads/main or
// refs/tags/v3.14.1.
//
string ref;
// The SHA of the most recent commit on ref before the push.
//
string before;
// The SHA of the most recent commit on ref after the push.
//
string after;
// True if this was a forced push of the ref. I.e., history was
// overwritten.
//
bool forced;
gh_repository repository;
explicit
gh_push_event (json::parser&);
gh_push_event () = default;
};
// Installation access token (IAT) returned when we authenticate as a GitHub
// app installation.
//
struct gh_installation_access_token
{
string token;
timestamp expires_at;
explicit
gh_installation_access_token (json::parser&);
gh_installation_access_token (string token, timestamp expires_at);
gh_installation_access_token () = default;
};
// Return the GitHub check run status corresponding to a build_state.
//
string
gh_to_status (build_state);
// Return the build_state corresponding to a GitHub check run status
// string. Throw invalid_argument if the passed status was invalid.
//
build_state
gh_from_status (const string&);
// If warning_success is true, then map result_status::warning to `SUCCESS`
// and to `FAILURE` otherwise.
//
// Throw invalid_argument in case of unsupported result_status value
// (currently skip, interrupt).
//
string
gh_to_conclusion (result_status, bool warning_success);
// Create a check_run name from a build. If the second argument is not
// NULL, return an abbreviated id if possible.
//
string
gh_check_run_name (const build&, const build_queued_hints* = nullptr);
// Throw system_error if the conversion fails due to underlying operating
// system errors.
//
string
gh_to_iso8601 (timestamp);
// Throw invalid_argument if the conversion fails due to the invalid
// argument and system_error if due to underlying operating system errors.
//
timestamp
gh_from_iso8601 (const string&);
ostream&
operator<< (ostream&, const gh_check_suite&);
ostream&
operator<< (ostream&, const gh_check_suite_ex&);
ostream&
operator<< (ostream&, const gh_check_run&);
ostream&
operator<< (ostream&, const gh_pull_request&);
ostream&
operator<< (ostream&, const gh_repository&);
ostream&
operator<< (ostream&, const gh_installation&);
ostream&
operator<< (ostream&, const gh_check_suite_event&);
ostream&
operator<< (ostream&, const gh_check_run_event&);
ostream&
operator<< (ostream&, const gh_pull_request_event&);
ostream&
operator<< (ostream&, const gh_push_event&);
ostream&
operator<< (ostream&, const gh_installation_access_token&);
}
#endif // MOD_MOD_CI_GITHUB_GH_HXX
|