forked from anirudhRowjee/bunsamosa-bot
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f717d93
commit 8cc0260
Showing
2 changed files
with
63 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ package handlers | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
|
@@ -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.
Sorry, something went wrong. |
||
comment = strings.TrimLeft(comment, " ") | ||
|
||
pattern := `^!bounty\s+(\d+)` | ||
// Compile the regular expression | ||
regex := regexp.MustCompile(pattern) | ||
This comment has been minimized.
Sorry, something went wrong.
anirudhRowjee
|
||
// 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 | ||
|
@@ -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) | ||
|
1 comment
on commit 8cc0260
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes #5
Please modify the function signature to return
(err, bool)
to keep with convention