@@ -148,6 +148,7 @@ type Action struct {
148148 Repo * repo_model.Repository `xorm:"-"`
149149 CommentID int64 `xorm:"INDEX"`
150150 Comment * issues_model.Comment `xorm:"-"`
151+ Issue * issues_model.Issue `xorm:"-"` // get the issue id from content
151152 IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
152153 RefName string
153154 IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
@@ -290,11 +291,6 @@ func (a *Action) GetRepoAbsoluteLink(ctx context.Context) string {
290291 return setting .AppURL + url .PathEscape (a .GetRepoUserName (ctx )) + "/" + url .PathEscape (a .GetRepoName (ctx ))
291292}
292293
293- // GetCommentHTMLURL returns link to action comment.
294- func (a * Action ) GetCommentHTMLURL (ctx context.Context ) string {
295- return a .getCommentHTMLURL (ctx )
296- }
297-
298294func (a * Action ) loadComment (ctx context.Context ) (err error ) {
299295 if a .CommentID == 0 || a .Comment != nil {
300296 return nil
@@ -303,69 +299,44 @@ func (a *Action) loadComment(ctx context.Context) (err error) {
303299 return err
304300}
305301
306- func (a * Action ) getCommentHTMLURL (ctx context.Context ) string {
302+ // GetCommentHTMLURL returns link to action comment.
303+ func (a * Action ) GetCommentHTMLURL (ctx context.Context ) string {
307304 if a == nil {
308305 return "#"
309306 }
310307 _ = a .loadComment (ctx )
311308 if a .Comment != nil {
312309 return a .Comment .HTMLURL (ctx )
313310 }
314- if len (a .GetIssueInfos ()) == 0 {
315- return "#"
316- }
317- // Return link to issue
318- issueIDString := a .GetIssueInfos ()[0 ]
319- issueID , err := strconv .ParseInt (issueIDString , 10 , 64 )
320- if err != nil {
321- return "#"
322- }
323311
324- issue , err := issues_model .GetIssueByID (ctx , issueID )
325- if err != nil {
312+ if err := a .LoadIssue (ctx ); err != nil || a .Issue == nil {
326313 return "#"
327314 }
328-
329- if err = issue .LoadRepo (ctx ); err != nil {
315+ if err := a .Issue .LoadRepo (ctx ); err != nil {
330316 return "#"
331317 }
332318
333- return issue .HTMLURL ()
319+ return a . Issue .HTMLURL ()
334320}
335321
336322// GetCommentLink returns link to action comment.
337323func (a * Action ) GetCommentLink (ctx context.Context ) string {
338- return a .getCommentLink (ctx )
339- }
340-
341- func (a * Action ) getCommentLink (ctx context.Context ) string {
342324 if a == nil {
343325 return "#"
344326 }
345327 _ = a .loadComment (ctx )
346328 if a .Comment != nil {
347329 return a .Comment .Link (ctx )
348330 }
349- if len (a .GetIssueInfos ()) == 0 {
350- return "#"
351- }
352- // Return link to issue
353- issueIDString := a .GetIssueInfos ()[0 ]
354- issueID , err := strconv .ParseInt (issueIDString , 10 , 64 )
355- if err != nil {
356- return "#"
357- }
358331
359- issue , err := issues_model .GetIssueByID (ctx , issueID )
360- if err != nil {
332+ if err := a .LoadIssue (ctx ); err != nil || a .Issue == nil {
361333 return "#"
362334 }
363-
364- if err = issue .LoadRepo (ctx ); err != nil {
335+ if err := a .Issue .LoadRepo (ctx ); err != nil {
365336 return "#"
366337 }
367338
368- return issue .Link ()
339+ return a . Issue .Link ()
369340}
370341
371342// GetBranch returns the action's repository branch.
@@ -393,6 +364,10 @@ func (a *Action) GetCreate() time.Time {
393364 return a .CreatedUnix .AsTime ()
394365}
395366
367+ func (a * Action ) IsIssueEvent () bool {
368+ return a .OpType .InActions ("comment_issue" , "approve_pull_request" , "reject_pull_request" , "comment_pull" , "merge_pull_request" )
369+ }
370+
396371// GetIssueInfos returns a list of associated information with the action.
397372func (a * Action ) GetIssueInfos () []string {
398373 // make sure it always returns 3 elements, because there are some access to the a[1] and a[2] without checking the length
@@ -403,27 +378,52 @@ func (a *Action) GetIssueInfos() []string {
403378 return ret
404379}
405380
381+ func (a * Action ) getIssueIndex () int64 {
382+ infos := a .GetIssueInfos ()
383+ if len (infos ) == 0 {
384+ return 0
385+ }
386+ index , _ := strconv .ParseInt (infos [0 ], 10 , 64 )
387+ return index
388+ }
389+
390+ func (a * Action ) LoadIssue (ctx context.Context ) error {
391+ if a .Issue != nil {
392+ return nil
393+ }
394+ if index := a .getIssueIndex (); index > 0 {
395+ issue , err := issues_model .GetIssueByIndex (ctx , a .RepoID , index )
396+ if err != nil {
397+ return err
398+ }
399+ a .Issue = issue
400+ a .Issue .Repo = a .Repo
401+ }
402+ return nil
403+ }
404+
406405// GetIssueTitle returns the title of first issue associated with the action.
407406func (a * Action ) GetIssueTitle (ctx context.Context ) string {
408- index , _ := strconv .ParseInt (a .GetIssueInfos ()[0 ], 10 , 64 )
409- issue , err := issues_model .GetIssueByIndex (ctx , a .RepoID , index )
410- if err != nil {
411- log .Error ("GetIssueByIndex: %v" , err )
412- return "500 when get issue"
407+ if err := a .LoadIssue (ctx ); err != nil {
408+ log .Error ("LoadIssue: %v" , err )
409+ return "<500 when get issue>"
410+ }
411+ if a .Issue == nil {
412+ return "<Issue not found>"
413413 }
414- return issue .Title
414+ return a . Issue .Title
415415}
416416
417- // GetIssueContent returns the content of first issue associated with
418- // this action.
417+ // GetIssueContent returns the content of first issue associated with this action.
419418func (a * Action ) GetIssueContent (ctx context.Context ) string {
420- index , _ := strconv .ParseInt (a .GetIssueInfos ()[0 ], 10 , 64 )
421- issue , err := issues_model .GetIssueByIndex (ctx , a .RepoID , index )
422- if err != nil {
423- log .Error ("GetIssueByIndex: %v" , err )
424- return "500 when get issue"
419+ if err := a .LoadIssue (ctx ); err != nil {
420+ log .Error ("LoadIssue: %v" , err )
421+ return "<500 when get issue>"
422+ }
423+ if a .Issue == nil {
424+ return "<Content not found>"
425425 }
426- return issue .Content
426+ return a . Issue .Content
427427}
428428
429429// GetFeedsOptions options for retrieving feeds
@@ -463,7 +463,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
463463 return nil , 0 , fmt .Errorf ("FindAndCount: %w" , err )
464464 }
465465
466- if err := ActionList (actions ).loadAttributes (ctx ); err != nil {
466+ if err := ActionList (actions ).LoadAttributes (ctx ); err != nil {
467467 return nil , 0 , fmt .Errorf ("LoadAttributes: %w" , err )
468468 }
469469
0 commit comments