Skip to content

Commit

Permalink
Make API EditIssue and EditPullRequest issue notifications
Browse files Browse the repository at this point in the history
Restructure models.UpdateIssueByAPI and EditIssue/EditPullRequest
to issue notifications

Fix go-gitea#10014

Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Apr 18, 2020
1 parent 4f597b1 commit 36d36ca
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
67 changes: 51 additions & 16 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,13 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed, isMergeP
}
}

issue.IsClosed = isClosed
return issue.doChangeStatus(e, doer, isMergePull)
}

func (issue *Issue) doChangeStatus(e *xorm.Session, doer *User, isMergePull bool) (*Comment, error) {
// Check for open dependencies
if isClosed && issue.Repo.isDependenciesEnabled(e) {
if issue.IsClosed && issue.Repo.isDependenciesEnabled(e) {
// only check if dependencies are enabled and we're about to close an issue, otherwise reopening an issue would fail when there are unsatisfied dependencies
noDeps, err := issueNoDependenciesLeft(e, issue)
if err != nil {
Expand All @@ -586,23 +591,22 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed, isMergeP
}
}

issue.IsClosed = isClosed
if isClosed {
if issue.IsClosed {
issue.ClosedUnix = timeutil.TimeStampNow()
} else {
issue.ClosedUnix = 0
}

if err = updateIssueCols(e, issue, "is_closed", "closed_unix"); err != nil {
if err := updateIssueCols(e, issue, "is_closed", "closed_unix"); err != nil {
return nil, err
}

// Update issue count of labels
if err = issue.getLabels(e); err != nil {
if err := issue.getLabels(e); err != nil {
return nil, err
}
for idx := range issue.Labels {
if err = updateLabel(e, issue.Labels[idx]); err != nil {
if err := updateLabel(e, issue.Labels[idx]); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -1600,28 +1604,59 @@ func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int6
}

// UpdateIssueByAPI updates all allowed fields of given issue.
func UpdateIssueByAPI(issue *Issue) error {
func UpdateIssueByAPI(issue *Issue, doer *User) (*Comment, bool, error) {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
return nil, false, err
}

if err := issue.loadRepo(sess); err != nil {
return nil, false, fmt.Errorf("loadRepo: %v", err)
}

// Reload the issue
currentIssue, err := getIssueByID(sess, issue.ID)
if err != nil {
return nil, false, err
}

if _, err := sess.ID(issue.ID).Cols(
"name", "is_closed", "content", "milestone_id", "priority",
"deadline_unix", "updated_unix", "closed_unix", "is_locked").
"name", "content", "milestone_id", "priority",
"deadline_unix", "updated_unix", "is_locked").
Update(issue); err != nil {
return err
return nil, false, err
}

if err := issue.loadPoster(sess); err != nil {
return err
titleChanged := currentIssue.Title != issue.Title
if titleChanged {
var opts = &CreateCommentOptions{
Type: CommentTypeChangeTitle,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
OldTitle: currentIssue.Title,
NewTitle: issue.Title,
}
_, err := createComment(sess, opts)
if err != nil {
return nil, false, fmt.Errorf("createComment: %v", err)
}
}

if err := issue.addCrossReferences(sess, issue.Poster, true); err != nil {
return err
var comment *Comment

if currentIssue.IsClosed != issue.IsClosed {
comment, err = issue.doChangeStatus(sess, doer, false)
if err != nil {
return nil, false, err
}
}
return sess.Commit()

if err := issue.addCrossReferences(sess, doer, true); err != nil {
return nil, false, err
}
return comment, titleChanged, sess.Commit()
}

// UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.
Expand Down
26 changes: 15 additions & 11 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/convert"
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -508,6 +509,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
return
}

oldTitle := issue.Title
if len(form.Title) > 0 {
issue.Title = form.Title
}
Expand Down Expand Up @@ -562,20 +564,22 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
return
}
}

if err = models.UpdateIssueByAPI(issue); err != nil {
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
if err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
return
}
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
return
}
if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
return
}
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
return
}

if titleChanged {
notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)
}

if statusChangeComment != nil {
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
}

// Refetch from database to assign some automatic values
Expand Down
25 changes: 15 additions & 10 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/routers/api/v1/utils"
Expand Down Expand Up @@ -393,6 +394,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
return
}

oldTitle := issue.Title
if len(form.Title) > 0 {
issue.Title = form.Title
}
Expand Down Expand Up @@ -458,19 +460,22 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
}
}

if err = models.UpdateIssueByAPI(issue); err != nil {
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
if err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
return
}
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
return
}
if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
return
}
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
return
}

if titleChanged {
notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)
}

if statusChangeComment != nil {
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
}

// Refetch from database
Expand Down

0 comments on commit 36d36ca

Please sign in to comment.