Compare cc1a3c8 ... +6 ... 88976cf

Showing 2 of 4 changed files with
68.29%
of changed lines covered. View details
@@ -2,10 +2,13 @@
Loading
2 2
3 3
import (
4 4
	"fmt"
5 +
	"log"
5 6
	"os/exec"
7 +
	"sync"
6 8
7 -
	"github.com/google/go-github/github"
8 9
	"golang.org/x/sync/errgroup"
10 +
11 +
	"github.com/google/go-github/github"
9 12
)
10 13
11 14
var _ = github.ScopeAdminOrg
@@ -50,6 +53,8 @@
Loading
50 53
	sha   string
51 54
52 55
	postedcs postedcomments
56 +
57 +
	muFlash sync.Mutex
53 58
}
54 59
55 60
// NewGitHubPullReqest returns a new GitHubPullRequest service.
@@ -66,6 +71,8 @@
Loading
66 71
// Post accepts a comment and holds it. Flash method actually posts comments to
67 72
// GitHub in parallel.
68 73
func (g *GitHubPullRequest) Post(c *Comment) error {
74 +
	g.muFlash.Lock()
75 +
	defer g.muFlash.Unlock()
69 76
	g.postComments = append(g.postComments, c)
70 77
	return nil
71 78
}
@@ -80,11 +87,55 @@
Loading
80 87
	return tool + bodyPrefix + "\n" + c.Body
81 88
}
82 89
90 +
var githubAPIHost = "api.github.com"
91 +
83 92
// Flash posts comments which has not been posted yet.
84 93
func (g *GitHubPullRequest) Flash() error {
94 +
	g.muFlash.Lock()
95 +
	defer g.muFlash.Unlock()
96 +
85 97
	if err := g.setPostedComment(); err != nil {
86 98
		return err
87 99
	}
100 +
	// TODO(haya14busa,#58): remove host check when GitHub Enterprise supports
101 +
	// Pull Request API.
102 +
	if g.cli.BaseURL.Host == githubAPIHost {
103 +
		return g.postAsReviewComment()
104 +
	}
105 +
	return g.postCommentsForEach()
106 +
}
107 +
108 +
func (g *GitHubPullRequest) postAsReviewComment() error {
109 +
	comments := make([]*ReviewComment, 0, len(g.postComments))
110 +
	for _, c := range g.postComments {
111 +
		if g.postedcs.IsPosted(c) {
112 +
			continue
113 +
		}
114 +
		cbody := commentBody(c)
115 +
		comments = append(comments, &ReviewComment{
116 +
			Path:     &c.Path,
117 +
			Position: &c.LnumDiff,
118 +
			Body:     &cbody,
119 +
		})
120 +
	}
121 +
122 +
	if len(comments) == 0 {
123 +
		return nil
124 +
	}
125 +
126 +
	// TODO(haya14busa): it might be useful to report overview results by "body"
127 +
	// field.
128 +
	event := "COMMENT"
129 +
	review := &Review{Event: &event, Comments: comments}
130 +
131 +
	_, _, err := g.CreateReview(g.owner, g.repo, g.pr, review)
132 +
	if err != nil {
133 +
		return err
134 +
	}
135 +
	return nil
136 +
}
137 +
138 +
func (g *GitHubPullRequest) postCommentsForEach() error {
88 139
	var eg errgroup.Group
89 140
	for _, c := range g.postComments {
90 141
		comment := c
@@ -157,3 +208,47 @@
Loading
157 208
	}
158 209
	return comments, nil
159 210
}
211 +
212 +
// ---
213 +
// GitHub PullRequest Review API Implementation
214 +
// ref: https://github.com/google/go-github/issues/495
215 +
216 +
const (
217 +
	mediaTypePullRequestReview = "application/vnd.github.black-cat-preview+json"
218 +
)
219 +
220 +
// Review represents a pull request review.
221 +
type Review struct {
222 +
	Body     *string          `json:"body,omitempty"`
223 +
	Event    *string          `json:"event,omitempty"`
224 +
	Comments []*ReviewComment `json:"comments,omitempty"`
225 +
}
226 +
227 +
// ReviewComment represents draft review comments.
228 +
type ReviewComment struct {
229 +
	Path     *string `json:"path,omitempty"`
230 +
	Position *int    `json:"position,omitempty"`
231 +
	Body     *string `json:"body,omitempty"`
232 +
}
233 +
234 +
// CreateReview creates a new review comment on the specified pull request.
235 +
//
236 +
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
237 +
func (g *GitHubPullRequest) CreateReview(owner, repo string, number int, review *Review) (*github.PullRequestReview, *github.Response, error) {
238 +
	u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
239 +
240 +
	req, err := g.cli.NewRequest("POST", u, review)
241 +
	if err != nil {
242 +
		return nil, nil, err
243 +
	}
244 +
245 +
	req.Header.Set("Accept", mediaTypePullRequestReview)
246 +
247 +
	r := new(github.PullRequestReview)
248 +
	resp, err := g.cli.Do(req, r)
249 +
	if err != nil {
250 +
		log.Printf("GitHub Review API error: %v", err)
251 +
		return nil, resp, err
252 +
	}
253 +
	return r, resp, err
254 +
}
@@ -160 +255 @@
Loading
@@ -91,9 +91,6 @@
Loading
91 91
		return err
92 92
	}
93 93
94 -
	if bulk, ok := w.c.(BulkCommentService); ok {
95 -
		defer bulk.Flash()
96 -
	}
97 94
	for _, result := range results {
98 95
		addedline := addedlines.Get(result.Path, result.Lnum)
99 96
		if filepath.IsAbs(result.Path) {
@@ -117,6 +114,10 @@
Loading
117 114
		}
118 115
	}
119 116
117 +
	if bulk, ok := w.c.(BulkCommentService); ok {
118 +
		return bulk.Flash()
119 +
	}
120 +
120 121
	return nil
121 122
}
122 123
@@ -123 +124 @@
Loading

Everything is accounted for!

No changed detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
cmd ø ø ø 82.52%
comment.go ø ø ø 73.33%
comment_iowriter.go ø ø ø 100%
diff ø ø ø 78.94%
diff.go ø ø ø 90.47%
github.go +28 +5 +6 +4.96% 62.50%
parser.go ø ø ø 92.72%
project ø ø ø 89.74%
reviewdog.go ø ø ø 73.68%
Project Totals (10 files) 596 49 106 79.36%
Loading