Skip to content

Commit 0434311

Browse files
committed
Remove defer function in NewComment
1 parent 385fda5 commit 0434311

File tree

1 file changed

+98
-84
lines changed

1 file changed

+98
-84
lines changed

routers/web/repo/issue_comment.go

Lines changed: 98 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -74,90 +74,6 @@ func NewComment(ctx *context.Context) {
7474
return
7575
}
7676

77-
defer func() {
78-
// Check if issue admin/poster changes the status of issue.
79-
if (ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) || (ctx.IsSigned && issue.IsPoster(ctx.Doer.ID))) &&
80-
(form.Status == "reopen" || form.Status == "close") &&
81-
!(issue.IsPull && issue.PullRequest.HasMerged) {
82-
// Duplication and conflict check should apply to reopen pull request.
83-
var pr *issues_model.PullRequest
84-
85-
if form.Status == "reopen" && issue.IsPull {
86-
pull := issue.PullRequest
87-
var err error
88-
pr, err = issues_model.GetUnmergedPullRequest(ctx, pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch, pull.Flow)
89-
if err != nil {
90-
if !issues_model.IsErrPullRequestNotExist(err) {
91-
ctx.JSONError(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
92-
return
93-
}
94-
}
95-
96-
// Regenerate patch and test conflict.
97-
if pr == nil {
98-
issue.PullRequest.HeadCommitID = ""
99-
pull_service.StartPullRequestCheckImmediately(ctx, issue.PullRequest)
100-
}
101-
102-
// check whether the ref of PR <refs/pulls/pr_index/head> in base repo is consistent with the head commit of head branch in the head repo
103-
// get head commit of PR
104-
if pull.Flow == issues_model.PullRequestFlowGithub {
105-
prHeadRef := pull.GetGitHeadRefName()
106-
if err := pull.LoadBaseRepo(ctx); err != nil {
107-
ctx.ServerError("Unable to load base repo", err)
108-
return
109-
}
110-
prHeadCommitID, err := git.GetFullCommitID(ctx, pull.BaseRepo.RepoPath(), prHeadRef)
111-
if err != nil {
112-
ctx.ServerError("Get head commit Id of pr fail", err)
113-
return
114-
}
115-
116-
// get head commit of branch in the head repo
117-
if err := pull.LoadHeadRepo(ctx); err != nil {
118-
ctx.ServerError("Unable to load head repo", err)
119-
return
120-
}
121-
if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok {
122-
// todo localize
123-
ctx.JSONError("The origin branch is delete, cannot reopen.")
124-
return
125-
}
126-
headBranchRef := pull.GetGitHeadBranchRefName()
127-
headBranchCommitID, err := git.GetFullCommitID(ctx, pull.HeadRepo.RepoPath(), headBranchRef)
128-
if err != nil {
129-
ctx.ServerError("Get head commit Id of head branch fail", err)
130-
return
131-
}
132-
133-
err = pull.LoadIssue(ctx)
134-
if err != nil {
135-
ctx.ServerError("load the issue of pull request error", err)
136-
return
137-
}
138-
139-
if prHeadCommitID != headBranchCommitID {
140-
// force push to base repo
141-
err := git.Push(ctx, pull.HeadRepo.RepoPath(), git.PushOptions{
142-
Remote: pull.BaseRepo.RepoPath(),
143-
Branch: pull.HeadBranch + ":" + prHeadRef,
144-
Force: true,
145-
Env: repo_module.InternalPushingEnvironment(pull.Issue.Poster, pull.BaseRepo),
146-
})
147-
if err != nil {
148-
ctx.ServerError("force push error", err)
149-
return
150-
}
151-
}
152-
}
153-
}
154-
155-
if pr != nil {
156-
ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
157-
}
158-
}
159-
}()
160-
16177
var createdComment *issues_model.Comment
16278
var err error
16379

@@ -168,13 +84,111 @@ func NewComment(ctx *context.Context) {
16884
return
16985
}
17086

