Skip to content

Commit 75193bd

Browse files
committed
feat(diff): Enable commenting on expanded lines in PR diffs
1 parent 22b92e3 commit 75193bd

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

routers/web/repo/compare.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515
"net/url"
1616
"path/filepath"
17+
"sort"
1718
"strings"
1819

1920
"code.gitea.io/gitea/models/db"
@@ -43,6 +44,7 @@ import (
4344
"code.gitea.io/gitea/services/context/upload"
4445
"code.gitea.io/gitea/services/gitdiff"
4546
pull_service "code.gitea.io/gitea/services/pull"
47+
user_service "code.gitea.io/gitea/services/user"
4648
)
4749

4850
const (
@@ -947,6 +949,41 @@ func ExcerptBlob(ctx *context.Context) {
947949
section.Lines = append(section.Lines, lineSection)
948950
}
949951
}
952+
if ctx.Doer != nil {
953+
ctx.Data["SignedUserID"] = ctx.Doer.ID
954+
}
955+
isPull := ctx.FormBool("is_pull")
956+
ctx.Data["PageIsPullFiles"] = isPull
957+
958+
if isPull {
959+
issueIndex := ctx.FormInt64("issue_index")
960+
ctx.Data["IssueIndex"] = issueIndex
961+
if issueIndex > 0 {
962+
if issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex); err == nil && issue.IsPull {
963+
ctx.Data["Issue"] = issue
964+
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
965+
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
966+
}
967+
968+
if allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, ctx.FormBool("show_outdated")); err == nil {
969+
if lineComments, ok := allComments[filePath]; ok {
970+
for _, line := range section.Lines {
971+
if comments, ok := lineComments[int64(line.LeftIdx*-1)]; ok {
972+
line.Comments = append(line.Comments, comments...)
973+
}
974+
if comments, ok := lineComments[int64(line.RightIdx)]; ok {
975+
line.Comments = append(line.Comments, comments...)
976+
}
977+
sort.SliceStable(line.Comments, func(i, j int) bool {
978+
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
979+
})
980+
}
981+
}
982+
}
983+
}
984+
}
985+
}
986+
950987
ctx.Data["section"] = section
951988
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
952989
ctx.Data["AfterCommitID"] = commitID

