Skip to content

Commit 4f36153

Browse files
authored
Merge branch 'master' into fix_14250
2 parents 90a0cd9 + 7369282 commit 4f36153

File tree

19 files changed

+235
-105
lines changed

19 files changed

+235
-105
lines changed

MAINTAINERS

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Mura Li <typeless@ctli.io> (@typeless)
3636
6543 <6543@obermui.de> (@6543)
3737
jaqra <jaqra@hotmail.com> (@jaqra)
3838
David Svantesson <davidsvantesson@gmail.com> (@davidsvantesson)
39-
CirnoT <gitea.m@i32.pl> (@CirnoT)
4039
a1012112796 <1012112796@qq.com> (@a1012112796)
4140
Karl Heinz Marbaise <kama@soebes.de> (@khmarbaise)
4241
Norwin Roosen <git@nroo.de> (@noerw)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.14
55
require (
66
code.gitea.io/gitea-vet v0.2.1
77
code.gitea.io/sdk/gitea v0.13.1
8-
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27
8+
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee
99
gitea.com/lunny/levelqueue v0.3.0
1010
gitea.com/macaron/binding v0.0.0-20190822013154-a5f53841ed2b
1111
gitea.com/macaron/cache v0.0.0-20200924044943-905232fba10b

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFj
4040
code.gitea.io/sdk/gitea v0.13.1 h1:Y7bpH2iO6Q0KhhMJfjP/LZ0AmiYITeRQlCD8b0oYqhk=
4141
code.gitea.io/sdk/gitea v0.13.1/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY=
4242
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
43-
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27 h1:cdb1OTNXGLwQ55gg+9tIPWufdsnrHWcIq8Qs+j/E8JU=
44-
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27/go.mod h1:Ozg8IchVNb/Udg+ui39iHRYqVHSvf3C99ixdpLR8Vu0=
43+
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee h1:9U6HuKUBt/cGK6T/64dEuz0r7Yp97WAAEJvXHDlY3ws=
44+
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee/go.mod h1:Ozg8IchVNb/Udg+ui39iHRYqVHSvf3C99ixdpLR8Vu0=
4545
gitea.com/lunny/levelqueue v0.3.0 h1:MHn1GuSZkxvVEDMyAPqlc7A3cOW+q8RcGhRgH/xtm6I=
4646
gitea.com/lunny/levelqueue v0.3.0/go.mod h1:HBqmLbz56JWpfEGG0prskAV97ATNRoj5LDmPicD22hU=
4747
gitea.com/lunny/log v0.0.0-20190322053110-01b5df579c4e h1:r1en/D7xJmcY24VkHkjkcJFa+7ZWubVWPBrvsHkmHxk=

models/issue_comment.go

+45-20
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
979979
if opts.Type != CommentTypeUnknown {
980980
cond = cond.And(builder.Eq{"comment.type": opts.Type})
981981
}
982-
if opts.Line > 0 {
982+
if opts.Line != 0 {
983983
cond = cond.And(builder.Eq{"comment.line": opts.Line})
984984
}
985985
if len(opts.TreePath) > 0 {
@@ -1078,18 +1078,35 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
10781078
if review == nil {
10791079
review = &Review{ID: 0}
10801080
}
1081-
//Find comments
10821081
opts := FindCommentsOptions{
10831082
Type: CommentTypeCode,
10841083
IssueID: issue.ID,
10851084
ReviewID: review.ID,
10861085
}
1086+
1087+
comments, err := findCodeComments(e, opts, issue, currentUser, review)
1088+
if err != nil {
1089+
return nil, err
1090+
}
1091+
1092+
for _, comment := range comments {
1093+
if pathToLineToComment[comment.TreePath] == nil {
1094+
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
1095+
}
1096+
pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
1097+
}
1098+
return pathToLineToComment, nil
1099+
}
1100+
1101+
func findCodeComments(e Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
1102+
var comments []*Comment
1103+
if review == nil {
1104+
review = &Review{ID: 0}
1105+
}
10871106
conds := opts.toConds()
10881107
if review.ID == 0 {
10891108
conds = conds.And(builder.Eq{"invalidated": false})
10901109
}
1091-
1092-
var comments []*Comment
10931110
if err := e.Where(conds).
10941111
Asc("comment.created_unix").
10951112
Asc("comment.id").
@@ -1117,7 +1134,19 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11171134
return nil, err
11181135
}
11191136

1137+
n := 0
11201138
for _, comment := range comments {
1139+
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1140+
// If the review is pending only the author can see the comments (except if the review is set)
1141+
if review.ID == 0 && re.Type == ReviewTypePending &&
1142+
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1143+
continue
1144+
}
1145+
comment.Review = re
1146+
}
1147+
comments[n] = comment
1148+
n++
1149+
11211150
if err := comment.LoadResolveDoer(); err != nil {
11221151
return nil, err
11231152
}
@@ -1126,25 +1155,21 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11261155
return nil, err
11271156
}
11281157

