aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-github.el
blob: 2fe90fdf3d661b5af29146d3f129fcfee378d77a (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
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; my-github.el -- Github 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:

;; Github client.

;;; Code:


(defun my-grok-github (url)
  "get github info of a project.
url is the url of the project
License; name; description; homepage; created at"
  (when (string-match "github.com\\(/[^/]+/[^/]+\\)/?.*$" url)
    (with-current-buffer (url-retrieve-synchronously
                          (concat "https://api.github.com/repos"
                                  (replace-regexp-in-string
                                   "\\.git$" "" (match-string 1 url))))
      (set-buffer-multibyte t)
      (my-delete-http-header)
      (my-grok-github-make-info (json-read)))))

(defun my-post-process-licensing-name (name)
  (cond ((equal name "MIT") "expat")
        (t name)))

(defun my-grok-github-make-info (raw)
  (list (cons "Title" (alist-get 'name raw))
        (cons "Description" (alist-get 'description raw))
        (cons "Source" (alist-get 'html_url raw))
        (cons "Website" (alist-get 'homepage raw))
        (cons "Released" (substring (alist-get 'created_at raw) 0 10))
        (cons "Pushed" (substring (alist-get 'pushed_at raw) 0 10))
        (cons "Subject" (string-join (alist-get 'topics raw) ", "))
        ;; FIXME: why did we comment this out?
        ;; (cons "License" (my-post-process-licensing-name
        ;;                  (alist-get 'spdx_id (alist-get 'license raw))))
        (cons "Developers" (my-grok-github-get-developer-name
                            (alist-get 'url (alist-get 'owner raw))))))

(defun my-grok-github-get-developer-name (url)
  (with-current-buffer (url-retrieve-synchronously url)
    (set-buffer-multibyte t)
    (my-delete-http-header)
    (alist-get 'name (json-read))))

;;; urls with github
;; TODO: generalise the following to common forges, including
;; savannah, cgit, gitlab etc.
(defun my-github-revision-url (file revision)
  "Returns the github url of the upstream repo containing FILE at REVISION."
  (let ((repo-url (vc-git-repository-url file))
        (revision (or revision (vc-working-revision default-directory))))
    (format "%s/commit/%s" repo-url revision)))

(defun my-github-kill-revision-url (revision)
  "Kill the github revision url for REVISION."
  (kill-new (my-github-revision-url revision)))

(defun my-github-file-loc-url (file-loc &optional revision)
  "Convert a file location to a github url."
  (pcase-let* ((`(,file ,line-no) (split-string file-loc ":"))
               (revision (or revision (vc-working-revision file))))
    (my-github-file-loc-url-internal file line-no revision)))

(defun my-github-file-loc-url-internal (file line-no revision)
  "Convert a FILE location at LINE-NO to a github url.

REVISION is the commit hash."
  (let* ((repo-url (vc-git-repository-url file))
         (repo-root (vc-git-root file))
         (path (file-relative-name file repo-root)))
    (format "%s/blob/%s/%s#L%s" repo-url revision path line-no)))

(defun my-github-kill-current-file-loc-url ()
  "Kill the github url from where the point is at."
  (interactive)
  (kill-new (my-github-file-loc-url-internal
             (buffer-file-name)
             (1+ (current-line))
             (vc-working-revision (buffer-file-name)))))

(defun my-org-backtrace-to-github (bt &optional revision)
  (string-join
   (mapcar
    (lambda (link)
      (string-match "\\[\\[\\(.*\\)\\]\\[\\(.*\\)\\]\\]" link)
      (let ((target (match-string 1 link))
            (label (match-string 2 link)))
        (format "[[%s][%s]]"
                (my-github-file-loc-url target revision)
                label)))
    (split-string bt " > "))
   " > "))

(defun my-org-backtrace-to-github-region (beg end)
  (interactive "r")
  (kill-new
   (my-org-backtrace-to-github (buffer-substring-no-properties beg end))))

(defun my-org-backtrace-to-github-slack (beg end)
  (interactive "r")
  (let ((bt (buffer-substring-no-properties beg end))
        (revision (when current-prefix-arg
                    (read-string "Rrevision: ")))
        )
    (with-temp-buffer
      (insert "#+options: ^:nil
")
      (goto-char (point-max))
      (insert (my-org-backtrace-to-github bt revision))
      (org-md-export-as-markdown))))

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