Skip to content

Commit 36d36ca

Browse files
committed
Make API EditIssue and EditPullRequest issue notifications
Restructure models.UpdateIssueByAPI and EditIssue/EditPullRequest to issue notifications Fix go-gitea#10014 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4f597b1 commit 36d36ca

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

models/issue.go

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,13 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed, isMergeP
573573
}
574574
}
575575

576+
issue.IsClosed = isClosed
577+
return issue.doChangeStatus(e, doer, isMergePull)
578+
}
579+
580+
func (issue *Issue) doChangeStatus(e *xorm.Session, doer *User, isMergePull bool) (*Comment, error) {
576581
// Check for open dependencies
577-
if isClosed && issue.Repo.isDependenciesEnabled(e) {
582+
if issue.IsClosed && issue.Repo.isDependenciesEnabled(e) {
578583
// 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
579584
noDeps, err := issueNoDependenciesLeft(e, issue)
580585
if err != nil {
@@ -586,23 +591,22 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed, isMergeP
586591
}
587592
}
588593

589-
issue.IsClosed = isClosed
590-
if isClosed {
594+
if issue.IsClosed {
591595
issue.ClosedUnix = timeutil.TimeStampNow()
592596
} else {
593597
issue.ClosedUnix = 0
594598
}
595599

596-
if err = updateIssueCols(e, issue, "is_closed", "closed_unix"); err != nil {
600+
if err := updateIssueCols(e, issue, "is_closed", "closed_unix"); err != nil {
597601
return nil, err
598602
}
599603

600604
// Update issue count of labels
601-
if err = issue.getLabels(e); err != nil {
605+
if err := issue.getLabels(e); err != nil {
602606
return nil, err
603607
}
604608
for idx := range issue.Labels {
605-
if err = updateLabel(e, issue.Labels[idx]); err != nil {
609+
if err := updateLabel(e, issue.Labels[idx]); err != nil {
606610
return nil, err
607611
}
608612
}
@@ -1600,28 +1604,59 @@ func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int6
16001604
}
16011605

16021606
// UpdateIssueByAPI updates all allowed fields of given issue.
1603-
func UpdateIssueByAPI(issue *Issue) error {
1607+
func UpdateIssueByAPI(issue *Issue, doer *User) (*Comment, bool, error) {
16041608
sess := x.NewSession()
16051609
defer sess.Close()
16061610
if err := sess.Begin(); err != nil {
1607-
return err
1611+
return nil, false, err
1612+
}
1613+
1614+
if err := issue.loadRepo(sess); err != nil {
1615+
return nil, false, fmt.Errorf("loadRepo: %v", err)
1616+
}
1617+
1618+
// Reload the issue
1619+
currentIssue, err := getIssueByID(sess, issue.ID)
1620+
if err != nil {
1621+
return nil, false, err
16081622
}
16091623

16101624
if _, err := sess.ID(issue.ID).Cols(
1611-
"name", "is_closed", "content", "milestone_id", "priority",
1612-
"deadline_unix", "updated_unix", "closed_unix", "is_locked").
1625+
"name", "content", "milestone_id", "priority",
1626+
"deadline_unix", "updated_unix", "is_locked").
16131627
Update(issue); err != nil {
1614-
return err
1628+
return nil, false, err
16151629
}
16161630

1617-
if err := issue.loadPoster(sess); err != nil {
1618-
return err
1631+
titleChanged := currentIssue.Title != issue.Title
1632+
if titleChanged {
1633+
var opts = &CreateCommentOptions{
1634+
Type: CommentTypeChangeTitle,
1635+
Doer: doer,
1636+
Repo: issue.Repo,
1637+
Issue: issue,
1638+
OldTitle: currentIssue.Title,
1639+
NewTitle: issue.Title,
1640+
}
1641+
_, err := createComment(sess, opts)
1642+
if err != nil {
1643+
return nil, false, fmt.Errorf("createComment: %v", err)
1644+
}
16191645
}
16201646

1621-
if err := issue.addCrossReferences(sess, issue.Poster, true); err != nil {
1622-
return err
1647+
var comment *Comment
1648+
1649+
if currentIssue.IsClosed != issue.IsClosed {
1650+
comment, err = issue.doChangeStatus(sess, doer, false)
1651+
if err != nil {
1652+
return nil, false, err
1653+
}
16231654
}
1624-
return sess.Commit()
1655+
1656+
if err := issue.addCrossReferences(sess, doer, true); err != nil {
1657+
return nil, false, err
1658+
}
1659+
return comment, titleChanged, sess.Commit()
16251660
}
16261661

16271662
// UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.

routers/api/v1/repo/issue.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/convert"
1717
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
1818
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/notification"
1920
"code.gitea.io/gitea/modules/setting"
2021
api "code.gitea.io/gitea/modules/structs"
2122
"code.gitea.io/gitea/modules/timeutil"
@@ -508,6 +509,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
508509
return
509510
}
510511

512+
oldTitle := issue.Title
511513
if len(form.Title) > 0 {
512514
issue.Title = form.Title
513515
}
@@ -562,20 +564,22 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
562564
return
563565
}
564566
}
565-
566-
if err = models.UpdateIssueByAPI(issue); err != nil {
567+
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
568+
if err != nil {
569+
if models.IsErrDependenciesLeft(err) {
570+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
571+
return
572+
}
567573
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
568574
return
569575
}
570-
if form.State != nil {
571-
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
572-
if models.IsErrDependenciesLeft(err) {
573-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
574-
return
575-
}
576-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
577-
return
578-
}
576+
577+
if titleChanged {
578+
notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)
579+
}
580+
581+
if statusChangeComment != nil {
582+
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
579583
}
580584

581585
// Refetch from database to assign some automatic values

routers/api/v1/repo/pull.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/convert"
1717
"code.gitea.io/gitea/modules/git"
1818
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/notification"
1920
api "code.gitea.io/gitea/modules/structs"
2021
"code.gitea.io/gitea/modules/timeutil"
2122
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -393,6 +394,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
393394
return
394395
}
395396

397+
oldTitle := issue.Title
396398
if len(form.Title) > 0 {
397399
issue.Title = form.Title
398400
}
@@ -458,19 +460,22 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
458460
}
459461
}
460462

461-
if err = models.UpdateIssueByAPI(issue); err != nil {
463+
statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
464+
if err != nil {
465+
if models.IsErrDependenciesLeft(err) {
466+
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
467+
return
468+
}
462469
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
463470
return
464471
}
465-
if form.State != nil {
466-
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
467-
if models.IsErrDependenciesLeft(err) {
468-
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
469-
return
470-
}
471-
ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
472-
return
473-
}
472+
473+
if titleChanged {
474+
notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)
475+
}
476+
477+
if statusChangeComment != nil {
478+
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
474479
}
475480

476481
// Refetch from database

0 commit comments

Comments
 (0)