1129-
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1130-
// If the review is pending only the author can see the comments (except the review is set)
1131-
if review.ID == 0 {
1132-
if re.Type == ReviewTypePending &&
1133-
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1134-
continue
1135-
}
1136-
}
1137-
comment.Review = re
1138-
}
1139-
11401158
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
11411159
issue.Repo.ComposeMetas()))
1142-
if pathToLineToComment[comment.TreePath] == nil {
1143-
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
1144-
}
1145-
pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
11461160
}
1147-
return pathToLineToComment, nil
1161+
return comments[:n], nil
1162+
}
1163+
1164+
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
1165+
func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, line int64) ([]*Comment, error) {
1166+
opts := FindCommentsOptions{
1167+
Type: CommentTypeCode,
1168+
IssueID: issue.ID,
1169+
TreePath: treePath,
1170+
Line: line,
1171+
}
1172+
return findCodeComments(x, opts, issue, currentUser, nil)
11481173
}
11491174

11501175
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line

modules/auth/repo_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ func (f *MergePullRequestForm) Validate(ctx *macaron.Context, errs binding.Error
548548

549549
// CodeCommentForm form for adding code comments for PRs
550550
type CodeCommentForm struct {
551+
Origin string `binding:"Required;In(timeline,diff)"`
551552
Content string `binding:"Required"`
552553
Side string `binding:"Required;In(previous,proposed)"`
553554
Line int64

routers/repo/pull_review.go

+62
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,40 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/auth"
12+
"code.gitea.io/gitea/modules/base"
1213
"code.gitea.io/gitea/modules/context"
1314
"code.gitea.io/gitea/modules/log"
1415
pull_service "code.gitea.io/gitea/services/pull"
1516
)
1617

18+
const (
19+
tplConversation base.TplName = "repo/diff/conversation"
20+
tplNewComment base.TplName = "repo/diff/new_comment"
21+
)
22+
23+
// RenderNewCodeCommentForm will render the form for creating a new review comment
24+
func RenderNewCodeCommentForm(ctx *context.Context) {
25+
issue := GetActionIssue(ctx)
26+
if !issue.IsPull {
27+
return
28+
}
29+
currentReview, err := models.GetCurrentReview(ctx.User, issue)
30+
if err != nil && !models.IsErrReviewNotExist(err) {
31+
ctx.ServerError("GetCurrentReview", err)
32+
return
33+
}
34+
ctx.Data["PageIsPullFiles"] = true
35+
ctx.Data["Issue"] = issue
36+
ctx.Data["CurrentReview"] = currentReview
37+
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(issue.PullRequest.GetGitRefName())
38+
if err != nil {
39+
ctx.ServerError("GetRefCommitID", err)
40+
return
41+
}
42+
ctx.Data["AfterCommitID"] = pullHeadCommitID
43+
ctx.HTML(200, tplNewComment)
44+
}
45+
1746
// CreateCodeComment will create a code comment including an pending review if required
1847
func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
1948
issue := GetActionIssue(ctx)
@@ -58,11 +87,17 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
5887
}
5988

6089
log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID)
90+
91+
if form.Origin == "diff" {
92+
renderConversation(ctx, comment)
93+
return
94+
}
6195
ctx.Redirect(comment.HTMLURL())
6296
}
6397

