Skip to content

Commit 3b54d0b

Browse files
authored
Write Non-Duplicated Comments (#39)
* add circle service load config and logger * implement circle get diff * implemented circleci write comments * fix typo * populate pr details in load config * add logger tests * lowercase errors * lower case error message * pr updates * wip * implement repo and pr service write comments * log finding message if can't post to github * uncomment and add additional env vars to list * use helpers in loadconfig * change logger debugs to info * wip * add tests for filtering out existing comments * allocate slice capacity for filteredComments
1 parent 5e227e1 commit 3b54d0b

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

internal/clients/diffreviewer/circleci/circleci_service.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,19 @@ func (s *Service) WriteComments(comments []*diffreviewer.Comment) error {
199199
return nil
200200
}
201201
if s.PrDetails.PrNumber != nil {
202+
existingComments, _, err := s.GithubClient.PullRequestsService().ListComments(
203+
context.Background(),
204+
s.PrDetails.Owner,
205+
s.PrDetails.Repo,
206+
*s.PrDetails.PrNumber,
207+
&github.PullRequestListCommentsOptions{},
208+
)
209+
if err != nil {
210+
s.Logger.Error(fmt.Sprintf("Error listing existing pull request comments: %s", err.Error()))
211+
}
202212
githubComments := s.createGithubPullRequestComments(comments)
203-
for _, c := range githubComments {
213+
filteredGithubComments := filterExistingComments(githubComments, existingComments)
214+
for _, c := range filteredGithubComments {
204215
_, _, err := s.GithubClient.PullRequestsService().CreateComment(
205216
context.Background(),
206217
s.PrDetails.Owner,
@@ -210,7 +221,7 @@ func (s *Service) WriteComments(comments []*diffreviewer.Comment) error {
210221
)
211222
if err != nil {
212223
s.Logger.Error(fmt.Sprintf("Error writing comment to pull request: %s", err.Error()))
213-
s.Logger.Error(*c.Body)
224+
s.Logger.Error(*c.Body) // log comment that was not written to circle ui
214225
}
215226
}
216227
} else {
@@ -259,3 +270,33 @@ func (s *Service) createGithubRepositoryComments(comments []*diffreviewer.Commen
259270
}
260271
return githubComments
261272
}
273+
274+
type prComment struct {
275+
Body string
276+
Path string
277+
Line int
278+
}
279+
280+
func filterExistingComments(comments []*github.PullRequestComment, existingComments []*github.PullRequestComment) []*github.PullRequestComment {
281+
existingCommentsMap := make(map[prComment]bool, len(existingComments))
282+
for _, ec := range existingComments {
283+
comment := prComment{
284+
Body: *ec.Body,
285+
Path: *ec.Path,
286+
Line: *ec.Line,
287+
}
288+
existingCommentsMap[comment] = true
289+
}
290+
filteredComments := make([]*github.PullRequestComment, 0, len(comments))
291+
for _, c := range comments {
292+
comment := prComment{
293+
Body: *c.Body,
294+
Path: *c.Path,
295+
Line: *c.Line,
296+
}
297+
if _, ok := existingCommentsMap[comment]; !ok {
298+
filteredComments = append(filteredComments, c)
299+
}
300+
}
301+
return filteredComments
302+
}

internal/clients/diffreviewer/circleci/circleci_service_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ func (c *circleCiTestSuite) TestWritePullRequestComments() {
273273
}
274274

275275
for _, tt := range tests {
276+
mockClient.EXPECT().PullRequestsService().Return(mockPullRequests)
277+
mockPullRequests.EXPECT().ListComments(
278+
context.Background(),
279+
testCircleService.PrDetails.Owner,
280+
testCircleService.PrDetails.Repo,
281+
*testCircleService.PrDetails.PrNumber,
282+
&github.PullRequestListCommentsOptions{},
283+
)
276284
for _, gc := range tt.giveGithubComments {
277285
mockClient.EXPECT().PullRequestsService().Return(mockPullRequests)
278286
mockPullRequests.EXPECT().CreateComment(
@@ -416,6 +424,52 @@ func makeTestGithubRepositoryComments(
416424
return comments, githubComments
417425
}
418426

427+
func (c *circleCiTestSuite) TestFilterExistingComments() {
428+
bodyStrs := []string{"a", "b", "c"}
429+
pathStrs := []string{"a.txt", "b.txt", "c.txt"}
430+
lineNums := []int{1, 2, 3, 6}
431+
existingComments := []*github.PullRequestComment{
432+
{
433+
Body: &bodyStrs[0],
434+
Path: &pathStrs[0],
435+
Line: &lineNums[0],
436+
},
437+
{
438+
Body: &bodyStrs[1],
439+
Path: &pathStrs[1],
440+
Line: &lineNums[1],
441+
},
442+
{
443+
Body: &bodyStrs[2],
444+
Path: &pathStrs[2],
445+
Line: &lineNums[2],
446+
},
447+
}
448+
newComment1 := &github.PullRequestComment{
449+
Body: &bodyStrs[0],
450+
Path: &pathStrs[0],
451+
Line: &lineNums[3],
452+
}
453+
newComment2 := &github.PullRequestComment{
454+
Body: &bodyStrs[1],
455+
Path: &pathStrs[1],
456+
Line: &lineNums[2],
457+
}
458+
comments := []*github.PullRequestComment{
459+
existingComments[0],
460+
existingComments[1],
461+
existingComments[2],
462+
newComment1,
463+
newComment2,
464+
}
465+
expectedFilteredComments := []*github.PullRequestComment{
466+
newComment1,
467+
newComment2,
468+
}
469+
filteredComments := filterExistingComments(comments, existingComments)
470+
c.Equal(expectedFilteredComments, filteredComments, "invalid filtered comments value")
471+
}
472+
419473
func TestCircleCiClient(t *testing.T) {
420474
suite.Run(t, new(circleCiTestSuite))
421475
}

internal/interfaces/githubintf/github_pullrequests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import (
1010

1111
type GithubPullRequests interface {
1212
CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.PullRequestComment) (*github.PullRequestComment, *github.Response, error)
13+
ListComments(ctx context.Context, owner string, repo string, number int, opts *github.PullRequestListCommentsOptions) ([]*github.PullRequestComment, *github.Response, error)
1314
}

internal/mocks/clients/githubpullrequests_mock/githubpullrequests_mock.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)