diff --git a/README.yaml b/README.yaml index f73060d..b25ebb2 100644 --- a/README.yaml +++ b/README.yaml @@ -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 | @@ -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" diff --git a/go.mod b/go.mod index 46b7d86..9ad215c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9a991d9..47c246d 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/main.go b/main.go index 67c4d30..295be23 100644 --- a/main.go +++ b/main.go @@ -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" ) @@ -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") @@ -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 != "" { @@ -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) @@ -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 != "" { @@ -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) @@ -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 != "" { @@ -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)