-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
gitlab_mr_commit.go
148 lines (131 loc) 路 4.13 KB
/
gitlab_mr_commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gitlab
import (
"context"
"fmt"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/xanzy/go-gitlab"
"golang.org/x/sync/errgroup"
"github.com/reviewdog/reviewdog"
"github.com/reviewdog/reviewdog/service/serviceutil"
)
var _ reviewdog.CommentService = &GitLabMergeRequestCommitCommenter{}
// GitLabMergeRequestCommitCommenter is a comment service for GitLab MergeRequest.
//
// API:
// https://docs.gitlab.com/ce/api/commits.html#post-comment-to-commit
// POST /projects/:id/repository/commits/:sha/comments
type GitLabMergeRequestCommitCommenter struct {
cli *gitlab.Client
pr int
sha string
projects string
muComments sync.Mutex
postComments []*reviewdog.Comment
postedcs serviceutil.PostedComments
// wd is working directory relative to root of repository.
wd string
}
// NewGitLabMergeRequestCommitCommenter returns a new GitLabMergeRequestCommitCommenter service.
// GitLabMergeRequestCommitCommenter service needs git command in $PATH.
func NewGitLabMergeRequestCommitCommenter(cli *gitlab.Client, owner, repo string, pr int, sha string) (*GitLabMergeRequestCommitCommenter, error) {
workDir, err := serviceutil.GitRelWorkdir()
if err != nil {
return nil, fmt.Errorf("GitLabMergeRequestCommitCommenter needs 'git' command: %v", err)
}
return &GitLabMergeRequestCommitCommenter{
cli: cli,
pr: pr,
sha: sha,
projects: owner + "/" + repo,
wd: workDir,
}, nil
}
// Post accepts a comment and holds it. Flush method actually posts comments to
// GitLab in parallel.
func (g *GitLabMergeRequestCommitCommenter) Post(_ context.Context, c *reviewdog.Comment) error {
c.Path = filepath.ToSlash(filepath.Join(g.wd, c.Path))
g.muComments.Lock()
defer g.muComments.Unlock()
g.postComments = append(g.postComments, c)
return nil
}
// Flush posts comments which has not been posted yet.
func (g *GitLabMergeRequestCommitCommenter) Flush(ctx context.Context) error {
g.muComments.Lock()
defer g.muComments.Unlock()
if err := g.setPostedComment(ctx); err != nil {
return err
}
return g.postCommentsForEach(ctx)
}
func (g *GitLabMergeRequestCommitCommenter) postCommentsForEach(ctx context.Context) error {
var eg errgroup.Group
for _, c := range g.postComments {
comment := c
if g.postedcs.IsPosted(comment, comment.Lnum) {
continue
}
eg.Go(func() error {
commitID, err := g.getLastCommitsID(comment.Path, comment.Lnum)
if err != nil {
commitID = g.sha
}
body := serviceutil.CommentBody(comment)
ltype := "new"
prcomment := &gitlab.PostCommitCommentOptions{
Note: &body,
Path: &comment.Path,
Line: &comment.Lnum,
LineType: <ype,
}
_, _, err = g.cli.Commits.PostCommitComment(g.projects, commitID, prcomment, gitlab.WithContext(ctx))
return err
})
}
return eg.Wait()
}
func (g *GitLabMergeRequestCommitCommenter) getLastCommitsID(path string, line int) (string, error) {
lineFormat := fmt.Sprintf("%d,%d", line, line)
s, err := exec.Command("git", "blame", "-l", "-L", lineFormat, path).Output()
if err != nil {
return "", fmt.Errorf("failed to get commitID: %v", err)
}
commitID := strings.Split(string(s), " ")[0]
return commitID, nil
}
func (g *GitLabMergeRequestCommitCommenter) setPostedComment(ctx context.Context) error {
g.postedcs = make(serviceutil.PostedComments)
cs, err := g.comment(ctx)
if err != nil {
return err
}
for _, c := range cs {
if c.Line == 0 || c.Path == "" || c.Note == "" {
// skip resolved comments. Or comments which do not have "path" nor
// "body".
continue
}
g.postedcs.AddPostedComment(c.Path, c.Line, c.Note)
}
return nil
}
func (g *GitLabMergeRequestCommitCommenter) comment(ctx context.Context) ([]*gitlab.CommitComment, error) {
commits, _, err := g.cli.MergeRequests.GetMergeRequestCommits(
g.projects, g.pr, nil, gitlab.WithContext(ctx))
if err != nil {
return nil, err
}
comments := make([]*gitlab.CommitComment, 0)
for _, c := range commits {
tmpComments, _, err := g.cli.Commits.GetCommitComments(
g.projects, c.ID, nil, gitlab.WithContext(ctx))
if err != nil {
continue
}
comments = append(comments, tmpComments...)
}
return comments, nil
}