From a1e01be01bd102ea3b9496b928199efc6615de2c Mon Sep 17 00:00:00 2001 From: Wanderson Ferreira Date: Tue, 30 Nov 2021 15:21:51 -0300 Subject: [PATCH] github support to get binary files --- code-review-github.el | 22 ++++++++++++++++++++++ code-review-section.el | 24 ++++++++++++++++++++++++ code-review-utils.el | 7 +++++++ code-review.el | 38 ++++++++++++++++++++++---------------- 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/code-review-github.el b/code-review-github.el index 69421a8..b4a6201 100644 --- a/code-review-github.el +++ b/code-review-github.el @@ -641,5 +641,27 @@ https://github.com/wandersoncferreira/code-review#configuration")) (title . ,title) (body . ,(oref value msg))))))) +(cl-defmethod code-review-core-files ((github code-review-github-repo) callback) + "Get files from GITHUB PR and call CALLBACK." + (ghub-get (format "/repos/%s/%s/pulls/%s/files" + (oref github owner) + (oref github repo) + (oref github number)) + nil + :auth 'code-review + :errorback #'code-review-github-errback + :callback callback)) + +(cl-defmethod code-review-core-files-deferred ((github code-review-github-repo)) + "Get files from GITHUB PR." + (let ((d (deferred:new #'identity))) + (code-review-core-files + github + (apply-partially + (lambda (d v &rest _) + (deferred:callback-post d v)) + d)) + d)) + (provide 'code-review-github) ;;; code-review-github.el ends here diff --git a/code-review-section.el b/code-review-section.el index 0818b8a..7c559d6 100644 --- a/code-review-section.el +++ b/code-review-section.el @@ -1144,6 +1144,15 @@ A quite good assumption: every comment in an outdated hunk will be outdated." amount-loc-incr)) (code-review-comment-insert-lines c)))))) +(defclass code-review-binary-file-section (magit-section) + ((keymap :initform 'code-review-binary-file-section-map))) + +(defvar code-review-binary-file-section-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "RET") 'code-review-utils--visit-binary-file-at-point) + map) + "Keymaps for binary files sections.") + (defun code-review-section--magit-diff-insert-file-section (file orig status modes rename header &optional long-status) "Overwrite the original Magit function on `magit-diff.el' FILE. @@ -1181,6 +1190,21 @@ ORIG, STATUS, MODES, RENAME, HEADER and LONG-STATUS are arguments of the origina (magit-insert-section (hunk '(rename)) (insert rename) (magit-insert-heading))) + (when (string-match-p "Binary files.*" header) + (let* ((pr (code-review-db-get-pullreq)) + (raw-infos (oref pr raw-infos)) + (binary-files (a-get raw-infos 'binary-files)) + (entry (alist-get file binary-files nil nil 'equal))) + (magit-insert-section (code-review-binary-file-section entry) + (insert (propertize + (format "Visit on %s" + (cond + ((code-review-github-repo-p pr) "Github") + (t + (error "Unsupported")))) + 'face + 'code-review-request-review-face) + "\n")))) (magit-wash-sequence #'magit-diff-wash-hunk))) (defun code-review-section--magit-diff-wash-hunk () diff --git a/code-review-utils.el b/code-review-utils.el index c397ee9..f76b7e3 100644 --- a/code-review-utils.el +++ b/code-review-utils.el @@ -556,5 +556,12 @@ Expect the same output as `git diff --no-prefix`" .latestOpinionatedReviews.nodes) groups))) +(defun code-review-utils--visit-binary-file-at-point () + "Visit binary file at point." + (interactive) + (let ((section (magit-current-section))) + (with-slots (value) section + (browse-url (a-get value 'blob_url))))) + (provide 'code-review-utils) ;;; code-review-utils.el ends here diff --git a/code-review.el b/code-review.el index d72d5b9..ddf5206 100644 --- a/code-review.el +++ b/code-review.el @@ -221,21 +221,26 @@ If you want to display a minibuffer MSG in the end." (cl-defmethod code-review--internal-build ((_github code-review-github-repo) progress res &optional buff-name msg) "Helper function to build process for GITHUB based on the fetched RES informing PROGRESS." - ;; 2. save raw diff data - (progress-reporter-update progress 3) - (code-review-db--pullreq-raw-diff-update - (code-review-utils--clean-diff-prefixes - (a-get (-first-item res) 'message))) - - ;; 2.1 save raw info data e.g. data from GraphQL API - (progress-reporter-update progress 4) - (code-review-db--pullreq-raw-infos-update - (a-get-in (-second-item res) (list 'data 'repository 'pullRequest))) - - ;; 2.2 trigger renders - (progress-reporter-update progress 5) - (code-review--trigger-hooks buff-name msg) - (progress-reporter-done progress)) + (let* ((raw-infos (a-get-in (-second-item res) (list 'data 'repository 'pullRequest))) + (files-info (-reduce-from + (lambda (acc file) + (a-assoc acc (a-get file 'filename) file)) + nil (-third-item res))) + (infos (a-assoc raw-infos 'binary-files files-info))) + ;; 2. save raw diff data + (progress-reporter-update progress 3) + (code-review-db--pullreq-raw-diff-update + (code-review-utils--clean-diff-prefixes + (a-get (-first-item res) 'message))) + + ;; 2.1 save raw info data e.g. data from GraphQL API + (progress-reporter-update progress 4) + (code-review-db--pullreq-raw-infos-update infos) + + ;; 2.2 trigger renders + (progress-reporter-update progress 5) + (code-review--trigger-hooks buff-name msg) + (progress-reporter-done progress))) (cl-defmethod code-review--internal-build ((_gitlab code-review-gitlab-repo) progress res &optional buff-name msg) "Helper function to build process for GITLAB based on the fetched RES informing PROGRESS." @@ -272,7 +277,8 @@ If you want to provide a MSG for the end of the process." (deferred:$ (deferred:parallel (lambda () (code-review-core-diff-deferred obj)) - (lambda () (code-review-core-infos-deferred obj))) + (lambda () (code-review-core-infos-deferred obj)) + (lambda () (code-review-core-files-deferred obj))) (deferred:nextc it (lambda (x) (progress-reporter-update progress 2)