@@ -9,12 +9,13 @@ import (
9
9
"strings"
10
10
"time"
11
11
12
- api "code.gitea.io/sdk/gitea"
13
-
14
12
"code.gitea.io/gitea/modules/log"
15
13
"code.gitea.io/gitea/modules/markup"
16
14
"code.gitea.io/gitea/modules/setting"
17
15
16
+ "code.gitea.io/git"
17
+ api "code.gitea.io/sdk/gitea"
18
+
18
19
"github.com/Unknwon/com"
19
20
"github.com/go-xorm/builder"
20
21
"github.com/go-xorm/xorm"
@@ -609,34 +610,132 @@ func ClearPullPushComment(issue *Issue) error {
609
610
return err
610
611
}
611
612
612
- // CreatePullPushComment creates a commit when push commit to a pull request.
613
- func CreatePullPushComment (doer * User , repo * Repository , issue * Issue , commit * PushCommit ) error {
614
- if len (commit .Sha1 ) == 0 {
615
- return fmt .Errorf ("cannot create reference with empty commit SHA" )
613
+ func createPullPushCommentsNonForcePush (doer * User , issues []* Issue , commits []* PushCommit ) error {
614
+ sess := x .NewSession ()
615
+ defer sess .Close ()
616
+
617
+ if err := sess .Begin (); err != nil {
618
+ return err
616
619
}
617
620
618
- // Check if same reference from same commit has already existed.
619
- has , err := x .Get (& Comment {
620
- Type : CommentTypePullPushCommit ,
621
- IssueID : issue .ID ,
622
- CommitSHA : commit .Sha1 ,
623
- })
624
- if err != nil {
625
- return fmt .Errorf ("check pull push comment: %v" , err )
626
- }
627
-
628
- if ! has {
629
- _ , err = CreateComment (& CreateCommentOptions {
630
- Type : CommentTypePullPushCommit ,
631
- Doer : doer ,
632
- Repo : repo ,
633
- Issue : issue ,
634
- CommitSHA : commit .Sha1 ,
635
- Content : commit .Message ,
636
- RepoFullName : repo .FullName (),
621
+ for _ , issue := range issues {
622
+ for i := len (commits ) - 1 ; i >= 0 ; i -- {
623
+ _ , err := createComment (sess , & CreateCommentOptions {
624
+ Type : CommentTypePullPushCommit ,
625
+ Doer : doer ,
626
+ Repo : issue .Repo ,
627
+ Issue : issue ,
628
+ CommitSHA : commits [i ].Sha1 ,
629
+ Content : commits [i ].Message ,
630
+ RepoFullName : issue .Repo .FullName (),
631
+ })
632
+ if err != nil {
633
+ return err
634
+ }
635
+ }
636
+ }
637
+
638
+ return sess .Commit ()
639
+ }
640
+
641
+ // CreatePullPushComments creates commit comments when push commits to a pull request.
642
+ func CreatePullPushComments (doer * User , issues []* Issue , commits []* PushCommit , isForcePush bool ) error {
643
+ if ! isForcePush {
644
+ return createPullPushCommentsNonForcePush (doer , issues , commits )
645
+ }
646
+
647
+ sess := x .NewSession ()
648
+ defer sess .Close ()
649
+
650
+ if err := sess .Begin (); err != nil {
651
+ return err
652
+ }
653
+
654
+ var commitIDCache = make (map [string ]string )
655
+ var ok bool
656
+ for _ , issue := range issues {
657
+ var commitID string
658
+ var key = fmt .Sprintf ("%s/%s" , issue .Repo .RepoPath (), issue .PullRequest .BaseBranch )
659
+ if commitID , ok = commitIDCache [key ]; ! ok {
660
+ gitRepo , err := git .OpenRepository (issue .Repo .RepoPath ())
661
+ if err != nil {
662
+ return fmt .Errorf ("OpenRepository: %v" , err )
663
+ }
664
+ commit , err := gitRepo .GetBranchCommit (issue .PullRequest .BaseBranch )
665
+ if err != nil {
666
+ return fmt .Errorf ("GetBranchCommit: %v" , err )
667
+ }
668
+ commitID = commit .ID .String ()
669
+ commitIDCache [key ] = commitID
670
+ }
671
+
672
+ var startIdx = - 1
673
+ for i := len (commits ) - 1 ; i >= 0 ; i -- {
674
+ if commitID == commits [i ].Sha1 {
675
+ startIdx = i
676
+ break
677
+ }
678
+ }
679
+ if startIdx > - 1 {
680
+ commits = commits [:startIdx ]
681
+ }
682
+ if len (commits ) <= 0 {
683
+ continue
684
+ }
685
+
686
+ var alreadyComments []* Comment
687
+ err := sess .Find (& alreadyComments , & Comment {
688
+ Type : CommentTypePullPushCommit ,
689
+ IssueID : issue .ID ,
637
690
})
691
+ if err != nil {
692
+ return fmt .Errorf ("check pull push comment: %v" , err )
693
+ }
694
+
695
+ var deleteCommitIDs = make ([]int64 , 0 , len (alreadyComments ))
696
+ var keepCommentSha1s = make (map [string ]struct {})
697
+ for _ , comment := range alreadyComments {
698
+ var find bool
699
+ for _ , commit := range commits {
700
+ if commit .Sha1 == comment .CommitSHA {
701
+ find = true
702
+ break
703
+ }
704
+ }
705
+ if ! find {
706
+ deleteCommitIDs = append (deleteCommitIDs , comment .ID )
707
+ } else {
708
+ keepCommentSha1s [comment .CommitSHA ] = struct {}{}
709
+ }
710
+ }
711
+
712
+ if len (deleteCommitIDs ) > 0 {
713
+ if _ , err := sess .In ("id" , deleteCommitIDs ).Delete (new (Comment )); err != nil {
714
+ return fmt .Errorf ("Delete unused commit comment: %v" , err )
715
+ }
716
+ }
717
+
718
+ for i := len (commits ) - 1 ; i >= 0 ; i -- {
719
+ if _ , ok := keepCommentSha1s [commits [i ].Sha1 ]; ok {
720
+ continue
721
+ }
722
+
723
+ _ , err := createComment (sess , & CreateCommentOptions {
724
+ Type : CommentTypePullPushCommit ,
725
+ Doer : doer ,
726
+ Repo : issue .Repo ,
727
+ Issue : issue ,
728
+ CommitSHA : commits [i ].Sha1 ,
729
+ Content : commits [i ].Message ,
730
+ RepoFullName : issue .Repo .FullName (),
731
+ })
732
+ if err != nil {
733
+ return err
734
+ }
735
+ }
638
736
}
639
- return err
737
+
738
+ return sess .Commit ()
640
739
}
641
740
642
741
func updateCommentRepoFullName (e Engine , repoID int64 , newRepoFullName string ) error {
0 commit comments