-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
init.lua
683 lines (626 loc) · 19.2 KB
/
init.lua
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
local graphql = require('litee.gh.ghcli.graphql')
local lib_notify = require('litee.lib.notify')
local M = {}
local function json_decode_safe(output)
local success, decoded = pcall(function () return vim.json.decode(output) end)
if success then
return decoded
else
return {message = "json decode error"}
end
end
-- gh_exec executs the (assumed) gh command which returns json.
--
-- if nil is returned the second returned argument is the error output.
--
-- if successful the json is decoded into a lua dictionary and returned.
local function gh_exec(cmd, no_json_decode)
local output = vim.fn.system(cmd)
if vim.v.shell_error ~= 0 then
return nil
end
if no_json_decode then
return output
end
local tbl = json_decode_safe(output)
if tbl["message"] ~= nil then
return nil
end
return tbl, ""
end
local function check_error(data)
if data["errors"] ~= nil then
for _, e in ipairs(data["errors"]) do
return e["type"]
end
end
if data["message"] ~= nil then
return data["message"]
end
return false
end
local function async_request(args, on_read, paginate, page, paged_data)
local buffer = ""
local stdout = vim.loop.new_pipe()
local stderr = vim.loop.new_pipe()
local handle = nil
if paginate then
if page ~= nil then
args[#args] = "page=" .. page
else
page = 1
table.insert(args, "-F")
table.insert(args, "page=" .. page)
end
end
handle = vim.loop.spawn('gh', {
args = args,
stdio = {nil, stdout, stderr},
},
function()
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
handle:close()
end
)
vim.loop.read_start(stdout, function(err, data)
if err then
return err, nil
end
if data then
buffer = buffer .. data
end
if data == nil then
data = json_decode_safe(buffer)
err = check_error(data)
if err ~= false then
vim.schedule(function() on_read(err, nil) end)
return
end
if paged_data == nil then
paged_data = data
else
for _, i in pairs(data) do
table.insert(paged_data, i)
end
end
if paginate then
if #data > 0 then
-- paginate
async_request(args, on_read, paginate, page+1, paged_data)
return
end
end
vim.schedule(function() on_read(false, paged_data) end)
end
end)
vim.loop.read_start(stderr, function(err, data)
vim.schedule(
function()
if err then
return err, nil
end
if data ~= nil then
data = json_decode_safe(buffer)
err = check_error(data)
if err ~= false then
vim.schedule(function() on_read(err, nil) end)
return
end
vim.schedule(function () on_read("UNKNOWN", data) end)
end
end)
end)
end
function M.list_collaborators_async(on_read)
local args = {"api", "-X", "GET", "-F", "per_page=100", "/repos/{owner}/{repo}/collaborators"}
async_request(args, on_read, true)
end
function M.list_repo_issues_async(on_read)
local args = {"api", "-X", "GET", "-F", "per_page=100", "/repos/{owner}/{repo}/issues"}
async_request(args, on_read)
end
function M.list_all_repo_issues_async(on_read)
lib_notify.notify_popup_with_timeout("Fetching all repo issues this could take a bit...", 7500, "info")
local args = {"api", "-X", "GET", "-F", "per_page=100", "/repos/{owner}/{repo}/issues", "-q", '. | map(select(. | (has("pull_request") | not)))'}
async_request(args, on_read, true)
end
function M.get_user_async(on_read)
local args = {"api", "/user"}
async_request(args, on_read)
end
function M.get_pull_files_async(pull_number, on_read)
local args = {"api", "-X", "GET", "-F", "per_page=100", string.format("/repos/{owner}/{repo}/pulls/%d/files", pull_number)}
async_request(args, on_read, true)
end
-- lists all pull requests for the repo in `cwd`
--
-- return @table: https://docs.github.com/en/rest/reference/pulls#list-pull-requests
function M.list_pulls()
local cmd = [[gh pr list --limit 100 --json "number,title,author"]]
return gh_exec(cmd)
end
function M.get_pull_async(pull_number, on_read)
local args = {"api", "/repos/{owner}/{repo}/pulls/" .. pull_number}
async_request(args, on_read)
end
function M.list_pulls_async(on_read)
local args = {"api", "-X", "GET", "-F", "per_page=100", "/repos/{owner}/{repo}/pulls/"}
async_request(args, on_read)
end
function M.update_issue_body_async(number, body, on_read)
local args = {
'api',
"-X",
"POST",
string.format("/repos/{owner}/{repo}/issues/%d", number),
'-f',
string.format([[body=%s]], body)
}
async_request(args, on_read)
end
function M.update_pull_body_async(pull_number, body, on_read)
local args = {
'api',
"-X",
"POST",
string.format("/repos/{owner}/{repo}/pulls/%d", pull_number),
'-f',
string.format([[body=%s]], body)
}
async_request(args, on_read)
end
function M.get_pull_commits_async(pull_number, on_read)
local args = {"api", "-X", "GET", "-F", "per_page=100", string.format([[/repos/{owner}/{repo}/pulls/%d/commits]], pull_number)}
async_request(args, on_read)
end
-- get the details of a commit including the files changed.
--
-- return @table: https://docs.github.com/en/rest/reference/commits#get-a-commit
function M.get_commit(ref)
local cmd = string.format([[gh api /repos/{owner}/{repo}/commits/%s]], ref)
return gh_exec(cmd)
end
function M.get_pull_issue_comments_async(pull_number, on_read)
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('number=%d', pull_number),
'-f',
string.format('query=%s', graphql.issue_comments_query)
}
async_request(args, on_read)
end
function M.get_pull_issue_comments_async_paginated(pull_number, on_read)
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('number=%d', pull_number),
'-f',
string.format('query=%s', graphql.issue_comments_query)
}
local paginated_data = nil
function paginate(err, data)
if err then
vim.schedule(function () lib_notify.notify_popup_with_timeout("Failed to fetch pull request issue comments: " .. err, 7500, "error") end)
return
end
if paginated_data == nil then
paginated_data = data
else
for _, edge in ipairs(data["data"]["repository"]["pullRequest"]["comments"]["edges"]) do
table.insert(paginated_data["data"]["repository"]["pullRequest"]["comments"]["edges"], edge)
end
end
local hasNextPage = data["data"]["repository"]["pullRequest"]["comments"]["pageInfo"]["hasNextPage"]
local endCursor = data["data"]["repository"]["pullRequest"]["comments"]["pageInfo"]["endCursor"]
if hasNextPage then
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('number=%d', pull_number),
'-F',
string.format('cursor=%s', endCursor),
'-f',
string.format('query=%s', graphql.issue_comments_query_cursor)
}
async_request(args, paginate)
else
on_read(err, paginated_data)
end
end
async_request(args, paginate)
end
function M.create_pull_issue_comment(number, body)
local cmd = string.format([[gh api -X POST /repos/{owner}/{repo}/issues/%d/comments -f body=%s]], number, body)
return gh_exec(cmd)
end
function M.update_pull_issue_comment(id, body)
local cmd = string.format([[gh api -X PATCH /repos/{owner}/{repo}/issues/comments/%d -f body=%s]], id, body)
return gh_exec(cmd)
end
function M.delete_pull_issue_comment(id)
local cmd = string.format([[gh api -X DELETE /repos/{owner}/{repo}/issues/comments/%d]], id)
return gh_exec(cmd, true)
end
function M.get_issue_async(number, on_read)
local args = {
'api',
'/repos/{owner}/{repo}/issues/' .. number
}
async_request(args, on_read)
end
function M.get_issue_comments_async(number, on_read)
local args = {
'api',
'-X',
'GET',
'-F',
'per_page=100',
'/repos/{owner}/{repo}/issues/' .. number .. '/comments'
}
async_request(args, on_read, true)
end
function M.get_review_threads_async(pull_number, on_read)
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('pull_number=%d', pull_number),
'-f',
string.format('query=%s', graphql.review_threads_query)
}
async_request(args, on_read)
end
-- because graphql really sucks, write a special paginated function for this.
function M.get_review_threads_async_paginated(pull_number, on_read)
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('pull_number=%d', pull_number),
'-f',
string.format('query=%s', graphql.review_threads_query)
}
local paginated_data = nil
function paginate(err, data)
if err then
vim.schedule(function () lib_notify.notify_popup_with_timeout("Failed to fetch review threads: " .. err, 7500, "error") end)
return
end
if paginated_data == nil then
paginated_data = data
else
for _, edge in ipairs(data["data"]["repository"]["pullRequest"]["reviewThreads"]["edges"]) do
table.insert(paginated_data["data"]["repository"]["pullRequest"]["reviewThreads"]["edges"], edge)
end
end
local hasNextPage = data["data"]["repository"]["pullRequest"]["reviewThreads"]["pageInfo"]["hasNextPage"]
local endCursor = data["data"]["repository"]["pullRequest"]["reviewThreads"]["pageInfo"]["endCursor"]
if hasNextPage then
local args = {
'api',
'graphql',
'-F',
'owner={owner}',
'-F',
'name={repo}',
'-F',
string.format('pull_number=%d', pull_number),
'-F',
string.format('cursor=%s', endCursor),
'-f',
string.format('query=%s', graphql.review_threads_query_cursor)
}
async_request(args, paginate)
else
on_read(err, paginated_data)
end
end
async_request(args, paginate)
end
function M.resolve_thread(thread_id)
local cmd = string.format([[gh api graphql -F thread_id="%s" -f query='%s']],
thread_id,
graphql.resolve_thread
)
local resp = gh_exec(cmd)
if resp == nil then
return nil
end
return resp
end
function M.unresolve_thread(thread_id)
local cmd = string.format([[gh api graphql -F thread_id="%s" -f query='%s']],
thread_id,
graphql.unresolve_thread
)
local resp = gh_exec(cmd)
if resp == nil then
return nil
end
return resp
end
function M.create_comment(pull_number, commit_sha, path, position, side, line, body)
local cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/comments -f commit_id=%s -f path=%s -f side=%s -F position=%d -F line=%d -f body=%s]],
pull_number,
commit_sha,
path,
side,
position,
line,
body
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.create_comment_multiline(pull_number, commit_sha, path, position, side, start_line, line, body)
local cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/comments -f commit_id=%s -f path=%s -f start_side=%s -f side=%s -F position=%d -F start_line=%d -F line=%d -f body=%s]],
pull_number,
commit_sha,
path,
side,
side,
position,
start_line,
line,
body
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
-- this is a graphql query so pass use the node_id for each argument that wants
-- and id.
function M.create_comment_review(pull_id, review_id, body, path, line, side)
local cmd = string.format([[gh api graphql -F pull="%s" -F review="%s" -F body=%s -F path="%s" -F line=%d -F side=%s -f query='%s']],
pull_id,
review_id,
body,
path,
line,
side,
graphql.create_comment_review
)
local resp = gh_exec(cmd)
if resp == nil then
return nil
end
return resp
end
-- this is a graphql query so pass use the node_id for each argument that wants
-- and id.
function M.create_comment_review_multiline(pull_id, review_id, body, path, start_line, line, side)
local cmd = string.format([[gh api graphql -F pull="%s" -F review="%s" -F body=%s -F path="%s" -F start_line=%d -F line=%d -F start_side=%s -F side=%s -f query='%s']],
pull_id,
review_id,
body,
path,
start_line,
line,
side,
side,
graphql.create_comment_review_multiline
)
local resp = gh_exec(cmd)
if resp == nil then
return nil
end
return resp
end
-- reply_comment replies to a comment outside of any review.
function M.reply_comment(pull_number, comment_rest_id, body)
local cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/comments/%s/replies -f body=%s]],
pull_number,
comment_rest_id,
body
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
-- reply_comment_review replies to a comment inside a review.
-- this is a graphql query so use "node_id" for all ids.
function M.reply_comment_review(pull_id, review_id, commit_sha, body, reply_id)
local cmd = string.format([[gh api graphql -F pull="%s" -F review="%s" -F commit="%s" -F body=%s -F reply="%s" -f query='%s']],
pull_id,
review_id,
commit_sha,
body,
reply_id,
graphql.reply_comment_review
)
local resp = gh_exec(cmd)
if resp == nil then
return nil
end
return resp
end
function M.update_comment(comment_rest_id, body)
local cmd = string.format([[gh api --method PATCH -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/comments/%d -f body=%s]],
comment_rest_id,
body
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.delete_comment(comment_rest_id)
local cmd = string.format([[gh api --method DELETE -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/comments/%d]],
comment_rest_id
)
local out = gh_exec(cmd, true)
if out == nil then
return nil
end
return out
end
function M.list_reviews_async(pull_number, on_read)
local args = {
"api",
'-X',
'GET',
'-F',
'per_page=100',
string.format("/repos/{owner}/{repo}/pulls/%d/reviews", pull_number)
}
async_request(args, on_read, true)
end
function M.add_reaction(id, reaction, on_read)
local args = {
'api',
'graphql',
'-F',
string.format('id=%s', id),
'-F',
string.format('content=%s', reaction),
'-f',
string.format('query=%s', graphql.add_reaction)
}
async_request(args, on_read)
end
function M.remove_reaction_async(id, reaction, on_read)
local args = {
'api',
'graphql',
'-F',
string.format('id=%s', id),
'-F',
string.format('content=%s', reaction),
'-f',
string.format('query=%s', graphql.remove_reaction)
}
async_request(args, on_read)
end
function M.create_review(pull_number, commit_id)
local cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/reviews -f commit_id=%s]],
pull_number,
commit_id
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.delete_review(pull_number, review_id)
local cmd = string.format([[gh api --method DELETE -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/reviews/%s]],
pull_number,
review_id
)
local out = gh_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.submit_review(pull_number, review_id, body, event)
local cmd = nil
if body ~= nil then
cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/reviews/%s/events -f event=%s -f body=%s]],
pull_number,
review_id,
event,
body
)
else
cmd = string.format([[gh api --method POST -H "Accept: application/vnd.github.v3+json" /repos/{owner}/{repo}/pulls/%d/reviews/%s/events -f event=%s ]],
pull_number,
review_id,
event
)
end
local out = gh_exec(cmd, true)
if out == nil then
return nil
end
return out
end
function M.list_labels_async(on_read)
local args = {
'api',
'-X',
'GET',
'-F',
'per_page=100',
'/repos/{owner}/{repo}/labels'
}
async_request(args, on_read, true)
end
function M.add_label_async(number, label, on_read)
local args = {
"issue",
"edit",
number,
"--add-label",
label
}
async_request(args, on_read)
end
function M.remove_label_async(number, label, on_read)
local args = {
"issue",
"edit",
number,
"--remove-label",
label
}
async_request(args, on_read)
end
function M.get_check_suites_async(commit_sha, on_read)
local args = {
'api',
'-X',
'GET',
'-F',
'per_page=100',
string.format("/repos/{owner}/{repo}/commits/%s/check-suites", commit_sha)
}
async_request(args, on_read, true)
end
function M.get_check_runs_by_suite(suite_id, on_read)
local args = {
'api',
'-X',
'GET',
'-F',
'per_page=100',
string.format("/repos/{owner}/{repo}/check-suites/%s/check-runs", suite_id)
}
async_request(args, on_read, true)
end
return M