-
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
Showing
7 changed files
with
204 additions
and
18 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
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
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
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
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
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
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,129 @@ | ||
package routes | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/disgo/webhook" | ||
"github.com/google/go-github/v52/github" | ||
"github.com/lavalink-devs/lavalink-bot/lavalinkbot" | ||
"github.com/topi314/tint" | ||
) | ||
|
||
var ( | ||
markdownHeaderRegex = regexp.MustCompile(`[ \t]*#+[ \t]+([^\r\n]+)`) | ||
markdownBulletRegex = regexp.MustCompile(`([ \t]*)[*|-][ \t]+([^\r\n]+)`) | ||
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+)`) | ||
commitURLRegex = regexp.MustCompile(`https?://github\.com/\w+/\w+/commit/([a-f\d]{7})[a-f\d]+`) | ||
mentionRegex = regexp.MustCompile(`@(\w+)`) | ||
) | ||
|
||
func HandleGithubWebhook(b *lavalinkbot.Bot) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
payload, err := github.ValidatePayload(r, []byte(b.Cfg.GitHub.WebhookSecret)) | ||
if err != nil { | ||
slog.Error("Failed to validate payload", tint.Err(err)) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
event, err := github.ParseWebHook(github.WebHookType(r), payload) | ||
if err != nil { | ||
slog.Error("Failed to parse webhook", tint.Err(err)) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
switch e := event.(type) { | ||
case *github.ReleaseEvent: | ||
err = processReleaseEvent(b, e) | ||
} | ||
if err != nil { | ||
slog.Error("Failed to process webhook", tint.Err(err)) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func processReleaseEvent(b *lavalinkbot.Bot, e *github.ReleaseEvent) error { | ||
if e.GetAction() != "published" { | ||
return nil | ||
} | ||
|
||
repo := e.GetRepo().GetName() | ||
fullName := e.GetRepo().GetFullName() | ||
|
||
cfg, ok := b.Cfg.GitHub.Releases[fullName] | ||
if !ok { | ||
return errors.New("no config found for this repo") | ||
} | ||
|
||
webhookClient, ok := b.Webhooks[fullName] | ||
if !ok { | ||
webhookClient = webhook.New(cfg.WebhookID, cfg.WebhookToken) | ||
b.Webhooks[fullName] = webhookClient | ||
} | ||
|
||
message := parseMarkdown(e.GetRelease().GetBody()) | ||
if len(message) > 1024 { | ||
message = substr(message, 0, 1024) | ||
if index := strings.LastIndex(message, "\n"); index != -1 { | ||
message = message[:index] | ||
} | ||
message += "\n…" | ||
} | ||
|
||
msg, err := webhookClient.CreateMessage(discord.NewWebhookMessageCreateBuilder(). | ||
SetContent(discord.RoleMention(cfg.PingRole)). | ||
SetEmbeds(discord.NewEmbedBuilder(). | ||
SetAuthor( | ||
fmt.Sprintf("%s version %s has been released", repo, e.Release.GetTagName()), | ||
e.GetRelease().GetHTMLURL(), | ||
e.GetRepo().GetOwner().GetAvatarURL(), | ||
). | ||
SetDescription(message). | ||
SetColor(0xff624a). | ||
SetFooter("Release by "+e.GetRelease().GetAuthor().GetLogin(), e.GetRelease().GetAuthor().GetAvatarURL()). | ||
SetTimestamp(e.GetRelease().GetCreatedAt().Time). | ||
Build(), | ||
). | ||
SetAvatarURL(e.GetRepo().GetOwner().GetAvatarURL()). | ||
Build(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = b.Client.Rest().CrosspostMessage(msg.ChannelID, msg.ID) | ||
return err | ||
} | ||
|
||
func substr(input string, start int, length int) string { | ||
asRunes := []rune(input) | ||
|
||
if start >= len(asRunes) { | ||
return "" | ||
} | ||
|
||
if start+length > len(asRunes) { | ||
length = len(asRunes) - start | ||
} | ||
|
||
return string(asRunes[start : start+length]) | ||
} | ||
|
||
func parseMarkdown(text 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 = commitURLRegex.ReplaceAllString(text, "[`$1`]($0)") | ||
text = mentionRegex.ReplaceAllString(text, "[@$1](https://github.com/$1)") | ||
return text | ||
} |