Skip to content

Commit 1f96c0e

Browse files
committed
handle review comments
1 parent 9cf249b commit 1f96c0e

File tree

3 files changed

+70
-93
lines changed

3 files changed

+70
-93
lines changed

routers/repo/pull_review.go

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,10 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
8989
log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID)
9090

9191
if form.Origin == "diff" {
92-
comments, err := models.FetchCodeCommentsByLine(issue, ctx.User, comment.TreePath, comment.Line)
93-
if err != nil {
94-
ctx.ServerError("FetchCodeCommentsByLine", err)
95-
return
96-
}
97-
ctx.Data["PageIsPullFiles"] = true
98-
ctx.Data["comments"] = comments
99-
ctx.Data["CanMarkConversation"] = true
100-
ctx.Data["Issue"] = issue
101-
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(issue.PullRequest.GetGitRefName())
102-
if err != nil {
103-
ctx.ServerError("GetRefCommitID", err)
104-
return
105-
}
106-
ctx.Data["AfterCommitID"] = pullHeadCommitID
107-
ctx.HTML(200, tplConversation)
108-
} else {
109-
ctx.Redirect(comment.HTMLURL())
92+
renderConversation(ctx, comment)
93+
return
11094
}
95+
ctx.Redirect(comment.HTMLURL())
11196
}
11297

11398
// UpdateResolveConversation add or remove an Conversation resolved mark
@@ -154,31 +139,35 @@ func UpdateResolveConversation(ctx *context.Context) {
154139
}
155140

156141
if origin == "diff" {
157-
comments, err := models.FetchCodeCommentsByLine(comment.Issue, ctx.User, comment.TreePath, comment.Line)
158-
if err != nil {
159-
ctx.ServerError("FetchCodeCommentsByLine", err)
160-
return
161-
}
162-
ctx.Data["PageIsPullFiles"] = true
163-
ctx.Data["comments"] = comments
164-
ctx.Data["CanMarkConversation"] = true
165-
ctx.Data["Issue"] = comment.Issue
166-
if err = comment.Issue.LoadPullRequest(); err != nil {
167-
ctx.ServerError("comment.Issue.LoadPullReqiest", err)
168-
return
169-
}
170-
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(comment.Issue.PullRequest.GetGitRefName())
171-
if err != nil {
172-
ctx.ServerError("GetRefCommitID", err)
173-
return
174-
}
175-
ctx.Data["AfterCommitID"] = pullHeadCommitID
176-
ctx.HTML(200, tplConversation)
177-
} else {
178-
ctx.JSON(200, map[string]interface{}{
179-
"ok": true,
180-
})
142+
renderConversation(ctx, comment)
143+
return
144+
}
145+
ctx.JSON(200, map[string]interface{}{
146+
"ok": true,
147+
})
148+
}
149+
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
181155
}
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)
182171
}
183172

184173
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist

