File: ghub+.el

package info (click to toggle)
ghub-plus-el 0.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 136 kB
  • sloc: lisp: 953; makefile: 3
file content (1072 lines) | stat: -rw-r--r-- 39,368 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
;;; ghub+.el --- a thick GitHub API client built on ghub  -*- lexical-binding: t; -*-

;; Copyright (C) 2017-2018  Sean Allred

;; Author: Sean Allred <code@seanallred.com>
;; Keywords: extensions, multimedia, tools
;; Homepage: https://github.com/vermiculus/ghub-plus
;; Package-Requires: ((emacs "25") (ghub "2.0") (apiwrap "0.5"))
;; Package-Version: 0.3

;; 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:

;; Provides some sugar for `ghub'.  See package `apiwrap' for
;; generated function usage instructions.

;;; Code:

(require 'url)
(require 'cl-lib)
(require 'ghub)
(require 'apiwrap)

(eval-and-compile
  (defun ghubp--make-link (alist)
    "Create a link from an ALIST of API endpoint properties."
    (format "https://developer.github.com/v3/%s" (alist-get 'link alist)))

  (defun ghubp--stringify-params (params)
    "Process PARAMS from textual data to Lisp structures."
    (mapcar (lambda (p)
              (if (listp p)
		  (let ((k (car p)) (v (cdr p)))
		    (cons k (alist-get v '((t . "true") (nil . "false")) v)))
		p))
            params))

  (defun ghubp--pre-process-params (params)
    (ghubp--stringify-params params))

  (defvar ghubp-contextualize-function nil
    "Function to contextualize `ghub' requests.
Can return an alist with any of the following properties:

* `auth'
* `headers'
* `host'
* `unpaginate'
* `username'

If (and only if) these properties are non-nil, they will provide
values for the eponymous `ghub-request' keyword arguments.

The function should be callable with no arguments.

See also `ghubp-get-context'.")

  (defvar ghubp-request-override-function nil
    "Function to use instead of `ghub-request' for base calls.
It is expected to have the same signature as `ghub-request'.")

  (defun ghubp-get-context ()
    "Get the current context with `ghubp-contextualize-function'."
    (when (functionp ghubp-contextualize-function)
      (funcall ghubp-contextualize-function)))

  (defun ghubp-get-in-all (props object-list)
    "Follow property-path PROPS in OBJECT-LIST.
Returns a list of the property-values."
    (declare (indent 1))
    (if (or (null props) (not (consp props)))
        object-list
      (ghubp-get-in-all (cdr props)
        (mapcar (lambda (o) (alist-get (car props) o))
                object-list))))

  (defun ghubp-request (method resource params data)
    "Using METHOD, get RESOURCE with PARAMS and DATA.

`ghubp-contextualize-function' is used to contextualize this
request.

If non-nil, `ghubp-request-override-function' is used instead of
`ghub-request'.

METHOD is one of `get', `put', `post', `head', `patch', and
`delete'.

RESOURCE is a string.

PARAMS is a plist.

DATA is an alist."
    (let-alist (ghubp-get-context)
      (let ((method (encode-coding-string (upcase (symbol-name method)) 'utf-8))
            (params (apiwrap-plist->alist params)))
        (funcall (or ghubp-request-override-function
                     #'ghub-request)
                 method resource nil
                 :query params
                 :payload data
                 :unpaginate .unpaginate
                 :headers .headers
                 :username .username
                 :auth .auth
                 :host .host))))

  ;; If ghub-404 is not defined as an error, define it.
  ;; This will be necessary until Ghub releases v2.
  ;; See also #8.
  (unless (get 'ghub-404 'error-conditions)
    (define-error 'ghub-404 "Not Found" 'ghub-http-error))

  (defun ghubp--catch (error-symbol &rest handlers)
    "Catch some Ghub signals as ERROR-SYMBOL with HANDLERS.
Each element of HANDLERS should be a list of

    (HTTP-CODE HANDLER)

where HTTP-CODE is an error code like 404.

For use inside `:condition-case' endpoint configurations.

See also `ghubp-catch' and `ghubp-catch*'.

For now, care is taken to support older versions of Ghub."
    (let (code handler form)
      (dolist (pair handlers)
        (setq code (car pair)
              handler (cdr pair))
        (push (cons (intern (format "ghub-%d" code)) handler) form))
      (setcdr (last form)
              `((ghub-http-error
                 (pcase (cadr ,error-symbol)
                   ,@handlers
                   (_ (signal (car ,error-symbol) (cdr ,error-symbol)))))))
      form))

  (defmacro ghubp-catch* (&rest handlers)
    "Catch some Ghub signals with HANDLERS.
For use inside `:condition-case' endpoint configurations.

For advanced error handling, the error is bound to the symbol `it'.

See `ghubp--catch'."
    (apply #'ghubp--catch 'it handlers))

  (defmacro ghubp-catch (error-symbol form &rest handlers)
    "Catch some Ghub signals as ERROR_SYMBOL in FORM with HANDLERS.
For general use.

See `ghubp--catch'"
    (declare (indent 2))
    (when (eq error-symbol '_)
      (setq error-symbol (cl-gensym)))
    `(condition-case ,error-symbol ,form
       ,@(apply #'ghubp--catch error-symbol handlers)))

  (apiwrap-new-backend "GitHub" "ghubp"
    '((repo . "REPO is a repository alist of the form returned by `ghubp-get-user-repos'.")
      (branch . "BRANCH is a branch object of the form returned by `ghubp-get-repos-owner-repo-branches-branch'.")
      (org  . "ORG is an organization alist of the form returned by `ghubp-get-user-orgs'.")
      (thread . "THREAD is a thread object of the form returned by `ghubp-get-repos-owner-repo-comments'.")
      (issue . "ISSUE is an issue object of the form returned by `ghubp-get-issues'.")
      (pull-request . "PULL-REQUEST is a pull request object of the form returned by `ghubp-get-repos-owner-repo-pulls'.")
      (review . "REVIEW is a review object of the form returned by `ghubp-get-repos-owner-repo-pulls-number-reviews'.")
      (label . "LABEL is a label object of the form returned by `ghubp-get-repos-owner-repo-issues-number-labels'.")
      (ref . "REF is a string and can be a SHA, a branch name, or a tag name.")
      (milestone . "MILESTONE is a milestone object.")
      (user . "USER is a user object.")
      (user-1 . "USER-1 is a user object.")
      (user-2 . "USER-2 is a user object.")
      (key . "KEY is a key object."))
    :request #'ghubp-request
    :link #'ghubp--make-link
    :pre-process-params #'ghubp--pre-process-params))


;;; Utilities:

(defmacro ghubp-unpaginate (&rest body)
  "Unpaginate API responses while executing BODY."
  `(ghubp-override-context unpaginate t ,@body))

(defmacro ghubp-override-context (context new-value &rest body)
  "Execute BODY while manually overriding CONTEXT with NEW-VALUE.
NEW-VALUE takes precedence over anything that
`ghubp-contextualize-function' provides for CONTEXT, but
`ghubp-contextualize-function' is otherwise respected."
  (declare (indent 2))
  (unless (memq context '(host auth username unpaginate headers))
    (error (concat "`ghubp-override-context' should only override one "
                   "of the symbols from `ghubp-contextualize-function'.")))
  (let ((sym-other-context (cl-gensym)))
    `(let ((,sym-other-context (ghubp-get-context))
           ghubp-contextualize-function)
       ;; override any existing value for CONTEXT
       (push (cons ',context ,new-value) ,sym-other-context)
       ;; and box the whole thing back into the var
       (setq ghubp-contextualize-function (lambda () ,sym-other-context))
       ,@body)))

(defun ghubp-keep-only (structure object)
  "Keep a specific STRUCTURE in OBJECT.
See URL `http://emacs.stackexchange.com/a/31050/2264'."
  (declare (indent 1))
  (if (and (consp object) (consp (car object)) (consp (caar object)))
      (mapcar (apply-partially #'ghubp-keep-only structure) object)
    (mapcar (lambda (el)
              (if (consp el)
                  (cons (car el)
                        (ghubp-keep-only (cdr el) (alist-get (car el) object)))
                (cons el (alist-get el object))))
            structure)))

(defun ghubp-header (header)
  "Get the value of HEADER from the last request as a string."
  (cdr (assoc-string header ghub-response-headers)))

(defun ghubp-ratelimit-reset-time ()
  "Get the reset time for the rate-limit as a time object."
  (declare (obsolete 'ghubp-ratelimit "2017-10-17"))
  (alist-get 'reset (ghubp-ratelimit)))

(defun ghubp-ratelimit-remaining ()
  "Get the remaining number of requests available."
  (declare (obsolete 'ghubp-ratelimit "2017-10-17"))
  (alist-get 'remaining (ghubp-ratelimit)))

(defun ghubp-ratelimit (&optional no-headers)
  "Get `/rate_limit.rate'.
Returns nil if the service is not rate-limited.  Otherwise,
returns an alist with the following properties:

  `.limit'
     number of requests we're allowed to make per hour.

  `.remaining'
     number of requests remaining for this hour.

  `.reset'
     time value of instant `.remaining' resets to `.limit'.

Unless NO-HEADERS is non-nil, tries to use response headers
instead of actually hitting /rate_limit."
  ;; todo: bug when headers are from other host
  (if (and (not no-headers)
           ghub-response-headers
           (assoc-string "X-RateLimit-Limit" ghub-response-headers))
      (let* ((headers (list "X-RateLimit-Limit" "X-RateLimit-Remaining" "X-RateLimit-Reset"))
             (headers (mapcar (lambda (x) (string-to-number (ghubp-header x))) headers)))
        `((limit     . ,(nth 0 headers))
          (remaining . ,(nth 1 headers))
          (reset     . ,(seconds-to-time
                         (nth 2 headers)))))
    (ghubp-catch _
        (let-alist (ghubp-request 'get "/rate_limit" nil nil)
          .resources.core)
      ;; Enterprise returns 404 if rate limiting is disabled
      (404 nil))))

(defun ghubp--follow (method resource &optional params data)
  "Using METHOD, follow the RESOURCE link with PARAMS and DATA.
This method is intended for use with callbacks."
  (let ((url (url-generic-parse-url resource)))
    (when (fboundp 'ghub--host)
      (unless (string-equal (url-host url) (ghub--host))
        (error "Bad link")))
    (ghubp-request method (url-filename url) params data)))

(defun ghubp-follow-get    (resource &optional params data)
  "GET wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'get    resource params data))
(defun ghubp-follow-put    (resource &optional params data)
  "PUT wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'put    resource params data))
(defun ghubp-follow-head   (resource &optional params data)
  "HEAD wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'head   resource params data))
(defun ghubp-follow-post   (resource &optional params data)
  "POST wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'post   resource params data))
(defun ghubp-follow-patch  (resource &optional params data)
  "PATCH wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'patch  resource params data))
(defun ghubp-follow-delete (resource &optional params data)
  "DELETE wrapper for `ghubp-follow'.
See that documentation for RESOURCE, PARAMS, and DATA."
  (ghubp--follow 'delete resource params data))

(defun ghubp-base-html-url ()
  "Get the base HTML URL from `ghub-default-host'."
  (if-let ((host (car (ignore-errors
			(process-lines "git" "config" "github.host")))))
      (and (string-match (rx bos (group (* any)) "/api/v3" eos) host)
           (match-string 1 host))
    "https://github.com"))

(defun ghubp-host ()
  "Exposes `ghub--host'."
  (ghub--host))

(defun ghubp-username ()
  "Exposes `ghub--username'."
  (ghub--username (ghub--host)))

(defun ghubp-token (package)
  "Exposes `ghub--token' for PACKAGE in a friendly way."
  (let* ((host (ghub--host))
         (user (ghub--username host)))
    (ghub--token host user package t)))


;;; Issues:

(defapiget-ghubp "/issues"
  "List all issues assigned to the authenticated user across all
visible repositories including owned repositories, member
repositories, and organization repositories."
  "issues/#list-issues")

(defapiget-ghubp "/user/issues"
  "List all issues across owned and member repositories assigned
to the authenticated user."
  "issues/#list-issues")

(defapiget-ghubp "/orgs/:org/issues"
  "List all issues for a given organization assigned to the
authenticated user."
  "issues/#list-issues"
  (org) "/org/:org.login/issues")

(defapiget-ghubp "/repos/:owner/:repo/issues"
  "List issues for a repository."
  "issues/#list-issues-for-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/issues")

(defapiget-ghubp "/repos/:owner/:repo/issues/:number"
  "Get a single issue."
  "issues/#get-a-single-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number")

(defapipost-ghubp "/repos/:owner/:repo/issues"
  "Create an issue.
Any user with pull access to a repository can create an issue."
  "issues/#create-an-issue"
  (repo) "/repos/:repo.owner.login/:repo.name/issues")

(defapipatch-ghubp "/repos/:owner/:repo/issues/:number"
  "Edit an issue.
Issue owners and users with push access can edit an issue."
  "issues/#edit-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number")

(defapiput-ghubp "/repos/:owner/:repo/issues/:number/lock"
  "Lock an issue.
Users with push access can lock an issue's conversation."
  "issues/#lock-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number")

(defapidelete-ghubp "/repos/:owner/:repo/issues/:number/lock"
  "Unlock an issue
Users with push access can unlock an issue's conversation."
  "issues/#unlock-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number")


;;; Issue Assignees:

(defapiget-ghubp "/repos/:owner/:repo/assignees"
  "List assignees.
This call lists all the available assignees to which issues may
be assigned."
  "issues/assignees/#list-assignees"
  (repo) "/repos/:repo.owner.login/:repo.name/assignees")

(defapiget-ghubp "/repos/:owner/:repo/assignees/:assignee"
  ;; todo: sugar to handle valid 404 response
  "Check assignee.
You may also check to see if a particular user is an assignee for
a repository."
  "issues/assignees/#check-assignee"
  (repo user) "/repos/:repo.owner.login/:repo.name/assignees/:user.login")

(defapipost-ghubp "/repos/:owner/:repo/issues/:number/assignees"
  "Add assignees to an Issue.
This call adds the users passed in the assignees key (as their
logins) to the issue."
  "issues/assignees/#add-assignees-to-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/assignees"
  :pre-process-data
  (lambda (users)
    `((assignees . ,(ghubp-get-in-all '(login) users)))))

(defapidelete-ghubp "/repos/:owner/:repo/issues/:number/assignees"
  "Remove assignees from an Issue.
This call removes the users passed in the assignees key (as their
logins) from the issue."
  "issues/assignees/#remove-assignees-from-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/assignees"
  :pre-process-data
  (lambda (users)
    `((assignees . ,(ghubp-get-in-all '(login) users)))))


;;; Issue Comments:

(defapiget-ghubp "/repos/:owner/:repo/issues/:number/comments"
  "List comments on an issue.
Issue Comments are ordered by ascending ID."
  "issues/comments/#list-comments-on-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/comments")

(defapiget-ghubp "/repos/:owner/:repo/issues/comments"
  "List comments in a repository.
By default, Issue Comments are ordered by ascending ID."
  "issues/comments/#list-comments-in-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/issues/comments")

(defapiget-ghubp "/repos/:owner/:repo/issues/comments/:id"
  "Get a single comment."
  "issues/comments/#get-a-single-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/comments/:thread.id")

(defapipatch-ghubp "/repos/:owner/:repo/issues/:number/comments"
  "Create a comment."
  "issues/comments/#create-a-comment"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/comments")

(defapipatch-ghubp "/repos/:owner/:repo/issues/comments/:id"
  "Edit a comment."
  "issues/comments/#edit-a-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/comments/:thread.id")

(defapidelete-ghubp "/repos/:owner/:repo/issues/comments/:id"
  "Delete a comment."
  "issues/comments/#delete-a-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/comments/:thread.id")


;;; Issue Events:

(defapiget-ghubp "/repos/:owner/:repo/issues/:number/events"
  ;; note: :number changed from :issue_number for consistency
  "List events for an issue."
  "issues/events/#list-events-for-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/events")

(defapiget-ghubp "/repos/:owner/:repo/issues/events"
  "List events for a repository."
  "issues/events/#list-events-for-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/issues/events")

(defapiget-ghubp "/repos/:owner/:repo/issues/events/:id"
  "Get a single event."
  "issues/events/#get-a-single-event"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/events/:thread.id")


;;; Issue Labels:

(defapiget-ghubp "/repos/:owner/:repo/labels"
  "List all labels for this repository."
  "issues/labels/#list-all-labels-for-this-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/labels")

(defapiget-ghubp "/repos/:owner/:repo/labels/:name"
  "Get a single label."
  "issues/labels/#get-a-single-label"
  (repo label) "/repos/:repo.owner.login/:repo.name/labels/:label.name")

(defapipost-ghubp "/repos/:owner/:repo/labels"
  "Create a label."
  "issues/labels/#create-a-label"
  (repo) "/repos/:repo.owner.login/:repo.name/labels")

(defapipatch-ghubp "/repos/:owner/:repo/labels/:name"
  "Update a label."
  "issues/labels/#update-a-label"
  (repo label) "/repos/:repo.owner.login/:repo.name/labels/:label.name")

(defapidelete-ghubp "/repos/:owner/:repo/labels/:name"
  "Delete a label."
  "issues/labels/#deleted-a-label"
  (repo label) "/repos/:repo.owner.login/:repo.name/labels/:label.name")

(defapiget-ghubp "/repos/:owner/:repo/issues/:number/labels"
  "List labels on an issue."
  "issues/labels/#list-labels-on-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/labels")

(defapipost-ghubp "/repos/:owner/:repo/issues/:number/labels"
  "Add labels to an issue."
  "issues/labels/#add-labels-to-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/labels"
  :pre-process-data (apply-partially #'ghubp-get-in-all '(name)))

(defapidelete-ghubp "/repos/:owner/:repo/issues/:number/labels/:name"
  "Remove a label from an issue."
  "issues/labels/#remove-a-label-from-an-issue"
  (repo issue label) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/labels/:label.name")

(defapipatch-ghubp "/repos/:owner/:repo/issues/:number/labels"
  "Replace all labels for an issue."
  "issues/labels/#replace-all-labels-for-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/labels"
  :pre-process-data (apply-partially #'ghubp-get-in-all '(name)))

(defapidelete-ghubp "/repos/:owner/:repo/issues/:number/labels"
  "Remove all labels from an issue."
  "issues/labels/#remove-all-labels-from-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/labels")

(defapiget-ghubp "/repos/:owner/:repo/milestones/:number/labels"
  "Get labels for every issue in a milestone."
  "issues/labels/#get-labels-for-every-issue-in-a-milestone"
  (repo milestone) "/repos/:repo.owner.login/:repo.name/milestones/:milestone.number/labels")


;;; Issue Milestones:

(defapiget-ghubp "/repos/:owner/:repo/milestones"
  "List milestones for a repository."
  "issues/milestones/#list-milestones-for-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/milestones")

(defapiget-ghubp "/repos/:owner/:repo/milestones/:number"
  "Get a single milestone."
  "issues/milestones/#get-a-single-milestone"
  (repo milestone) "/repos/:repo.owner.login/:repo.name/milestones/:milestone.number")

(defapipost-ghubp "/repos/:owner/:repo/milestones"
  "Create a milestone."
  "issues/milestones/#create-a-milestone"
  (repo) "/repos/:repo.owner.login/:repo.name/milestones")

(defapipatch-ghubp "/repos/:owner/:repo/milestones/:number"
  "Update a milestone."
  "issues/milestones/#create-a-milestone"
  (repo milestone) "/repos/:repo.owner.login/:repo.name/milestones/:milestone.number")

(defapidelete-ghubp "/repos/:owner/:repo/milestones/:number"
  "Delete a milestone."
  "issues/milestones/#delete-a-milestone"
  (repo milestone) "/repos/:repo.owner.login/:repo.name/milestones/:milestone.number")


;;; Organizations:

(defapiget-ghubp "/user/orgs"
  "List organizations for the authenticated user."
  "orgs/#list-your-organizations")

(defapiget-ghubp "/organizations"
  "Lists all organizations in the order that they were created on GitHub."
  "orgs/#list-all-organizations")

(defapiget-ghubp "/users/:username/orgs"
  "List public organization memberships for the specified user."
  "orgs/#list-user-organizations"
  (user) "/users/:user.login/orgs")

(defapiget-ghubp "/orgs/:org"
  "Get an organization."
  "orgs/#get-an-organization"
  (org) "/orgs/:org.login")

(defapipatch-ghubp "/orgs/:org"
  "Edit an organization."
  "orgs/#edit-an-organization"
  (org) "/orgs/:org.login")


;;; Pull Request:

(defapiget-ghubp "/repos/:owner/:repo/pulls"
  "List pull requests."
  "pulls/#list-pull-requests"
  (repo) "/repos/:repo.owner.login/:repo.name/pulls")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number"
  "Get a single pull request."
  "pulls/#get-a-single-pull-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number")

(defapipost-ghubp "/repos/:owner/:repo/pulls"
  "Create a pull request."
  "pulls/#create-a-pull-request"
  (repo) "/repos/:repo.owner.login/:repo.name/pulls")

(defapipatch-ghubp "/repos/:owner/:repo/pulls/:number"
  "Update a pull request."
  "pulls/#update-a-pull-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/commits"
  "List commits on a pull request."
  "pulls/#list-commits-on-a-pull-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/commits")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/files"
  "List pull request files."
  "pulls/#list-pull-requests-files"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/files")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/merge"
  "Get if a pull request has been merged."
  "pulls/#get-if-a-pull-request-has-been-merged"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/merge")

(defapiput-ghubp "/repos/:owner/:repo/pulls/:number/merge"
  "Merge a pull request (Merge Button)"
  "pulls/#merge-a-pull-request-merge-button"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/merge")


;;; Pull Request Reviews:

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/reviews"
  "List reviews on a pull request."
  "pulls/reviews/#list-reviews-on-a-pull-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/reviews/:id"
  "Get a single review."
  "pulls/reviews/#list-reviews-on-a-pull-request"
  (repo pull-request review) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews/:review.id")

(defapidelete-ghubp "/repos/:owner/:repo/pulls/:number/reviews/:id"
  "Delete a pending review."
  "pulls/reviews/#delete-a-pending-review"
  (repo pull-request review) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews/:review.id")

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/reviews/:id/comments"
  "Get comments for a single review."
  "pulls/reviews/#get-comments-for-a-single-review"
  (repo pull-request review) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews/:review.id/comments")

(defapipost-ghubp "/repos/:owner/:repo/pulls/:number/reviews"
  "Create a pull request review."
  "pulls/reviews/#create-a-pull-request-review"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews")

(defapipost-ghubp "/repos/:owner/:repo/pulls/:number/reviews/:id/events"
  "Submit a pull request review."
  "pulls/reviews/#submit-a-pull-request-review"
  (repo pull-request review) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews/:review.id/events")

(defapiput-ghubp "/repos/:owner/:repo/pulls/:number/reviews/:id/dismissals"
  "Dismiss a pull request review."
  "pulls/reviews/#dismiss-a-pull-request-review"
  (repo pull-request review) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/reviews/:review.id/dismissals")


;;; Pull Request Review Comments:

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/comments"
  "List comments on a pull request."
  "pulls/comments/#list-comments-on-a-pull-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/comments")

(defapiget-ghubp "/repos/:owner/:repo/pulls/comments"
  "List comments in a repository."
  "pulls/comments/#list-comments-in-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/pulls/comments")

(defapiget-ghubp "/repos/:owner/:repo/pulls/comments/:id"
  "Get a single comment."
  "pulls/comments/#get-a-single-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/pulls/comments/:thread.id")

(defapipost-ghubp "/repos/:owner/:repo/pulls/:number/comments"
  "Create a comment."
  "pulls/comments/#create-a-comment"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/comments")

(defapipatch-ghubp "/repos/:owner/:repo/pulls/comments/:id"
  "Edit a comment."
  "pulls/comments/#edit-a-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/pulls/comments/:thread.id")

(defapidelete-ghubp "/repos/:owner/:repo/pulls/comments/:id"
  "Delete a comment."
  "pulls/comments/#delete-a-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/pulls/comments/:thread.id")


;;; Pull Request Review Requests:

(defapiget-ghubp "/repos/:owner/:repo/pulls/:number/requested_reviewers"
  "List review requests."
  "pulls/review_requests/#list-review-requests"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/requested_reviewers")

(defapipost-ghubp "/repos/:owner/:repo/pulls/:number/requested_reviewers"
  "Create a review request."
  "pulls/review_requests/#create-a-review-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/requested_reviewers")

(defapidelete-ghubp "/repos/:owner/:repo/pulls/:number/requested_reviewers"
  "Delete a review request."
  "pulls/review_requests/#delete-a-review-request"
  (repo pull-request) "/repos/:repo.owner.login/:repo.name/pulls/:pull-request.number/requested_reviewers")


;;; Reactions:

(defapiget-ghubp "/repos/:owner/:repo/comments/:id/reactions"
  "List reactions for a commit comment."
  "reactions/#list-reactions-for-a-commit-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/comments/:thread.id/reactions")

(defapipost-ghubp "/repos/:owner/:repo/comments/:id/reactions"
  "Create reaction for a commit comment."
  "reactions/#create-reaction-for-a-commit-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/comments/:thread.id/reactions")

(defapiget-ghubp "/repos/:owner/:repo/issues/:number/reactions"
  "List reactions for an issue."
  "reactions/#list-reactions-for-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/reactions")

(defapipost-ghubp "/repos/:owner/:repo/issues/:number/reactions"
  "Create reaction for an issue."
  "reactions/#create-reaction-for-an-issue"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/reactions")

(defapiget-ghubp "/repos/:owner/:repo/issues/comments/:id/reactions"
  "List reactions for an issue comment."
  "reactions/#list-reactions-for-an-issue-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/comments/:thread.id/reactions")

(defapipost-ghubp "/repos/:owner/:repo/issues/comments/:id/reactions"
  "Create reaction for an issue comment."
  "reactions/#create-reaction-for-an-issue-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/issues/comments/:thread.id/reactions")

(defapiget-ghubp "/repos/:owner/:repo/pulls/comments/:id/reactions"
  "List reactions for a pull request review comment."
  "reactions/#list-reactions-for-a-pull-request-review-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/pulls/comments/:thread.id/reactions")

(defapipost-ghubp "/repos/:owner/:repo/pulls/comments/:id/reactions"
  "Create reaction for a pull request review comment."
  "reactions/#create-reaction-for-a-pull-request-review-comment"
  (repo thread) "/repos/:repo.owner.login/:repo.name/pulls/comments/:thread.id/reactions")

(defapidelete-ghubp "/reactions/:id"
  "Delete a reaction."
  "reactions/#delete-a-reaction"
  (thread) "/reactions/:thread.id")


;;; Repositories:

(defapiget-ghubp "/user/repos"
  "List your repositories.
List repositories that are accessible to the authenticated user.

This includes repositories owned by the authenticated user,
repositories where the authenticated user is a collaborator, and
repositories that the authenticated user has access to through an
organization membership."
  "repos/#list-your-repositories")

(defapiget-ghubp "/users/:username/repos"
  "List user repositories.
List public repositories for the specified user."
  "repos/#list-user-repositories"
  (user) "/users/:user.login/repos")

(defapiget-ghubp "/orgs/:org/repos"
  "List organization repositories.
List repositories for the specified org."
  "repos/#list-organization-repositories"
  (org) "/orgs/:org.login/repos")

(defapiget-ghubp "/repositories"
  "List all public repositories.
This provides a dump of every public repository, in the order
that they were created."
  "repos/#list-all-public-repositories")

(defapipost-ghubp "/user/repos"
  "Create.
Create a new repository for the authenticated user.  (Currently
not enabled for Integrations)."
  "repos/#create")

(defapipost-ghubp "/orgs/:org/repos"
  "Create a new repository in this organization.
The authenticated user must be a member of the specified
organization."
  "repos/#create"
  (org) "/orgs/:org.login/repos")

(defapiget-ghubp "/repos/:owner/:repo"
  "Get a specific repository object."
  "repos/#get"
  (repo) "/repos/:repo.owner.login/:repo.name"
  :condition-case
  (ghubp-catch*
   (404 nil)))


;;; Branches:

(defapiget-ghubp "/repos/:owner/:repo/branches/:branch"
  "Get branch"
  "repos/branches/#get-branch"
  (repo branch) "/repos/:repo.owner.login/:repo.name/branches/:branch.name"
  :condition-case
  (ghubp-catch*
   (404 nil)))


;;; Users:
(defapiget-ghubp "/users/:username"
  "Get a single user."
  "users/#get-a-single-user"
  (user) "/users/:user.login")

(defapiget-ghubp "/user"
  "Get the authenticated user."
  "users/#get-the-authenticated-user")

(defapipatch-ghubp "/user"
  "Update the authenticated user."
  "users/#update-the-authenticated-user")

(defapiget-ghubp "/users"
  "Get all users.
Lists all users, in the order that they signed up on GitHub. This
list includes personal user accounts and organization accounts."
  "users/#get-all-users")

;; Users - Emails

(defapiget-ghubp "/user/emails"
  "List email addresses for a user."
  "users/emails/#list-email-addresses-for-a-user")

(defapiget-ghubp "/user/public_emails"
  "List public email addresses for a user."
  "users/emails/#list-public-email-addresses-for-a-user")

(defapipost-ghubp "/user/emails"
  "Add email address(es).
You can post a single email address or an array of addresses."
  "users/emails/#add-email-addresses")

(defapidelete-ghubp "/user/emails"
  "Delete email address(es).
You can post a single email address or an array of addresses."
  "users/emails/#add-email-addresses")

(defapipatch-ghubp "/user/email/visibility"
  "Toggle primary email visibility."
  "users/emails/#toggle-primary-email-visibility")

;; Users - Followers

(defapiget-ghubp "/users/:username/followers"
  "List a user's followers."
  "users/followers/#list-followers-of-a-user"
  (user) "/users/:user.login/followers")

(defapiget-ghubp "/user/followers"
  "List the authenticated user's followers."
  "users/followers/#list-followers-of-a-user")

(defapiget-ghubp "/users/:username/following"
  "List who USER is following."
  "users/followers/#list-users-followed-by-another-user"
  (user) "/users/:user.login/following")

(defapiget-ghubp "/user/following"
  "List who the authenticated user is following."
  "users/followers/#list-users-followed-by-another-user")

(defapiget-ghubp "/user/following/:username"
  "Check if you are following USER."
  "users/followers/#check-if-you-are-following-a-user"
  (user) "/user/following/:user.login")

(defapiget-ghubp "/users/:username/following/:target_user"
  "Check if USER-1 follows USER-2."
  "users/followers/#check-if-you-are-following-a-user"
  (user-1 user-2) "/users/:user-1.login/following/:user-2.login")

(defapiput-ghubp "/user/following/:username"
  "Follow USER."
  "users/followers/#follow-a-user"
  (user) "/user/following/:user.login")

(defapidelete-ghubp "/user/following/:username"
  "Unfollow USER."
  "users/followers/#unfollow-a-user"
  (user) "/user/following/:user.login")

;; Users - Git SSH Keys

(defapiget-ghubp "/users/:username/keys"
  "Lists the verified public keys for a user.
This is accessible by anyone."
  "users/keys/#list-public-keys-for-a-user"
  (user) "/users/:user.login/keys")

(defapiget-ghubp "/user/keys"
  "List your public keys."
  "users/keys/#list-your-public-keys")

(defapiget-ghubp "/user/keys/:id"
  "Get a single public key."
  "users/keys/#get-a-single-public-key"
  (key) "/user/keys/:key.id")

(defapiput-ghubp "/user/keys"
  "Create a public key."
  "users/keys/#create-a-public-key")

(defapidelete-ghubp "/user/keys/:id"
  "Delete a single public key."
  "users/keys/#get-a-single-public-key"
  (key) "/user/keys/:key.id")

;; Users - GPG Keys

;; TODO: Currently in preview.

;; https://developer.github.com/v3/users/gpg_keys/

;; Users - Blocking

;; TODO: Currently in preview.

;; https://developer.github.com/v3/users/blocking/

;; Activity - Notifications

;; TODO: It would be nice if ghub+ could offer 'smart' polling for
;; notifications to trim down on API requests.  This smart polling is
;; supported by GitHub for notifications in particular.  If done
;; right, we could offer a 'push'-type interface to handle when new
;; notifications are received.

;; (ghubp-notifications-{start,stop}-polling)
;; ghubp-notifications-received-hook

(defapiget-ghubp "/notifications"
  "Get the user's notifications."
  "activity/notifications/#list-your-notifications")

(defapiget-ghubp "/repos/:owner/:repo/notifications"
  "List your notifications in a repository."
  "activity/notifications/#list-your-notifications-in-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/notifications")

(defapiput-ghubp "/notifications"
  "Mark as read.
Marking a notification as \"read\" removes it from the default
view on GitHub."
  "activity/notifications/#mark-as-read")

(defapiput-ghubp "/repos/:owner/:repo/notifications"
  "Mark notifications as read in a repository.
Marking all notifications in a repository as \"read\" removes
them from the default view on GitHub."
  "activity/notifications/#mark-notifications-as-read-in-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/notifications")

(defapiget-ghubp "/notifications/threads/:id"
  "View a single thread."
  "activity/notifications/#view-a-single-thread"
  (thread) "/notifications/threads/:thread.id")

(defapipatch-ghubp "/notifications/threads/:id"
  "Mark a thread as read."
  "activity/notifications/#mark-a-thread-as-read"
  (thread) "/notifications/threads/:thread.id")

(defapiget-ghubp "/notifications/threads/:id/subscription"
  "Get a thread subscription.
This checks to see if the current user is subscribed to a
thread."
  "activity/notifications/#get-a-thread-subscription"
  (thread) "/notifications/threads/:thread.id/subscription")

(defapiput-ghubp "/notifications/threads/:id/subscription"
  "Set a thread subscription.
This lets you subscribe or unsubscribe from a conversation.
Unsubscribing from a conversation mutes all future
notifications (until you comment or get @mentioned once more)."
  "activity/notifications/#set-a-thread-subscription"
  (thread) "/notifications/threads/:thread.id/subscription")

(defapidelete-ghubp "/notifications/threads/:id/subscription"
  "Delete a thread subscription."
  "activity/notifications/#delete-a-thread-subscription"
  (thread) "/notifications/threads/:thread.id/subscription")


;;; Unfiled:

(defapiget-ghubp "/repos/:owner/:repo/commits/:ref/statuses"
  "List statuses for a specific ref"
  "repos/statuses/#list-statuses-for-a-specific-ref"
  (repo ref) "/repos/:repo.owner.login/:repo.name/commits/:ref/statuses")

(defapiget-ghubp "/repos/:owner/:repo/commits/:ref/status"
  "Get the combined status for a specific ref"
  "repos/statuses/#get-the-combined-status-for-a-specific-ref"
  (repo ref) "/repos/:repo.owner.login/:repo.name/commits/:ref/status"
  :condition-case
  (ghubp-catch*
   (404 nil)))

(defapipost-ghubp "/repos/:owner/:repo/forks"
  "Create a fork for the authenticated user."
  "repos/forks/#create-a-fork"
  (repo) "/repos/:repo.owner.login/:repo.name/forks")

(defapipost-ghubp "/repos/:owner/:repo/issues/:number/comments"
  "Post a comment to an issue"
  "issues/comments/#create-a-comment"
  (repo issue) "/repos/:repo.owner.login/:repo.name/issues/:issue.number/comments")

(defapiget-ghubp "/repos/:owner/:repo/commits"
  "List commits on a repository"
  "repos/commits/#list-commits-on-a-repository"
  (repo) "/repos/:repo.owner.login/:repo.name/commits")

(defun ghubp-url-parse (url)
  "Parse URL for its type and API callback.

A cons cell is returned.  The car is one of

 - `issue'
 - `pull-request'

and the cdr is a callback suitable for `ghub-get', etc."
  (let ((callback (url-filename (url-generic-parse-url url))))
    (cons
     (cond
      ((string-match-p (rx bol "/repos/" (+? any) "/" (+? any) "/issues/" (+ digit) eol)
                       callback)
       'issue)
      ((string-match-p (rx bol "/repos/" (+? any) "/" (+? any) "/pulls/" (+ digit) eol)
                       callback)
       'pull-request)
      (t 'unknown))
     callback)))

(provide 'ghub+)
;;; ghub+.el ends here