forked from pwntester/octo.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.lua
1214 lines (1137 loc) · 36.3 KB
/
provider.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local gh = require "octo.gh"
local graphql = require "octo.gh.graphql"
local navigation = require "octo.navigation"
local previewers = require "octo.pickers.telescope.previewers"
local entry_maker = require "octo.pickers.telescope.entry_maker"
local reviews = require "octo.reviews"
local utils = require "octo.utils"
local octo_config = require "octo.config"
local actions = require "telescope.actions"
local action_set = require "telescope.actions.set"
local action_state = require "telescope.actions.state"
local conf = require("telescope.config").values
local finders = require "telescope.finders"
local pickers = require "telescope.pickers"
local sorters = require "telescope.sorters"
local vim = vim
local M = {}
function M.not_implemented()
utils.error "Not implemented yet"
end
local dropdown_opts = require("telescope.themes").get_dropdown {
layout_config = {
width = 0.4,
height = 15,
},
prompt_title = false,
results_title = false,
previewer = false,
}
local function get_filter(opts, kind)
local filter = ""
local allowed_values = {}
if kind == "issue" then
allowed_values = { "since", "createdBy", "assignee", "mentioned", "labels", "milestone", "states" }
elseif kind == "pull_request" then
allowed_values = { "baseRefName", "headRefName", "labels", "states" }
end
for _, value in pairs(allowed_values) do
if opts[value] then
local val
if #vim.split(opts[value], ",") > 1 then
-- list
val = vim.split(opts[value], ",")
else
-- string
val = opts[value]
end
val = vim.fn.json_encode(val)
val = string.gsub(val, '"OPEN"', "OPEN")
val = string.gsub(val, '"CLOSED"', "CLOSED")
val = string.gsub(val, '"MERGED"', "MERGED")
filter = filter .. value .. ":" .. val .. ","
end
end
return filter
end
local function open(command)
return function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
if command == "default" then
vim.cmd [[:buffer %]]
elseif command == "horizontal" then
vim.cmd [[:sbuffer %]]
elseif command == "vertical" then
vim.cmd [[:vert sbuffer %]]
elseif command == "tab" then
vim.cmd [[:tab sb %]]
end
utils.get(selection.kind, selection.repo, selection.value)
end
end
local function open_preview_buffer(command)
return function(prompt_bufnr)
actions.close(prompt_bufnr)
local preview_bufnr = require("telescope.state").get_global_key "last_preview_bufnr"
if command == "default" then
vim.cmd(string.format(":buffer %d", preview_bufnr))
elseif command == "horizontal" then
vim.cmd(string.format(":sbuffer %d", preview_bufnr))
elseif command == "vertical" then
vim.cmd(string.format(":vert sbuffer %d", preview_bufnr))
elseif command == "tab" then
vim.cmd(string.format(":tab sb %d", preview_bufnr))
end
vim.cmd [[stopinsert]]
end
end
local function open_in_browser()
return function(prompt_bufnr)
local entry = action_state.get_selected_entry(prompt_bufnr)
local number
local repo = entry.repo
if entry.kind ~= "repo" then
number = entry.value
end
actions.close(prompt_bufnr)
navigation.open_in_browser(entry.kind, repo, number)
end
end
local function copy_url()
return function(prompt_bufnr)
local entry = action_state.get_selected_entry(prompt_bufnr)
local url = entry.obj.url
vim.fn.setreg("+", url, "c")
utils.info("Copied '" .. url .. "' to the system clipboard (+ register)")
end
end
--
-- ISSUES
--
local function open_issue_buffer(prompt_bufnr, type)
open(type)(prompt_bufnr)
end
local function develop_issue(prompt_bufnr, type)
local selection = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
utils.develop_issue(selection.obj.number)
end
function M.issues(opts, develop)
local replace
if develop then
replace = develop_issue
else
replace = open_issue_buffer
end
opts = opts or {}
if not opts.states then
opts.states = "OPEN"
end
local filter = get_filter(opts, "issue")
if utils.is_blank(opts.repo) then
opts.repo = utils.get_remote_name()
end
if not opts.repo then
utils.error "Cannot find repo"
return
end
local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.values
local order_by = cfg.issues.order_by
local query = graphql("issues_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
utils.info "Fetching issues (this may take a while) ..."
gh.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = utils.aggregate_pages(output, "data.repository.issues.nodes")
local issues = resp.data.repository.issues.nodes
if #issues == 0 then
utils.error(string.format("There are no matching issues in %s.", opts.repo))
return
end
local max_number = -1
for _, issue in ipairs(issues) do
if #tostring(issue.number) > max_number then
max_number = #tostring(issue.number)
end
end
opts.preview_title = opts.preview_title or ""
opts.prompt_title = opts.prompt_title or ""
opts.results_title = opts.results_title or ""
pickers
.new(opts, {
finder = finders.new_table {
results = issues,
entry_maker = entry_maker.gen_from_issue(max_number),
},
sorter = conf.generic_sorter(opts),
previewer = previewers.issue.new(opts),
attach_mappings = function(_, map)
action_set.select:replace(replace)
map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser())
map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url())
return true
end,
})
:find()
end
end,
}
end
--
-- GISTS
--
local function open_gist(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
local gist = selection.gist
actions.close(prompt_bufnr)
for _, file in ipairs(gist.files) do
local bufnr = vim.api.nvim_create_buf(true, true)
if file.text then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(file.text, "\n"))
else
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, gist.description)
end
vim.api.nvim_buf_set_name(bufnr, file.name)
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_buf_call(bufnr, function()
vim.cmd [[filetype detect]]
end)
end
end
function M.gists(opts)
local privacy
if opts.public then
privacy = "PUBLIC"
elseif opts.secret then
privacy = "SECRET"
else
privacy = "ALL"
end
local query = graphql("gists_query", privacy)
gh.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = utils.aggregate_pages(output, "data.viewer.gists.nodes")
local gists = resp.data.viewer.gists.nodes
opts.preview_title = opts.preview_title or ""
opts.prompt_title = opts.prompt_title or ""
opts.results_title = opts.results_title or ""
pickers
.new(opts, {
finder = finders.new_table {
results = gists,
entry_maker = entry_maker.gen_from_gist(),
},
previewer = previewers.gist.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, map)
map("i", "<CR>", open_gist)
return true
end,
})
:find()
end
end,
}
end
--
-- PULL REQUESTS
--
local function checkout_pull_request()
return function(prompt_bufnr)
local sel = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
utils.checkout_pr(sel.obj.number)
end
end
local function merge_pull_request()
return function(prompt_bufnr)
local sel = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
utils.merge_pr(sel.obj.number)
end
end
function M.pull_requests(opts)
opts = opts or {}
if not opts.states then
opts.states = "OPEN"
end
local filter = get_filter(opts, "pull_request")
if utils.is_blank(opts.repo) then
opts.repo = utils.get_remote_name()
end
if not opts.repo then
utils.error "Cannot find repo"
return
end
local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.values
local order_by = cfg.pull_requests.order_by
local query =
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
utils.info "Fetching pull requests (this may take a while) ..."
gh.run {
args = { "api", "graphql", "--paginate", "--jq", ".", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = utils.aggregate_pages(output, "data.repository.pullRequests.nodes")
local pull_requests = resp.data.repository.pullRequests.nodes
if #pull_requests == 0 then
utils.error(string.format("There are no matching pull requests in %s.", opts.repo))
return
end
local max_number = -1
for _, pull in ipairs(pull_requests) do
if #tostring(pull.number) > max_number then
max_number = #tostring(pull.number)
end
end
opts.preview_title = opts.preview_title or ""
opts.prompt_title = opts.prompt_title or ""
opts.results_title = opts.results_title or ""
pickers
.new(opts, {
finder = finders.new_table {
results = pull_requests,
entry_maker = entry_maker.gen_from_issue(max_number),
},
sorter = conf.generic_sorter(opts),
previewer = previewers.issue.new(opts),
attach_mappings = function(_, map)
action_set.select:replace(function(prompt_bufnr, type)
open(type)(prompt_bufnr)
end)
map("i", cfg.picker_config.mappings.checkout_pr.lhs, checkout_pull_request())
map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser())
map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url())
map("i", cfg.picker_config.mappings.merge_pr.lhs, merge_pull_request())
return true
end,
})
:find()
end
end,
}
end
--
-- COMMITS
--
function M.commits()
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer or not buffer:isPullRequest() then
return
end
-- TODO: graphql
local url = string.format("repos/%s/pulls/%d/commits", buffer.repo, buffer.number)
gh.run {
args = { "api", "--paginate", url },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local results = vim.fn.json_decode(output)
pickers
.new({}, {
prompt_title = false,
results_title = false,
preview_title = false,
finder = finders.new_table {
results = results,
entry_maker = entry_maker.gen_from_git_commits(),
},
sorter = conf.generic_sorter {},
previewer = previewers.commit.new { repo = buffer.repo },
attach_mappings = function()
action_set.select:replace(function(prompt_bufnr, type)
open_preview_buffer(type)(prompt_bufnr)
end)
return true
end,
})
:find()
end
end,
}
end
function M.review_commits(callback)
local current_review = require("octo.reviews").get_current_review()
if not current_review then
utils.error "No review in progress"
return
end
-- TODO: graphql
local url =
string.format("repos/%s/pulls/%d/commits", current_review.pull_request.repo, current_review.pull_request.number)
gh.run {
args = { "api", "--paginate", url },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local results = vim.fn.json_decode(output)
-- add a fake entry to represent the entire pull request
table.insert(results, {
sha = current_review.pull_request.right.commit,
commit = {
message = "[[ENTIRE PULL REQUEST]]",
author = {
name = "",
email = "",
date = "",
},
},
parents = {
{
sha = current_review.pull_request.left.commit,
},
},
})
pickers
.new({}, {
prompt_title = false,
results_title = false,
preview_title = false,
finder = finders.new_table {
results = results,
entry_maker = entry_maker.gen_from_git_commits(),
},
sorter = conf.generic_sorter {},
previewer = previewers.commit.new { repo = current_review.pull_request.repo },
attach_mappings = function()
action_set.select:replace(function(prompt_bufnr)
local commit = action_state.get_selected_entry(prompt_bufnr)
local right = commit.value
local left = commit.parent
actions.close(prompt_bufnr)
callback(right, left)
end)
return true
end,
})
:find()
end
end,
}
end
--
-- FILES
--
function M.changed_files()
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer or not buffer:isPullRequest() then
return
end
local url = string.format("repos/%s/pulls/%d/files", buffer.repo, buffer.number)
gh.run {
args = { "api", "--paginate", url },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local results = vim.fn.json_decode(output)
pickers
.new({}, {
prompt_title = false,
results_title = false,
preview_title = false,
finder = finders.new_table {
results = results,
entry_maker = entry_maker.gen_from_git_changed_files(),
},
sorter = conf.generic_sorter {},
previewer = previewers.changed_files.new { repo = buffer.repo, number = buffer.number },
attach_mappings = function()
action_set.select:replace(function(prompt_bufnr, type)
open_preview_buffer(type)(prompt_bufnr)
end)
return true
end,
})
:find()
end
end,
}
end
---
-- SEARCH
---
function M.search(opts)
opts = opts or {}
local cfg = octo_config.values
local requester = function()
return function(prompt)
if not opts.prompt and utils.is_blank(prompt) then
return {}
end
if type(opts.prompt) == "string" then
opts.prompt = { opts.prompt }
end
local results = {}
for _, val in ipairs(opts.prompt) do
local _prompt = prompt
if val then
_prompt = string.format("%s %s", val, _prompt)
end
local query = graphql("search_query", _prompt)
local output = gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
mode = "sync",
}
if output then
local resp = vim.fn.json_decode(output)
for _, issue in ipairs(resp.data.search.nodes) do
table.insert(results, issue)
end
end
end
return results
end
end
local finder = finders.new_dynamic {
fn = requester(),
entry_maker = entry_maker.gen_from_issue(6),
}
if opts.static then
local results = requester() ""
finder = finders.new_table {
results = results,
entry_maker = entry_maker.gen_from_issue(6, true),
}
end
opts.preview_title = opts.preview_title or ""
opts.prompt_title = opts.prompt_title or ""
opts.results_title = opts.results_title or ""
pickers
.new(opts, {
finder = finder,
sorter = conf.generic_sorter(opts),
previewer = previewers.issue.new(opts),
attach_mappings = function(_, map)
action_set.select:replace(function(prompt_bufnr, type)
open(type)(prompt_bufnr)
end)
map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser())
map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url())
return true
end,
})
:find()
end
---
-- REVIEW COMMENTS
---
function M.pending_threads(threads)
local max_linenr_length = -1
for _, thread in ipairs(threads) do
max_linenr_length = math.max(max_linenr_length, #tostring(thread.startLine))
max_linenr_length = math.max(max_linenr_length, #tostring(thread.line))
end
pickers
.new({}, {
prompt_title = false,
results_title = false,
preview_title = false,
finder = finders.new_table {
results = threads,
entry_maker = entry_maker.gen_from_review_thread(max_linenr_length),
},
sorter = conf.generic_sorter {},
previewer = previewers.review_thread.new {},
attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr)
local thread = action_state.get_selected_entry(prompt_bufnr).thread
actions.close(prompt_bufnr)
reviews.jump_to_pending_review_thread(thread)
end)
return true
end,
})
:find()
end
---
-- PROJECTS
---
function M.select_project_card(cb)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
local cards = buffer.node.projectCards
if not cards or #cards.nodes == 0 then
utils.error "Cant find any project cards"
return
end
if #cards.nodes == 1 then
cb(cards.nodes[1].id)
else
local opts = vim.deepcopy(dropdown_opts)
pickers
.new(opts, {
finder = finders.new_table {
results = cards.nodes,
entry_maker = entry_maker.gen_from_project_card(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local source_card = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(source_card.card.id)
end)
return true
end,
})
:find()
end
end
function M.select_target_project_column(cb)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end
local query = graphql("projects_query", buffer.owner, buffer.name, vim.g.octo_viewer, buffer.owner)
gh.run {
args = { "api", "graphql", "--paginate", "-f", string.format("query=%s", query) },
cb = function(output)
if output then
local resp = vim.fn.json_decode(output)
local projects = {}
local user_projects = resp.data.user and resp.data.user.projects.nodes or {}
local repo_projects = resp.data.repository and resp.data.repository.projects.nodes or {}
local org_projects = not resp.errors and resp.data.organization.projects.nodes or {}
vim.list_extend(projects, repo_projects)
vim.list_extend(projects, user_projects)
vim.list_extend(projects, org_projects)
if #projects == 0 then
utils.error(string.format("There are no matching projects for %s.", buffer.repo))
return
end
local opts = vim.deepcopy(dropdown_opts)
pickers
.new(opts, {
finder = finders.new_table {
results = projects,
entry_maker = entry_maker.gen_from_project(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function()
action_set.select:replace(function(prompt_bufnr)
local selected_project = action_state.get_selected_entry(prompt_bufnr)
actions._close(prompt_bufnr, true)
local opts2 = vim.deepcopy(dropdown_opts)
pickers
.new(opts2, {
finder = finders.new_table {
results = selected_project.project.columns.nodes,
entry_maker = entry_maker.gen_from_project_column(),
},
sorter = conf.generic_sorter(opts2),
attach_mappings = function()
action_set.select:replace(function(prompt_bufnr2)
local selected_column = action_state.get_selected_entry(prompt_bufnr2)
actions.close(prompt_bufnr2)
cb(selected_column.column.id)
end)
return true
end,
})
:find()
end)
return true
end,
})
:find()
end
end,
}
end
--
-- LABELS
--
function M.select_label(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end
local query = graphql("labels_query", buffer.owner, buffer.name)
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local labels = resp.data.repository.labels.nodes
pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
end)
return true
end,
})
:find()
end
end,
}
end
function M.select_assigned_label(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end
local query, key
if buffer:isIssue() then
query = graphql("issue_labels_query", buffer.owner, buffer.name, buffer.number)
key = "issue"
elseif buffer:isPullRequest() then
query = graphql("pull_request_labels_query", buffer.owner, buffer.name, buffer.number)
key = "pullRequest"
end
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local labels = resp.data.repository[key].labels.nodes
pickers
.new(opts, {
finder = finders.new_table {
results = labels,
entry_maker = entry_maker.gen_from_label(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_label = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_label.label.id)
end)
return true
end,
})
:find()
end
end,
}
end
--
-- ASSIGNEES
--
local function get_user_requester()
return function(prompt)
-- skip empty queries
if not prompt or prompt == "" or utils.is_blank(prompt) then
return {}
end
local query = graphql("users_query", prompt)
local output = gh.run {
args = { "api", "graphql", "--paginate", "-f", string.format("query=%s", query) },
mode = "sync",
}
if output then
return {}
end
local users = {}
local orgs = {}
local responses = utils.get_pages(output)
for _, resp in ipairs(responses) do
for _, user in ipairs(resp.data.search.nodes) do
if not user.teams then
-- regular user
if not vim.tbl_contains(vim.tbl_keys(users), user.login) then
users[user.login] = {
id = user.id,
login = user.login,
}
end
elseif user.teams and user.teams.totalCount > 0 then
-- organization, collect all teams
if not vim.tbl_contains(vim.tbl_keys(orgs), user.login) then
orgs[user.login] = {
id = user.id,
login = user.login,
teams = user.teams.nodes,
}
else
vim.list_extend(orgs[user.login].teams, user.teams.nodes)
end
end
end
end
local results = {}
-- process orgs with teams
for _, user in pairs(users) do
table.insert(results, user)
end
for _, org in pairs(orgs) do
org.login = string.format("%s (%d)", org.login, #org.teams)
table.insert(results, org)
end
return results
end
end
local function get_users(query_name, node_name)
local repo = utils.get_remote_name()
local owner, name = utils.split_repo(repo)
local query = graphql(query_name, owner, name, { escape = true })
local output = gh.run {
args = { "api", "graphql", "--paginate", "-f", string.format("query=%s", query) },
mode = "sync",
}
if not output then
return {}
end
local responses = utils.get_pages(output)
local users = {}
for _, resp in ipairs(responses) do
local nodes = resp.data.repository[node_name].nodes
for _, user in ipairs(nodes) do
table.insert(users, {
id = user.id,
login = user.login,
})
end
end
return users
end
local function get_assignable_users()
return get_users("assignable_users_query", "assignableUsers")
end
local function get_mentionable_users()
return get_users("mentionable_users_query", "mentionableUsers")
end
local function create_user_finder()
local cfg = octo_config.values
local finder
local user_entry_maker = entry_maker.gen_from_user()
if cfg.users == "search" then
finder = finders.new_dynamic {
entry_maker = user_entry_maker,
fn = get_user_requester(),
}
elseif cfg.users == "assignable" then
finder = finders.new_table {
results = get_assignable_users(),
entry_maker = user_entry_maker,
}
else
finder = finders.new_table {
results = get_mentionable_users(),
entry_maker = user_entry_maker,
}
end
return finder
end
function M.select_user(cb)
local opts = vim.deepcopy(dropdown_opts)
opts.layout_config = {
width = 0.4,
height = 15,
}
local finder = create_user_finder()
pickers
.new(opts, {
finder = finder,
sorter = sorters.get_fuzzy_file(opts),
attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr)
local selected_user = action_state.get_selected_entry(prompt_bufnr)
actions._close(prompt_bufnr, true)
if not selected_user.teams then
-- user
cb(selected_user.value)
else
-- organization, pick a team
pickers
.new(opts, {
prompt_title = false,
results_title = false,
preview_title = false,
finder = finders.new_table {
results = selected_user.teams,
entry_maker = entry_maker.gen_from_team(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr)
local selected_team = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_team.team.id)
end)
return true
end,
})
:find()
end
end)
return true
end,
})
:find()
end
--
-- ASSIGNEES
--
function M.select_assignee(cb)
local opts = vim.deepcopy(dropdown_opts)
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer then
return
end
local query, key
if buffer:isIssue() then
query = graphql("issue_assignees_query", buffer.owner, buffer.name, buffer.number)
key = "issue"
elseif buffer:isPullRequest() then
query = graphql("pull_request_assignees_query", buffer.owner, buffer.name, buffer.number)
key = "pullRequest"
end
gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
local assignees = resp.data.repository[key].assignees.nodes
pickers
.new(opts, {
finder = finders.new_table {
results = assignees,
entry_maker = entry_maker.gen_from_user(),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
actions.select_default:replace(function(prompt_bufnr)
local selected_assignee = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selected_assignee.user.id)
end)
return true
end,
})
:find()
end