Merge pull request #16 from rowbawts/test

Merging based on reactions
This commit is contained in:
openest-source-bot[bot] 2023-08-28 22:49:43 +00:00 committed by GitHub
commit 9f6c1f77f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,8 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"log" "log"
"net/http" "net/http"
"strconv"
"strings"
) )
// Wrap the shared transport for use with the integration ID and authenticating with installation ID. // Wrap the shared transport for use with the integration ID and authenticating with installation ID.
@ -38,7 +40,6 @@ func listenForWebhook() {
http.HandleFunc("/", webhookHandler) http.HandleFunc("/", webhookHandler)
err := http.ListenAndServe(":3333", nil) err := http.ListenAndServe(":3333", nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -49,6 +50,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
event, err := github.ParseWebHook(github.WebHookType(r), payload) event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil { if err != nil {
panic(err) panic(err)
@ -61,7 +63,10 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
processIssuesEvent(event) processIssuesEvent(event)
break break
case *github.IssueCommentEvent: case *github.IssueCommentEvent:
processIssueCommentEvent(event) if event.GetComment().GetUser().GetLogin() != "openest-source-bot[bot]" {
processIssueCommentEvent(event)
break
}
break break
case *github.PullRequestEvent: case *github.PullRequestEvent:
processPullRequestEvent(event) processPullRequestEvent(event)
@ -73,13 +78,19 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
} }
func processIssuesEvent(event *github.IssuesEvent) { func processIssuesEvent(event *github.IssuesEvent) {
owner := event.GetRepo().GetOwner().GetLogin()
repo := event.GetRepo().GetName()
issueNumber := event.GetIssue().GetNumber()
if event.GetAction() == "opened" { if event.GetAction() == "opened" {
commentText := "Thanks for opening this issue!"
// Respond with a comment // Respond with a comment
comment := &github.IssueComment{ comment := &github.IssueComment{
Body: github.String("Thanks for opening this issue!"), Body: github.String(commentText),
} }
_, _, err := client.Issues.CreateComment(ctx, event.GetRepo().GetOwner().GetLogin(), event.GetRepo().GetName(), event.GetIssue().GetNumber(), comment) _, _, err := client.Issues.CreateComment(ctx, owner, repo, issueNumber, comment)
if err != nil { if err != nil {
log.Println("Error creating comment:", err) log.Println("Error creating comment:", err)
} }
@ -91,9 +102,10 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) {
repo := event.GetRepo().GetName() repo := event.GetRepo().GetName()
prNumber := event.GetIssue().GetNumber() prNumber := event.GetIssue().GetNumber()
reactionCount := 0 reactionCount := 0
reactionCountGoal := 2
if event.GetIssue().IsPullRequest() { if event.GetIssue().IsPullRequest() {
comments, _, err := client.PullRequests.ListComments(ctx, owner, repo, prNumber, nil) comments, _, err := client.Issues.ListComments(ctx, owner, repo, prNumber, nil)
if err != nil { if err != nil {
log.Println("Error fetching reactions:", err) log.Println("Error fetching reactions:", err)
return return
@ -101,10 +113,10 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) {
// Check if there are thumbs up (:+1:) reactions // Check if there are thumbs up (:+1:) reactions
for _, comment := range comments { for _, comment := range comments {
if *comment.Body == "+1" { if strings.Contains(comment.GetBody(), "+1") {
reactionCount++ reactionCount++
if reactionCount >= 1 { if reactionCount >= reactionCountGoal {
// Merge the pull request // Merge the pull request
merge := &github.PullRequestOptions{ merge := &github.PullRequestOptions{
MergeMethod: "merge", // Change this as needed MergeMethod: "merge", // Change this as needed
@ -117,8 +129,21 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) {
log.Println("Pull request merged successfully") log.Println("Pull request merged successfully")
} }
reactionCount = 0
return return
} else {
commentText := "Current :+1: count is (#{reactionCount}) need (#{reactionRemainingCount}) more to merge"
commentText = strings.Replace(commentText, "(#{reactionCount})", strconv.Itoa(reactionCount), 1)
commentText = strings.Replace(commentText, "(#{reactionRemainingCount})", strconv.Itoa(reactionCountGoal-reactionCount), 1)
// Respond with a comment
comment := &github.IssueComment{
Body: github.String(commentText),
}
_, _, err := client.Issues.CreateComment(ctx, owner, repo, prNumber, comment)
if err != nil {
log.Println("Error creating comment:", err)
}
} }
} }
} }
@ -129,7 +154,7 @@ func processPullRequestEvent(event *github.PullRequestEvent) {
if event.GetAction() == "opened" || event.GetAction() == "reopened" { if event.GetAction() == "opened" || event.GetAction() == "reopened" {
// Respond with a comment // Respond with a comment
comment := &github.IssueComment{ comment := &github.IssueComment{
Body: github.String("React to this comment with :+1: to vote for getting it merged!"), Body: github.String("Comment on this PR with :+1: to vote for getting it merged!"),
} }
_, _, err := client.Issues.CreateComment(ctx, event.GetRepo().GetOwner().GetLogin(), event.GetRepo().GetName(), event.GetPullRequest().GetNumber(), comment) _, _, err := client.Issues.CreateComment(ctx, event.GetRepo().GetOwner().GetLogin(), event.GetRepo().GetName(), event.GetPullRequest().GetNumber(), comment)