-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmagithub-core.el
1281 lines (1085 loc) · 45.9 KB
/
magithub-core.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
;;; magithub-core.el --- core functions for magithub -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2018 Sean Allred
;; Author: Sean Allred <[email protected]>
;; Keywords: tools
;; This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Core functions for Magithub.
;;; Code:
(require 'magit)
(require 'dash)
(require 's)
(require 'subr-x)
(require 'ghub)
(require 'ghub+)
(require 'bug-reference)
(require 'cl-lib)
(require 'markdown-mode)
(require 'parse-time)
(require 'thingatpt)
(require 'recentf)
(require 'magithub-settings)
(require 'magithub-faces)
(defconst magithub-github-token-scopes '(repo user notifications)
"The authentication scopes Magithub requests.")
;;; Debugging utilities
(defvar magithub-debug-mode nil
"Controls what kinds of debugging information shows.
List of symbols.
`dry-api' - don't actually make API requests
`forms' - show forms being evaluated in the cache")
(defun magithub-debug-mode (&optional submode)
"True if debug mode is on.
If SUBMODE is supplied, specifically check for that mode in
`magithub-debug-mode'."
(and (listp magithub-debug-mode)
(memq submode magithub-debug-mode)))
(defun magithub-message (fmt &rest args)
"Print a message."
(message "magithub: %s" (apply #'format fmt args)))
(defun magithub-debug-message (fmt &rest args)
"Print a debug message.
Respects `magithub-debug-mode' and `debug-on-error'."
(when (or magithub-debug-mode debug-on-error)
(let ((print-quoted t))
(message "magithub: (%s) %s"
(format-time-string "%M:%S.%3N" (current-time))
(apply #'format fmt args)))))
(defun magithub-debug--ghub-request-wrapper (oldfun &rest args)
"Report ghub requests as they're being made.
Intended as around-advice for `ghub-requst'."
(magithub-debug-message "ghub-request%S" args)
(unless (magithub-debug-mode 'dry-api)
(apply oldfun args)))
(defun magithub-instrument ()
"Instrument Magithub for debugging."
(interactive)
(advice-add #'ghub-request :around #'magithub-debug--ghub-request-wrapper))
(defcustom magithub-dir
(expand-file-name "magithub" user-emacs-directory)
"Data directory.
Various Magithub data (such as the cache) will be dumped into the
root of this directory.
If it does not exist, it will be created."
:group 'magithub
:type 'directory)
(add-to-list 'recentf-exclude
(lambda (filename)
(file-in-directory-p filename magithub-dir)))
;;; Turning Magithub on/off
(defmacro magithub-in-data-dir (&rest forms)
"Execute forms in `magithub-dir'.
If `magithub-dir' does not yet exist, it and its parents will be
created automatically."
(declare (debug t))
`(progn
(unless (file-directory-p magithub-dir)
(mkdir magithub-dir t))
(let ((default-directory magithub-dir))
,@forms)))
;;; Caching; Online/Offline mode
(defcustom magithub-cache-file "cache"
"Use this file for Magithub's persistent cache."
:group 'magithub
:type 'file)
(defun magithub-cache-read-from-disk ()
"Returns the cache as read from `magithub-cache-file'."
(magithub-in-data-dir
(when (file-readable-p magithub-cache-file)
(with-temp-buffer
(insert-file-contents magithub-cache-file)
(read (current-buffer))))))
(defvar magithub-cache--cache
(or (ignore-errors
(magithub-cache-read-from-disk))
(make-hash-table :test 'equal))
"The actual cache.
Holds all information ever cached by Magithub.
Occasionally written to `magithub-cache-file' by
`magithub-cache-write-to-disk'.")
(defvar magithub-cache--needs-write nil
"Signals that the cache has been updated.
When non-nil, the cache will be written to disk next time the
idle timer runs.")
(defvar magithub-cache--refreshed-forms nil
"Forms that have been refreshed this session.
See also `magithub--refresh'.")
(cl-defun magithub-cache (class form &key message after-update)
"The cached value for FORM if available.
If FORM has not been cached or its CLASS dictates the cache has
expired, FORM will be re-evaluated.
CLASS: The kind of data this is; see `magithub-cache--refresh'.
MESSAGE may be specified for intensive functions. We'll display
this with `with-temp-message' while the form is evaluating.
AFTER-UPDATE is a function to run after the cache is updated."
(declare (indent defun))
(let* ((no-value-sym (cl-gensym))
(entry (list (ghubp-get-context) class form))
(online (magithub-online-p))
(cached-value (gethash entry magithub-cache--cache no-value-sym))
(value-does-not-exist (eq cached-value no-value-sym))
(cached-value (if value-does-not-exist nil cached-value))
make-request new-value)
(when online
(if (or (eq magithub-cache--refresh t)
(eq magithub-cache--refresh class))
;; if we're refreshing (and we haven't seen this form
;; before), go ahead and make the request if it's the class
;; we're refreshing (or t, which encompasses all classes)
(setq make-request (not (member entry magithub-cache--refreshed-forms)))
(setq make-request value-does-not-exist)))
(or (and make-request
(prog1 (setq new-value (with-temp-message message (eval form)))
(puthash entry new-value magithub-cache--cache)
(unless magithub-cache--needs-write
(setq magithub-cache--needs-write t)
(run-with-idle-timer 600 nil #'magithub-cache-write-to-disk))
(when magithub-cache--refresh
(push entry magithub-cache--refreshed-forms))
(if (functionp after-update)
(funcall after-update new-value)
new-value)))
cached-value)))
(defun magithub-maybe-report-offline-mode ()
"Conditionally inserts the OFFLINE header.
If this is a Magithub-enabled repository and we're offline, we
insert a header notifying the user that all data shown is cached.
To aid in determining if the cache should be refreshed, we report
the age of the oldest cached information."
(when (and (magithub-usable-p)
(not (magithub-online-p)))
(magit-insert-section (magithub nil t)
(insert
(format
"Magithub: %s; use %s to refresh GitHub content or %s to go back online%s\n"
(propertize "OFFLINE" 'face 'magit-head)
(propertize
(substitute-command-keys "\\[universal-argument] \\[magit-refresh]")
'face 'magit-header-line-key)
(propertize
(substitute-command-keys "\\[magithub-dispatch-popup] C o")
'face 'magit-header-line-key)
(propertize "..." 'face 'magit-dimmed)))
(magit-insert-heading)
(let* ((msg "When Magithub is offline, no API requests are ever made \
automatically. Even when online, cached API responses never expire, so \
they must be updated manually with %s.")
(msg (s-word-wrap (- fill-column 10) msg))
(msg (format msg (propertize
(substitute-command-keys
"\\[universal-argument] \\[magit-refresh]")
'face 'magit-header-line-key))))
(insert (format "%s\n" (replace-regexp-in-string
(rx bol) (make-string 10 ?\ ) msg)))))))
(eval-after-load 'magit
'(add-hook 'magit-status-headers-hook
#'magithub-maybe-report-offline-mode
'append))
(defun magithub-cache--time-out (time)
"Convert TIME into a human-readable string.
Returns \"Xd Xh Xm Xs\" (counting from zero)"
(let ((seconds (time-to-seconds time)))
(format-time-string
(cond
((< seconds 60) "%-Ss")
((< seconds 3600) "%-Mm %-Ss")
((< seconds 86400) "%-Hh %-Mm %-Ss")
(t "%-jd %-Hh %-Mm %-Ss"))
time)))
(defun magithub-cache-write-to-disk ()
"Write the cache to disk.
The cache is written to `magithub-cache-file' in
`magithub-dir'"
(if (active-minibuffer-window)
(run-with-idle-timer 600 nil #'magithub-cache-write-to-disk) ;defer
(when magithub-cache--needs-write
(magithub-in-data-dir
(with-temp-buffer
(let ((require-final-newline nil))
(insert (prin1-to-string magithub-cache--cache))
(write-file magithub-cache-file))))
(setq magithub-cache--needs-write nil)
(magithub-debug-message "wrote cache to disk: %S"
(expand-file-name magithub-cache-file
magithub-dir)))))
(defmacro magithub-cache-without-cache (class &rest body)
"For CLASS, execute BODY without using CLASS's caches.
Use t to ignore previously cached values completely.
See also `magithub-cache--refresh'."
(declare (indent 1) (debug t))
`(let ((magithub-cache--refresh ,class))
,@body))
(add-hook 'kill-emacs-hook
#'magithub-cache-write-to-disk)
;;; API availability checking
(define-error 'magithub-error "Magithub Error")
(define-error 'magithub-api-timeout "Magithub API Timeout" 'magithub-error)
(defvar magithub--api-last-checked
;; see https://travis-ci.org/vermiculus/magithub/jobs/259006323
;; (eval-when-compile (date-to-time "1/1/1970"))
'(14445 17280)
"The last time the API was available.
Used to avoid pinging GitHub multiple times a second.")
(defcustom magithub-api-timeout 3
"Number of seconds we'll wait for the API to respond."
:group 'magithub
:type 'integer)
(defcustom magithub-api-low-threshold 15
"Low threshold for API requests.
This variable is not currently respected; see tarsius/ghub#16.
If the number of available API requests drops to or below this
threshold, you'll be asked if you'd like to go offline."
:group 'magithub
:type 'integer)
(defcustom magithub-api-available-check-frequency 10
"Minimum number of seconds between each API availability check.
While online (see `magithub-go-online'), we check to ensure the
API is available before making a real request. This involves a
`/rate_limit' call (or for some Enterprise instances, a `/meta'
call). Use this setting to configure how often this is done. It
will be done no more frequently than other API actions.
These calls are guaranteed to not count against your rate limit."
:group 'magithub
:type 'integer)
(defvar magithub--quick-abort-api-check nil
"When non-nil, we'll assume the API is unavailable.
Do not modify this variable in code outside Magithub.")
(defvar magithub--api-offline-reason nil
"The reason we're going offline.
Could be one of several strings:
* authentication issue
* response timeout
* generic error
and possibly others as error handlers are added to
`magithub--api-available-p'.")
(defun magithub--api-available-p ()
"Non-nil if the API is available.
Pings the API a maximum of once every ten seconds."
(setq magithub--api-offline-reason nil)
(when (magithub-enabled-p)
(magithub-debug-message "checking if the API is available")
(prog1 (when
(progn
(magithub-debug-message "making sure authinfo is unlocked")
(ghubp-token 'magithub))
(if (and magithub--api-last-checked
(< (time-to-seconds (time-since magithub--api-last-checked))
magithub-api-available-check-frequency))
(prog1 magithub--api-last-checked
(magithub-debug-message "used cached value for api-last-checked"))
(magithub-debug-message "cache expired; retrieving new value for api-last-checked")
(setq magithub--api-last-checked (current-time))
(let (api-status error-data response)
(condition-case err
(progn
(with-timeout (magithub-api-timeout
(signal 'magithub-api-timeout nil))
(setq response
;; /rate_limit is free for GitHub.com.
;; If rate limiting is disabled
;; (i.e. GHE), try using /meta which
;; should (hopefully) always work. See
;; also issue #107.
(magithub-request
(or (ghubp-ratelimit)
(ghubp-request 'get "/meta" nil nil)))
api-status (and response t)))
(magithub-debug-message
"new value retrieved for api-last-available: %S" response))
;; Sometimes, the API can take a long time to respond
;; (whether that's GitHub not responding or requests being
;; blocked by some client-side firewall). Handle this
;; possibility gracefully.
(magithub-api-timeout
(setq error-data err
magithub--api-offline-reason
(concat "API is not responding quickly; "
"consider customizing `magithub-api-timeout' "
"if this happens often")))
;; Never hurts to be cautious :-)
(error
(setq error-data err)
(setq magithub--api-offline-reason
(format "unknown issue: %S" err))))
(when error-data
(magithub-debug-message
"consider reporting unknown error while checking api-available: %S"
error-data))
api-status)))
(when magithub--api-offline-reason
(magit-set "false" "magithub.online")
(run-with-idle-timer 2 nil #'magithub--api-offline-reason)))))
(defun magithub--api-offline-reason ()
"Report the reason we're going offline and go offline.
Refresh the status buffer if necessary.
See `magithub--api-offline-reason'."
(when magithub--api-offline-reason
(message "Magithub is now offline: %s"
magithub--api-offline-reason)
(setq magithub--api-offline-reason nil)))
;;; Repository parsing
(defcustom magithub-github-hosts
(list "github.com")
"A list of top-level domains that should be recognized as GitHub hosts.
See also `magithub-github-repository-p'."
:group 'magithub
:type '(repeat string))
(defun magithub-github-repository-p ()
"Non-nil if \"origin\" points to GitHub or a whitelisted domain.
See also `magithub-github-hosts'."
(when-let ((origin (magit-get "remote" (magithub-settings-context-remote) "url")))
(-some? (lambda (domain) (s-contains? domain origin))
magithub-github-hosts)))
(defalias 'magithub--parse-url 'magithub--repo-parse-url)
(make-obsolete 'magithub--parse-url 'magithub--repo-parse-url "0.1.4")
(defun magithub--repo-parse-url (url)
"Parse URL into its components.
URL may be of several different formats:
- [email protected]:vermiculus/magithub.git
- https://github.com/vermiculus/magithub"
(and url
(or (and (string-match
;; [email protected]:vermiculus/magithub.git
(rx bol
(group (+? any)) ;sshuser -- git
"@"
(group (+? any)) ;domain -- github.com
":"
(group (+? (| alnum "-" "." "_"))) ;owner.login -- vermiculus
"/"
(group (+? (| alnum "-" "." "_"))) ;name -- magithub
(? ".git")
eol)
url)
`((kind . 'ssh)
(ssh-user . ,(match-string 1 url))
(domain . ,(match-string 2 url))
(sparse-repo (owner (login . ,(match-string 3 url)))
(name . ,(match-string 4 url)))))
(and (string-match
;; https://github.com/vermiculus/magithub.git
;; git://github.com/vermiculus/magithub.git
;; ssh://[email protected]/vermiculus/magithub
;; git+ssh://github.com/vermiculus/magithub.git
(rx bol
(or (seq "http" (? "s"))
(seq "ssh")
(seq "git" (? "+ssh")))
"://"
(group (+? any)) ;domain -- github.com
"/"
(group (+? (| alnum "-" "." "_"))) ;owner.login -- vermiculus
"/"
(group (+? (| alnum "-" "." "_"))) ;name -- magithub
(? ".git")
eol)
url)
`((kind . 'http)
(domain . ,(match-string 1 url))
(sparse-repo (owner (login . ,(match-string 2 url)))
(name . ,(match-string 3 url))))))))
(defun magithub--url->repo (url)
"Tries to parse a remote url into a GitHub repository object"
(cdr (assq 'sparse-repo (magithub--repo-parse-url url))))
(defun magithub-source--sparse-repo ()
"Returns the sparse repository object for the current context.
Only information that can be determined without API calls will be
included in the returned object."
(magithub-repo-from-remote--sparse
(magithub-settings-context-remote)))
(defun magithub-repo-from-remote (remote)
(when-let ((repo (magithub-repo-from-remote--sparse remote)))
(magithub-repo repo)))
(defun magithub-repo-from-remote--sparse (remote)
(magithub--url->repo (magit-get "remote" remote "url")))
(defalias 'magithub-source-repo 'magithub-repo)
(make-obsolete 'magithub-source-repo 'magithub-repo "0.1.4")
(defun magithub-repo (&optional sparse-repo)
"Turn SPARSE-REPO into a full repository object.
If SPARSE-REPO is null, the current context is used.
SPARSE-REPO may either be a partial repository object (with at
least the `.owner.login' and `.name' keys) or a string identifier
of the form `owner/name' (as in `vermiculus/magithub')."
(if (and (stringp sparse-repo)
(string-match (rx bos
(group (+? (| alnum "-" "." "_"))) ;owner.login -- vermiculus
"/"
(group (+? (| alnum "-" "." "_"))) ;name -- magithub
eos)
sparse-repo))
(magithub-repo `((owner (login . ,(match-string 1 sparse-repo)))
(name . ,(match-string 2 sparse-repo))))
(when-let ((sparse-repo (or sparse-repo (magithub-source--sparse-repo))))
(or (magithub-cache :repo-demographics
`(or (magithub-request
(ghubp-get-repos-owner-repo ',sparse-repo))
(and (not (magithub--api-available-p))
sparse-repo)))
(when (magithub-online-p)
(let ((magithub-cache--refresh t))
(magithub-repo sparse-repo)))
sparse-repo))))
;;; Repository utilities
(defvar magit-magithub-repo-section-map
(let ((m (make-sparse-keymap)))
(define-key m [remap magit-visit-thing] #'magithub-repo-visit)
m))
(defun magithub-repo-visit (repo)
"Visit REPO on GitHub."
(interactive (list (thing-at-point 'github-repository)))
(if-let ((url (alist-get 'html_url repo)))
(browse-url url)
(user-error "No URL for repo")))
(defun magithub-repo-visit-issues (repo)
"Visit REPO's issues on GitHub."
(interactive (list (thing-at-point 'github-repository)))
(if-let ((url (alist-get 'html_url repo)))
(browse-url (format "%s/issues" url))
(user-error "No URL for repo")))
(defun magithub-repo-name (repo)
"Return the full name of REPO.
If the `full_name' object is present, use that. Otherwise,
concatenate `.owner.login' and `.name' with `/'."
(let-alist repo (or .full_name (concat .owner.login "/" .name))))
(defun magithub-repo-admin-p (&optional repo)
"Non-nil if the currently-authenticated user can manage REPO.
REPO defaults to the current repository."
(let-alist (magithub-repo (or repo (thing-at-point 'github-repository)))
.permissions.admin))
(defun magithub-repo-push-p (&optional repo)
"Non-nil if the currently-authenticated user can manage REPO.
REPO defaults to the current repository."
(let-alist (magithub-repo (or repo (thing-at-point 'github-repository)))
.permissions.push))
(defun magithub--repo-simplify (repo)
"Convert full repository object REPO to a sparse repository object."
(let (login name)
;; There are syntax problems with things like `,.owner.login'
(let-alist repo
(setq login .owner.login
name .name))
`((owner (login . ,login))
(name . ,name))))
(defun magithub-repo-remotes ()
"Return GitHub repositories in this repository.
`magit-list-remotes' is filtered to those remotes that point to
GitHub repositories."
(delq nil (mapcar (lambda (r)
(when-let ((repo (magithub-repo-from-remote r)))
(cons r repo)))
(magit-list-remotes))))
(defun magithub-read-repo (prompt &optional default-input skip-prompt-for-sole-remote)
"Using PROMPT, read a GitHub repository.
See also `magithub-repo-remotes'.
If there's only one remote available, optionally return it without prompting."
(let* ((remotes (magithub-repo-remotes))
(maxlen (->> remotes
(mapcar #'car)
(mapcar #'length)
(apply #'max)))
(fmt (format "%%-%ds (%%s/%%s)" maxlen)))
(if (and skip-prompt-for-sole-remote (= (length remotes) 1))
(car remotes)
(magithub-repo
(cdr (magithub--completing-read
prompt (magithub-repo-remotes)
(lambda (remote-repo-pair)
(let-alist (cdr remote-repo-pair)
(format fmt (car remote-repo-pair) .owner.login .name)))
nil nil nil default-input))))))
(defun magithub-repo-remotes-for-repo (repo)
(-filter (lambda (remote)
(let-alist (list (cons 'repo repo)
(cons 'remote (magithub-repo-from-remote remote)))
(and (string= .repo.owner.login
.remote.owner.login)
(string= .repo.name .remote.name))))
(magit-list-remotes)))
;;; Feature checking
(declare-function magithub-pull-request-merge "magithub-issue-tricks"
(pull-request &optional args))
(declare-function magithub-maybe-insert-ci-status-header "magithub-ci" ())
(declare-function magithub-issue--insert-pr-section "magithub-issue" ())
(declare-function magithub-issue--insert-issue-section "magithub-issue" ())
(declare-function magithub-completion-enable "magithub-completion" ())
(defconst magithub-feature-list
;; features must only return nil if they fail to install
`((pull-request-merge . ,(lambda ()
(require 'transient nil t)
(when (functionp 'magit-am)
(transient-append-suffix 'magit-am "a"
'("P" "Apply patches from pull request" magithub-pull-request-merge))
t)))
(commit-browse . ,(lambda ()
(define-key magit-commit-section-map "w"
#'magithub-commit-browse)
t))
(status-checks-header . ,(lambda ()
(add-hook 'magit-status-headers-hook
#'magithub-maybe-insert-ci-status-header
t)
t))
(completion . ,(lambda ()
(dolist (hook '(git-commit-setup-hook magithub-edit-mode-hook))
(add-hook hook #'magithub-completion-enable))
t))
;; order is important in this list; pull request section should
;; come before issues section by default
(pull-requests-section . ,(lambda ()
(add-hook 'magit-status-sections-hook
#'magithub-issue--insert-pr-section
t)
t))
(issues-section . ,(lambda ()
(add-hook 'magit-status-sections-hook
#'magithub-issue--insert-issue-section
t)
t)))
"All Magit-integration features of Magithub.
See `magithub-feature-autoinject'.
- `pull-request-merge'
Apply patches from pull requests.
(`magithub-pull-request-merge' inserted into `magit-am-popup')
- `commit-browse'
Browse commits using \\<magithub-map>\\[magithub-browse-thing].
- `completion'
Enable `completion-at-point' support for #issue and @user references
where possible.
- `issues-section'
View issues in the `magit-status' buffer.
- `pull-requests-section'
View pull requests in the `magit-status' buffer.
- `status-checks-header'
View project status in the `magit-status' buffer (e.g., CI).")
(defvar magithub-features nil
"An alist of feature-symbols to Booleans.
When a feature symbol maps to non-nil, that feature is considered
'loaded'. Thus, to disable all messages, prepend '(t . t) to
this list.
Example:
((pull-request-merge . t) (other-feature . nil))
signals that `pull-request-merge' is a loaded feature and
`other-feature' has not been loaded and will not be loaded.
See `magithub-feature-list'.")
;;;###autoload
(defun magithub-feature-autoinject (feature)
"Configure FEATURE to recommended settings.
If FEATURE is `all' or t, all known features will be loaded. If
FEATURE is a list, then it is a list of FEATURE symbols to load.
See `magithub-feature-list' for a list of available features and
`magithub-features' for a list of currently-installed features."
(cond
((memq feature '(t all))
(mapc #'magithub-feature-autoinject
(mapcar #'car magithub-feature-list)))
((listp feature)
(mapc #'magithub-feature-autoinject feature))
(t
(if-let ((install (cdr (assq feature magithub-feature-list))))
(if (functionp install)
(if-let ((result (funcall install)))
(add-to-list 'magithub-features (cons feature t))
(error "feature %S failed to install: %S" feature result))
(error "install form for %S not a function: %S" feature install))
(user-error "unknown feature %S" feature)))))
(defun magithub-feature-check (feature)
"Check if a Magithub FEATURE has been configured.
See `magithub-features'."
(if (listp magithub-features)
(let* ((p (assq feature magithub-features)))
(if (consp p) (cdr p)
(cdr (assq t magithub-features))))
magithub-features))
(defun magithub-feature-maybe-idle-notify (&rest feature-list)
"Notify user if any of FEATURES are not yet configured."
(unless (-all? #'magithub-feature-check feature-list)
(let ((m "Magithub features not configured: %S")
(s "see variable `magithub-features' to turn off this message"))
(run-with-idle-timer
1 nil (lambda ()
(message (concat m "; " s) feature-list)
(add-to-list 'feature-list '(t . t) t))))))
;;; Getting help
(defun magithub--meta-new-issue ()
"Open a new Magithub issue.
See /.github/ISSUE_TEMPLATE.md in this repository."
(interactive)
(browse-url "https://github.com/vermiculus/magithub/issues/new"))
(defun magithub--meta-help ()
"Open Magithub help."
(interactive)
(browse-url "https://gitter.im/vermiculus/magithub"))
(defun magithub-error (err-message &optional tag trace)
"Report a Magithub error.
ERR-MESSAGE is a string to be shown to the user.
TAG, if provided, is a user-friendly description of the error.
It defaults to ERR-MESSAGE.
If TRACE is provided, it should be an appropriate backtrace to
describe the error. If not provided, it is retrieved."
(unless (stringp err-message)
;; just in case. it'd be embarassing if the bug-reporter was
;; perceived as buggy
(setq err-message (prin1-to-string err-message)))
(setq trace (or trace (with-output-to-string (backtrace)))
tag (or tag err-message))
(when (magithub-confirm-no-error 'report-error tag)
(with-current-buffer-window
(get-buffer-create "*magithub issue*")
#'display-buffer-pop-up-window nil
(when (fboundp 'markdown-mode) (markdown-mode))
(insert
(kill-new
(format
"## Automated error report
%s
### Description
%s
### Backtrace
```
%s```
"
err-message
(read-string "Briefly describe what you were doing: ")
trace))))
(magithub--meta-new-issue))
(error err-message))
;;; Miscellaneous utilities
(defcustom magithub-datetime-format "%c"
"The display format string for date-time values.
See also `format-time-string'."
:group 'magithub
:type 'string)
(defun magithub--parse-time-string (iso8601)
"Parse ISO8601 into a time value.
ISO8601 is expected to not have a TZ component.
We first use a crude parsing and if it fails we fall back to a more
general purpose function. This is done to speed up parsing time."
(if (string-match "^\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)T\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)Z$" iso8601)
(encode-time
(string-to-number (match-string 6 iso8601))
(string-to-number (match-string 5 iso8601))
(string-to-number (match-string 4 iso8601))
(string-to-number (match-string 3 iso8601))
(string-to-number (match-string 2 iso8601))
(string-to-number (match-string 1 iso8601))
t)
(parse-iso8601-time-string (concat iso8601 "+00:00"))))
(defun magithub--format-time (time)
"Format TIME according to `magithub-datetime-format'.
TIME may be a time value or a string.
Eventually, TIME will always be a time value."
;; todo: ghub+ needs to convert time values for defined response fields
(format-time-string
magithub-datetime-format
(or (and (stringp time)
(magithub--parse-time-string time))
time)))
(defun magithub--completing-read
(prompt collection &optional format-function predicate require-match default initial-input)
"Using PROMPT, get a list of elements in COLLECTION.
This function continues until all candidates have been entered or
until the user enters a value of \"\". Duplicate entries are not
allowed."
(let* ((format-function (or format-function (lambda (o) (format "%S" o))))
(collection (if (functionp predicate) (-filter predicate collection) collection))
(collection (magithub--zip collection format-function nil)))
(cdr (assoc-string
(completing-read prompt collection nil require-match
(or initial-input
(and default
(funcall format-function default))))
collection))))
(defun magithub--completing-read-multiple
(prompt collection &optional format-function predicate require-match default)
"Using PROMPT, get a list of elements in COLLECTION.
This function continues until all candidates have been entered or
until the user enters a value of \"\". Duplicate entries are not
allowed."
(let ((this t) (coll (copy-tree collection)) ret)
(while (and collection this)
(setq this (magithub--completing-read
prompt coll format-function
predicate require-match default))
(when this
(push this ret)
(setq coll (delete this coll))))
ret))
(defconst magithub-hash-regexp
(rx bow (= 40 (| digit (any (?A . ?F) (?a . ?f)))) eow)
"Regexp for matching commit hashes.")
(defun magithub-usable-p ()
"Non-nil if Magithub should do its thing."
(and (magithub-enabled-p)
(magithub-github-repository-p)
(magithub-source--sparse-repo)))
(defun magithub--zip-case (p e)
"Get an appropriate value for element E given property/function P."
(cond
((null p) e)
((functionp p) (funcall p e))
((symbolp p) (plist-get e p))
(t nil)))
(defun magithub--zip (object-list prop1 prop2)
"Process OBJECT-LIST into an alist defined by PROP1 and PROP2.
If a prop is a symbol, that property will be used.
If a prop is a function, it will be called with the
current element of OBJECT-LIST.
If a prop is nil, the entire element is used."
(delq nil
(-zip-with
(lambda (e1 e2)
(let ((p1 (magithub--zip-case prop1 e1))
(p2 (magithub--zip-case prop2 e2)))
(unless (or (and prop1 (not p1))
(and prop2 (not p2)))
(cons (if prop1 p1 e1)
(if prop2 p2 e2)))))
object-list object-list)))
(defun magithub--satisfies-p (preds obj)
"Non-nil when all functions in PREDS are non-nil for OBJ."
(while (and (listp preds)
(functionp (car preds))
(funcall (car preds) obj))
(setq preds (cdr preds)))
(null preds))
(defun magithub-section-type (section)
"If SECTION is a magithub-type section, return the type.
For example, if
(eq (magit-section-type SECTION) \\='magithub-issue)
return the interned symbol `issue'."
(let* ((type (oref section type))
(name (symbol-name type)))
(and (string-prefix-p "magithub-" name)
(intern (substring name 9)))))
(defvar magithub--section-value-at-point-specializations
'((user assignee))
"Alist of general types to specific types.
Specific types offer more relevant functionality to a given
section, but are inconvenient for
`magithub--section-value-at-point'. This alist defines
equivalencies such that a search for the general type will also
return sections of a specialized type.")
(define-obsolete-function-alias
'magithub-thing-at-point
#'magithub--section-value-at-point
"0.1.5")
;;;###autoload
(defun magithub--section-value-at-point (type)
"Determine the thing of TYPE at point.
This is intended for use as a resolving function for
`thing-at-point'.
The following symbols are defined, but other values may work with
this function: `github-user', `github-issue', `github-label',
`github-comment', `github-repository', `github-pull-request',
`github-notification',"
(let ((search-sym (intern (concat "magithub-" (symbol-name type))))
this-section)
(if (and (boundp search-sym) (symbol-value search-sym))
(symbol-value search-sym)
(setq this-section (magit-current-section))
(while (and this-section
(not (let ((this-type (magithub-section-type this-section)))
(or
;; exact match
(eq type this-type)
;; equivalency
(thread-last magithub--section-value-at-point-specializations
(alist-get type)
(memq this-type))))))
(setq this-section (oref this-section parent)))
(and this-section (oref this-section value)))))
(defvar-local magithub-issue nil
"Issue object.")
(defvar-local magithub-comment nil
"Comment object.")
(defvar-local magithub-repo nil
"Repository object.")
;;;###autoload
(put 'github-user 'thing-at-point
(lambda ()
(magithub--section-value-at-point 'user)))
;;;###autoload
(put 'github-issue 'thing-at-point
(lambda ()
(or magithub-issue
(magithub--section-value-at-point 'issue))))
;;;###autoload
(put 'github-label 'thing-at-point
(lambda ()
(magithub--section-value-at-point 'label)))
;;;###autoload
(put 'github-comment 'thing-at-point
(lambda ()
(or magithub-comment
(magithub--section-value-at-point 'comment))))
;;;###autoload
(put 'github-notification 'thing-at-point
(lambda ()
(magithub--section-value-at-point 'notification)))
;;;###autoload
(put 'github-repository 'thing-at-point
(lambda ()
(or (magithub--section-value-at-point 'repository)
magithub-repo
(magithub-repo))))
;;;###autoload
(put 'github-pull-request 'thing-at-point
(lambda ()
(or (magithub--section-value-at-point 'pull-request)
(when-let ((issue (thing-at-point 'github-issue)))
(and
(magithub-issue--issue-is-pull-p issue)
(magithub-cache :issues
`(magithub-request