Skip to content

Commit

Permalink
github support to get binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
wandersoncferreira committed Nov 30, 2021
1 parent 4427bfd commit a1e01be
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
22 changes: 22 additions & 0 deletions code-review-github.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions code-review-section.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ()
Expand Down
7 changes: 7 additions & 0 deletions code-review-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 22 additions & 16 deletions code-review.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a1e01be

Please sign in to comment.