mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 c655eea759be1db69c5e6b45c228139d8390122a
commit | author | age
5cb5f7 1 ;;; glab.el --- minuscule client library for the Gitlab API  -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2016-2018  Jonas Bernoulli
4
5 ;; Author: Jonas Bernoulli <jonas@bernoul.li>
6 ;; Homepage: https://github.com/magit/ghub
7 ;; Keywords: tools
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; For a copy of the GPL see https://www.gnu.org/licenses/gpl.txt.
22
23 ;;; Commentary:
24
25 ;; Glab is a library that provides basic support for using the Gitlab API
26 ;; from Emacs packages.  It abstracts access to API resources using only
27 ;; a handful of functions that are not resource-specific.
28
29 ;; This library is implemented on top of Ghub.  Unlike Ghub, Glab does
30 ;; not support the guided creation of tokens because Gitlab lacks the
31 ;; features that would be necessary to implement that.  Users have to
32 ;; create tokens through the web interface.
33
34 ;;; Code:
35
36 (require 'ghub)
37
38 (defconst glab-default-host "gitlab.com/api/v4"
39   "The default host that is used if `glab.host' is not set.")
40
41 (cl-defun glab-head (resource &optional params
42                               &key query payload headers
43                               silent unpaginate noerror reader
44                               username auth host
45                               callback errorback extra)
46   "Make a `HEAD' request for RESOURCE, with optional query PARAMS.
47 Like calling `ghub-request' (which see) with \"HEAD\" as METHOD
48 and `gitlab' as FORGE."
49   (ghub-request "HEAD" resource params :forge 'gitlab
50                 :query query :payload payload :headers headers
51                 :silent silent :unpaginate unpaginate
52                 :noerror noerror :reader reader
53                 :username username :auth auth :host host
54                 :callback callback :errorback errorback :extra extra))
55
56 (cl-defun glab-get (resource &optional params
57                              &key query payload headers
58                              silent unpaginate noerror reader
59                              username auth host
60                              callback errorback extra)
61   "Make a `GET' request for RESOURCE, with optional query PARAMS.
62 Like calling `ghub-request' (which see) with \"GET\" as METHOD
63 and `gitlab' as FORGE."
64   (ghub-request "GET" resource params :forge 'gitlab
65                 :query query :payload payload :headers headers
66                 :silent silent :unpaginate unpaginate
67                 :noerror noerror :reader reader
68                 :username username :auth auth :host host
69                 :callback callback :errorback errorback :extra extra))
70
71 (cl-defun glab-put (resource &optional params
72                              &key query payload headers
73                              silent unpaginate noerror reader
74                              username auth host
75                              callback errorback extra)
76   "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
77 Like calling `ghub-request' (which see) with \"PUT\" as METHOD
78 and `gitlab' as FORGE."
79   (ghub-request "PUT" resource params :forge 'gitlab
80                 :query query :payload payload :headers headers
81                 :silent silent :unpaginate unpaginate
82                 :noerror noerror :reader reader
83                 :username username :auth auth :host host
84                 :callback callback :errorback errorback :extra extra))
85
86 (cl-defun glab-post (resource &optional params
87                               &key query payload headers
88                               silent unpaginate noerror reader
89                               username auth host
90                               callback errorback extra)
91   "Make a `POST' request for RESOURCE, with optional payload PARAMS.
92 Like calling `ghub-request' (which see) with \"POST\" as METHOD
93 and `gitlab' as FORGE."
94   (ghub-request "POST" resource params :forge 'gitlab
95                 :query query :payload payload :headers headers
96                 :silent silent :unpaginate unpaginate
97                 :noerror noerror :reader reader
98                 :username username :auth auth :host host
99                 :callback callback :errorback errorback :extra extra))
100
101 (cl-defun glab-patch (resource &optional params
102                                &key query payload headers
103                                silent unpaginate noerror reader
104                                username auth host
105                                callback errorback extra)
106   "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
107 Like calling `ghub-request' (which see) with \"PATCH\" as METHOD
108 and `gitlab' as FORGE."
109   (ghub-request "PATCH" resource params :forge 'gitlab
110                 :query query :payload payload :headers headers
111                 :silent silent :unpaginate unpaginate
112                 :noerror noerror :reader reader
113                 :username username :auth auth :host host
114                 :callback callback :errorback errorback :extra extra))
115
116 (cl-defun glab-delete (resource &optional params
117                                 &key query payload headers
118                                 silent unpaginate noerror reader
119                                 username auth host
120                                 callback errorback extra)
121   "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
122 Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
123 and `gitlab' as FORGE."
124   (ghub-request "DELETE" resource params :forge 'gitlab
125                 :query query :payload payload :headers headers
126                 :silent silent :unpaginate unpaginate
127                 :noerror noerror :reader reader
128                 :username username :auth auth :host host
129                 :callback callback :errorback errorback :extra extra))
130
131 (cl-defun glab-request (method resource &optional params
132                                &key query payload headers
133                                silent unpaginate noerror reader
134                                username auth host
135                                callback errorback extra)
136   "Make a request for RESOURCE and return the response body.
137 Like calling `ghub-request' (which see) with `gitlab' as FORGE."
138   (ghub-request method resource params :forge 'gitlab
139                 :query query :payload payload :headers headers
140                 :silent silent :unpaginate unpaginate
141                 :noerror noerror :reader reader
142                 :username username :auth auth :host host
143                 :callback callback :errorback errorback :extra extra))
144
145 (cl-defun glab-repository-id (owner name &key username auth host)
146   "Return the id of the repository specified by OWNER, NAME and HOST."
147   (number-to-string
148    (cdr (assq 'id (glab-get (format "/projects/%s%%2F%s" owner name)
149                             nil :username username :auth auth :host host)))))
150
151 ;;; _
152 (provide 'glab)
153 ;;; glab.el ends here