Skip to content

Commit eba6bfb

Browse files
authored
Fix 500 error when state params is set when editing issue/PR by API (#31880)
A quick fix for #31871
1 parent 661a1e1 commit eba6bfb

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

routers/api/v1/repo/issue.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,27 @@ func EditIssue(ctx *context.APIContext) {
891891
return
892892
}
893893
}
894-
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", api.StateClosed == api.StateType(*form.State)); err != nil {
895-
if issues_model.IsErrDependenciesLeft(err) {
896-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
894+
895+
var isClosed bool
896+
switch state := api.StateType(*form.State); state {
897+
case api.StateOpen:
898+
isClosed = false
899+
case api.StateClosed:
900+
isClosed = true
901+
default:
902+
ctx.Error(http.StatusPreconditionFailed, "UnknownIssueStateError", fmt.Sprintf("unknown state: %s", state))
903+
return
904+
}
905+
906+
if issue.IsClosed != isClosed {
907+
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", isClosed); err != nil {
908+
if issues_model.IsErrDependenciesLeft(err) {
909+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
910+
return
911+
}
912+
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
897913
return
898914
}
899-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
900-
return
901915
}
902916
}
903917

routers/api/v1/repo/pull.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -716,13 +716,27 @@ func EditPullRequest(ctx *context.APIContext) {
716716
ctx.Error(http.StatusPreconditionFailed, "MergedPRState", "cannot change state of this pull request, it was already merged")
717717
return
718718
}
719-
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", api.StateClosed == api.StateType(*form.State)); err != nil {
720-
if issues_model.IsErrDependenciesLeft(err) {
721-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
719+
720+
var isClosed bool
721+
switch state := api.StateType(*form.State); state {
722+
case api.StateOpen:
723+
isClosed = false
724+
case api.StateClosed:
725+
isClosed = true
726+
default:
727+
ctx.Error(http.StatusPreconditionFailed, "UnknownPRStateError", fmt.Sprintf("unknown state: %s", state))
728+
return
729+
}
730+
731+
if issue.IsClosed != isClosed {
732+
if err := issue_service.ChangeStatus(ctx, issue, ctx.Doer, "", isClosed); err != nil {
733+
if issues_model.IsErrDependenciesLeft(err) {
734+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
735+
return
736+
}
737+
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
722738
return
723739
}
724-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
725-
return
726740
}
727741
}
728742

services/issue/status.go

Lines changed: 3 additions & 0 deletions
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)