87+
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) &&
88+
!issue.IsPoster(ctx.Doer.ID) &&
89+
!ctx.Doer.IsAdmin {
90+
ctx.JSONError(ctx.Tr("repo.issues.reopen_not_allowed"))
91+
return
92+
}
93+
94+
if issue.IsPull && issue.PullRequest.HasMerged {
95+
ctx.JSONError(ctx.Tr("repo.issues.reopen_not_allowed_merged"))
96+
return
97+
}
98+
99+
// check if an opened pull request exists with the same head branch and base branch
100+
pull := issue.PullRequest
101+
var err error
102+
pr, err := issues_model.GetUnmergedPullRequest(ctx, pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch, pull.Flow)
103+
if err != nil {
104+
if !issues_model.IsErrPullRequestNotExist(err) {
105+
ctx.JSONError(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
106+
return
107+
}
108+
}
109+
if pr != nil {
110+
ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
111+
return
112+
}
113+
171114
createdComment, err = issue_service.ReopenIssue(ctx, issue, ctx.Doer, "", form.Content, attachments)
115+
if err != nil {
116+
if errors.Is(err, user_model.ErrBlockedUser) {
117+
ctx.JSONError(ctx.Tr("repo.issues.comment.blocked_user"))
118+
} else {
119+
ctx.ServerError("ReopenIssue", err)
120+
}
121+
return
122+
}
123+
124+
// Regenerate patch and test conflict.
125+
pull.HeadCommitID = ""
126+
pull_service.StartPullRequestCheckImmediately(ctx, pull)
127+
128+
// check whether the ref of PR <refs/pulls/pr_index/head> in base repo is consistent with the head commit of head branch in the head repo
129+
// get head commit of PR
130+
if pull.Flow == issues_model.PullRequestFlowGithub {
131+
prHeadRef := pull.GetGitHeadRefName()
132+
if err := pull.LoadBaseRepo(ctx); err != nil {
133+
ctx.ServerError("Unable to load base repo", err)
134+
return
135+
}
136+
prHeadCommitID, err := git.GetFullCommitID(ctx, pull.BaseRepo.RepoPath(), prHeadRef)
137+
if err != nil {
138+
ctx.ServerError("Get head commit Id of pr fail", err)
139+
return
140+
}
141+
142+
// get head commit of branch in the head repo
143+
if err := pull.LoadHeadRepo(ctx); err != nil {
144+
ctx.ServerError("Unable to load head repo", err)
145+
return
146+
}
147+
if ok := gitrepo.IsBranchExist(ctx, pull.HeadRepo, pull.BaseBranch); !ok {
148+
// todo localize
149+
ctx.JSONError("The origin branch is delete, cannot reopen.")
150+
return
151+
}
152+
headBranchRef := pull.GetGitHeadBranchRefName()
153+
headBranchCommitID, err := git.GetFullCommitID(ctx, pull.HeadRepo.RepoPath(), headBranchRef)
154+
if err != nil {
155+
ctx.ServerError("Get head commit Id of head branch fail", err)
156+
return
157+
}
158+
159+
err = pull.LoadIssue(ctx)
160+
if err != nil {
161+
ctx.ServerError("load the issue of pull request error", err)
162+
return
163+
}
164+
165+
if prHeadCommitID != headBranchCommitID {
166+
// force push to base repo
167+
err := git.Push(ctx, pull.HeadRepo.RepoPath(), git.PushOptions{
168+
Remote: pull.BaseRepo.RepoPath(),
169+
Branch: pull.HeadBranch + ":" + prHeadRef,
170+
Force: true,
171+
Env: repo_module.InternalPushingEnvironment(pull.Issue.Poster, pull.BaseRepo),
172+
})
173+
if err != nil {
174+
ctx.ServerError("force push error", err)
175+
return
176+
}
177+
}
178+
}
172179
case "close":
173180
if issue.IsClosed {
174181
ctx.JSONError(ctx.Tr("repo.issues.already_closed"))
175182
return
176183
}
177184

185+
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) &&
186+
!issue.IsPoster(ctx.Doer.ID) &&
187+
!ctx.Doer.IsAdmin {
188+
ctx.JSONError(ctx.Tr("repo.issues.close_not_allowed"))
189+
return
190+
}
191+
178192
createdComment, err = issue_service.CloseIssue(ctx, issue, ctx.Doer, "", form.Content, attachments)
179193
default:
180194
if len(form.Content) == 0 && len(attachments) == 0 {

0 commit comments

Comments
 (0)