From 809e8ee237ce1dfc718e0763ebd7e92ee4d25a4a Mon Sep 17 00:00:00 2001 From: okseby Date: Tue, 29 Aug 2023 09:17:58 -0400 Subject: [PATCH] Add checks for previous votes to prevent duplicate voting --- bot/utils.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bot/utils.go b/bot/utils.go index 7a5caa0..088acc3 100644 --- a/bot/utils.go +++ b/bot/utils.go @@ -22,6 +22,7 @@ var itr, _ = ghinstallation.New(http.DefaultTransport, 381312, 41105280, []byte( // Use installation transport with client. var client = github.NewClient(&http.Client{Transport: itr}) var ctx = context.Background() +var approvals map[int]string func initGitHubClient() { log.Println("Initializing......") @@ -136,7 +137,21 @@ func processIssueCommentEvent(event *github.IssueCommentEvent) { // Check if there are thumbs up (:+1:) reactions for _, comment := range comments { if strings.Contains(comment.GetBody(), "+1") && !strings.Contains(comment.GetUser().GetLogin(), "bot") { - reactionCount++ + value, exists := approvals[prNumber] + if !(exists && strings.Contains(value, comment.GetUser().GetLogin())) { + reactionCount++ + approvals[prNumber] = comment.GetUser().GetLogin() + } else { + // Respond with a comment + comment := &github.IssueComment{ + Body: github.String("Your vote has already been counted :x:"), + } + + _, _, err := client.Issues.CreateComment(ctx, owner, repo, prNumber, comment) + if err != nil { + log.Println("Error creating comment:", err) + } + } } }