routers/routes/macaron.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
854854
m.Group("/files", func() {
855855
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
856856
m.Group("/reviews", func() {
857-
m.Post("/new_comment", repo.RenderNewCodeCommentForm)
857+
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
858858
m.Post("/comments", bindIgnErr(auth.CodeCommentForm{}), repo.CreateCodeComment)
859859
m.Post("/submit", bindIgnErr(auth.SubmitReviewForm{}), repo.SubmitReview)
860860
}, context.RepoMustNotBeArchived())

web_src/js/index.js

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ function initPullRequestReview() {
12811281
$(this).closest('.menu').toggle('visible');
12821282
});
12831283

1284-
$('a.add-code-comment').on('click', function (e) {
1284+
$('a.add-code-comment').on('click', async function (e) {
12851285
if ($(e.target).hasClass('btn-add-single')) return; // https://github.com/go-gitea/gitea/issues/4745
12861286
e.preventDefault();
12871287

@@ -1314,22 +1314,18 @@ function initPullRequestReview() {
13141314
const td = ntr.find(`.add-comment-${side}`);
13151315
let commentCloud = td.find('.comment-code-cloud');
13161316
if (commentCloud.length === 0 && !ntr.find('button[name="is_review"]').length) {
1317-
const url = $(this).data('new-comment-url');
1318-
$.post(url, {
1319-
_csrf: csrf
1320-
}, (data) => {
1321-
td.html(data);
1322-
commentCloud = td.find('.comment-code-cloud');
1323-
assingMenuAttributes(commentCloud.find('.menu'));
1324-
td.find("input[name='line']").val(idx);
1325-
td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
1326-
td.find("input[name='path']").val(path);
1327-
const $textarea = commentCloud.find('textarea');
1328-
attachTribute($textarea.get(), {mentions: true, emoji: true});
1329-
const $simplemde = setCommentSimpleMDE($textarea);
1330-
$textarea.focus();
1331-
$simplemde.codemirror.focus();
1332-
});
1317+
const data = await $.get($(this).data('new-comment-url'));
1318+
td.html(data);
1319+
commentCloud = td.find('.comment-code-cloud');
1320+
assingMenuAttributes(commentCloud.find('.menu'));
1321+
td.find("input[name='line']").val(idx);
1322+
td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
1323+
td.find("input[name='path']").val(path);
1324+
const $textarea = commentCloud.find('textarea');
1325+
attachTribute($textarea.get(), {mentions: true, emoji: true});
1326+
const $simplemde = setCommentSimpleMDE($textarea);
1327+
$textarea.focus();
1328+
$simplemde.codemirror.focus();
13331329
}
13341330
});
13351331
}
@@ -2491,28 +2487,24 @@ $(document).ready(async () => {
24912487
$(e).trigger('click');
24922488
});
24932489

2494-
$(document).on('click', '.resolve-conversation', function (e) {
2490+
$(document).on('click', '.resolve-conversation', async function (e) {
24952491
e.preventDefault();
2496-
const id = $(this).data('comment-id');
2492+
const comment_id = $(this).data('comment-id');
24972493
const origin = $(this).data('origin');
24982494
const action = $(this).data('action');
24992495
const url = $(this).data('update-url');
25002496

2501-
$.post(url, {
2502-
_csrf: csrf,
2503-
origin,
2504-
action,
2505-
comment_id: id,
2506-
}, (data) => {
2497+
const data = await $.post(url, {_csrf: csrf, origin, action, comment_id});
2498+
2499+
if ($(this).closest('.conversation-holder').length) {
25072500
const conversation = $(data);
2508-
if ($(this).closest('.conversation-holder').replaceWith(conversation)) {
2509-
conversation.find('.dropdown').dropdown();
2510-
initReactionSelector(conversation);
2511-
initClipboard();
2512-
} else {
2513-
reload();
2514-
}
2515-
});
2501+
$(this).closest('.conversation-holder').replaceWith(conversation);
2502+
conversation.find('.dropdown').dropdown();
2503+
initReactionSelector(conversation);
2504+
initClipboard();
2505+
} else {
2506+
reload();
2507+
}
25162508
});
25172509

25182510
buttonsClickOnEnter();
@@ -3631,25 +3623,21 @@ $(document).on('click', 'button[name="is_review"]', (e) => {
36313623
$(e.target).closest('form').append('<input type="hidden" name="is_review" value="true">');
36323624
});
36333625

3634-
$(document).on('submit', '.conversation-holder form', (e) => {
3626+
$(document).on('submit', '.conversation-holder form', async (e) => {
36353627
e.preventDefault();
36363628
const form = $(e.target);
3637-
$.post(form.attr('action'), form.serialize(), (data) => {
3638-
const conversation = $(data);
3639-
const path = conversation.data('path');
3640-
const side = conversation.data('side');
3641-
const idx = conversation.data('idx');
3642-
const lineType = form.closest('tr').data('line-type');
3643-
form.closest('.conversation-holder').replaceWith(conversation);
3644-
if (lineType === 'same') {
3645-
$(`a.add-code-comment[data-path="${path}"][data-idx="${idx}"]`).addClass('invisible');
3646-
} else {
3647-
$(`a.add-code-comment[data-path="${path}"][data-side="${side}"][data-idx="${idx}"]`).addClass('invisible');
3648-
}
3649-
conversation.find('.dropdown').dropdown();
3650-
initReactionSelector(conversation);
3651-
initClipboard();
3652-
});
3629+
const newConversationHolder = $(await $.post(form.attr('action'), form.serialize()));
3630+
const {path, side, idx} = newConversationHolder.data();
3631+
3632+
form.closest('.conversation-holder').replaceWith(newConversationHolder);
3633+
if (form.closest('tr').data('line-type') === 'same') {
3634+
$(`a.add-code-comment[data-path="${path}"][data-idx="${idx}"]`).addClass('invisible');
3635+
} else {
3636+
$(`a.add-code-comment[data-path="${path}"][data-side="${side}"][data-idx="${idx}"]`).addClass('invisible');
3637+
}
3638+
newConversationHolder.find('.dropdown').dropdown();
3639+
initReactionSelector(newConversationHolder);
3640+
initClipboard();
36533641
});
36543642

36553643
window.cancelCodeComment = function (btn) {

0 commit comments

Comments
 (0)