@@ -83,9 +83,10 @@ const (
83
83
type Comment struct {
84
84
ID int64 `xorm:"pk autoincr"`
85
85
Type CommentType
86
- PosterID int64 `xorm:"INDEX"`
87
- Poster * User `xorm:"-"`
88
- IssueID int64 `xorm:"INDEX"`
86
+ PosterID int64 `xorm:"INDEX"`
87
+ Poster * User `xorm:"-"`
88
+ IssueID int64 `xorm:"INDEX"`
89
+ Issue * Issue `xorm:"-"`
89
90
LabelID int64
90
91
Label * Label `xorm:"-"`
91
92
OldMilestoneID int64
@@ -116,6 +117,15 @@ type Comment struct {
116
117
ShowTag CommentTag `xorm:"-"`
117
118
}
118
119
120
+ // LoadIssue loads issue from database
121
+ func (c * Comment ) LoadIssue () (err error ) {
122
+ if c .Issue != nil {
123
+ return nil
124
+ }
125
+ c .Issue , err = GetIssueByID (c .IssueID )
126
+ return
127
+ }
128
+
119
129
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
120
130
func (c * Comment ) AfterLoad (session * xorm.Session ) {
121
131
var err error
@@ -146,40 +156,40 @@ func (c *Comment) AfterDelete() {
146
156
147
157
// HTMLURL formats a URL-string to the issue-comment
148
158
func (c * Comment ) HTMLURL () string {
149
- issue , err := GetIssueByID ( c . IssueID )
159
+ err := c . LoadIssue ( )
150
160
if err != nil { // Silently dropping errors :unamused:
151
- log .Error (4 , "GetIssueByID (%d): %v" , c .IssueID , err )
161
+ log .Error (4 , "LoadIssue (%d): %v" , c .IssueID , err )
152
162
return ""
153
163
}
154
- return fmt .Sprintf ("%s#%s" , issue .HTMLURL (), c .HashTag ())
164
+ return fmt .Sprintf ("%s#%s" , c . Issue .HTMLURL (), c .HashTag ())
155
165
}
156
166
157
167
// IssueURL formats a URL-string to the issue
158
168
func (c * Comment ) IssueURL () string {
159
- issue , err := GetIssueByID ( c . IssueID )
169
+ err := c . LoadIssue ( )
160
170
if err != nil { // Silently dropping errors :unamused:
161
- log .Error (4 , "GetIssueByID (%d): %v" , c .IssueID , err )
171
+ log .Error (4 , "LoadIssue (%d): %v" , c .IssueID , err )
162
172
return ""
163
173
}
164
174
165
- if issue .IsPull {
175
+ if c . Issue .IsPull {
166
176
return ""
167
177
}
168
- return issue .HTMLURL ()
178
+ return c . Issue .HTMLURL ()
169
179
}
170
180
171
181
// PRURL formats a URL-string to the pull-request
172
182
func (c * Comment ) PRURL () string {
173
- issue , err := GetIssueByID ( c . IssueID )
183
+ err := c . LoadIssue ( )
174
184
if err != nil { // Silently dropping errors :unamused:
175
- log .Error (4 , "GetIssueByID (%d): %v" , c .IssueID , err )
185
+ log .Error (4 , "LoadIssue (%d): %v" , c .IssueID , err )
176
186
return ""
177
187
}
178
188
179
- if ! issue .IsPull {
189
+ if ! c . Issue .IsPull {
180
190
return ""
181
191
}
182
- return issue .HTMLURL ()
192
+ return c . Issue .HTMLURL ()
183
193
}
184
194
185
195
// APIFormat converts a Comment to the api.Comment format
@@ -196,9 +206,14 @@ func (c *Comment) APIFormat() *api.Comment {
196
206
}
197
207
}
198
208
209
+ // CommentHashTag returns unique hash tag for comment id.
210
+ func CommentHashTag (id int64 ) string {
211
+ return fmt .Sprintf ("issuecomment-%d" , id )
212
+ }
213
+
199
214
// HashTag returns unique hash tag for comment.
200
215
func (c * Comment ) HashTag () string {
201
- return "issuecomment-" + com . ToStr (c .ID )
216
+ return CommentHashTag (c .ID )
202
217
}
203
218
204
219
// EventTag returns unique event hash tag for comment.
@@ -576,14 +591,29 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
576
591
577
592
// CreateIssueComment creates a plain issue comment.
578
593
func CreateIssueComment (doer * User , repo * Repository , issue * Issue , content string , attachments []string ) (* Comment , error ) {
579
- return CreateComment (& CreateCommentOptions {
594
+ comment , err := CreateComment (& CreateCommentOptions {
580
595
Type : CommentTypeComment ,
581
596
Doer : doer ,
582
597
Repo : repo ,
583
598
Issue : issue ,
584
599
Content : content ,
585
600
Attachments : attachments ,
586
601
})
602
+ if err != nil {
603
+ return nil , fmt .Errorf ("CreateComment: %v" , err )
604
+ }
605
+
606
+ mode , _ := AccessLevel (doer .ID , repo )
607
+ if err = PrepareWebhooks (repo , HookEventIssueComment , & api.IssueCommentPayload {
608
+ Action : api .HookIssueCommentCreated ,
609
+ Issue : issue .APIFormat (),
610
+ Comment : comment .APIFormat (),
611
+ Repository : repo .APIFormat (mode ),
612
+ Sender : doer .APIFormat (),
613
+ }); err != nil {
614
+ log .Error (2 , "PrepareWebhooks [comment_id: %d]: %v" , comment .ID , err )
615
+ }
616
+ return comment , nil
587
617
}
588
618
589
619
// CreateRefComment creates a commit reference comment to issue.
@@ -696,17 +726,41 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
696
726
}
697
727
698
728
// UpdateComment updates information of comment.
699
- func UpdateComment (c * Comment ) error {
729
+ func UpdateComment (doer * User , c * Comment , oldContent string ) error {
700
730
if _ , err := x .ID (c .ID ).AllCols ().Update (c ); err != nil {
701
731
return err
702
732
} else if c .Type == CommentTypeComment {
703
733
UpdateIssueIndexer (c .IssueID )
704
734
}
735
+
736
+ if err := c .LoadIssue (); err != nil {
737
+ return err
738
+ }
739
+ if err := c .Issue .LoadAttributes (); err != nil {
740
+ return err
741
+ }
742
+
743
+ mode , _ := AccessLevel (doer .ID , c .Issue .Repo )
744
+ if err := PrepareWebhooks (c .Issue .Repo , HookEventIssueComment , & api.IssueCommentPayload {
745
+ Action : api .HookIssueCommentEdited ,
746
+ Issue : c .Issue .APIFormat (),
747
+ Comment : c .APIFormat (),
748
+ Changes : & api.ChangesPayload {
749
+ Body : & api.ChangesFromPayload {
750
+ From : oldContent ,
751
+ },
752
+ },
753
+ Repository : c .Issue .Repo .APIFormat (mode ),
754
+ Sender : doer .APIFormat (),
755
+ }); err != nil {
756
+ log .Error (2 , "PrepareWebhooks [comment_id: %d]: %v" , c .ID , err )
757
+ }
758
+
705
759
return nil
706
760
}
707
761
708
762
// DeleteComment deletes the comment
709
- func DeleteComment (comment * Comment ) error {
763
+ func DeleteComment (doer * User , comment * Comment ) error {
710
764
sess := x .NewSession ()
711
765
defer sess .Close ()
712
766
if err := sess .Begin (); err != nil {
@@ -733,5 +787,25 @@ func DeleteComment(comment *Comment) error {
733
787
} else if comment .Type == CommentTypeComment {
734
788
UpdateIssueIndexer (comment .IssueID )
735
789
}
790
+
791
+ if err := comment .LoadIssue (); err != nil {
792
+ return err
793
+ }
794
+ if err := comment .Issue .LoadAttributes (); err != nil {
795
+ return err
796
+ }
797
+
798
+ mode , _ := AccessLevel (doer .ID , comment .Issue .Repo )
799
+
800
+ if err := PrepareWebhooks (comment .Issue .Repo , HookEventIssueComment , & api.IssueCommentPayload {
801
+ Action : api .HookIssueCommentDeleted ,
802
+ Issue : comment .Issue .APIFormat (),
803
+ Comment : comment .APIFormat (),
804
+ Repository : comment .Issue .Repo .APIFormat (mode ),
805
+ Sender : doer .APIFormat (),
806
+ }); err != nil {
807
+ log .Error (2 , "PrepareWebhooks [comment_id: %d]: %v" , comment .ID , err )
808
+ }
809
+
736
810
return nil
737
811
}
0 commit comments