Skip to content

Commit

Permalink
Added Parsing for !bounty comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Smuzzy-waiii committed Oct 5, 2023
1 parent f717d93 commit 8cc0260
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
34 changes: 34 additions & 0 deletions handlers/WebHookHandlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handlers

import "testing"

type parseBountyTest struct {
comment string
valid bool
bounty int
}

var parseBountyTests = []parseBountyTest{
{comment: "!bounty 20", valid: true, bounty: 20},
{comment: " !bounty 20", valid: true, bounty: 20},
{comment: " !bounty 20\n", valid: true, bounty: 20},
{comment: "!bounty 30 @appy", valid: true, bounty: 30},
{comment: "!bounty", valid: false, bounty: -1},
{comment: "!bounty ", valid: false, bounty: -1},
{comment: "!bounty abcd", valid: false, bounty: -1},
}

func TestParseBountyPoints(t *testing.T) {
for _, test := range parseBountyTests {
bounty, valid := parseBountyPoints(test.comment)
if test.valid {
if !(valid && bounty == test.bounty) {
t.Errorf("Expected valid=true, Got valid=%v\nExpected bounty=%d, Got bounty=%d", test.valid, test.bounty, bounty)
}
} else {
if valid {
t.Errorf("Expected valid=false, Got valid=%v\nExpected bounty=%d, Got bounty=%d", test.valid, test.bounty, bounty)
}
}
}
}
40 changes: 29 additions & 11 deletions handlers/WebhookHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handlers

import (
"context"
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"strings"

Expand All @@ -23,6 +25,27 @@ func contains(s []string, str string) bool {
return false
}

// function to validate if PR bounty comment is in the correct format
// and get bounty points
func parseBountyPoints(comment string) (int, bool) {

This comment has been minimized.

Copy link
@anirudhRowjee

anirudhRowjee Oct 5, 2023

Please modify the function signature to return (err, bool) to keep with convention

comment = strings.TrimLeft(comment, " ")

pattern := `^!bounty\s+(\d+)`
// Compile the regular expression
regex := regexp.MustCompile(pattern)

This comment has been minimized.

Copy link
@anirudhRowjee

anirudhRowjee Oct 5, 2023

It's probably not a good idea to compile the regex every time: in a more low-latency scenario, we'll probably store the compiled regex as global state somewhere, but for right now this will do.

// Use FindStringSubmatch to search for the pattern in the text
matches := regex.FindStringSubmatch(comment)
//fmt.Println(matches)
if len(matches) > 0 {
// Extract the bounty number from the captured group
bounty := matches[1]
bounty_num, err := strconv.Atoi(bounty)
return bounty_num, err == nil
} else {
return -1, false
}
}

// Function to check if a URL is a Pull Request URL
func is_pull_request(url string) bool {
// Github Pull Request URLs are of the form
Expand Down Expand Up @@ -113,30 +136,25 @@ func newPRCommentHandler(parsed_hook *ghwebhooks.IssueCommentPayload) {
log.Printf("[PR_COMMENTHANDLER] Successfully Commented on Pull Request -> Repository [%s] PR (#%d)[%s]\n", parsed_hook.Repository.FullName, parsed_hook.Issue.Number, parsed_hook.Issue.Title)

// parse the comment here to give a bounty
comment_text_parts := strings.Split(parsed_hook.Comment.Body, " ")
if comment_text_parts[0] == "!bounty" {
bounty, valid := parseBountyPoints(parsed_hook.Comment.Body)

// Convert the points
points, err := strconv.Atoi(comment_text_parts[1])
if err != nil {
log.Println("[ERROR][BOUNTY] Invalid Points Assigned -> ", comment_text_parts[1])
}
if valid {

// Assign the bounty points
err = globals.Myapp.Dbmanager.AssignBounty(
err := globals.Myapp.Dbmanager.AssignBounty(
parsed_hook.Sender.Login,
parsed_hook.Issue.User.Login,
parsed_hook.Issue.PullRequest.HTMLURL,
points,
bounty,
)
if err != nil {
log.Println("[ERROR][BOUNTY] Could not assign bounty points ->", err)
return
}

log.Printf("[PR_COMMENTHANDLER] Successfully Assigned Bounty on Pull Request -> Repository [%s] PR (#%d)[%s] to user %s for %s points\n", parsed_hook.Repository.FullName, parsed_hook.Issue.Number, parsed_hook.Issue.Title, parsed_hook.Issue.User.Login, comment_text_parts[1])
log.Printf("[PR_COMMENTHANDLER] Successfully Assigned Bounty on Pull Request -> Repository [%s] PR (#%d)[%s] to user %s for %d points\n", parsed_hook.Repository.FullName, parsed_hook.Issue.Number, parsed_hook.Issue.Title, parsed_hook.Issue.User.Login, bounty)

response := "Assigned " + comment_text_parts[1] + " Bounty points to user @" + parsed_hook.Issue.User.Login + " !"
response := "Assigned " + fmt.Sprint(bounty) + " Bounty points to user @" + parsed_hook.Issue.User.Login + " !"
comment := v3.IssueComment{Body: &response}

_, _, new_err := globals.Myapp.RuntimeClient.Issues.CreateComment(context.TODO(), parsed_hook.Repository.Owner.Login, parsed_hook.Repository.Name, int(parsed_hook.Issue.Number), &comment)
Expand Down

1 comment on commit 8cc0260

@Smuzzy-waiii
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #5

Please sign in to comment.