From 2716c1df8c75a4ec435d51ff1422e3813aea991f Mon Sep 17 00:00:00 2001 From: Maikel van Dort Date: Fri, 13 Jun 2025 15:51:29 +0200 Subject: [PATCH 1/2] Initial commit --- main_test.go | 1 + 1 file changed, 1 insertion(+) create mode 100644 main_test.go diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/main_test.go @@ -0,0 +1 @@ +package main From b861be0181437bebb2365e9ff93c15f03211f378 Mon Sep 17 00:00:00 2001 From: Maikel Date: Wed, 18 Jun 2025 13:57:47 +0200 Subject: [PATCH 2/2] feat: init --- go.mod | 7 ++++ go.sum | 6 +++ main.go | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bf7d816 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/netail/pr-comments + +go 1.24.4 + +require github.com/google/go-github/v72 v72.0.0 + +require github.com/google/go-querystring v1.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0d81a57 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-github/v72 v72.0.0 h1:FcIO37BLoVPBO9igQQ6tStsv2asG4IPcYFi655PPvBM= +github.com/google/go-github/v72 v72.0.0/go.mod h1:WWtw8GMRiL62mvIquf1kO3onRHeWWKmK01qdCY8c5fg= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b8d172c --- /dev/null +++ b/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + "slices" + "strings" + + "github.com/google/go-github/v72/github" +) + +func main() { + ghToken := flag.String("token", "", "Github Token") + if len(*ghToken) == 0 { + fmt.Println("A GitHub token is required.") + os.Exit(1) + } + + prNumber := flag.Int("pr", 0, "Pull Request Number") + if *prNumber == 0 { + fmt.Println("A pull request number is required.") + os.Exit(1) + } + + repoOwner := flag.String("repoOwner", "", "Github Repo Owner") + if len(*repoOwner) == 0 { + fmt.Println("A repoOwner is required.") + os.Exit(1) + } + + repo := flag.String("repo", "", "Github Repo") + if len(*repo) == 0 { + fmt.Println("A repo is required.") + os.Exit(1) + } + + body := flag.String("body", "", "Body to send") + if len(*body) == 0 { + fmt.Println("A body is required.") + os.Exit(1) + } + + commentId := flag.Int64("comment-id", 0, "Comment ID") + bodyIncludes := flag.String("body-includes", "", "Comment ID") + + client := github.NewClient(nil).WithAuthToken(*ghToken) + + if *commentId == 0 && len(*bodyIncludes) > 0 { + *commentId = findComment(client, *repoOwner, *repo, *prNumber, *bodyIncludes) + } + + if *commentId == 0 { + createComment(client, *repoOwner, *repo, *prNumber, *body) + } else { + updateComment(client, *repoOwner, *repo, *commentId, *body) + } +} + +func createComment(client *github.Client, repoOwner string, repo string, prNumber int, comment string) { + _, _, err := client.Issues.CreateComment(context.Background(), repoOwner, repo, prNumber, &github.IssueComment{Body: github.Ptr(comment)}) + + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + fmt.Println("Comment created...") +} + +func updateComment(client *github.Client, repoOwner string, repo string, commentId int64, comment string) { + _, _, err := client.Issues.EditComment(context.Background(), repoOwner, repo, commentId, &github.IssueComment{Body: github.Ptr(comment)}) + + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + fmt.Println("Comment updated...") +} + +func findComment(client *github.Client, repoOwner string, repo string, prNumber int, bodyIncludes string) int64 { + comments, _, err := client.Issues.ListComments(context.Background(), repoOwner, repo, prNumber, &github.IssueListCommentsOptions{}) + + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + matchingComments := slices.Collect( + func(yield func(*github.IssueComment) bool) { + for _, comment := range comments { + if strings.Contains(comment.GetBody(), bodyIncludes) { + if !yield(comment) { + return + } + } + } + }, + ) + + if len(matchingComments) > 0 { + fmt.Println("Comment found...") + + return matchingComments[0].GetID() + } else { + fmt.Println("Comment not found...") + + return 0 + } +}