Skip to content

Commit b5500cd

Browse files
GiteaBotyp05327
andauthored
Fix 500 error when state params is set when editing issue/PR by API (#31880) (#31952)
Backport #31880 by @yp05327 A quick fix for #31871 Co-authored-by: yp05327 <[email protected]>
1 parent 0de69c2 commit b5500cd

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

routers/api/v1/repo/issue.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,27 @@ func EditIssue(ctx *context.APIContext) {
886886
return
887887
}
888888
}
889-
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", api.StateClosed == api.StateType(*form.State)); err != nil {
890-
if issues_model.IsErrDependenciesLeft(err) {
891-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
889+
890+
var isClosed bool
891+
switch state := api.StateType(*form.State); state {
892+
case api.StateOpen:
893+
isClosed = false
894+
case api.StateClosed:
895+
isClosed = true
896+
default:
897+
ctx.Error(http.StatusPreconditionFailed, "UnknownIssueStateError", fmt.Sprintf("unknown state: %s", state))
898+
return
899+
}
900+
901+
if issue.IsClosed != isClosed {
902+
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", isClosed); err != nil {
903+
if issues_model.IsErrDependenciesLeft(err) {
904+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
905+
return
906+
}
907+
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
892908
return
893909
}
894-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
895-
return
896910
}
897911
}
898912

routers/api/v1/repo/pull.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,27 @@ func EditPullRequest(ctx *context.APIContext) {
695695
ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged")
696696
return
697697
}
698-
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", api.StateClosed == api.StateType(*form.State)); err != nil {
699-
if issues_model.IsErrDependenciesLeft(err) {
700-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
698+
699+
var isClosed bool
700+
switch state := api.StateType(*form.State); state {
701+
case api.StateOpen:
702+
isClosed = false
703+
case api.StateClosed:
704+
isClosed = true
705+
default:
706+
ctx.Error(http.StatusPreconditionFailed, "UnknownPRStateError", fmt.Sprintf("unknown state: %s", state))
707+
return
708+
}
709+
710+
if issue.IsClosed != isClosed {
711+
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", isClosed); err != nil {
712+
if issues_model.IsErrDependenciesLeft(err) {
713+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
714+
return
715+
}
716+
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
701717
return
702718
}
703-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
704-
return
705719
}
706720
}
707721

services/issue/status.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
)
1414

1515
// ChangeStatus changes issue status to open or closed.
16+
// closed means the target status
17+
// Fix me: you should check whether the current issue status is same to the target status before call this function
18+
// as in function changeIssueStatus we will return WasClosedError, even the issue status and target status are both open
1619
func ChangeStatus(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, commitID string, closed bool) error {
1720
comment, err := issues_model.ChangeIssueStatus(ctx, issue, doer, closed)
1821
if err != nil {

0 commit comments

Comments
 (0)