diff --git a/bot/utils.go b/bot/utils.go index efbb07a..508e666 100644 --- a/bot/utils.go +++ b/bot/utils.go @@ -83,11 +83,15 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) { processIssuesEvent(event) break case *github.IssueCommentEvent: - if !strings.Contains(event.GetComment().GetUser().GetLogin(), "bot") { + eventLogin := event.GetComment().GetUser().GetLogin() + commentBody := event.GetComment().GetBody() + + if !strings.Contains(eventLogin, "bot") && strings.Contains(commentBody, "+1") { log.Println("Received Issue Comment Event: processing now!") processIssueCommentEvent(event) break } + break case *github.PullRequestEvent: log.Println("Received Pull Request Event: processing now!") @@ -123,9 +127,10 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) { owner := event.GetRepo().GetOwner().GetLogin() repo := event.GetRepo().GetName() prNumber := event.GetIssue().GetNumber() - reactionCountGoal := 5 + eventSender := event.GetSender().GetLogin() - var approvals = map[string]string{} + reactionCountGoal := 5 + approvals := map[string]int{} if event.GetIssue().IsPullRequest() { comments, _, err := client.Issues.ListComments(ctx, owner, repo, prNumber, nil) @@ -138,25 +143,12 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) { for _, comment := range comments { commentAuthor := comment.GetUser().GetLogin() - if strings.Contains(comment.GetBody(), "+1") && !strings.Contains(commentAuthor, "bot") { + if !strings.Contains(commentAuthor, "bot") { _, exists := approvals[commentAuthor] if !exists { - approvals[commentAuthor] = commentAuthor + approvals[commentAuthor] = 1 } else { - commentText := "@(#{commentAuthor}) your vote has already been counted :x:" - commentText = strings.Replace(commentText, "(#{commentAuthor})", commentAuthor, 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) - } - - return + approvals[commentAuthor]++ } } } @@ -187,7 +179,7 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) { return } else { commentText := "Votes: (#{reactionCount})/(#{reactionCountGoal})" - commentText = strings.Replace(commentText, "(#{reactionCount})", strconv.Itoa(reactionCount), 1) + commentText = strings.Replace(commentText, "(#{reactionCount})", strconv.Itoa(len(approvals)), 1) commentText = strings.Replace(commentText, "(#{reactionCountGoal})", strconv.Itoa(reactionCountGoal), 1) // Respond with a comment @@ -200,17 +192,36 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) { log.Println("Error creating comment:", err) } } + + if approvals[eventSender] > 1 { + commentText := "@(#{commentAuthor}) your vote has already been counted :x:" + commentText = strings.Replace(commentText, "(#{commentAuthor})", eventSender, 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) + } + } } } func processPullRequestEvent(event *github.PullRequestEvent) { + owner := event.GetRepo().GetOwner().GetLogin() + repo := event.GetRepo().GetName() + prNumber := event.GetPullRequest().GetNumber() + if event.GetAction() == "opened" || event.GetAction() == "reopened" { // Respond with a comment comment := &github.IssueComment{ 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, owner, repo, prNumber, comment) if err != nil { log.Println("Error creating comment:", err) }