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
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
112 changes: 112 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main