6498
// UpdateResolveConversation add or remove an Conversation resolved mark
6599
func UpdateResolveConversation(ctx *context.Context) {
100+
origin := ctx.Query("origin")
66101
action := ctx.Query("action")
67102
commentID := ctx.QueryInt64("comment_id")
68103

@@ -103,11 +138,38 @@ func UpdateResolveConversation(ctx *context.Context) {
103138
return
104139
}
105140

141+
if origin == "diff" {
142+
renderConversation(ctx, comment)
143+
return
144+
}
106145
ctx.JSON(200, map[string]interface{}{
107146
"ok": true,
108147
})
109148
}
110149

150+
func renderConversation(ctx *context.Context, comment *models.Comment) {
151+
comments, err := models.FetchCodeCommentsByLine(comment.Issue, ctx.User, comment.TreePath, comment.Line)
152+
if err != nil {
153+
ctx.ServerError("FetchCodeCommentsByLine", err)
154+
return
155+
}
156+
ctx.Data["PageIsPullFiles"] = true
157+
ctx.Data["comments"] = comments
158+
ctx.Data["CanMarkConversation"] = true
159+
ctx.Data["Issue"] = comment.Issue
160+
if err = comment.Issue.LoadPullRequest(); err != nil {
161+
ctx.ServerError("comment.Issue.LoadPullRequest", err)
162+
return
163+
}
164+
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(comment.Issue.PullRequest.GetGitRefName())
165+
if err != nil {
166+
ctx.ServerError("GetRefCommitID", err)
167+
return
168+
}
169+
ctx.Data["AfterCommitID"] = pullHeadCommitID
170+
ctx.HTML(200, tplConversation)
171+
}
172+
111173
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
112174
func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
113175
issue := GetActionIssue(ctx)

routers/routes/chi.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
176176
}
177177
}
178178

