diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2018-05-16 22:01:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-16 22:01:55 +0800 |
commit | 24941a10464dc27eaebafda2a208fa827b74ff8d (patch) | |
tree | c875dd6b7d659e2dbd926dbd3a04a036f9f41c94 /models/issue_comment.go | |
parent | 188fe6c301f9c44d569b75cb339d6a6b3f6e03ad (diff) | |
download | gitea-24941a10464dc27eaebafda2a208fa827b74ff8d.tar.gz gitea-24941a10464dc27eaebafda2a208fa827b74ff8d.zip |
Add more webhooks support and refactor webhook templates directory (#3929)
* add more webhook support
* move hooks templates to standalone dir and add more webhooks ui
* fix tests
* update vendor checksum
* add more webhook support
* move hooks templates to standalone dir and add more webhooks ui
* fix tests
* update vendor checksum
* update vendor
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* load attributes when created release
* update comparsion doc
Diffstat (limited to 'models/issue_comment.go')
-rw-r--r-- | models/issue_comment.go | 110 |
1 files changed, 92 insertions, 18 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go index 2c5875c29c..ad200934bc 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -83,9 +83,10 @@ const ( type Comment struct { ID int64 `xorm:"pk autoincr"` Type CommentType - PosterID int64 `xorm:"INDEX"` - Poster *User `xorm:"-"` - IssueID int64 `xorm:"INDEX"` + PosterID int64 `xorm:"INDEX"` + Poster *User `xorm:"-"` + IssueID int64 `xorm:"INDEX"` + Issue *Issue `xorm:"-"` LabelID int64 Label *Label `xorm:"-"` OldMilestoneID int64 @@ -116,6 +117,15 @@ type Comment struct { ShowTag CommentTag `xorm:"-"` } +// LoadIssue loads issue from database +func (c *Comment) LoadIssue() (err error) { + if c.Issue != nil { + return nil + } + c.Issue, err = GetIssueByID(c.IssueID) + return +} + // AfterLoad is invoked from XORM after setting the values of all fields of this object. func (c *Comment) AfterLoad(session *xorm.Session) { var err error @@ -146,40 +156,40 @@ func (c *Comment) AfterDelete() { // HTMLURL formats a URL-string to the issue-comment func (c *Comment) HTMLURL() string { - issue, err := GetIssueByID(c.IssueID) + err := c.LoadIssue() if err != nil { // Silently dropping errors :unamused: - log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err) + log.Error(4, "LoadIssue(%d): %v", c.IssueID, err) return "" } - return fmt.Sprintf("%s#%s", issue.HTMLURL(), c.HashTag()) + return fmt.Sprintf("%s#%s", c.Issue.HTMLURL(), c.HashTag()) } // IssueURL formats a URL-string to the issue func (c *Comment) IssueURL() string { - issue, err := GetIssueByID(c.IssueID) + err := c.LoadIssue() if err != nil { // Silently dropping errors :unamused: - log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err) + log.Error(4, "LoadIssue(%d): %v", c.IssueID, err) return "" } - if issue.IsPull { + if c.Issue.IsPull { return "" } - return issue.HTMLURL() + return c.Issue.HTMLURL() } // PRURL formats a URL-string to the pull-request func (c *Comment) PRURL() string { - issue, err := GetIssueByID(c.IssueID) + err := c.LoadIssue() if err != nil { // Silently dropping errors :unamused: - log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err) + log.Error(4, "LoadIssue(%d): %v", c.IssueID, err) return "" } - if !issue.IsPull { + if !c.Issue.IsPull { return "" } - return issue.HTMLURL() + return c.Issue.HTMLURL() } // APIFormat converts a Comment to the api.Comment format @@ -196,9 +206,14 @@ func (c *Comment) APIFormat() *api.Comment { } } +// CommentHashTag returns unique hash tag for comment id. +func CommentHashTag(id int64) string { + return fmt.Sprintf("issuecomment-%d", id) +} + // HashTag returns unique hash tag for comment. func (c *Comment) HashTag() string { - return "issuecomment-" + com.ToStr(c.ID) + return CommentHashTag(c.ID) } // EventTag returns unique event hash tag for comment. @@ -576,7 +591,7 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { // CreateIssueComment creates a plain issue comment. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) { - return CreateComment(&CreateCommentOptions{ + comment, err := CreateComment(&CreateCommentOptions{ Type: CommentTypeComment, Doer: doer, Repo: repo, @@ -584,6 +599,21 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri Content: content, Attachments: attachments, }) + if err != nil { + return nil, fmt.Errorf("CreateComment: %v", err) + } + + mode, _ := AccessLevel(doer.ID, 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(2, "PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) + } + return comment, nil } // CreateRefComment creates a commit reference comment to issue. @@ -696,17 +726,41 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) { } // UpdateComment updates information of comment. -func UpdateComment(c *Comment) error { +func UpdateComment(doer *User, c *Comment, oldContent string) error { if _, err := x.ID(c.ID).AllCols().Update(c); err != nil { return err } else if c.Type == CommentTypeComment { UpdateIssueIndexer(c.IssueID) } + + if err := c.LoadIssue(); err != nil { + return err + } + if err := c.Issue.LoadAttributes(); err != nil { + return err + } + + mode, _ := AccessLevel(doer.ID, 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(2, "PrepareWebhooks [comment_id: %d]: %v", c.ID, err) + } + return nil } // DeleteComment deletes the comment -func DeleteComment(comment *Comment) error { +func DeleteComment(doer *User, comment *Comment) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -733,5 +787,25 @@ func DeleteComment(comment *Comment) error { } else if comment.Type == CommentTypeComment { UpdateIssueIndexer(comment.IssueID) } + + if err := comment.LoadIssue(); err != nil { + return err + } + if err := comment.Issue.LoadAttributes(); err != nil { + return err + } + + mode, _ := AccessLevel(doer.ID, 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(2, "PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) + } + return nil } |