@@ -733,7 +733,7 @@ func CreateIssue(ctx *context.APIContext) {
733733 }
734734
735735 if form .Closed {
736- if err := issue_service .ChangeStatus (ctx , issue , ctx .Doer , "" , true ); err != nil {
736+ if err := issue_service .CloseIssue (ctx , issue , ctx .Doer , "" ); err != nil {
737737 if issues_model .IsErrDependenciesLeft (err ) {
738738 ctx .Error (http .StatusPreconditionFailed , "DependenciesLeft" , "cannot close this issue because it still has open dependencies" )
739739 return
@@ -912,27 +912,11 @@ func EditIssue(ctx *context.APIContext) {
912912 }
913913 }
914914
915- var isClosed bool
916- switch state := api .StateType (* form .State ); state {
917- case api .StateOpen :
918- isClosed = false
919- case api .StateClosed :
920- isClosed = true
921- default :
922- ctx .Error (http .StatusPreconditionFailed , "UnknownIssueStateError" , fmt .Sprintf ("unknown state: %s" , state ))
915+ state := api .StateType (* form .State )
916+ closeOrReopenIssue (ctx , issue , state )
917+ if ctx .Written () {
923918 return
924919 }
925-
926- if issue .IsClosed != isClosed {
927- if err := issue_service .ChangeStatus (ctx , issue , ctx .Doer , "" , isClosed ); err != nil {
928- if issues_model .IsErrDependenciesLeft (err ) {
929- ctx .Error (http .StatusPreconditionFailed , "DependenciesLeft" , "cannot close this issue because it still has open dependencies" )
930- return
931- }
932- ctx .Error (http .StatusInternalServerError , "ChangeStatus" , err )
933- return
934- }
935- }
936920 }
937921
938922 // Refetch from database to assign some automatic values
@@ -1055,3 +1039,26 @@ func UpdateIssueDeadline(ctx *context.APIContext) {
10551039
10561040 ctx .JSON (http .StatusCreated , api.IssueDeadline {Deadline : deadlineUnix .AsTimePtr ()})
10571041}
1042+
1043+ func closeOrReopenIssue (ctx * context.APIContext , issue * issues_model.Issue , state api.StateType ) {
1044+ if state != api .StateOpen && state != api .StateClosed {
1045+ ctx .Error (http .StatusPreconditionFailed , "UnknownIssueStateError" , fmt .Sprintf ("unknown state: %s" , state ))
1046+ return
1047+ }
1048+
1049+ if state == api .StateClosed && ! issue .IsClosed {
1050+ if err := issue_service .CloseIssue (ctx , issue , ctx .Doer , "" ); err != nil {
1051+ if issues_model .IsErrDependenciesLeft (err ) {
1052+ ctx .Error (http .StatusPreconditionFailed , "DependenciesLeft" , "cannot close this issue or pull request because it still has open dependencies" )
1053+ return
1054+ }
1055+ ctx .Error (http .StatusInternalServerError , "CloseIssue" , err )
1056+ return
1057+ }
1058+ } else if state == api .StateOpen && issue .IsClosed {
1059+ if err := issue_service .ReopenIssue (ctx , issue , ctx .Doer , "" ); err != nil {
1060+ ctx .Error (http .StatusInternalServerError , "ReopenIssue" , err )
1061+ return
1062+ }
1063+ }
1064+ }
0 commit comments