@@ -10,11 +10,11 @@ import (
1010 "net/http"
1111 "strconv"
1212
13+ git_model "code.gitea.io/gitea/models/git"
1314 issues_model "code.gitea.io/gitea/models/issues"
1415 "code.gitea.io/gitea/models/renderhelper"
1516 user_model "code.gitea.io/gitea/models/user"
1617 "code.gitea.io/gitea/modules/git"
17- "code.gitea.io/gitea/modules/gitrepo"
1818 "code.gitea.io/gitea/modules/log"
1919 "code.gitea.io/gitea/modules/markup/markdown"
2020 repo_module "code.gitea.io/gitea/modules/repository"
@@ -91,47 +91,10 @@ func NewComment(ctx *context.Context) {
9191 return
9292 }
9393
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 (err .Error ())
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-
114- 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- // 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
125- // get head commit of PR
126- if pull .Flow == issues_model .PullRequestFlowGithub {
127- prHeadRef := pull .GetGitHeadRefName ()
128- if err := pull .LoadBaseRepo (ctx ); err != nil {
129- ctx .ServerError ("Unable to load base repo" , err )
130- return
131- }
132- prHeadCommitID , err := git .GetFullCommitID (ctx , pull .BaseRepo .RepoPath (), prHeadRef )
133- if err != nil {
134- ctx .ServerError ("Get head commit Id of pr fail" , err )
94+ if issue .IsPull {
95+ pull := issue .PullRequest
96+ if pull .HasMerged {
97+ ctx .JSONError (ctx .Tr ("repo.issues.reopen_not_allowed_merged" ))
13598 return
13699 }
137100
@@ -140,42 +103,88 @@ func NewComment(ctx *context.Context) {
140103 ctx .ServerError ("Unable to load head repo" , err )
141104 return
142105 }
143- if ok := gitrepo .IsBranchExist (ctx , pull .HeadRepo , pull .BaseBranch ); ! ok {
144- // todo localize
145- ctx .JSONError ( "The origin branch is delete, cannot reopen." )
106+ branchExist , err := git_model .IsBranchExist (ctx , pull .HeadRepo . ID , pull .HeadBranch )
107+ if err != nil {
108+ ctx .ServerError ( "IsBranchExist" , err )
146109 return
147110 }
148- headBranchRef := pull .GetGitHeadBranchRefName ()
149- headBranchCommitID , err := git .GetFullCommitID (ctx , pull .HeadRepo .RepoPath (), headBranchRef )
150- if err != nil {
151- ctx .ServerError ("Get head commit Id of head branch fail" , err )
111+ if ! branchExist {
112+ ctx .JSONError (ctx .Tr ("repo.pulls.head_branch_not_exist" ))
152113 return
153114 }
154115
155- err = pull .LoadIssue (ctx )
116+ // check if an opened pull request exists with the same head branch and base branch
117+ pr , err := issues_model .GetUnmergedPullRequest (ctx , pull .HeadRepoID , pull .BaseRepoID , pull .HeadBranch , pull .BaseBranch , pull .Flow )
156118 if err != nil {
157- ctx .ServerError ("load the issue of pull request error" , err )
119+ if ! issues_model .IsErrPullRequestNotExist (err ) {
120+ ctx .JSONError (err .Error ())
121+ return
122+ }
123+ }
124+ if pr != nil {
125+ ctx .Flash .Info (ctx .Tr ("repo.pulls.open_unmerged_pull_exists" , pr .Index ))
158126 return
159127 }
128+ }
160129
161- if prHeadCommitID != headBranchCommitID {
162- // force push to base repo
163- err := git .Push (ctx , pull .HeadRepo .RepoPath (), git.PushOptions {
164- Remote : pull .BaseRepo .RepoPath (),
165- Branch : pull .HeadBranch + ":" + prHeadRef ,
166- Force : true ,
167- Env : repo_module .InternalPushingEnvironment (pull .Issue .Poster , pull .BaseRepo ),
168- })
130+ createdComment , err = issue_service .ReopenIssueWithComment (ctx , issue , ctx .Doer , "" , form .Content , attachments )
131+ if err != nil {
132+ if errors .Is (err , user_model .ErrBlockedUser ) {
133+ ctx .JSONError (ctx .Tr ("repo.issues.comment.blocked_user" ))
134+ } else {
135+ ctx .ServerError ("ReopenIssue" , err )
136+ }
137+ return
138+ }
139+
140+ if issue .IsPull {
141+ pull := issue .PullRequest
142+ // 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
143+ // get head commit of PR
144+ if pull .Flow == issues_model .PullRequestFlowGithub {
145+ prHeadRef := pull .GetGitHeadRefName ()
146+ if err := pull .LoadBaseRepo (ctx ); err != nil {
147+ ctx .ServerError ("Unable to load base repo" , err )
148+ return
149+ }
150+ prHeadCommitID , err := git .GetFullCommitID (ctx , pull .BaseRepo .RepoPath (), prHeadRef )
151+ if err != nil {
152+ ctx .ServerError ("Get head commit Id of pr fail" , err )
153+ return
154+ }
155+
156+ headBranchRef := pull .GetGitHeadBranchRefName ()
157+ headBranchCommitID , err := git .GetFullCommitID (ctx , pull .HeadRepo .RepoPath (), headBranchRef )
169158 if err != nil {
170- ctx .ServerError ("force push error" , err )
159+ ctx .ServerError ("Get head commit Id of head branch fail" , err )
160+ return
161+ }
162+
163+ if err = pull .LoadIssue (ctx ); err != nil {
164+ ctx .ServerError ("load the issue of pull request error" , err )
171165 return
172166 }
167+
168+ // if the head commit ID of the PR is different from the head branch
169+ if prHeadCommitID != headBranchCommitID {
170+ // force push to base repo
171+ err := git .Push (ctx , pull .HeadRepo .RepoPath (), git.PushOptions {
172+ Remote : pull .BaseRepo .RepoPath (),
173+ Branch : pull .HeadBranch + ":" + prHeadRef ,
174+ Force : true ,
175+ Env : repo_module .InternalPushingEnvironment (pull .Issue .Poster , pull .BaseRepo ),
176+ })
177+ if err != nil {
178+ ctx .ServerError ("force push error" , err )
179+ return
180+ }
181+ }
173182 }
174- }
175183
176- // Regenerate patch and test conflict.
177- pull .HeadCommitID = ""
178- pull_service .StartPullRequestCheckImmediately (ctx , pull )
184+ // Regenerate patch and test conflict.
185+ pull .HeadCommitID = ""
186+ pull_service .StartPullRequestCheckImmediately (ctx , pull )
187+ }
179188 case "close" :
180189 if issue .IsClosed {
181190 ctx .JSONError (ctx .Tr ("repo.issues.already_closed" ))
@@ -189,7 +198,7 @@ func NewComment(ctx *context.Context) {
189198 return
190199 }
191200
192- createdComment , err = issue_service .CloseIssue (ctx , issue , ctx .Doer , "" , form .Content , attachments )
201+ createdComment , err = issue_service .CloseIssueWithComment (ctx , issue , ctx .Doer , "" , form .Content , attachments )
193202 default :
194203 if len (form .Content ) == 0 && len (attachments ) == 0 {
195204 ctx .JSONError (ctx .Tr ("repo.issues.comment.empty_content" ))
0 commit comments