diff --git a/commenter/commenter.go b/commenter/commenter.go index e93a8f5..e310723 100644 --- a/commenter/commenter.go +++ b/commenter/commenter.go @@ -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 { diff --git a/commenter/connector.go b/commenter/connector.go index 47746e7..c6d616b 100644 --- a/commenter/connector.go +++ b/commenter/connector.go @@ -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 @@ -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, @@ -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 { diff --git a/test/commenter_stage_test.go b/test/commenter_stage_test.go index b43aa42..7292f5b 100644 --- a/test/commenter_stage_test.go +++ b/test/commenter_stage_test.go @@ -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 { @@ -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 diff --git a/test/commenter_test.go b/test/commenter_test.go index e29d5dd..e874975 100644 --- a/test/commenter_test.go +++ b/test/commenter_test.go @@ -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() @@ -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)