Skip to content

Commit 4d91dd6

Browse files
authored
Merge pull request #79 from wandersoncferreira/master
2 parents 341b7a1 + 34bfb7c commit 4d91dd6

3 files changed

Lines changed: 182 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ machine api.github.com login yourlogin^github-review password MYTOKENGOESHERE
119119
If you use GitHub Enterprise, you can use the `github-review-host` custom variable to
120120
configure the endpoint of your GitHub Enterprise installation, this should look like `api.git.mycompany.com`.
121121

122+
- By default, `github-review` fetches only top level comments in a pull request.
123+
You can set `github-review-view-comments-in-code-lines` to `t` to also fetch
124+
comments made between code lines.
125+
126+
You can also enable comments between code lines that are outdated by setting
127+
`github-review-view-comments-in-code-lines-outdated` to `t`, however we cannot
128+
guarantee correct placement of these comments in the review buffer.
129+
122130
## Notice
123131

124132
*I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my

github-review.el

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@
6060
:group 'github-review
6161
:type 'string)
6262

63+
(defcustom github-review-view-comments-in-code-lines nil
64+
"Flag to enable displaying comments in code lines."
65+
:group 'github-review)
66+
67+
(defcustom github-review-view-comments-in-code-lines-outdated nil
68+
"Flag to enable displaying outdated comments in code lines."
69+
:group 'github-review)
70+
6371
(defconst github-review-diffheader '(("Accept" . "application/vnd.github.v3.diff"))
6472
"Header for requesting diffs from GitHub.")
6573

@@ -119,8 +127,26 @@ return a deferred object"
119127
nodes { author { login } bodyText state }
120128
} }
121129
}
130+
}" .repo .owner .num))
131+
(query-with-comments (format "query {
132+
repository(name: \"%s\", owner: \"%s\") {
133+
pullRequest(number: %s){
134+
headRef { target{ oid } }
135+
title
136+
bodyText
137+
comments(first:50) {
138+
nodes { author { login } bodyText }
139+
}
140+
reviews(first: 50) {
141+
nodes { author { login } bodyText state
142+
comments(first: 50)
143+
{ nodes { bodyText originalPosition position outdated path} }}
144+
} }
145+
}
122146
}" .repo .owner .num)))
123-
(ghub-graphql query
147+
(ghub-graphql (if github-review-view-comments-in-code-lines
148+
query-with-comments
149+
query)
124150
'()
125151
:auth 'github-review
126152
:host (github-review-api-host pr-alist)
@@ -340,7 +366,82 @@ This function infers the PR name based on the current filename"
340366
(defun github-review-format-review (review)
341367
"Format a REVIEW object to string."
342368
(let-alist review
343-
(format "Reviewed by @%s[%s]: %s" .author.login .state .bodyText)))
369+
(if (not (string-empty-p .bodyText))
370+
(format "Reviewed by @%s[%s]: %s" .author.login .state .bodyText)
371+
"")))
372+
373+
(defvar github-review-comment-pos ()
374+
"Variable to count how many comments in code lines were added in the diff.
375+
This is necessary to adjust the new comments to the correct position in the diff given that
376+
Github API provides only the originalPosition in the query.")
377+
378+
(defun github-review--get-how-many-comments-written (path)
379+
(or (a-get github-review-comment-pos path) 0))
380+
381+
(defun github-review-place-review-comments (gitdiff review)
382+
(if (not (a-get-in review (list 'comments 'nodes)))
383+
gitdiff
384+
(let* ((at (a-get-in review (list 'author 'login)))
385+
(body (a-get review 'bodyText))
386+
(body-lines (split-string body "\n"))
387+
(state (a-get review 'state))
388+
389+
(comments (a-get-in review (list 'comments 'nodes)))
390+
(default-shift-pos 1))
391+
(-reduce-from
392+
(lambda (acc-diff comment)
393+
(if (and (not github-review-view-comments-in-code-lines-outdated)
394+
(a-get comment 'outdated))
395+
acc-diff
396+
(let* ((path (a-get comment 'path))
397+
(original-pos (a-get comment 'originalPosition))
398+
(-position (a-get comment 'position))
399+
(position (when (numberp -position) -position))
400+
(adjusted-pos (+ (or position original-pos)
401+
default-shift-pos
402+
(github-review--get-how-many-comments-written path)))
403+
(comment-lines (split-string (a-get comment 'bodyText) "\n"))
404+
405+
;; get diff lines specific for the current path
406+
(gitdiff-path (s-concat "+++ b/" path "\n"))
407+
(gitdiff-splitted-on-path (split-string acc-diff gitdiff-path))
408+
(gitdiff-on-path-lines (split-string (-second-item gitdiff-splitted-on-path) "\n"))
409+
(gitdiff-on-path-splitted (-split-at adjusted-pos gitdiff-on-path-lines)))
410+
411+
;; save how many lines of comments was written in the buffer for this path
412+
(setf (alist-get path github-review-comment-pos nil nil 'equal)
413+
(+ (github-review--get-how-many-comments-written path)
414+
(length comment-lines)
415+
(if (string-empty-p body)
416+
0
417+
(length body-lines))))
418+
419+
;; include comments on buffer for this path
420+
(let* ((result
421+
(-concat
422+
(-first-item gitdiff-on-path-splitted)
423+
(list (format "~ Reviewed by @%s[%s]: %s" at state
424+
(if (string-empty-p body)
425+
(-first-item comment-lines)
426+
(-first-item body-lines))))
427+
(-map
428+
(lambda (commentLine) (s-concat "~ " (s-trim-left commentLine)))
429+
(-concat
430+
(-drop 1 body-lines)
431+
(if (string-empty-p body)
432+
(-drop 1 comment-lines)
433+
comment-lines)))
434+
(-second-item gitdiff-on-path-splitted)))
435+
(gitdiff-on-path-new (s-concat
436+
gitdiff-path
437+
(s-join "\n" result))))
438+
439+
;; join this path with beginning of the diff
440+
(s-concat
441+
(-first-item gitdiff-splitted-on-path)
442+
gitdiff-on-path-new)))))
443+
gitdiff
444+
comments))))
344445

345446
(defun github-review-format-diff (gitdiff pr)
346447
"Formats a GITDIFF and PR to save it for review."
@@ -366,7 +467,15 @@ This function infers the PR name based on the current filename"
366467
#'github-review-to-comments
367468
(-map #'github-review-format-review reviews)))
368469
"\n"))
369-
(a-get gitdiff 'message))))
470+
(if github-review-view-comments-in-code-lines
471+
(progn
472+
(setq github-review-comment-pos ())
473+
(-reduce-from
474+
(lambda (acc-gitdiff node)
475+
(github-review-place-review-comments acc-gitdiff node))
476+
(a-get gitdiff 'message)
477+
.reviews.nodes))
478+
(a-get gitdiff 'message)))))
370479

371480
;;;;;;;;;;;;;;;;;;;;;
372481
;; User facing API ;;

test/github-review-test.el

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ index 9eced0230..4512bb335 100644
125125
+type MultiBalanceReport = PeriodicReport AccountName MixedAmount
126126
+type MultiBalanceReportRow = PeriodicReportRow AccountName MixedAmount
127127
128+
-- type alias just to remind us which AccountNames might be depth-clipped, below.
129+
type ClippedAccountName = AccountName
130+
")
131+
132+
(defconst example-diff-before-comments-in-code-line "diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
133+
index 9eced0230..4512bb335 100644
134+
--- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
135+
+++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
136+
137+
-type MultiBalanceReport = PeriodicReport AccountLeaf MixedAmount
138+
-type MultiBalanceReportRow = PeriodicReportRow AccountLeaf MixedAmount
139+
+type MultiBalanceReport = PeriodicReport AccountName MixedAmount
140+
+type MultiBalanceReportRow = PeriodicReportRow AccountName MixedAmount
141+
142+
-- type alias just to remind us which AccountNames might be depth-clipped, below.
143+
type ClippedAccountName = AccountName
144+
")
145+
146+
(defconst example-diff-after-comments-in-code-line "diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
147+
index 9eced0230..4512bb335 100644
148+
--- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
149+
+++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs
150+
151+
-type MultiBalanceReport = PeriodicReport AccountLeaf MixedAmount
152+
-type MultiBalanceReportRow = PeriodicReportRow AccountLeaf MixedAmount
153+
~ Reviewed by @babar[COMMENTED]: Very interesting change
154+
~ we should move forward
155+
+type MultiBalanceReport = PeriodicReport AccountName MixedAmount
156+
+type MultiBalanceReportRow = PeriodicReportRow AccountName MixedAmount
157+
~ Reviewed by @babar[COMMENTED]: Change this code
158+
128159
-- type alias just to remind us which AccountNames might be depth-clipped, below.
129160
type ClippedAccountName = AccountName
130161
")
@@ -294,11 +325,41 @@ index 58baa4b..eae7707 100644
294325
'bodyText "LGTM"
295326
'state "APPROVED")))))
296327

328+
(defconst review-with-comments
329+
(a-alist 'author (a-alist 'login "babar")
330+
'state "COMMENTED"
331+
'bodyText ""
332+
'comments (a-alist
333+
'nodes
334+
(list (a-alist 'bodyText "Very interesting change\nwe should move forward"
335+
'originalPosition 2
336+
'outdated nil
337+
'position ""
338+
'path "hledger-lib/Hledger/Reports/MultiBalanceReport.hs")
339+
(a-alist 'bodyText "Change this code"
340+
'originalPosition 4
341+
'outdated nil
342+
'position ""
343+
'path "hledger-lib/Hledger/Reports/MultiBalanceReport.hs")))))
344+
297345
(describe "github-review-format-diff"
298346
(it "can format a simple diff"
299347
(expect (a-equal (github-review-format-diff simple-diff simple-pr)simple-context-expected-review)))
300348
(it "can format a diff with top level comments and review"
301-
(expect (a-equal (github-review-format-diff simple-diff pr-with-tl-comments) expected-review-tl-comment)))))
349+
(expect (a-equal (github-review-format-diff simple-diff pr-with-tl-comments) expected-review-tl-comment))))
350+
351+
(describe "github-review-place-review-comments"
352+
(before-all
353+
(setq github-review-comment-pos ())
354+
(setq github-review-view-comments-in-code-lines nil)
355+
(setq github-review-view-comments-in-code-lines-outdated nil))
356+
(it "can include PR comments made in code lines"
357+
(expect (github-review-place-review-comments example-diff-before-comments-in-code-line review-with-comments)
358+
:to-equal
359+
example-diff-after-comments-in-code-line))
360+
(it "`github-review-comment-pos' should have increased to 3 because we have 2 comments with 3 lines"
361+
(expect github-review-comment-pos :to-equal '(("hledger-lib/Hledger/Reports/MultiBalanceReport.hs" . 3))))))
362+
302363
(describe "entrypoints"
303364
(describe "github-review-start"
304365
:var (github-review-save-diff

0 commit comments

Comments
 (0)