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
|
// file : mod/mod-ci-github-gq.hxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef MOD_MOD_CI_GITHUB_GQ_HXX
#define MOD_MOD_CI_GITHUB_GQ_HXX
#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>
#include <mod/mod-ci-github-gh.hxx>
#include <mod/mod-ci-github-service-data.hxx>
namespace brep
{
// GraphQL functions (all start with gq_).
//
// @@ TODO:
gq_create_check_run ();
gq_update_check_run ();
// @@ TODO Pass error, trace in same order everywhere.
// Fetch from GitHub the check run with the specified name (hints-shortened
// build ID).
//
// Return the check run or nullopt if no such check run exists.
//
// In case of error diagnostics will be issued and false returned in second.
//
// Note that the existence of more than one check run with the same name is
// considered an error and reported as such. The API docs imply that there
// can be more than one check run with the same name in a check suite, but
// the observed behavior is that creating a check run destroys the extant
// one, leaving only the new one with different node ID.
//
pair<optional<gh::check_run>, bool>
gq_fetch_check_run (const string& iat,
const string& check_suite_id,
const string& cr_name,
const basic_mark& error) noexcept
{
try
{
// Example request:
//
// query {
// node(id: "CS_kwDOLc8CoM8AAAAFQPQYEw") {
// ... on CheckSuite {
// checkRuns(last: 100, filterBy: {checkName: "linux_debian_..."}) {
// totalCount,
// edges {
// node {
// id, name, status
// }
// }
// }
// }
// }
// }
//
// This request does the following:
//
// - Look up the check suite by node ID ("direct node lookup"). This
// returns a Node (GraphQL interface).
//
// - Get to the concrete CheckSuite type by using a GraphQL "inline
// fragment" (`... on CheckSuite`).
//
// - Get the check suite's check runs
// - Filter by the sought name
// - Return only two check runs, just enough to be able to tell
// whether there are more than one check runs with this name (which
// is an error).
//
// - Return the id, name, and status fields from the matching check run
// objects.
//
string rq;
{
ostringstream os;
os << "query {" << '\n';
os << "node(id: " << gq_str (check_suite_id) << ") {" << '\n'
<< " ... on CheckSuite {" << '\n'
<< " checkRuns(last: 2," << '\n'
<< " filterBy: {" << '\n'
<< "checkName: " << gq_str (cr_name) << '\n'
<< " })" << '\n'
// Specify the selection set (fields to be returned). Note that
// edges and node are mandatory.
//
<< " {" << '\n'
<< " totalCount," << '\n'
<< " edges {" << '\n'
<< " node {" << '\n'
<< " id, name, status" << '\n'
<< " }" << '\n'
<< " }" << '\n'
<< " }" << '\n'
<< " }" << '\n'
<< "}" << '\n';
os << "}" << '\n';
rq = os.str ();
}
// Example response (the part we need to parse here, at least):
//
// {
// "node": {
// "checkRuns": {
// "totalCount": 1,
// "edges": [
// {
// "node": {
// "id": "CR_kwDOLc8CoM8AAAAFgeoweg",
// "name": "linux_debian_...",
// "status": "IN_PROGRESS"
// }
// }
// ]
// }
// }
// }
//
struct resp
{
optional<check_run> cr;
size_t cr_count = 0;
resp (json::parser& p)
{
using event = json::event;
parse_graphql_response (p, [this] (json::parser& p)
{
p.next_expect (event::begin_object);
p.next_expect_member_object ("node");
p.next_expect_member_object ("checkRuns");
cr_count = p.next_expect_member_number<size_t> ("totalCount");
p.next_expect_member_array ("edges");
for (size_t i (0); i != cr_count; ++i)
{
p.next_expect (event::begin_object);
p.next_expect_name ("node");
check_run cr (p);
p.next_expect (event::end_object);
if (i == 0)
this->cr = move (cr);
}
p.next_expect (event::end_array); // edges
p.next_expect (event::end_object); // checkRuns
p.next_expect (event::end_object); // node
p.next_expect (event::end_object);
});
}
resp () = default;
} rs;
uint16_t sc (github_post (rs,
"graphql",
strings {"Authorization: Bearer " + iat},
graphql_request (rq)));
if (sc == 200)
{
if (rs.cr_count <= 1)
return {rs.cr, true};
else
{
error << "unexpected number of check runs (" << rs.cr_count
<< ") in response";
}
}
else
error << "failed to get check run by name: error HTTP "
<< "response status " << sc;
}
catch (const json::invalid_json_input& e)
{
// Note: e.name is the GitHub API endpoint.
//
error << "malformed JSON in response from " << e.name
<< ", line: " << e.line << ", column: " << e.column
<< ", byte offset: " << e.position << ", error: " << e;
}
catch (const invalid_argument& e)
{
error << "malformed header(s) in response: " << e;
}
catch (const system_error& e)
{
error << "unable to get check run by name (errno=" << e.code ()
<< "): " << e.what ();
}
catch (const std::exception& e)
{
error << "unable to get check run by name: " << e.what ();
}
return {nullopt, false};
}
}
#endif // MOD_MOD_CI_GITHUB_GQ_HXX
|