Browse Source

Move create issue comment to comments package (#8212)

* move create issue comment to comments package

* extract actions on update/delete comment from models to comment service

* fix lint

* fix lint
tags/v1.10.0-rc1
Lunny Xiao 4 years ago
parent
commit
061388379a
4 changed files with 117 additions and 117 deletions
  1. 6
    111
      models/issue_comment.go
  2. 4
    3
      routers/api/v1/repo/issue_comment.go
  3. 4
    3
      routers/repo/issue.go
  4. 103
    0
      services/comments/comments.go

+ 6
- 111
models/issue_comment.go View File

} }


func (c *Comment) loadPoster(e Engine) (err error) { func (c *Comment) loadPoster(e Engine) (err error) {
if c.Poster != nil {
if c.PosterID <= 0 || c.Poster != nil {
return nil return nil
} }




// LoadPoster loads comment poster // LoadPoster loads comment poster
func (c *Comment) LoadPoster() error { func (c *Comment) LoadPoster() error {
if c.PosterID <= 0 || c.Poster != nil {
return nil
}

var err error
c.Poster, err = getUserByID(x, c.PosterID)
if err != nil {
if IsErrUserNotExist(err) {
c.PosterID = -1
c.Poster = NewGhostUser()
} else {
log.Error("getUserByID[%d]: %v", c.ID, err)
}
}
return nil
return c.loadPoster(x)
} }


// LoadAttachments loads attachments // LoadAttachments loads attachments
} }
if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() { if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
c.Invalidated = true c.Invalidated = true
return UpdateComment(doer, c, "")
return UpdateComment(c, doer)
} }
return nil return nil
} }
return comment, nil return comment, nil
} }


// CreateIssueComment creates a plain issue comment.
func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
comment, err := CreateComment(&CreateCommentOptions{
Type: CommentTypeComment,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
Attachments: attachments,
})
if err != nil {
return nil, fmt.Errorf("CreateComment: %v", err)
}

mode, _ := AccessLevel(doer, repo)
if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go HookQueue.Add(repo.ID)
}
return comment, nil
}

// CreateRefComment creates a commit reference comment to issue. // CreateRefComment creates a commit reference comment to issue.
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error { func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
if len(commitSHA) == 0 { if len(commitSHA) == 0 {
} }


// UpdateComment updates information of comment. // UpdateComment updates information of comment.
func UpdateComment(doer *User, c *Comment, oldContent string) error {
func UpdateComment(c *Comment, doer *User) error {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if err := sess.Begin(); err != nil { if err := sess.Begin(); err != nil {
if err := sess.Commit(); err != nil { if err := sess.Commit(); err != nil {
return fmt.Errorf("Commit: %v", err) return fmt.Errorf("Commit: %v", err)
} }
sess.Close()

if err := c.LoadPoster(); err != nil {
return err
}
if err := c.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := AccessLevel(doer, c.Issue.Repo)
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go HookQueue.Add(c.Issue.Repo.ID)
}


return nil return nil
} }


// DeleteComment deletes the comment // DeleteComment deletes the comment
func DeleteComment(doer *User, comment *Comment) error {
func DeleteComment(comment *Comment, doer *User) error {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if err := sess.Begin(); err != nil { if err := sess.Begin(); err != nil {
return err return err
} }


if err := sess.Commit(); err != nil {
return err
}
sess.Close()

if err := comment.LoadPoster(); err != nil {
return err
}
if err := comment.LoadIssue(); err != nil {
return err
}

if err := comment.Issue.LoadAttributes(); err != nil {
return err
}
if err := comment.loadPoster(x); err != nil {
return err
}
if err := comment.neuterCrossReferences(x); err != nil {
return err
}

mode, _ := AccessLevel(doer, comment.Issue.Repo)

if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: comment.Issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: comment.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go HookQueue.Add(comment.Issue.Repo.ID)
}

return nil
return sess.Commit()
} }


// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS

+ 4
- 3
routers/api/v1/repo/issue_comment.go View File

"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
comment_service "code.gitea.io/gitea/services/comments"
) )


// ListIssueComments list all the comments of an issue // ListIssueComments list all the comments of an issue
return return
} }


comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
if err != nil { if err != nil {
ctx.Error(500, "CreateIssueComment", err) ctx.Error(500, "CreateIssueComment", err)
return return


oldContent := comment.Content oldContent := comment.Content
comment.Content = form.Body comment.Content = form.Body
if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
ctx.Error(500, "UpdateComment", err) ctx.Error(500, "UpdateComment", err)
return return
} }
return return
} }


if err = models.DeleteComment(ctx.User, comment); err != nil {
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
ctx.Error(500, "DeleteCommentByID", err) ctx.Error(500, "DeleteCommentByID", err)
return return
} }

+ 4
- 3
routers/repo/issue.go View File

"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
comment_service "code.gitea.io/gitea/services/comments"
milestone_service "code.gitea.io/gitea/services/milestone" milestone_service "code.gitea.io/gitea/services/milestone"


"github.com/unknwon/com" "github.com/unknwon/com"
return return
} }


comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
if err != nil { if err != nil {
ctx.ServerError("CreateIssueComment", err) ctx.ServerError("CreateIssueComment", err)
return return
}) })
return return
} }
if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err = comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
ctx.ServerError("UpdateComment", err) ctx.ServerError("UpdateComment", err)
return return
} }
return return
} }


if err = models.DeleteComment(ctx.User, comment); err != nil {
if err = models.DeleteComment(comment, ctx.User); err != nil {
ctx.ServerError("DeleteCommentByID", err) ctx.ServerError("DeleteCommentByID", err)
return return
} }

+ 103
- 0
services/comments/comments.go View File



"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/gitdiff" "code.gitea.io/gitea/services/gitdiff"
) )


// CreateIssueComment creates a plain issue comment.
func CreateIssueComment(doer *models.User, repo *models.Repository, issue *models.Issue, content string, attachments []string) (*models.Comment, error) {
comment, err := models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeComment,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
Attachments: attachments,
})
if err != nil {
return nil, err
}

mode, _ := models.AccessLevel(doer, repo)
if err = models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go models.HookQueue.Add(repo.ID)
}
return comment, nil
}

// CreateCodeComment creates a plain code comment at the specified line / path // CreateCodeComment creates a plain code comment at the specified line / path
func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) { func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
var commitID, patch string var commitID, patch string
Patch: patch, Patch: patch,
}) })
} }

// UpdateComment updates information of comment.
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
if err := models.UpdateComment(c, doer); err != nil {
return err
}

if err := c.LoadPoster(); err != nil {
return err
}
if err := c.LoadIssue(); err != nil {
return err
}

if err := c.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := models.AccessLevel(doer, c.Issue.Repo)
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go models.HookQueue.Add(c.Issue.Repo.ID)
}

return nil
}

// DeleteComment deletes the comment
func DeleteComment(comment *models.Comment, doer *models.User) error {
if err := models.DeleteComment(comment, doer); err != nil {
return err
}

if err := comment.LoadPoster(); err != nil {
return err
}
if err := comment.LoadIssue(); err != nil {
return err
}

if err := comment.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := models.AccessLevel(doer, comment.Issue.Repo)

if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: comment.Issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: comment.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go models.HookQueue.Add(comment.Issue.Repo.ID)
}

return nil
}

Loading…
Cancel
Save