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