@@ -148,6 +148,7 @@ type Action struct {
148
148
Repo * repo_model.Repository `xorm:"-"`
149
149
CommentID int64 `xorm:"INDEX"`
150
150
Comment * issues_model.Comment `xorm:"-"`
151
+ Issue * issues_model.Issue `xorm:"-"` // get the issue id from content
151
152
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
152
153
RefName string
153
154
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
@@ -290,11 +291,6 @@ func (a *Action) GetRepoAbsoluteLink(ctx context.Context) string {
290
291
return setting .AppURL + url .PathEscape (a .GetRepoUserName (ctx )) + "/" + url .PathEscape (a .GetRepoName (ctx ))
291
292
}
292
293
293
- // GetCommentHTMLURL returns link to action comment.
294
- func (a * Action ) GetCommentHTMLURL (ctx context.Context ) string {
295
- return a .getCommentHTMLURL (ctx )
296
- }
297
-
298
294
func (a * Action ) loadComment (ctx context.Context ) (err error ) {
299
295
if a .CommentID == 0 || a .Comment != nil {
300
296
return nil
@@ -303,69 +299,44 @@ func (a *Action) loadComment(ctx context.Context) (err error) {
303
299
return err
304
300
}
305
301
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 {
307
304
if a == nil {
308
305
return "#"
309
306
}
310
307
_ = a .loadComment (ctx )
311
308
if a .Comment != nil {
312
309
return a .Comment .HTMLURL (ctx )
313
310
}
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
- }
323
311
324
- issue , err := issues_model .GetIssueByID (ctx , issueID )
325
- if err != nil {
312
+ if err := a .LoadIssue (ctx ); err != nil || a .Issue == nil {
326
313
return "#"
327
314
}
328
-
329
- if err = issue .LoadRepo (ctx ); err != nil {
315
+ if err := a .Issue .LoadRepo (ctx ); err != nil {
330
316
return "#"
331
317
}
332
318
333
- return issue .HTMLURL ()
319
+ return a . Issue .HTMLURL ()
334
320
}
335
321
336
322
// GetCommentLink returns link to action comment.
337
323
func (a * Action ) GetCommentLink (ctx context.Context ) string {
338
- return a .getCommentLink (ctx )
339
- }
340
-
341
- func (a * Action ) getCommentLink (ctx context.Context ) string {
342
324
if a == nil {
343
325
return "#"
344
326
}
345
327
_ = a .loadComment (ctx )
346
328
if a .Comment != nil {
347
329
return a .Comment .Link (ctx )
348
330
}
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
- }
358
331
359
- issue , err := issues_model .GetIssueByID (ctx , issueID )
360
- if err != nil {
332
+ if err := a .LoadIssue (ctx ); err != nil || a .Issue == nil {
361
333
return "#"
362
334
}
363
-
364
- if err = issue .LoadRepo (ctx ); err != nil {
335
+ if err := a .Issue .LoadRepo (ctx ); err != nil {
365
336
return "#"
366
337
}
367
338
368
- return issue .Link ()
339
+ return a . Issue .Link ()
369
340
}
370
341
371
342
// GetBranch returns the action's repository branch.
@@ -393,6 +364,10 @@ func (a *Action) GetCreate() time.Time {
393
364
return a .CreatedUnix .AsTime ()
394
365
}
395
366
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
+
396
371
// GetIssueInfos returns a list of associated information with the action.
397
372
func (a * Action ) GetIssueInfos () []string {
398
373
// 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 {
403
378
return ret
404
379
}
405
380
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
+
406
405
// GetIssueTitle returns the title of first issue associated with the action.
407
406
func (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>"
413
413
}
414
- return issue .Title
414
+ return a . Issue .Title
415
415
}
416
416
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.
419
418
func (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>"
425
425
}
426
- return issue .Content
426
+ return a . Issue .Content
427
427
}
428
428
429
429
// GetFeedsOptions options for retrieving feeds
@@ -463,7 +463,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
463
463
return nil , 0 , fmt .Errorf ("FindAndCount: %w" , err )
464
464
}
465
465
466
- if err := ActionList (actions ).loadAttributes (ctx ); err != nil {
466
+ if err := ActionList (actions ).LoadAttributes (ctx ); err != nil {
467
467
return nil , 0 , fmt .Errorf ("LoadAttributes: %w" , err )
468
468
}
469
469
0 commit comments