179+
var (
180+
sessionManager *session.Manager
181+
)
182+
179183
// NewChi creates a chi Router
180184
func NewChi() chi.Router {
181185
c := chi.NewRouter()
@@ -185,7 +189,8 @@ func NewChi() chi.Router {
185189
c.Use(LoggerHandler(setting.RouterLogLevel))
186190
}
187191
}
188-
c.Use(session.Sessioner(session.Options{
192+
193+
var opt = session.Options{
189194
Provider: setting.SessionConfig.Provider,
190195
ProviderConfig: setting.SessionConfig.ProviderConfig,
191196
CookieName: setting.SessionConfig.CookieName,
@@ -194,7 +199,14 @@ func NewChi() chi.Router {
194199
Maxlifetime: setting.SessionConfig.Maxlifetime,
195200
Secure: setting.SessionConfig.Secure,
196201
Domain: setting.SessionConfig.Domain,
197-
}))
202+
}
203+
opt = session.PrepareOptions([]session.Options{opt})
204+
205+
var err error
206+
sessionManager, err = session.NewManager(opt.Provider, opt)
207+
if err != nil {
208+
panic(err)
209+
}
198210

199211
c.Use(Recovery())
200212
if setting.EnableAccessLog {

routers/routes/macaron.go

+1
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
856856
m.Group("/files", func() {
857857
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
858858
m.Group("/reviews", func() {
859+
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
859860
m.Post("/comments", bindIgnErr(auth.CodeCommentForm{}), repo.CreateCodeComment)
860861
m.Post("/submit", bindIgnErr(auth.SubmitReviewForm{}), repo.SubmitReview)
861862
}, context.RepoMustNotBeArchived())

routers/routes/recovery.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/templates"
1616

17-
"gitea.com/go-chi/session"
1817
"github.com/unrolled/render"
1918
)
2019

@@ -64,7 +63,13 @@ func Recovery() func(next http.Handler) http.Handler {
6463
log.Error("%v", combinedErr)
6564

6665
lc := middlewares.Locale(w, req)
67-
sess := session.GetSession(req)
66+
67+
// TODO: this should be replaced by real session after macaron removed totally
68+
sessionStore, err := sessionManager.Start(w, req)
69+
if err != nil {
70+
// Just invoke the above recover catch
71+
panic("session(start): " + err.Error())
72+
}
6873

6974
var store = dataStore{
7075
Data: templates.Vars{
@@ -75,7 +80,7 @@ func Recovery() func(next http.Handler) http.Handler {
7580
}
7681

7782
// Get user from session if logged in.
78-
user, _ := sso.SignedInUser(req, w, &store, sess)
83+
user, _ := sso.SignedInUser(req, w, &store, sessionStore)
7984
if user != nil {
8085
store.Data["IsSigned"] = true
8186
store.Data["SignedUser"] = user
@@ -92,7 +97,7 @@ func Recovery() func(next http.Handler) http.Handler {
9297
if setting.RunMode != "prod" {
9398
store.Data["ErrMsg"] = combinedErr
9499
}
95-
err := rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
100+
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
96101
if err != nil {
97102
log.Error("%v", err)
98103
}

templates/repo/diff/box.tmpl

+17-20
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,25 @@
144144
{{end}}
145145

146146
{{if not $.Repository.IsArchived}}
147-
<div id="pull_review_add_comment" class="hide">
148-
{{template "repo/diff/new_comment" dict "root" .}}
147+
<div class="hide" id="edit-content-form">
148+
<div class="ui comment form">
149+
<div class="ui top attached tabular menu">
150+
<a class="active write item">{{$.i18n.Tr "write"}}</a>
151+
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
149152
</div>
150-
<div class="hide" id="edit-content-form">
151-
<div class="ui comment form">
152-
<div class="ui top attached tabular menu">
153-
<a class="active write item">{{$.i18n.Tr "write"}}</a>
154-
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
155-
</div>
156-
<div class="ui bottom attached active write tab segment">
157-
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
158-
</div>
159-
<div class="ui bottom attached tab preview segment markdown">
160-
{{$.i18n.Tr "loading"}}
161-
</div>
162-
<div class="text right edit buttons">
163-
<div class="ui basic blue cancel button" tabindex="3">{{.i18n.Tr "repo.issues.cancel"}}</div>
164-
<div class="ui green save button" tabindex="2">{{.i18n.Tr "repo.issues.save"}}</div>
165-
</div>
166-
</div>
153+
<div class="ui bottom attached active write tab segment">
154+
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
155+
</div>
156+
<div class="ui bottom attached tab preview segment markdown">
157+
{{$.i18n.Tr "loading"}}
158+
</div>
159+
<div class="text right edit buttons">
160+
<div class="ui basic blue cancel button" tabindex="3">{{.i18n.Tr "repo.issues.cancel"}}</div>
161+
<div class="ui green save button" tabindex="2">{{.i18n.Tr "repo.issues.save"}}</div>
167162
</div>
168-
{{end}}
163+
</div>
164+
</div>
165+
{{end}}
169166

170167
{{if .IsSplitStyle}}
171168
<script>

templates/repo/diff/comment_form.tmpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{end}}
77
<form class="ui form {{if $.hidden}}hide comment-form comment-form-reply{{end}}" action="{{$.root.Issue.HTMLURL}}/files/reviews/comments" method="post">
88
{{$.root.CsrfTokenHtml}}
9+
<input type="hidden" name="origin" value="{{if $.root.PageIsPullFiles}}diff{{else}}timeline{{end}}">
910
<input type="hidden" name="latest_commit_id" value="{{$.root.AfterCommitID}}"/>
1011
<input type="hidden" name="side" value="{{if $.Side}}{{$.Side}}{{end}}">
1112
<input type="hidden" name="line" value="{{if $.Line}}{{$.Line}}{{end}}">
@@ -29,7 +30,7 @@
2930
<span class="markdown-info">{{svg "octicon-markdown"}} {{$.root.i18n.Tr "repo.diff.comment.markdown_info"}}</span>
3031
<div class="ui right">
3132
{{if $.reply}}
32-
<button class="ui submit green tiny button btn-reply" onclick="window.submitReply(this);">{{$.root.i18n.Tr "repo.diff.comment.reply"}}</button>
33+
<button class="ui submit green tiny button btn-reply" type="submit">{{$.root.i18n.Tr "repo.diff.comment.reply"}}</button>
3334
<input type="hidden" name="reply" value="{{$.reply}}">
3435
{{else}}
3536
{{if $.root.CurrentReview}}

0 commit comments

Comments
 (0)