aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-gitlab.el
blob: ad7f0ed3c4ba0b5018e33dcba114ef63dc230b5f (plain) (blame)
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
;;; my-gitlab.el -- gitlab client -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; gitlab client.

;;; Code:

(require 'infobox)

(defun my-gitlab-get-project-id (url)
  (with-current-buffer (url-retrieve-synchronously
                        (replace-regexp-in-string "\\.git$" "" url))
    (let ((dom (libxml-parse-html-region (point-min) (point-max))))
      (dom-attr (car
                 (dom-search dom (lambda (n) (dom-attr n 'data-project-id))))
                'data-project-id))))

(defun my-gitlab-api-projects (url)
  (when-let* ((urlobj (url-generic-parse-url url))
              (project-id (my-gitlab-get-project-id url)))
    (my-url-fetch-json
     (format "%s://%s/api/v4/projects/%s"
             (url-type urlobj)
             (url-host urlobj)
             project-id))))

(defvar my-gitlab-readme-get-raw nil "Whether to get raw or html readme")

(defun my-gitlab-project-info (url)
  "Given a url, returns project info."
  (let ((info (my-gitlab-api-projects url)))
    (let-alist info
      (when .readme_url
        (setf (alist-get 'readme info)
              (if my-gitlab-readme-get-raw
                  (format
                   "\n%s"
                   (my-url-fetch-raw
                    (replace-regexp-in-string "/-/blob/" "/-/raw/" .readme_url)))
                (alist-get
                 'html
                 (my-url-fetch-json
                  (format "%s?format=json&viewer=rich" .readme_url)))))))
    info))

(defun my-gitlab-format-time-string (t)
  (format-time-string "%Y-%m-%d %M:%M:%S" (encode-time (parse-time-string t))))

(defun my-gitlab-project-url-p (url)
  (let ((urlobj (url-generic-parse-url url)))
    (and (equal (url-host urlobj) "gitlab.com")
         (string-match-p "^/[^/]+/[^/]+$" (url-filename urlobj)))))

(require 'my-buffer)

(defvar my-gitlab-project-info-specs
  `((http_url_to_repo . "Clone")
    (name_with_namespace . "Name")
    (description . "Description")
    (created_at . ("Created at" . my-gitlab-format-time-string))
    (last_activity_at . ("Updated at" . my-gitlab-format-time-string))
    (topics . ("Topics" . ,(lambda (xs)
                             (mapconcat #'identity xs "; "))))
    (star_count . ("Stars" . number-to-string))
    (forks_count . ("Forks" . number-to-string))
    (readme . (body . ,(lambda (text)
                         (with-temp-buffer
                           (insert text)
                           (shr-render-region (point-min) (point-max))
                           (buffer-string)))))))

(defun my-gitlab-project-infobox (url)
  "Display a gitlab project info at URL in a help buffer.

A good example would be
<https://gitlab.com/woob/woob>
"
  (interactive "sGitlab project URL: ")
  (infobox-render
   (infobox-translate
    (my-gitlab-project-info url) my-gitlab-project-info-specs)
   `(my-gitlab-project-infobox ,url)
   (called-interactively-p 'interactive)))

(defun my-grok-gitlab (url)
  (my-grok-gitlab-make-info (my-gitlab-api-projects url)))

(defun my-grok-gitlab-make-info (raw)
  (list (cons "Title" (alist-get 'name raw))
        (cons "Description" (my-clean-property-value
                             (alist-get 'description raw)))
        (cons "Source" (alist-get 'web_url raw))
        (cons "Subject" (string-join
                         (alist-get 'tag_list raw) ", "))
        (cons "Released" (substring (alist-get 'created_at raw) 0 10))
        (cons "Last-activity" (substring
                               (alist-get 'last_activity_at raw) 0 10))
        (cons "Developers" (alist-get 'name (alist-get 'namespace raw)))))

(provide 'my-gitlab)
;;; my-gitlab.el ends here