Skip to content

Commit 9e4d716

Browse files
committed
don't delete duplicated commit comment
1 parent a5444f8 commit 9e4d716

File tree

2 files changed

+132
-71
lines changed

2 files changed

+132
-71
lines changed

models/action.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
"time"
1515
"unicode"
1616

17-
"github.com/Unknwon/com"
18-
"github.com/go-xorm/builder"
17+
"code.gitea.io/gitea/modules/base"
18+
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/setting"
1920

2021
"code.gitea.io/git"
2122
api "code.gitea.io/sdk/gitea"
2223

23-
"code.gitea.io/gitea/modules/base"
24-
"code.gitea.io/gitea/modules/log"
25-
"code.gitea.io/gitea/modules/setting"
24+
"github.com/Unknwon/com"
25+
"github.com/go-xorm/builder"
2626
)
2727

2828
// ActionType represents the type of an action.
@@ -669,46 +669,8 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
669669
return fmt.Errorf("IssueList.LoadRepositories: %v", err)
670670
}
671671

672-
var commitIDCache = make(map[string]string)
673-
var ok bool
674-
for _, issue := range issues {
675-
var commitID string
676-
if isForcePush {
677-
if err := ClearPullPushComment(issue); err != nil {
678-
return fmt.Errorf("ClearPullPushComent: %v", err)
679-
}
680-
681-
var key = fmt.Sprintf("%s/%s", issue.Repo.RepoPath(), issue.PullRequest.BaseBranch)
682-
if commitID, ok = commitIDCache[key]; !ok {
683-
gitRepo, err := git.OpenRepository(issue.Repo.RepoPath())
684-
if err != nil {
685-
return fmt.Errorf("OpenRepository: %v", err)
686-
}
687-
commit, err := gitRepo.GetBranchCommit(issue.PullRequest.BaseBranch)
688-
if err != nil {
689-
return fmt.Errorf("GetBranchCommit: %v", err)
690-
}
691-
commitID = commit.ID.String()
692-
commitIDCache[key] = commitID
693-
}
694-
}
695-
696-
var findStart = false
697-
for i := len(commentCommits) - 1; i >= 0; i-- {
698-
if isForcePush {
699-
if commitID == commentCommits[i].Sha1 {
700-
findStart = true
701-
continue
702-
}
703-
if !findStart {
704-
continue
705-
}
706-
}
707-
708-
if err := CreatePullPushComment(pusher, issue.Repo, issue, commentCommits[i]); err != nil {
709-
return fmt.Errorf("CreatePullPushComment: %v", err)
710-
}
711-
}
672+
if err := CreatePullPushComments(pusher, issues, commentCommits, isForcePush); err != nil {
673+
return fmt.Errorf("CreatePullPushComments: %v", err)
712674
}
713675
}
714676

models/issue_comment.go

Lines changed: 125 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"strings"
1010
"time"
1111

12-
api "code.gitea.io/sdk/gitea"
13-
1412
"code.gitea.io/gitea/modules/log"
1513
"code.gitea.io/gitea/modules/markup"
1614
"code.gitea.io/gitea/modules/setting"
1715

16+
"code.gitea.io/git"
17+
api "code.gitea.io/sdk/gitea"
18+
1819
"github.com/Unknwon/com"
1920
"github.com/go-xorm/builder"
2021
"github.com/go-xorm/xorm"
@@ -609,34 +610,132 @@ func ClearPullPushComment(issue *Issue) error {
609610
return err
610611
}
611612

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
616619
}
617620

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,
637690
})
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+
}
638736
}
639-
return err
737+
738+
return sess.Commit()
640739
}
641740

642741
func updateCommentRepoFullName(e Engine, repoID int64, newRepoFullName string) error {

0 commit comments

Comments
 (0)