Ok the code should work, there just isn't a WebHook for reactions on comments :( I guess I'll rework it at some point

This commit is contained in:
Sebastian Cabrera 2023-08-28 17:15:09 -04:00
parent 268caa3d52
commit ce7a9b9dae

View file

@ -7,7 +7,6 @@ import (
"golang.org/x/net/context"
"log"
"net/http"
"strings"
)
// Wrap the shared transport for use with the integration ID and authenticating with installation ID.
@ -61,12 +60,12 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
case *github.IssuesEvent:
processIssuesEvent(event)
break
case *github.IssueCommentEvent:
processIssueCommentEvent(event)
break
case *github.PullRequestEvent:
processPullRequestEvent(event)
break
case *github.PullRequestReviewCommentEvent:
processPullRequestReviewCommentEvent(event)
break
default:
fmt.Println("Unhandled Event!")
break
@ -87,6 +86,37 @@ func processIssuesEvent(event *github.IssuesEvent) {
}
}
func processIssueCommentEvent(event *github.IssueCommentEvent) {
owner := event.GetRepo().GetOwner().GetLogin()
repo := event.GetRepo().GetName()
prNumber := event.GetIssue().GetNumber()
if event.GetIssue().IsPullRequest() {
reactions, _, err := client.Reactions.ListIssueCommentReactions(ctx, owner, repo, event.GetComment().GetID(), nil)
if err != nil {
log.Println("Error fetching reactions:", err)
return
}
// Check if there are thumbs up (:+1:) reactions
for _, reaction := range reactions {
if *reaction.Content == "+1" {
// Merge the pull request
merge := &github.PullRequestOptions{
MergeMethod: "merge", // Change this as needed
}
_, _, err := client.PullRequests.Merge(ctx, owner, repo, prNumber, "Merging based on reactions", merge)
if err != nil {
log.Println("Error merging pull request:", err)
} else {
log.Println("Pull request merged successfully")
}
return
}
}
}
}
func processPullRequestEvent(event *github.PullRequestEvent) {
if event.GetAction() == "opened" || event.GetAction() == "reopened" {
// Respond with a comment
@ -100,36 +130,3 @@ func processPullRequestEvent(event *github.PullRequestEvent) {
}
}
}
func processPullRequestReviewCommentEvent(event *github.PullRequestReviewCommentEvent) {
if strings.HasPrefix(event.GetAction(), "created") {
// Get the pull request details
owner := event.GetRepo().GetOwner().GetLogin()
repo := event.GetRepo().GetName()
prNumber := event.GetPullRequest().GetNumber()
// Fetch the reactions for the comment
reactions, _, err := client.Reactions.ListIssueCommentReactions(context.Background(), owner, repo, event.GetComment().GetID(), nil)
if err != nil {
log.Println("Error fetching reactions:", err)
return
}
// Check if there are thumbs up (:+1:) reactions
for _, reaction := range reactions {
if *reaction.Content == "+1" {
// Merge the pull request
merge := &github.PullRequestOptions{
MergeMethod: "merge", // Change this as needed
}
_, _, err := client.PullRequests.Merge(context.Background(), owner, repo, prNumber, "Merging based on reactions", merge)
if err != nil {
log.Println("Error merging pull request:", err)
} else {
log.Println("Pull request merged successfully")
}
return
}
}
}
}