feat: add ghpr-list-prs

This commit is contained in:
Lucas Sta Maria 2025-08-22 22:48:35 +08:00
parent 28e18f7ecb
commit 8723768c9d
Signed by: lucas
GPG key ID: F07FB16A826E3B47
4 changed files with 119 additions and 1 deletions

View file

@ -23,6 +23,59 @@
;;; Code:
(require 'auth-source)
(require 'request)
(require 'json)
(defun ghpr--get-token ()
"Retrieve GitHub authentication token from auth-sources.
Returns the token string if found, nil otherwise."
(auth-source-pick-first-password :host "api.github.com"))
(defun ghpr--parse-api-pr (pr)
"Parse a single pull request object, keeping only essential fields."
(let* ((get (lambda (field) (alist-get field pr)))
(base (get 'base))
(user (get 'user)))
`((url . ,(get 'url))
(id . ,(get 'id))
(diff_url . ,(get 'diff_url))
(patch_url . ,(get 'patch_url))
(number . ,(get 'number))
(state . ,(get 'state))
(locked . ,(get 'locked))
(title . ,(get 'title))
(body . ,(get 'body))
(username . ,(alist-get 'login user))
(base_sha . ,(alist-get 'sha base))
(merge_commit_sha . ,(get 'merge_commit_sha)))))
(defun ghpr--parse-api-pr-list (pr-list)
"Parse a list of pull request objects, keeping only essential fields."
(mapcar #'ghpr--parse-api-pr pr-list))
(defun ghpr--list-open-prs (repo-name)
"List open pull requests for REPO-NAME in owner/repo format.
Returns a list of pull request objects on success, nil on failure."
(let ((token (ghpr--get-token))
(url (format "https://api.github.com/repos/%s/pulls?state=open" repo-name))
(result nil))
(when token
(request url
:type "GET"
:headers `(("Accept" . "application/vnd.github+json")
("Authorization" . ,(format "Bearer %s" token))
("X-GitHub-Api-Version" . "2022-11-28"))
:parser 'json-read
:sync t
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq result (ghpr--parse-api-pr-list data))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(message "Error fetching pull requests: %s" error-thrown)))))
result))
(provide 'ghpr-api)
;;; ghpr-api.el ends here