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
|
// file : mod/mod-ci-github.hxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef MOD_MOD_CI_GITHUB_HXX
#define MOD_MOD_CI_GITHUB_HXX
#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>
#include <mod/module.hxx>
#include <mod/module-options.hxx>
#include <mod/ci-common.hxx>
#include <mod/tenant-service.hxx>
namespace butl
{
namespace json
{
class parser;
}
}
namespace brep
{
// GitHub request/response types.
//
// Note that the GitHub REST and GraphQL APIs use different ID types and
// values. In the REST API they are usually integers (but sometimes
// strings!) 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.
//
// In the structures below we always use the RESP API/webhook names for ID
// fields. I.e., `id` always refers to the REST/webhook ID, and `node_id`
// always refers to the GraphQL ID.
//
// Note that having the below types directly in brep causes clashes (e.g.,
// for the repository name).
//
namespace gh
{
namespace json = butl::json;
// The "check_suite" object within a check_suite webhook event request.
//
struct check_suite
{
uint64_t id; // Note: used for installation access token (REST API).
string node_id;
string head_branch;
string head_sha;
string before;
string after;
explicit
check_suite (json::parser&);
check_suite () = default;
};
struct check_run
{
string node_id;
string name;
string status;
explicit
check_run (json::parser&);
check_run () = default;
};
struct repository
{
string node_id;
string name;
string full_name;
string default_branch;
string clone_url;
explicit
repository (json::parser&);
repository () = default;
};
struct installation
{
uint64_t id;
explicit
installation (json::parser&);
installation () = default;
};
// The check_suite webhook event request.
//
struct check_suite_event
{
string action;
gh::check_suite check_suite;
gh::repository repository;
gh::installation installation;
explicit
check_suite_event (json::parser&);
check_suite_event () = default;
};
struct installation_access_token
{
string token;
timestamp expires_at;
explicit
installation_access_token (json::parser&);
installation_access_token () = default;
};
ostream&
operator<< (ostream&, const check_suite&);
ostream&
operator<< (ostream&, const check_run&);
ostream&
operator<< (ostream&, const repository&);
ostream&
operator<< (ostream&, const installation&);
ostream&
operator<< (ostream&, const check_suite_event&);
ostream&
operator<< (ostream&, const installation_access_token&);
}
class ci_github: public handler,
private ci_start,
public tenant_service_build_queued,
public tenant_service_build_building,
public tenant_service_build_built
{
public:
explicit
ci_github (tenant_service_map&);
// Create a shallow copy (handling instance) if initialized and a deep
// copy (context exemplar) otherwise.
//
explicit
ci_github (const ci_github&, tenant_service_map&);
virtual bool
handle (request&, response&);
virtual const cli::options&
cli_options () const {return options::ci_github::description ();}
virtual function<optional<string> (const tenant_service&)>
build_queued (const tenant_service&,
const vector<build>&,
optional<build_state> initial_state,
const fail_mark<server_error>& fail,
const basic_mark& error,
const basic_mark& warn,
const basic_mark& trace) const override;
virtual function<optional<string> (const tenant_service&)>
build_building (const tenant_service&, const build&,
const fail_mark<server_error>& fail,
const basic_mark& error,
const basic_mark& warn,
const basic_mark& trace) const override;
virtual function<optional<string> (const tenant_service&)>
build_built (const tenant_service&, const build&,
const fail_mark<server_error>& fail,
const basic_mark& error,
const basic_mark& warn,
const basic_mark& trace) const override;
private:
virtual void
init (cli::scanner&);
// Handle the check_suite event `requested` and `rerequested` actions.
//
bool
handle_check_suite_request (gh::check_suite_event);
string
generate_jwt () const;
// Authenticate to GitHub as an app installation.
//
gh::installation_access_token
obtain_installation_access_token (uint64_t install_id, string jwt) const;
private:
shared_ptr<options::ci_github> options_;
tenant_service_map& tenant_service_map_;
};
}
#endif // MOD_MOD_CI_GITHUB_HXX
|