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 #1 from owenrumney/initial-commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
owenrumney authored Nov 7, 2020
2 parents 839d7db + 39fe984 commit 9f81078
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 212 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())
}
}
}
```
27 changes: 0 additions & 27 deletions commentBlock.go

This file was deleted.

60 changes: 0 additions & 60 deletions comment_block_stage_test.go

This file was deleted.

58 changes: 0 additions & 58 deletions comment_block_test.go

This file was deleted.

Loading

0 comments on commit 9f81078

Please sign in to comment.