-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconsult-gh-embark.el
1193 lines (1025 loc) · 50.7 KB
/
consult-gh-embark.el
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
;;; consult-gh-embark.el --- Embark Actions for consult-gh -*- lexical-binding: t -*-
;; Copyright (C) 2023 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2023
;; Version: 2.4
;; Package-Requires: ((emacs "29.4") (consult "2.0") (consult-gh "2.4") (embark-consult "1.1"))
;; Homepage: https://github.com/armindarvish/consult-gh
;; Keywords: matching, git, repositories, forges, completion
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides embark actions for consult-gh.
;; (see URL `https://github.com/armindarvish/consult-gh' for more info).
;;; Code:
;;; Requirements
(require 'embark-consult)
(require 'consult-gh)
;;; Define Embark Action Functions
;;;; Default Actions
(defun consult-gh-embark-default-action (cand)
"Open CAND link in an Emacs buffer."
(when (stringp cand)
(let* ((class (get-text-property 0 :class cand)))
(consult-gh-with-host
(consult-gh--auth-account-host)
(pcase class
("code"
(funcall consult-gh-code-action cand))
("issue"
(funcall consult-gh-issue-action cand))
("pr"
(funcall consult-gh-pr-action cand))
("file"
(funcall consult-gh-file-action cand))
("notification"
(funcall consult-gh-notifications-action cand))
("dashboard"
(funcall consult-gh-dashboard-action cand))
(_
(funcall consult-gh-repo-action cand)))))))
(defun consult-gh-embark-show-preview (cand)
"Open preview of CAND."
(when (stringp cand)
(let* ((class (get-text-property 0 :class cand)))
(consult-gh-with-host
(consult-gh--auth-account-host)
(pcase class
("code"
(funcall (consult-gh--code-state) 'preview cand))
("issue"
(funcall (consult-gh--issue-state) 'preview cand))
("pr"
(funcall (consult-gh--pr-state) 'preview cand))
("file"
(funcall (consult-gh--file-state) 'preview cand))
("notification"
(funcall (consult-gh--notifications-state) 'preview cand))
("dashboard"
(funcall (consult-gh--dashboard-state) 'preview cand))
(_
(funcall (consult-gh--repo-state) 'preview cand)))))))
;;;; Add/Remove from Favorites
(defun consult-gh-embark-add-repo-to-known-repos (cand)
"Add CAND repo to `consult-gh--known-repos-list'."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand)))
(add-to-list 'consult-gh--known-repos-list repo))))
(defun consult-gh-embark-remove-repo-from-known-repos (cand)
"Remove CAND repo from `consult-gh--known-repos-list'."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand)))
(setq consult-gh--known-repos-list (delete repo consult-gh--known-repos-list)))))
(defun consult-gh-embark-add-org-to-known-orgs (cand)
"Add CAND org to `consult-gh--known-orgs-list'."
(when (stringp cand)
(let* ((org (get-text-property 0 :user cand)))
(add-to-list 'consult-gh--known-orgs-list (format "%s" org)))))
(defun consult-gh-embark-remove-org-from-known-orgs (cand)
"Remove CAND org from `consult-gh--known-orgs-list'."
(when (stringp cand)
(let* ((org (get-text-property 0 :user cand)))
(setq consult-gh--known-orgs-list (delete org consult-gh--known-orgs-list)))))
(defun consult-gh-embark-add-org-to-favorite-list (cand)
"Add CAND org to `consult-gh--known-orgs-list'."
(when (stringp cand)
(let* ((org (get-text-property 0 :user cand)))
(add-to-list 'consult-gh-favorite-orgs-list (format "%s" org)))))
(define-obsolete-function-alias 'consult-gh-embark-add-org-to-default-list #'consult-gh-embark-add-org-to-favorite-list "2.0")
(defun consult-gh-embark-remove-org-from-favorite-list (cand)
"Remove CAND org from `consult-gh--known-orgs-list'."
(when (stringp cand)
(let* ((org (get-text-property 0 :user cand)))
(setq consult-gh-favorite-orgs-list (delete org consult-gh-favorite-orgs-list)))))
(define-obsolete-function-alias 'consult-gh-embark-remove-org-from-default-list #'consult-gh-embark-remove-org-from-favorite-list "2.0")
;;;; Get other props
(defun consult-gh-embark-get-title (cand)
"Get the title of CAND.
When `current-prefix-args' is non-nil, add repository's name at the front."
(when (stringp cand)
(let* ((class (get-text-property 0 :class cand))
(repo (get-text-property 0 :repo cand))
(title (get-text-property 0 :title cand))
(number (get-text-property 0 :number cand))
(path (get-text-property 0 :path cand))
(title (pcase class
((or "file" "code")
(if current-prefix-arg
(format "%s/%s" repo path)
(format "%s" path)))
((or "issue" "pr")
(if current-prefix-arg
(format "%s/#%s: %s" repo number title)
(concat "#" (format "%s: %s" number title))))
(_ (format "%s" repo)))))
(string-trim title))))
(defun consult-gh-embark-get-url (cand)
"Get url link of CAND.
The candidate can be a repo, issue, PR, file path, or a branch."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((repo (get-text-property 0 :repo cand))
(class (or (get-text-property 0 :class cand) nil))
(number (or (get-text-property 0 :number cand) nil))
(path (or (get-text-property 0 :path cand) nil))
(branch (or (get-text-property 0 :branch cand) nil)))
(pcase class
("issue"
(string-trim (concat (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")) (format "/issues/%s" number))))
((or "file" "code")
(string-trim (concat (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")) (format "/blob/%s/%s" (or branch "HEAD") path))))
("pr"
(string-trim (concat (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")) (format "/pull/%s" number))))
(_
(string-trim (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")))))))))
(defun consult-gh-embark-get-other-repos-by-same-user (cand)
"List other repos by the same user/organization as CAND at point."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(consult-gh-repo-list user))))
(defun consult-gh-embark-get-user-name (cand)
"Get the name of the user from CAND at point."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(consult-gh--get-user-fullname user))))
(defun consult-gh-embark-get-user-email (cand)
"Get the email of the user from CAND at point."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(consult-gh--get-user-email user))))
(defun consult-gh-embark-get-user-link (cand)
"Get the email of the user from CAND at point."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(consult-gh--get-user-link user))))
;;;; Open Actions
(defun consult-gh-embark-open-in-system-browser (cand)
"Open the url link of CAND in the system's default browser."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(class (or (get-text-property 0 :class cand) nil))
(number (or (get-text-property 0 :number cand) nil))
(path (or (get-text-property 0 :path cand) nil))
(branch (or (get-text-property 0 :branch cand) nil)))
(consult-gh-with-host
(consult-gh--auth-account-host)
(pcase class
("issue"
(consult-gh--call-process "issue" "view" "--web" "--repo" (substring-no-properties repo) (substring-no-properties number)))
("file"
(funcall #'browse-url (concat (string-trim (consult-gh--command-to-string "browse" "--repo" repo "--no-browser")) (format "/blob/%s/" (or branch "HEAD")) path)))
("pr"
(consult-gh--call-process "pr" "view" "--web" "--repo" (substring-no-properties repo) (substring-no-properties number)))
(_
(consult-gh--call-process "repo" "view" "--web" (substring repo))))))))
(defun consult-gh-embark-open-in-default-browser (cand)
"Open the url link of CAND with `consult-gh-browse-url-func'."
(when (stringp cand)
(let* ((url (consult-gh-embark-get-url cand)))
(funcall consult-gh-browse-url-func url))))
(defun consult-gh-embark-open-repo-in-system-browser (cand)
"Open the url link for user in CAND in the system's default browser."
(when (stringp cand)
(if-let* ((repo (get-text-property 0 :repo cand)))
(consult-gh-with-host
(consult-gh--auth-account-host)
(consult-gh--call-process "repo" "view" "--web" (substring repo)))
(message "No repo at point!"))))
(defun consult-gh-embark-open-repo-in-default-browser (cand)
"Open the url link for user in CAND in the system's default browser."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(if-let* ((repo (get-text-property 0 :repo cand))
(url (string-trim (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")))))
(funcall consult-gh-browse-url-func url)
(message "No repo link at point!")))))
(defun consult-gh-embark-user-open-in-system-browser (cand)
"Open the url link for user in CAND in the system's default browser."
(when (stringp cand)
(if-let* ((url (consult-gh-embark-get-user-link cand)))
(funcall #'browse-url url)
(message "No user's link at point!"))))
(defun consult-gh-embark-user-open-in-default-browser (cand)
"Open the url link for user in CAND in the system's default browser."
(when (stringp cand)
(if-let* ((url (consult-gh-embark-get-user-link cand)))
(funcall consult-gh-browse-url-func url)
(message "No user's link at point!"))))
;;;; View Actions
(defun consult-gh-embark-view-issues-of-repo (cand)
"Browse issues of CAND repo at point."
(when (stringp cand)
(let ((repo (or (get-text-property 0 :repo cand))))
(consult-gh-issue-list repo))))
(defun consult-gh-embark-view-prs-of-repo (cand)
"Browse PRs of CAND repo at point."
(when (stringp cand)
(let ((repo (or (get-text-property 0 :repo cand))))
(consult-gh-pr-list repo))))
(defun consult-gh-embark-view-issues-involves-user (cand)
"Browse issues involving the user in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(if-let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo))
(candidates (consult--slow-operation "Collecting issues ..."
(cl-loop for item in (split-string (consult-gh--command-to-string "search" "issues" "--involves" user) "[\r\n]+" t)
collect (consult-gh--search-issues-format item "" nil)))))
(consult--read candidates
:prompt "Select Issue: "
:lookup #'consult--lookup-member
:state (funcall #'consult-gh--issue-state)
:group #'consult-gh--issue-group
:require-match t
:category 'consult-gh-issues
:preview-key consult-gh-preview-key
:sort nil)
(message "No user at point!")))))
(defun consult-gh-embark-view-prs-involves-user (cand)
"Search pullrequests involving the user in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(if-let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo))
(candidates (consult--slow-operation "Collecting pullrequests ..."
(cl-loop for item in (split-string (consult-gh--command-to-string "search" "prs" "--involves" user) "[\r\n]+" t)
collect (consult-gh--search-prs-format item "" nil)))))
(consult--read candidates
:prompt "Select PullRequest: "
:lookup #'consult--lookup-member
:state (funcall #'consult-gh--pr-state)
:group #'consult-gh--pr-search-group
:require-match t
:category 'consult-gh-prs
:preview-key consult-gh-preview-key
:sort nil)
(message "No user at point!")))))
(defun consult-gh-embark-view-user-assignment (cand)
"Search issues and prs assigned to the user in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(if-let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo))
(candidates (consult--slow-operation "Collecting..."
(cl-loop for item in (split-string (consult-gh--command-to-string "search" "issues" "--include-prs" "--involves" user) "[\r\n]+" t)
collect (consult-gh--search-issues-include-prs-format item "" nil)))))
(consult--read candidates
:prompt "Select PullRequest: "
:lookup #'consult--lookup-member
:state (funcall #'consult-gh--dashboard-state)
:group #'consult-gh--dashboard-group
:require-match t
:category 'consult-gh-dashboard
:preview-key consult-gh-preview-key
:sort nil)
(message "No user at point!")))))
(defun consult-gh-embark-view-files-of-repo (cand)
"Browse files of CAND at point."
(when (stringp cand)
(let ((repo (or (get-text-property 0 :repo cand) (consult-gh--nonutf-cleanup cand))))
(consult-gh-find-file repo))))
(defun consult-gh-embark-view-pr-diff (cand)
"View the diff of a pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh--pr-view-diff-action cand))))
;;;; Copy/Kill Actions
(defun consult-gh-embark-copy-title-as-kill (cand)
"Copy the title of CAND to `kill-ring'."
(when (stringp cand)
(if-let* ((title (consult-gh-embark-get-title cand)))
(kill-new (string-trim title)))))
(defun consult-gh-embark-copy-url-as-kill (cand)
"Copy url link of CAND to `kill-ring'.
CAND can be a repo, issue, PR, file path, ..."
(when (stringp cand)
(let* ((url (consult-gh-embark-get-url cand)))
(kill-new url))))
(defun consult-gh-embark-copy-org-link-as-kill (cand)
"Copy the org-format url link of CAND to `kill-ring'."
(when (stringp cand)
(let* ((class (get-text-property 0 :class cand))
(repo (get-text-property 0 :repo cand))
(url (consult-gh-embark-get-url cand))
(title (consult-gh-embark-get-title cand))
(path (or (get-text-property 0 :path cand) nil))
(branch (or (get-text-property 0 :branch cand) nil))
(str nil))
(pcase class
((or "issue" "pr")
(when (not current-prefix-arg) (setq title (concat repo "/" title))))
((or "file" "code")
(when (not current-prefix-arg) (setq title (concat repo "/" branch "/" path)))))
(when url
(setq str
(cond
((and url title) (format " [[%s][%s]] " url title))
(url (format " [[%s]] " url))
(t nil))))
(when (stringp str)
(kill-new str)))))
(defun consult-gh-embark-copy-https-link-as-kill (cand)
"Copy http link of CAND to `kill-ring'."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let ((repo (get-text-property 0 :repo cand)))
(kill-new (concat (string-trim (consult-gh--command-to-string "browse" "--repo" (string-trim repo) "--no-browser")) ".git"))))))
(defun consult-gh-embark-copy-ssh-link-as-kill (cand)
"Copy shh link of CAND to `kill-ring'."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((repo (get-text-property 0 :repo cand)))
(kill-new (consult-gh--json-to-hashtable (consult-gh--command-to-string "repo" "view" (string-trim repo) "--json" "sshUrl") :sshUrl))))))
(defun consult-gh-embark-copy-straight-usepackage-link-as-kill (cand)
"Copy a drop-in “straight use-package” script of CAND to `kill-ring'."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(package (car (last (split-string repo "\/")))))
(kill-new (concat "(use-package " package "\n\t:straight (" package " :type git :host github :repo \"" repo "\")\n)")))))
(defun consult-gh-embark-copy-usepackage-link-as-kill (cand)
"Copy a drop-in “use-package” script of CAND to `kill-ring'."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(package (car (last (split-string repo "\/")))))
(kill-new (concat "(use-package " package)))))
(defun consult-gh-embark-copy-file-contents-as-kill (cand)
"Copy the contents of CAND file to `kill-ring'."
(when (stringp cand)
(let ((url (get-text-property 0 :url cand)))
(when (and url (stringp url))
(kill-new (consult-gh--files-get-content url))))))
(defun consult-gh-embark-copy-user-as-kill (cand)
"Copy the user in CAND to `kill-ring'."
(when (stringp cand)
(when-let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(kill-new user))))
(defun consult-gh-embark-copy-user-name-as-kill (cand)
"Copy the name of the user in CAND to `kill-ring'."
(when (stringp cand)
(when-let ((name (consult-gh-embark-get-user-name cand)))
(kill-new name))))
(defun consult-gh-embark-copy-user-email-as-kill (cand)
"Copy the email of the user in CAND to `kill-ring'."
(when (stringp cand)
(when-let ((email (consult-gh-embark-get-user-email cand)))
(kill-new email))))
(defun consult-gh-embark-copy-user-link-as-kill (cand)
"Copy the link of the user page in CAND to `kill-ring'."
(when (stringp cand)
(when-let ((link (consult-gh-embark-get-user-link cand)))
(kill-new link))))
;;;; Insert Actions
(defun consult-gh-embark-insert-title (cand)
"Insert the title of CAND at point."
(when (stringp cand)
(if-let* ((title (consult-gh-embark-get-title cand)))
(embark-insert (list (string-trim title))))))
(defun consult-gh-embark-insert-url (cand)
"Insert the url of CAND at point."
(when (stringp cand)
(if-let* ((url (consult-gh-embark-get-url cand)))
(embark-insert (list (string-trim url))))))
(defun consult-gh-embark-insert-link (cand)
"Insert the link of CAND at point.
In `org-mode' or `markdown-mode',the link is formatted accordingly."
(when (stringp cand)
(let* ((class (get-text-property 0 :class cand))
(repo (get-text-property 0 :repo cand))
(url (consult-gh-embark-get-url cand))
(title (consult-gh-embark-get-title cand))
(path (or (get-text-property 0 :path cand) nil))
(branch (or (get-text-property 0 :branch cand) nil)))
(pcase class
((or "issue" "pr")
(when (not current-prefix-arg) (setq title (concat repo "/" title))))
((or "file" "code")
(when (not current-prefix-arg) (setq title (concat repo "/" branch "/" path)))))
(when url
(cond
((derived-mode-p 'org-mode)
(insert (cond
((and url title) (format " [[%s][%s]] " url title))
(url (format " [[%s]] " url))
(t ""))))
((derived-mode-p 'markdown-mode)
(insert (cond
((and url title) (format " [%s](%s) " url title))
(url (format " <%s> " url))
(t ""))))
(t
(insert (cond
((and url title) (format " %s (%s) " title url))
(url (format " %s " url))
(t "")))))))))
(defun consult-gh-embark-insert-file-contents (cand)
"Insert the contents of CAND file at point."
(when (stringp cand)
(let ((url (get-text-property 0 :url cand)))
(when (and url (stringp url))
(embark-insert (consult-gh--files-get-content url))))))
(defun consult-gh-embark-insert-user (cand)
"Insert the user in CAND at point."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(user (consult-gh--get-username repo)))
(if user (embark-insert (list user))
(message "No user at point!")))))
(defun consult-gh-embark-insert-user-name (cand)
"Insert the name of the user in CAND at point."
(when (stringp cand)
(if-let ((user (consult-gh-embark-get-user-name cand)))
(embark-insert (list user))
(message "No user at point!"))))
(defun consult-gh-embark-insert-user-email (cand)
"Insert the email of the user in CAND at point."
(when (stringp cand)
(if-let ((email (consult-gh-embark-get-user-email cand)))
(embark-insert (list email))
(message "No email found for user at point!"))))
(defun consult-gh-embark-insert-user-link (cand)
"Copy the org-format url link of CAND to `kill-ring'."
(when (stringp cand)
(let* ((repo (get-text-property 0 :repo cand))
(title (consult-gh--get-username repo))
(url (consult-gh-embark-get-user-link cand)))
(if url
(cond
((derived-mode-p 'org-mode)
(insert (cond
((and url title) (format " [[%s][%s]] " url title))
(url (format " [[%s]] " url))
(t ""))))
((derived-mode-p 'markdown-mode)
(insert (cond
((and url title) (format " [%s](%s) " url title))
(url (format " <%s> " url))
(t ""))))
(t
(insert (cond
((and url title) (format " %s (%s) " title url))
(url (format " %s " url))
(t "")))))
(message "No link found for user at point!")))))
;;;; Svae/Clone/Fork Actions
(defun consult-gh-embark-clone-repo (cand)
"Clone the CAND repo at point."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh--repo-clone-action cand))))
(defun consult-gh-embark-fork-repo (cand)
"Fork the CAND repo at point."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh--repo-fork-action cand))))
(defun consult-gh-embark-save-file (cand)
"Save the CAND file at point."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh--files-save-file-action cand))))
;;;; Create Actions
(defun consult-gh-embark-create-repo (cand)
"Create a new repo with CAND as name."
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-repo-create (and (stringp cand) (string-remove-prefix (consult-gh--get-split-style-character) (substring-no-properties cand))))))
(defun consult-gh-embark-create-issue (cand)
"Create an issue in repo of CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((repo (get-text-property 0 :repo cand))
(class (get-text-property 0 :class cand))
(title (pcase class
((or "repo" "issue") nil)
("pr" (get-text-property 0 :title cand))
(_ (or (get-text-property 0 :title cand) (string-remove-prefix (consult-gh--get-split-style-character) (substring-no-properties cand))))))
(ref (pcase class
((or "pr" "notification" "dashboard" "code" "file")
(consult-gh-embark-get-url cand))
(_ nil)))
(body (when ref (not (string-empty-p ref)) (format "%s" ref))))
(funcall #'consult-gh-issue-create repo title body)))))
(defun consult-gh-embark-create-pr (cand)
"Create a pull request in repo of CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((repo (get-text-property 0 :repo cand))
(class (get-text-property 0 :class cand))
(title (pcase class
((or "repo" "pr") nil)
("issue" (get-text-property 0 :title cand))
(_ (or (get-text-property 0 :title cand) (string-remove-prefix (consult-gh--get-split-style-character) (substring-no-properties cand))))))
(ref (pcase class
((or "issue" "notification" "dashboard" "code" "file")
(consult-gh-embark-get-url cand))
(_ nil)))
(body (when ref (not (string-empty-p ref)) (format "%s" ref))))
(funcall #'consult-gh-pr-create repo title body)))))
;;;; Edit Issue Actions
(defun consult-gh-embark-toggle-issue-open (cand)
"Close/Re-open the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((state (get-text-property 0 :state cand)))
(if (equal state "OPEN")
(funcall #'consult-gh-issue-close cand)
(funcall #'consult-gh-issue-reopen cand))))))
(defun consult-gh-embark-toggle-issue-pin (cand)
"Pin/Unpin the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(when-let* ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand))
(state (when (and repo number)
(consult-gh--json-to-hashtable (consult-gh--command-to-string "issue" "view" number "--repo" repo "--json" "isPinned") :isPinned))))
(if (eq state 't)
(funcall #'consult-gh-issue-unpin cand)
(funcall #'consult-gh-issue-pin cand))))))
(defun consult-gh-embark-toggle-issue-lock (cand)
"Lock/Unlock the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(when-let* ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand))
(state (when (and repo number)
(consult-gh--json-to-hashtable (consult-gh--api-command-string (format "/repos/%s/issues/%s" repo number)) :locked))))
(if (eq state 't)
(funcall #'consult-gh-issue-unlock cand)
(funcall #'consult-gh-issue-lock cand))))))
(defun consult-gh-embark-transfer-issue (cand)
"Transfer the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-issue-transfer cand))))
(defun consult-gh-embark-delete-issue (cand)
"Delete the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-issue-delete cand))))
(defun consult-gh-embark-develop-issue (cand)
"Make a linked branch for the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-issue-develop cand))))
(defun consult-gh-embark-edit-issue (cand)
"Make a linked branch for the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-issue-edit cand))))
(defun consult-gh-embark-comment-on-issue (cand)
"Edit the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand)))
(with-current-buffer (funcall #'consult-gh--issue-view repo number)
(consult-gh-topics-comment-create))))))
;;;; Edit PR Actions
(defun consult-gh-embark-edit-pr (cand)
"Edit the pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-pr-edit cand))))
(defun consult-gh-embark-merge-pr (cand)
"Merge the pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-pr-merge cand))))
(defun consult-gh-embark-review-pr (cand)
"Review the pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(funcall #'consult-gh-pr-review cand))))
(defun consult-gh-embark-comment-on-pr (cand)
"Make a linked branch for the issue in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand)))
(with-current-buffer (funcall #'consult-gh--pr-view repo number)
(consult-gh-topics-comment-create))))))
(defun consult-gh-embark-toggle-pr-open (cand)
"Close/Re-open the pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((state (get-text-property 0 :state cand)))
(if (equal state "OPEN")
(funcall #'consult-gh-pr-close cand)
(funcall #'consult-gh-pr-reopen cand))))))
(defun consult-gh-embark-toggle-pr-lock (cand)
"Lock/Unlock the pull request in CAND."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(when-let* ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand))
(state (when (and repo number)
(consult-gh--json-to-hashtable (consult-gh--api-command-string (format "/repos/%s/pulls/%s" repo number)) :locked))))
(if (eq state 't)
(funcall #'consult-gh-pr-unlock cand)
(funcall #'consult-gh-pr-lock cand))))))
(defun consult-gh-embark-toggle-pr-draft (cand)
"Toggle the pull request in CAND as draft/ready."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(when-let* ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand))
(state (when (and repo number)
(consult-gh--json-to-hashtable (consult-gh--command-to-string "pr" "view" number "--repo" repo "--json" "isDraft") :isDraft))))
(if (eq state 't)
(funcall #'consult-gh-pr-mark-ready cand)
(funcall #'consult-gh-pr-mark-draft cand))))))
(defun consult-gh-embark-link-pr-to-issue (cand)
"Link CAND to Issue or PR.
CAND can be a PR or an issue."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(save-match-data
(when-let* ((repo (get-text-property 0 :repo cand))
(number (get-text-property 0 :number cand))
(class (get-text-property 0 :class cand)))
(pcase class
("pr"
(let* ((issue (get-text-property 0 :number (consult-gh-issue-list repo t)))
(info (consult-gh--pr-read-json repo number))
(old-body (when (and issue (not (string-empty-p issue)) (hash-table-p info)) (gethash :body info)))
(new-body (when (and issue (not (string-match (format "\\(close\\|closes\\|closed\\|fix\\|fixes\\|fixed\\) #%s" issue) old-body)))
(concat (when old-body old-body)
(format "\n\nclose #%s" issue)))))
(when (and number new-body)
(apply #'consult-gh--command-to-string "pr" "edit" number (list "--repo" repo "--body" (substring-no-properties new-body))))))
("issue"
(let* ((pr (consult-gh-pr-list repo t))
(pr-number (get-text-property 0 :number pr))
(info (when (and pr pr-number) (consult-gh--pr-read-json repo pr-number)))
(old-body (when (and pr pr-number (hash-table-p info)) (gethash :body info)))
(new-body (when (and pr-number (not (string-match (format "\\(close\\|closes\\|closed\\|fix\\|fixes\\|fixed\\) #%s" number) old-body)))
(concat (when old-body old-body)
(format "\n\nclose #%s" number)))))
(when (and pr-number new-body)
(apply #'consult-gh--command-to-string "pr" "edit" pr-number (list "--repo" repo "--body" (substring-no-properties new-body))))))
(_ nil)))))))
;;;; Edit Notification Actions
(defun consult-gh-embark-toggle-notification-read (cand)
"Mark the notification in CAND as read/unread."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let ((state (get-text-property 0 :state cand)))
(if (equal state "Unread")
(consult-gh--notifications-mark-as-read cand))))))
(defun consult-gh-embark-notification-toggle-subscription (cand)
"Mark the notification in CAND as read/unread."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let* ((thread (get-text-property 0 :thread cand))
(subscription (consult-gh--json-to-hashtable (consult-gh--api-command-string (format "/notifications/threads/%s/subscription" thread)) :subscribed)))
(if (eq subscription 't)
(consult-gh--notifications-unsubscribe cand)
(consult-gh--notifications-subscribe cand))))))
;;;; Other Actions
(defun consult-gh-embark-email-user (cand)
"Insert the email of the user in CAND at point."
(when (stringp cand)
(if-let ((email (consult-gh-embark-get-user-email cand)))
(compose-mail email)
(message "No email at point!"))))
(defun consult-gh-embark-search-code-in-repo (cand)
"Search for code in CAND repo at point."
(when (stringp cand)
(consult-gh-with-host
(consult-gh--auth-account-host)
(let ((repo (or (get-text-property 0 :repo cand) (substring-no-properties cand))))
(consult-gh-search-code nil repo)))))
;;; Define Embark Keymaps
;;;; General Keymaps
;;;;; Bookmark Menu Keymap
(defvar-keymap consult-gh-embark-bookmark-repos-menu-map
:doc "Keymap for bookmarking repos menu"
:parent nil
"r" '("add to known repos" . consult-gh-embark-add-repo-to-known-repos)
"k" '("remove from known repos" . consult-gh-embark-remove-repo-from-known-repos))
(fset 'consult-gh-embark-bookmark-repos-menu-map consult-gh-embark-bookmark-repos-menu-map)
(defvar-keymap consult-gh-embark-bookmark-orgs-menu-map
:doc "Keymap for bookmarking orgs menu"
:parent nil
"o" '("add to known orgs" . consult-gh-embark-add-org-to-known-orgs)
"k" '("remove from known orgs" . consult-gh-embark-remove-org-from-known-orgs)
"d" '("add to favorite orgs" . consult-gh-embark-add-org-to-favorite-list)
"D" '("remove from favorite orgs" . consult-gh-embark-remove-org-from-favorite-list))
(fset 'consult-gh-embark-bookmark-orgs-menu-map consult-gh-embark-bookmark-orgs-menu-map)
(defvar-keymap consult-gh-embark-bookmarks-menu-map
:doc "Keymap for bookmarks menu"
:parent nil
"r" '("bookmark repos" . consult-gh-embark-bookmark-repos-menu-map)
"o" '("bookmark orgs" . consult-gh-embark-bookmark-orgs-menu-map))
(fset 'consult-gh-embark-bookmarks-menu-map consult-gh-embark-bookmarks-menu-map)
;;;;; Copy Menu Keymap
;;;;;; Copy User's Info Keymap
(defvar-keymap consult-gh-embark-user-copy-menu-map
:doc "Keymap for copying user info as kill menu"
:parent nil
"w" '("GitHubusername" . consult-gh-embark-copy-user-as-kill)
"n" '("full name" . consult-gh-embark-copy-user-name-as-kill)
"e" '("email" . consult-gh-embark-copy-user-email-as-kill)
"l" '("user page" . consult-gh-embark-copy-user-link-as-kill))
(fset 'consult-gh-embark-user-copy-menu-map consult-gh-embark-user-copy-menu-map)
(defvar-keymap consult-gh-embark-copy-menu-map
:doc "Keymap for copy-as-kill menu"
:parent nil
"t" '("title" . consult-gh-embark-copy-title-as-kill)
"u" '("url link" . consult-gh-embark-copy-url-as-kill)
"h" '("https link" . consult-gh-embark-copy-https-link-as-kill)
"s" '("ssh link" . consult-gh-embark-copy-ssh-link-as-kill)
"o" '("org-mode link" . consult-gh-embark-copy-org-link-as-kill)
"U" '("user's info" . consult-gh-embark-user-copy-menu-map))
(fset 'consult-gh-embark-copy-menu-map consult-gh-embark-copy-menu-map)
;;;;; Create Menu Keymap
(defvar-keymap consult-gh-embark-create-menu-map
:doc "Keymap for create menu"
:parent nil
"i" '("create issue" . consult-gh-embark-create-issue)
"p" '("create pull request" . consult-gh-embark-create-pr)
"r" '("create repo" . consult-gh-embark-create-repo)
"c" '("create a clone of repo" . consult-gh-embark-clone-repo)
"f" '("create a fork repo" . consult-gh-embark-fork-repo))
(fset 'consult-gh-embark-create-menu-map consult-gh-embark-create-menu-map)
;;;;; Find Menu Keymap
(defvar-keymap consult-gh-embark-find-menu-map
:doc "Keymap for find menu"
:parent nil
"f" '("find file (browse files of repo)" . consult-gh-embark-view-files-of-repo)
"r" '("find repos by user" . consult-gh-embark-get-other-repos-by-same-user)
"i" '("find issues of repo" . consult-gh-embark-view-issues-of-repo)
"p" '("find prs of repo" . consult-gh-embark-view-prs-of-repo)
"c" '("find code in repo" . consult-gh-embark-search-code-in-repo))
(fset 'consult-gh-embark-find-menu-map consult-gh-embark-find-menu-map)
;;;;; Insert Menu Keymap
;;;;;; Insert User's Info Keymap
(defvar-keymap consult-gh-embark-user-insert-menu-map
:doc "Keymap for inserting user info menu"
:parent nil
"u" '("insert GitHub username" . consult-gh-embark-insert-user)
"n" '("insert ful name" . consult-gh-embark-insert-user-name)
"e" '("insert email" . consult-gh-embark-insert-user-email)
"l" '("insert user url" . consult-gh-embark-insert-user-link))
(fset 'consult-gh-embark-user-insert-menu-map consult-gh-embark-user-insert-menu-map)
(defvar-keymap consult-gh-embark-insert-menu-map
:doc "Keymap for insert menu"
:parent nil
"t" '("insert title" . consult-gh-embark-insert-title)
"u" '("insert url" . consult-gh-embark-insert-url)
"l" '("insert link" . consult-gh-embark-insert-link)
"U" '("insert user info" . consult-gh-embark-user-insert-menu-map))
(fset 'consult-gh-embark-insert-menu-map consult-gh-embark-insert-menu-map)
;;;;; Links Menu Keymap
(defvar-keymap consult-gh-embark-links-menu-map
:doc "Keymap for links menu"
:parent nil
"h" '("copy https url" . consult-gh-embark-copy-https-link-as-kill)
"s" '("copy ssh url" . consult-gh-embark-copy-ssh-link-as-kill)
"l" '("copy url" . consult-gh-embark-copy-url-as-kill)
"o" '("copy org-mode link" . consult-gh-embark-copy-org-link-as-kill)
"u" '("copy straight use-package link" . consult-gh-embark-copy-straight-usepackage-link-as-kill)
"U" '("copy user page link" . consult-gh-embark-copy-user-link-as-kill))
(fset 'consult-gh-embark-links-menu-map consult-gh-embark-links-menu-map)
;;;;; Open Menu Keymap
(defvar-keymap consult-gh-embark-open-menu-map
:doc "Keymap for open menu"
:parent nil
"O" '("system browser" . consult-gh-embark-open-in-system-browser)
"o" '("consult-gh default browser" . consult-gh-embark-open-in-default-browser)
"p" '("preview buffer" . consult-gh-embark-show-preview)
"RET" '("open in emacs" . consult-gh-embark-default-action))
(fset 'consult-gh-embark-open-menu-map consult-gh-embark-open-menu-map)
;;;;; Repo Menu Keymap
(defvar-keymap consult-gh-embark-repo-menu-map
:doc "Keymap for repo actions menu"
:parent nil
"c" '("clone repo" . consult-gh-embark-clone-repo)
"f" '("fork repo" . consult-gh-embark-fork-repo)
"r" '("other repos of user" . consult-gh-embark-get-other-repos-by-same-user)
"i" '("issues of repo" . consult-gh-embark-view-issues-of-repo)
"p" '("prs of repo" . consult-gh-embark-view-prs-of-repo)
"s" '("search for code in repo" . consult-gh-embark-search-code-in-repo)
"b" '("browse files of repo" . consult-gh-embark-view-files-of-repo)
"o" '("open repo page in default browser" . consult-gh-embark-open-repo-in-default-browser)
"O" '("open repo page in system browser" . consult-gh-embark-open-repo-in-system-browser))
(fset 'consult-gh-embark-repo-menu-map consult-gh-embark-repo-menu-map)
;;;;; User Menu Keymap
(defvar-keymap consult-gh-embark-user-menu-map
:doc "Keymap for user actions menu"
:parent nil
"e" '("email user" . consult-gh-embark-email-user)
"r" '("repos of user" . consult-gh-embark-get-other-repos-by-same-user)
"i" '("issues involving user" . consult-gh-embark-view-issues-involves-user)
"p" '("prs involving user" . consult-gh-embark-view-prs-involves-user)
"a" '("user assignment" . consult-gh-embark-view-user-assignment)
"u" '("insert user info" . consult-gh-embark-user-insert-menu-map)