Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from owenrumney/add-general-commenter
Browse files Browse the repository at this point in the history
Add general commenting
  • Loading branch information
owenrumney authored Apr 20, 2021
2 parents d9b9fa3 + 0ffcb83 commit 9a533ca
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
14 changes: 14 additions & 0 deletions commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ func (c *Commenter) WriteLineComment(file, comment string, line int) error {
return c.writeCommentIfRequired(prComment)
}

func (c *Commenter) WriteGeneralComment(comment string) error {
if !c.loaded {
err := c.loadPr()
if err != nil {
return err
}
}

issueComment := &github.IssueComment{
Body: &comment,
}
return c.pr.writeGeneralComment(issueComment)
}

func (c *Commenter) writeCommentIfRequired(prComment *github.PullRequestComment) error {
for _, existing := range c.existingComments {
err := func(ec *existingComment) error {
Expand Down
14 changes: 14 additions & 0 deletions commenter/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package commenter

import (
"context"

"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)

type connector struct {
prs *github.PullRequestsService
comments *github.IssuesService
owner string
repo string
prNumber int
Expand All @@ -27,6 +29,7 @@ func createConnector(token, owner, repo string, prNumber int) *connector {

return &connector{
prs: client.PullRequests,
comments: client.Issues,
owner: owner,
repo: repo,
prNumber: prNumber,
Expand All @@ -44,6 +47,17 @@ func (c *connector) writeReviewComment(block *github.PullRequestComment) error {
return nil
}

func (c *connector) writeGeneralComment(comment *github.IssueComment) error {
ctx := context.Background()

var _, _, err = c.comments.CreateComment(ctx, c.owner, c.repo, c.prNumber, comment)
if err != nil {
return err
}

return nil
}

func (c *connector) getFilesForPr() ([]*github.CommitFile, error) {
files, _, err := c.prs.ListFiles(context.Background(), c.owner, c.repo, c.prNumber, nil)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions test/commenter_stage_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package test

import (
"github.com/owenrumney/go-github-pr-commenter/commenter"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"

"github.com/owenrumney/go-github-pr-commenter/commenter"
"github.com/stretchr/testify/assert"
)

type commenterTest struct {
Expand Down Expand Up @@ -69,6 +70,11 @@ func (ct *commenterTest) thereIsAnError() *commenterTest {
return ct
}

func (ct *commenterTest) aNewGeneralCommentIsCreated(comment string) {
err := ct.commenter.WriteGeneralComment(comment)
ct.err = err
}

func (ct *commenterTest) aSingleLineCommentIsCreated() {
err := ct.commenter.WriteLineComment("commitFileInfo.go", "This is awesome", 7)
ct.err = err
Expand Down
16 changes: 15 additions & 1 deletion test/commenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Test_can_add_a_single_line_comment(t *testing.T) {
func Test_add_a_single_line_comment_that_is_on_a_line_that_isnt_in_pr(t *testing.T) {
given, when, then := newCommenterTest(t)

given.thePullRequest(1).
given.thePullRequest(5).
forOwner("owenrumney").
inRepo("go-github-pr-commenter").
usingTokenFromEnvironment()
Expand All @@ -86,6 +86,20 @@ func Test_add_a_single_line_comment_on_a_file_that_that_isnt_in_pr(t *testing.T)
and().theErrorIsCommentIsInvalid()
}

func Test_add_a_general_comment(t *testing.T) {
given, when, then := newCommenterTest(t)

given.thePullRequest(1).
forOwner("owenrumney").
inRepo("go-github-pr-commenter").
usingTokenFromEnvironment()

when.aNewCommenterIsCreated().
and().aNewGeneralCommentIsCreated("test comment")

then.thereIsNoErrors()
}

func Test_can_add_a_single_line_comment_second_one_has_an_error(t *testing.T) {
given, when, then := newCommenterTest(t)

Expand Down

0 comments on commit 9a533ca

Please sign in to comment.