templates/repo/diff/blob_excerpt.tmpl

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) (QueryBuild "?" "anchor" $.Anchor)}}
1+
{{$blobExcerptLink := print $.RepoLink (Iif $.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.AfterCommitID) "?" (Iif $.PageIsPullFiles (print "is_pull=true&issue_index=" $.IssueIndex "&") "")}}
22
{{if $.IsSplitStyle}}
33
{{range $k, $line := $.section.Lines}}
4-
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded">
4+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded" data-line-type="{{.GetHTMLDiffLineType}}">
55
{{if eq .GetType 4}}
66
{{$expandDirection := $line.GetExpandDirection}}
77
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}">
@@ -33,6 +33,11 @@
3333
<td class="lines-escape lines-escape-old">{{if and $line.LeftIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
3434
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
3535
<td class="lines-code lines-code-old">
36+
{{- if and $.SignedUserID $.PageIsPullFiles $line.LeftIdx -}}
37+
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-left{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="left" data-idx="{{$line.LeftIdx}}">
38+
{{- svg "octicon-plus" -}}
39+
</button>
40+
{{- end -}}
3641
{{- if $line.LeftIdx -}}
3742
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
3843
{{- else -}}
@@ -43,6 +48,11 @@
4348
<td class="lines-escape lines-escape-new">{{if and $line.RightIdx $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
4449
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="tw-font-mono" data-type-marker=""></span>{{end}}</td>
4550
<td class="lines-code lines-code-new">
51+
{{- if and $.SignedUserID $.PageIsPullFiles $line.RightIdx -}}
52+
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="right" data-idx="{{$line.RightIdx}}">
53+
{{- svg "octicon-plus" -}}
54+
</button>
55+
{{- end -}}
4656
{{- if $line.RightIdx -}}
4757
{{- template "repo/diff/section_code" dict "diff" $inlineDiff -}}
4858
{{- else -}}
@@ -51,10 +61,24 @@
5161
</td>
5262
{{end}}
5363
</tr>
64+
{{if $line.Comments}}
65+
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
66+
<td class="add-comment-left" colspan="4">
67+
{{if eq $line.GetCommentSide "previous"}}
68+
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
69+
{{end}}
70+
</td>
71+
<td class="add-comment-right" colspan="4">
72+
{{if eq $line.GetCommentSide "proposed"}}
73+
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
74+
{{end}}
75+
</td>
76+
</tr>
77+
{{end}}
5478
{{end}}
5579
{{else}}
5680
{{range $k, $line := $.section.Lines}}
57-
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded">
81+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}} line-expanded" data-line-type="{{.GetHTMLDiffLineType}}">
5882
{{if eq .GetType 4}}
5983
{{$expandDirection := $line.GetExpandDirection}}
6084
<td colspan="2" class="lines-num">
@@ -83,7 +107,21 @@
83107
{{$inlineDiff := $.section.GetComputedInlineDiffFor $line ctx.Locale}}
84108
<td class="lines-escape">{{if $inlineDiff.EscapeStatus.Escaped}}<button class="toggle-escape-button btn interact-bg" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"></button>{{end}}</td>
85109
<td class="lines-type-marker"><span class="tw-font-mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span></td>
86-
<td class="lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}"><code {{if $inlineDiff.EscapeStatus.Escaped}}class="code-inner has-escaped" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"{{else}}class="code-inner"{{end}}>{{$inlineDiff.Content}}</code></td>
110+
<td class="lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">
111+
{{- if and $.SignedUserID $.PageIsPullFiles -}}
112+
<button type="button" aria-label="{{ctx.Locale.Tr "repo.diff.comment.add_line_comment"}}" class="ui primary button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}{{if (not $line.CanComment)}} tw-invisible{{end}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}">
113+
{{- svg "octicon-plus" -}}
114+
</button>
115+
{{- end -}}
116+
<code {{if $inlineDiff.EscapeStatus.Escaped}}class="code-inner has-escaped" title="{{template "repo/diff/escape_title" dict "diff" $inlineDiff}}"{{else}}class="code-inner"{{end}}>{{$inlineDiff.Content}}</code>
117+
</td>
87118
</tr>
119+
{{if $line.Comments}}
120+
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
121+
<td class="add-comment-left add-comment-right" colspan="5">
122+
{{template "repo/diff/conversation" dict "." $ "comments" $line.Comments}}
123+
</td>
124+
</tr>
125+
{{end}}
88126
{{end}}
89127
{{end}}

templates/repo/diff/section_split.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{$file := .file}}
2-
{{$blobExcerptLink := print (or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink) (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.root.AfterCommitID) "?"}}
2+
{{$blobExcerptLink := print (or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink) (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $.root.AfterCommitID) "?" (Iif $.root.PageIsPullFiles (print "is_pull=true&issue_index=" $.root.Issue.Index "&") "")}}
33
<colgroup>
44
<col width="50">
55
<col width="10">

templates/repo/diff/section_unified.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{$file := .file}}
22
{{$repoLink := or ctx.RootData.CommitRepoLink ctx.RootData.RepoLink}}
33
{{$afterCommitID := or $.root.AfterCommitID "no-after-commit-id"}}{{/* this tmpl is also used by the PR Conversation page, so the "AfterCommitID" may not exist */}}
4-
{{$blobExcerptLink := print $repoLink (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $afterCommitID) "?"}}
4+
{{$blobExcerptLink := print $repoLink (Iif $.root.PageIsWiki "/wiki" "") "/blob_excerpt/" (PathEscape $afterCommitID) "?" (Iif $.root.PageIsPullFiles (print "is_pull=true&issue_index=" $.root.Issue.Index "&") "")}}
55
<colgroup>
66
<col width="50">
77
<col width="50">

0 commit comments

Comments
 (0)