Skip to content

Commit

Permalink
First relesase looking okay
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney committed Nov 7, 2020
1 parent fd27b78 commit 39fe984
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 201 deletions.
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
# go-github-pr-commenter
Library for adding comments to github PRs

## What is it?

A convenience libary that wraps the [go-github](https://github.com/google/go-github) library and allows you to quickly add comments to the lines of changes in a comment.

The intention is this is used with CI tools to automatically comment on new Github pull requests when static analysis checks are failing.

For an example of this in use, see the [tfsec-pr-commenter-action](https://github.com/tfsec/tfsec-pr-commenter-action). This Github action will run against your Terraform and report any security issues that are present in the code before it is committed.

## How do I use it?

The intention is to keep the interface as clean as possible; steps are

- create a commenter for a repo and PR
- write comments to the commenter
- comments which exist will not be written
- comments that aren't appropriate (not part of the PR) will not be written

### Expected Errors

The following errors can be handled - I hope these are self explanatory

```go
type PrDoesNotExistError struct {
owner string
repo string
prNumber int
}

type NotPartOfPrError struct {
filepath string
}

type CommentAlreadyWrittenError struct {
filepath string
comment string
}

type CommentNotValidError struct {
filepath string
lineNo int
}
```

### Basic Usage Example

```go
package main

import (
commenter "github.com/owenrumney/go-github-pr-commenter"
log "github.com/sirupsen/logrus"
"os"
)


// Create the commenter
token := os.Getenv("GITHUB_TOKEN")

c, err := commenter.NewCommenter(token, "tfsec", "tfsec-example-project", 8)
if err != nil {
fmt.Println(err.Error())
}

// process whatever static analysis results you've gathered
for _, result := myResults {
err = c.WriteMultiLineComment(result.Path, result.Comment, result.StartLine, result.EndLine)
if err != nil {
if errors.Is(err, commenter.CommentNotValidError{}) {
log.Debugf("result not relevant for commit. %s", err.Error())
} else {
log.Errorf("an error occurred writing the comment: %s", err.Error())
}
}
}
```
25 changes: 0 additions & 25 deletions cmd/commenter/main.go

This file was deleted.

13 changes: 0 additions & 13 deletions commentBlock.go

This file was deleted.

50 changes: 0 additions & 50 deletions comment_block_stage_test.go

This file was deleted.

18 changes: 0 additions & 18 deletions comment_block_test.go

This file was deleted.

Loading

0 comments on commit 39fe984

Please sign in to comment.