diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 9fa9b6220a..37bd248c97 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -912,32 +912,11 @@ func EditIssue(ctx *context.APIContext) { } } - var closeOrReopen bool - switch state := api.StateType(*form.State); state { - case api.StateOpen: - closeOrReopen = false - case api.StateClosed: - closeOrReopen = true - default: - ctx.Error(http.StatusPreconditionFailed, "UnknownIssueStateError", fmt.Sprintf("unknown state: %s", state)) + state := api.StateType(*form.State) + closeOrReopenIssue(ctx, issue, state) + if ctx.Written() { return } - - if closeOrReopen && !issue.IsClosed { - if err := issue_service.CloseIssue(ctx, issue, ctx.Doer, ""); err != nil { - if issues_model.IsErrDependenciesLeft(err) { - ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies") - return - } - ctx.Error(http.StatusInternalServerError, "CloseIssue", err) - return - } - } else if !closeOrReopen && issue.IsClosed { - if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { - ctx.Error(http.StatusInternalServerError, "ReopenIssue", err) - return - } - } } // Refetch from database to assign some automatic values @@ -1060,3 +1039,26 @@ func UpdateIssueDeadline(ctx *context.APIContext) { ctx.JSON(http.StatusCreated, api.IssueDeadline{Deadline: deadlineUnix.AsTimePtr()}) } + +func closeOrReopenIssue(ctx *context.APIContext, issue *issues_model.Issue, state api.StateType) { + if state != api.StateOpen && state != api.StateClosed { + ctx.Error(http.StatusPreconditionFailed, "UnknownIssueStateError", fmt.Sprintf("unknown state: %s", state)) + return + } + + if state == api.StateClosed && !issue.IsClosed { + if err := issue_service.CloseIssue(ctx, issue, ctx.Doer, ""); err != nil { + if issues_model.IsErrDependenciesLeft(err) { + ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue or pull request because it still has open dependencies") + return + } + ctx.Error(http.StatusInternalServerError, "CloseIssue", err) + return + } + } else if state == api.StateOpen && issue.IsClosed { + if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { + ctx.Error(http.StatusInternalServerError, "ReopenIssue", err) + return + } + } +} diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 100f4dd8cf..1de937ef04 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -753,32 +753,11 @@ func EditPullRequest(ctx *context.APIContext) { return } - var closeOrReopen bool - switch state := api.StateType(*form.State); state { - case api.StateOpen: - closeOrReopen = false - case api.StateClosed: - closeOrReopen = true - default: - ctx.Error(http.StatusPreconditionFailed, "UnknownPRStateError", fmt.Sprintf("unknown state: %s", state)) + state := api.StateType(*form.State) + closeOrReopenIssue(ctx, issue, state) + if ctx.Written() { return } - - if closeOrReopen && !issue.IsClosed { - if err := issue_service.CloseIssue(ctx, issue, ctx.Doer, ""); err != nil { - if issues_model.IsErrDependenciesLeft(err) { - ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies") - return - } - ctx.Error(http.StatusInternalServerError, "CloseIssue", err) - return - } - } else if !closeOrReopen && issue.IsClosed { - if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { - ctx.Error(http.StatusInternalServerError, "ReopenIssue", err) - return - } - } } // change pull target branch diff --git a/routers/web/repo/issue_comment.go b/routers/web/repo/issue_comment.go index 3c6dae584e..2bb47f2588 100644 --- a/routers/web/repo/issue_comment.go +++ b/routers/web/repo/issue_comment.go @@ -154,8 +154,7 @@ func NewComment(ctx *context.Context) { if pr != nil { ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index)) } else { - closeOrReopen := form.Status == "close" - if closeOrReopen && !issue.IsClosed { + if form.Status == "close" && !issue.IsClosed { if err := issue_service.CloseIssue(ctx, issue, ctx.Doer, ""); err != nil { log.Error("CloseIssue: %v", err) if issues_model.IsErrDependenciesLeft(err) { @@ -173,7 +172,7 @@ func NewComment(ctx *context.Context) { } log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed) } - } else if !closeOrReopen && issue.IsClosed { + } else if form.Status == "reopen" && issue.IsClosed { if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { log.Error("ReopenIssue: %v", err) } diff --git a/routers/web/repo/issue_list.go b/routers/web/repo/issue_list.go index 201cba3821..cdb769999d 100644 --- a/routers/web/repo/issue_list.go +++ b/routers/web/repo/issue_list.go @@ -435,13 +435,8 @@ func UpdateIssueStatus(ctx *context.Context) { return } - var closeOrReopen bool // true: close, false: reopen - switch action := ctx.FormString("action"); action { - case "open": - closeOrReopen = false - case "close": - closeOrReopen = true - default: + action := ctx.FormString("action") + if action != "open" && action != "close" { log.Warn("Unrecognized action: %s", action) ctx.JSONOK() return @@ -460,7 +455,7 @@ func UpdateIssueStatus(ctx *context.Context) { if issue.IsPull && issue.PullRequest.HasMerged { continue } - if closeOrReopen && !issue.IsClosed { + if action == "close" && !issue.IsClosed { if err := issue_service.CloseIssue(ctx, issue, ctx.Doer, ""); err != nil { if issues_model.IsErrDependenciesLeft(err) { ctx.JSON(http.StatusPreconditionFailed, map[string]any{ @@ -471,7 +466,7 @@ func UpdateIssueStatus(ctx *context.Context) { ctx.ServerError("CloseIssue", err) return } - } else if !closeOrReopen && issue.IsClosed { + } else if action == "open" && issue.IsClosed { if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { ctx.ServerError("ReopenIssue", err) return diff --git a/services/issue/commit.go b/services/issue/commit.go index 805cdd8672..963d0359fd 100644 --- a/services/issue/commit.go +++ b/services/issue/commit.go @@ -189,9 +189,8 @@ func UpdateIssuesCommit(ctx context.Context, doer *user_model.User, repo *repo_m } } - closeOrReopen := ref.Action == references.XRefActionCloses refIssue.Repo = refRepo - if closeOrReopen && !refIssue.IsClosed { + if ref.Action == references.XRefActionCloses && !refIssue.IsClosed { if len(ref.TimeLog) > 0 { if err := issueAddTime(ctx, refIssue, doer, c.Timestamp, ref.TimeLog); err != nil { return err @@ -200,7 +199,7 @@ func UpdateIssuesCommit(ctx context.Context, doer *user_model.User, repo *repo_m if err := CloseIssue(ctx, refIssue, doer, c.Sha1); err != nil { return err } - } else if !closeOrReopen && refIssue.IsClosed { + } else if ref.Action == references.XRefActionReopens && refIssue.IsClosed { if err := ReopenIssue(ctx, refIssue, doer, c.Sha1); err != nil { return err } diff --git a/services/pull/merge.go b/services/pull/merge.go index 14dfe235ab..84b16a160e 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -242,15 +242,14 @@ func handleCloseCrossReferences(ctx context.Context, pr *issues_model.PullReques if err = ref.Issue.LoadRepo(ctx); err != nil { return err } - closeOrReopen := ref.RefAction == references.XRefActionCloses - if closeOrReopen && !ref.Issue.IsClosed { + if ref.RefAction == references.XRefActionCloses && !ref.Issue.IsClosed { if err = issue_service.CloseIssue(ctx, ref.Issue, doer, pr.MergedCommitID); err != nil { // Allow ErrDependenciesLeft if !issues_model.IsErrDependenciesLeft(err) { return err } } - } else if !closeOrReopen && ref.Issue.IsClosed { + } else if ref.RefAction == references.XRefActionReopens && ref.Issue.IsClosed { if err = issue_service.ReopenIssue(ctx, ref.Issue, doer, pr.MergedCommitID); err != nil { return err }