Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,6 +84,7 @@ usage: |-
| format_file | GITHUB_COMMENT_FORMAT_FILE | Alias of `template_file` |
| comment | GITHUB_COMMENT | Comment text. If neither `comment` nor `GITHUB_COMMENT` provided, will read from `stdin` |
| delete-comment-regex | GITHUB_DELETE_COMMENT_REGEX | Regex to find previous comments to delete before creating the new comment. Supported for comment types `commit`, `pr-file`, `issue` and `pr` |
| edit-comment-regex | GITHUB_EDIT_COMMENT_REGEX | Regex to find previous comments to replace with new content, or create new comment if none found. Supported for comment types `commit`, `pr-file`, `issue` and `pr` |
| baseURL | GITHUB_BASE_URL | Github Enterprise URL. _E.g._ `https://github.example.com/api/v3` |
| uploadURL | GITHUB_UPLOAD_URL | Github Enterprise Upload URL to pass to the Github client |
| insecure | GITHUB_INSECURE | Boolean to ignore SSL certificate check |
Expand DownExpand Up@@ -276,6 +277,10 @@ examples: |-
-comment "New comment on the PR file"
```

### `edit-comment-regex`

The `-edit-comment-regex` flag operates similarly to the `-delete-comment-regex` except existing comments will be updated instead of deleted. If no matching comment is found a new comment is created.

# Contributors to this project
contributors:
- name: "Erik Osterman"
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,7 @@ require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/go-github/v34 v34.0.0
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.9 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,9 @@ github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZC
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github/v34 v34.0.0 h1:/siYFImY8KwGc5QD1gaPf+f8QX6tLwxNIco2RkYxoFA=
github.com/google/go-github/v34 v34.0.0/go.mod h1:w/2qlrXUfty+lbyO6tatnzIw97v1CM+/jZcwXMDiPQQ=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
Expand All@@ -30,13 +31,16 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
Expand Down
102 changes: 98 additions & 4 deletions main.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ import (
"text/template"

"github.com/Masterminds/sprig"
"github.com/google/go-github/github"
"github.com/google/go-github/v34/github"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
Expand DownExpand Up@@ -47,6 +47,7 @@ var (
formatFile = flag.String("format_file", os.Getenv("GITHUB_COMMENT_FORMAT_FILE"), "Alias of `template_file`")
comment = flag.String("comment", os.Getenv("GITHUB_COMMENT"), "Comment text")
deleteCommentRegex = flag.String("delete-comment-regex", os.Getenv("GITHUB_DELETE_COMMENT_REGEX"), "Regex to find previous comments to delete before creating the new comment. Supported for comment types `commit`, `pr-file`, `issue` and `pr`")
editCommentRegex = flag.String("edit-comment-regex", os.Getenv("GITHUB_EDIT_COMMENT_REGEX"), "Regex to find previous comments to replace with new content, or create new comment if none found. Supported for comment types `commit`, `pr-file`, `issue` and `pr`")
baseURL = flag.String("baseURL", os.Getenv("GITHUB_BASE_URL"), "Base URL of github enterprise")
uploadURL = flag.String("uploadURL", os.Getenv("GITHUB_UPLOAD_URL"), "Upload URL of github enterprise")
insecure = flag.Bool("insecure", strings.ToLower(os.Getenv("GITHUB_INSECURE")) == "true", "Ignore SSL certificate check")
Expand DownExpand Up@@ -199,6 +200,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
commitComment := &github.RepositoryComment{Body: &formattedComment}

// Find and delete existing comment(s) before creating the new one
if *deleteCommentRegex != "" {
Expand All@@ -225,7 +227,36 @@ func main() {
}
}

commitComment := &github.RepositoryComment{Body: &formattedComment}
// Find and update existing comment with new content
if *editCommentRegex != "" {
found := false
r, err := regexp.Compile(*editCommentRegex)
if err != nil {
log.Fatal(err)
}

listOptions := &github.ListOptions{}
comments, _, err := githubClient.Repositories.ListCommitComments(context.Background(), *owner, *repo, *sha, listOptions)
if err != nil {
log.Println("github-commenter: Error listing commit comments: ", err)
} else {
for _, comment := range comments {
if r.MatchString(*comment.Body) {
found = true
_, _, err = githubClient.Repositories.UpdateComment(context.Background(), *owner, *repo, *comment.ID, commitComment)
if err != nil {
log.Fatal("github-commenter: Error updating commit comment: ", err)
} else {
log.Println("github-commenter: Updated commit comment: ", *comment.ID)
}
}
}
}
if found {
return // exit
}
}

commitComment, _, err = githubClient.Repositories.CreateComment(context.Background(), *owner, *repo, *sha, commitComment)
if err != nil {
log.Fatal(err)
Expand DownExpand Up@@ -272,6 +303,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
issueComment := &github.IssueComment{Body: &formattedComment}

// Find and delete existing comment(s) before creating the new one
if *deleteCommentRegex != "" {
Expand All@@ -297,8 +329,36 @@ func main() {
}
}
}
// Find and update existing comment(s) with new content
if *editCommentRegex != "" {
found := false
r, err := regexp.Compile(*editCommentRegex)
if err != nil {
log.Fatal(err)
}

listOptions := &github.IssueListCommentsOptions{}
comments, _, err := githubClient.Issues.ListComments(context.Background(), *owner, *repo, num, listOptions)
if err != nil {
log.Println("github-commenter: Error listing Issue/PR comments: ", err)
} else {
for _, comment := range comments {
if r.MatchString(*comment.Body) {
found = true
_, _, err = githubClient.Issues.EditComment(context.Background(), *owner, *repo, *comment.ID, issueComment)
if err != nil {
log.Fatal("github-commenter: Error updating Issue/PR comment: ", err)
} else {
log.Println("github-commenter: Updated Issue/PR comment: ", *comment.ID)
}
}
}
}
if found {
return // exit
}
}

issueComment := &github.IssueComment{Body: &formattedComment}
issueComment, _, err = githubClient.Issues.CreateComment(context.Background(), *owner, *repo, num, issueComment)
if err != nil {
log.Fatal(err)
Expand DownExpand Up@@ -336,6 +396,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
pullRequestComment := &github.PullRequestComment{Body: &formattedComment, Path: file, Position: &position, CommitID: sha}

// Find and delete existing comment(s) before creating the new one
if *deleteCommentRegex != "" {
Expand All@@ -362,7 +423,40 @@ func main() {
}
}

pullRequestComment := &github.PullRequestComment{Body: &formattedComment, Path: file, Position: &position, CommitID: sha}
// Find and update existing comment with new content
if *editCommentRegex != "" {
found := false
r, err := regexp.Compile(*editCommentRegex)
if err != nil {
log.Fatal(err)
}

// edit mode: create a new request with only the new comment body.
// The API call will fail if the req includes other fields (path, commit_id, position)
editComment := &github.PullRequestComment{Body: pullRequestComment.Body}

listOptions := &github.PullRequestListCommentsOptions{}
comments, _, err := githubClient.PullRequests.ListComments(context.Background(), *owner, *repo, num, listOptions)
if err != nil {
log.Println("github-commenter: Error listing PR file commit comments: ", err)
} else {
for _, comment := range comments {
if r.MatchString(*comment.Body) {
found = true
_, _, err = githubClient.PullRequests.EditComment(context.Background(), *owner, *repo, *comment.ID, editComment)
if err != nil {
log.Fatal("github-commenter: Error updating PR file comment: ", err)
} else {
log.Println("github-commenter: Updated PR file comment: ", *comment.ID)
}
}
}
}
if found {
return // exit
}
}

pullRequestComment, _, err = githubClient.PullRequests.CreateComment(context.Background(), *owner, *repo, num, pullRequestComment)
if err != nil {
log.Fatal(err)
Expand Down