forked from alire-project/alire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alire-github.adb
345 lines (298 loc) · 10.2 KB
/
alire-github.adb
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
337
338
339
340
341
342
343
344
345
with Ada.Calendar;
with Ada.Exceptions;
with Alire.Errors;
with Alire.OS_Lib;
with Alire.Publish;
with Alire.URI;
with Alire.Utils.TTY;
with Alire.Version;
with GNATCOLL.JSON.Utility;
with GNATCOLL.Strings;
with Minirest;
package body Alire.GitHub is
use Minirest;
use URI.Operators;
Base_URL : constant URL := "https://api.github.com";
Header_Rate : constant String := "X-Ratelimit-Remaining";
-----------------
-- JSON_Escape --
-----------------
function JSON_Escape (S : String) return String
is
use GNATCOLL;
X : constant Strings.XString := Strings.To_XString (S);
begin
return +GNATCOLL.JSON.Utility.Escape_String (X);
end JSON_Escape;
--------------
-- API_Call --
--------------
type Kinds is new Minirest.Request_Kinds;
function API_Call (Proc : String;
Args : Minirest.Parameters := Minirest.No_Arguments;
Kind : Kinds := GET;
Token : String := OS_Lib.Getenv (Env_GH_Token, ""))
return Minirest.Response
is
Full_URL : constant String :=
Base_URL
& (if Proc (Proc'First) /= '/' then "/" else "")
& Proc;
Headers : Minirest.Parameters :=
"Accept" = "Application/vnd.github.v3.full+json"
and "X-GitHub-Api-Version" = "2022-11-28";
begin
if Token /= "" then
Headers := Headers and "Authorization" = "Bearer " & Token;
end if;
Trace.Debug
("GitHub API call " & Kind'Image & " to " & Full_URL);
Trace.Debug
("Headers: " & Minirest.Image (Headers, JSON_Escape'Access));
Trace.Debug
("Parameters: " & Minirest.Image (Args, JSON_Escape'Access));
return This : constant Response :=
(case Kind is
when GET =>
Minirest.Get
(Full_URL,
Arguments => Args,
Headers => Headers),
when POST | PATCH =>
Minirest.Post
(Full_URL,
Data => Args,
Headers => Headers,
Escape => JSON_Escape'Access,
Kind => Minirest.Request_Kinds (Kind)))
do
Trace.Debug
("GitHub API response: " & This.Status_Line);
Trace.Debug
("Headers: " & This.Raw_Headers.Flatten (ASCII.LF));
Trace.Debug
("Data: " & This.Content.Flatten (ASCII.LF));
if not This.Succeeded then
-- Log info about why the API call failed
Trace.Debug ("Failed GitHub request to API: "
& Utils.TTY.URL (Full_URL));
-- Raise if API is rate-limiting to avoid misleading failures
if This.Headers.Contains (Header_Rate) and then
Integer'Value (This.Headers.Get (Header_Rate)) <= 0
then
Raise_Checked_Error
("GitHub API rate limit exceeded, please wait for a while"
& " before retrying.");
end if;
end if;
end return;
exception
when E : Minirest.Rest_Error =>
Log_Exception (E);
Raise_Checked_Error
(Errors.Wrap
("Error querying GitHub API",
Ada.Exceptions.Exception_Message (E)));
end API_Call;
--------------
-- API_Call --
--------------
function API_Call (Proc : String;
Args : Minirest.Parameters := Minirest.No_Arguments;
Kind : Kinds := GET;
Token : String := OS_Lib.Getenv (Env_GH_Token, "");
Error : String := "GitHub API call failed")
return GNATCOLL.JSON.JSON_Value
is
Response : constant Minirest.Response
:= API_Call (Proc => Proc,
Args => Args,
Kind => Kind,
Token => Token);
begin
if Response.Succeeded then
return GNATCOLL.JSON.Read (Response.Content.Flatten (""));
else
Raise_Checked_Error
(Errors.New_Wrapper
.Wrap (Error)
.Wrap ("Status line: " & Response.Status_Line)
.Wrap ("Response body:")
.Wrap (Response.Content.Flatten (ASCII.LF))
.Get);
end if;
end API_Call;
-------------------
-- Branch_Exists --
-------------------
function Branch_Exists
(User : String := User_Info.User_GitHub_Login;
Repo : String := Index.Community_Repo_Name;
Branch : String := Index.Community_Branch)
return Boolean
is (API_Call ("repos" / User / Repo / "branches" / Branch).Succeeded);
-------------------------
-- Create_Pull_Request --
-------------------------
function Create_Pull_Request
(User : String := User_Info.User_GitHub_Login;
Base : String := Index.Community_Organization;
Repo : String := Index.Community_Repo_Name;
Head_Branch : String := Index.Community_Branch;
Base_Branch : String := Index.Community_Branch;
Draft : Boolean := False;
Maintainer_Can_Modify : Boolean := True;
Token : String;
Title : String;
Message : String -- What goes in the body of the PR
)
return Natural
is
begin
return
API_Call
(Kind => POST,
Token => Token,
Error => "Pull request could not be created",
Proc => "repos" / Base / Repo / "pulls",
Args =>
"title" = Title
and "body" = Message
and "base" = Base_Branch
and "head" = User & ":" & Head_Branch
and "draft" = Draft
and "maintainer_can_modify" = Maintainer_Can_Modify)
.Get ("number").Get;
end Create_Pull_Request;
---------------
-- Get_Pulls --
---------------
function Get_Pulls (Args : Minirest.Parameters)
return GNATCOLL.JSON.JSON_Value
is
begin
return
API_Call ("repos"
/ Index.Community_Organization
/ Index.Community_Repo_Name
/ "pulls",
Error => "Could not list pull requests",
Kind => GET,
Args => Args and "per_page" = 100);
end Get_Pulls;
-----------------------
-- Find_Pull_Request --
-----------------------
function Find_Pull_Request (M : Milestones.Milestone)
return GNATCOLL.JSON.JSON_Value
is (Get_Pulls ("state" = "all"
and "head" = User_Info.User_GitHub_Login & ":"
& Publish.Branch_Name (M)));
-----------------------
-- Find_Pull_Request --
-----------------------
function Find_Pull_Request (Number : Natural)
return GNATCOLL.JSON.JSON_Value
is (API_Call
("repos"
/ Index.Community_Organization
/ Index.Community_Repo_Name
/ "pulls"
/ AAA.Strings.Trim (Number'Image),
Error => "Could not retrieve pull request information",
Kind => GET));
------------------------
-- Find_Pull_Requests --
------------------------
function Find_Pull_Requests return GNATCOLL.JSON.JSON_Value
is (Get_Pulls ("state" = "open"
and "head" = User_Info.User_GitHub_Login));
-------------
-- Comment --
-------------
procedure Comment (Number : Natural; Text : String) is
Unused : constant GNATCOLL.JSON.JSON_Value
:= API_Call
("repos"
/ Index.Community_Organization
/ Index.Community_Repo_Name
/ "issues"
/ AAA.Strings.Trim (Number'Image)
/ "comments",
Kind => POST,
Args => "body" = Text);
begin
Trace.Debug ("Comment via GitHub REST API successful: " & Text);
end Comment;
-----------
-- Close --
-----------
procedure Close (Number : Natural; Reason : String) is
use AAA.Strings;
Unused : constant GNATCOLL.JSON.JSON_Value
:= API_Call ("repos"
/ Index.Community_Organization
/ Index.Community_Repo_Name
/ "pulls"
/ Trim (Number'Image),
Error => "Failed to close pull request via REST API",
Kind => PATCH,
Args => "state" = "closed");
begin
Comment (Number,
"Closed using `alr " & Version.Current & "` with reason: "
& Reason);
end Close;
----------
-- Fork --
----------
function Fork
(User : String := User_Info.User_GitHub_Login;
Owner : String;
Repo : String;
Token : String;
Timeout : Duration := 10.0)
return Async_Result
is
use Ada.Calendar;
Start : constant Time := Clock;
Next : Time := Start + 1.0;
Unused : constant GNATCOLL.JSON.JSON_Value
:= API_Call ("repos" / Owner / Repo / "forks",
Kind => POST,
Token => Token,
Error =>
"Attempt to fork repo [" & Repo & "] owned by ["
& Owner & "] via " & "GitHub REST API failed"
);
begin
declare
Wait : Trace.Ongoing := Trace.Activity ("Waiting for GitHub");
begin
while Clock - Start < Timeout loop
delay until Next;
Next := Next + 1.0;
Wait.Step;
if Repo_Exists (User, Repo) then
return Completed;
end if;
end loop;
end;
return Pending;
end Fork;
-----------------
-- Repo_Exists --
-----------------
function Repo_Exists
(User : String := User_Info.User_GitHub_Login;
Repo : String := Index.Community_Repo_Name)
return Boolean
is (API_Call ("repos" / User / Repo).Succeeded);
-----------------
-- User_Exists --
-----------------
function User_Exists
(User : String := User_Info.User_GitHub_Login)
return Boolean
is (API_Call ("users" / User).Succeeded);
end Alire.GitHub;