Skip to content

Commit

Permalink
parse pr numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 5, 2024
1 parent 7ff18e8 commit 1426a7a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions routes/github_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
markdownCheckBoxCheckedRegex = regexp.MustCompile(`([ \t]*)[*|-][ \t]{0,4}\[x][ \t]+([^\r\n]+)`)
markdownCheckBoxUncheckedRegex = regexp.MustCompile(`([ \t]*)[*|-][ \t]{0,4}\[ ][ \t]+([^\r\n]+)`)
prURLRegex = regexp.MustCompile(`https?://github\.com/(\w+/\w+)/pull/(\d+)`)
prNumberRegex = regexp.MustCompile(`#(\d+)`)
commitURLRegex = regexp.MustCompile(`https?://github\.com/\w+/\w+/commit/([a-f\d]{7})[a-f\d]+`)
mentionRegex = regexp.MustCompile(`@(\w+)`)
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func processReleaseEvent(b *lavalinkbot.Bot, e *github.ReleaseEvent) error {
b.Webhooks[fullName] = webhookClient
}

message := parseMarkdown(e.GetRelease().GetBody())
message := parseMarkdown(e.GetRelease().GetBody(), fullName)
if len(message) > 1024 {
message = substr(message, 0, 1024)
if index := strings.LastIndex(message, "\n"); index != -1 {
Expand Down Expand Up @@ -116,12 +117,13 @@ func substr(input string, start int, length int) string {
return string(asRunes[start : start+length])
}

func parseMarkdown(text string) string {
func parseMarkdown(text string, repo string) string {
text = markdownCheckBoxCheckedRegex.ReplaceAllString(text, "$1:ballot_box_with_check: $2")
text = markdownCheckBoxUncheckedRegex.ReplaceAllString(text, "$1:white_square_button: $2")
text = markdownHeaderRegex.ReplaceAllString(text, "**$1**")
text = markdownBulletRegex.ReplaceAllString(text, "$1• $2")
text = prURLRegex.ReplaceAllString(text, "[$1#$2]($0)")
text = prNumberRegex.ReplaceAllString(text, "[#$1](https://github.com/"+repo+"/pull/$1)")
text = commitURLRegex.ReplaceAllString(text, "[`$1`]($0)")
text = mentionRegex.ReplaceAllString(text, "[@$1](https://github.com/$1)")
return text
Expand Down

0 comments on commit 1426a7a

Please sign in to comment.