-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
150 lines (123 loc) Β· 3.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/ethanthatonekid/gitcord/gitcord"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"golang.org/x/oauth2"
)
func main() {
godotenv.Load(".env")
app := NewApp()
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
type App struct {
*cli.App
client *gitcord.Client
}
func NewApp() *App {
app := &App{}
app.App = &cli.App{
Name: "gitcord",
HelpName: "expand GitHub into Discord",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "force open threads",
},
},
Action: func(ctx *cli.Context) error {
channelID, err := discord.ParseSnowflake(os.Getenv("DISCORD_CHANNEL_ID"))
if err != nil {
return errors.Wrap(err, "failed to parse Discord channel ID")
}
colors, err := parseEnvColors()
if err != nil {
return err
}
config := gitcord.Config{
GitHubOAuth: oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: os.Getenv("GITHUB_TOKEN"),
}),
DiscordToken: "Bot " + os.Getenv("DISCORD_TOKEN"),
DiscordChannelID: discord.ChannelID(channelID),
ColorScheme: colors,
ForceOpen: ctx.Bool("force"),
Logger: log.Default(),
}
app.client = gitcord.NewClient(config).WithContext(ctx.Context)
eventIDStr := ctx.Args().First()
switch eventIDStr {
case "":
eventName := os.Getenv("GITHUB_EVENT_NAME")
if eventName == "" {
return errors.New("no github event name provided")
}
eventPayload := os.Getenv("GITHUB_EVENT_PAYLOAD")
if eventPayload == "" {
return errors.New("no github event payload provided")
}
return app.client.DoEventPayload(pascalFromSnake(eventName)+"Event", eventPayload)
default:
eventID, err := strconv.ParseInt(eventIDStr, 10, 64)
if err != nil {
return errors.Wrap(err, "failed to parse GitHub event ID")
}
return app.client.DoEventID(eventID)
}
},
}
return app
}
// colorEnvMap maps environment variable prefixes to their respective color
// scheme key.
var colorEnvMap = map[string]gitcord.ColorSchemeKey{
"GITCORD_COLOR_ISSUE_OPENED": gitcord.IssueOpened,
}
func parseEnvColors() (gitcord.ColorScheme, error) {
newScheme := gitcord.ColorScheme{}
for env, schemeKey := range colorEnvMap {
colors := gitcord.DefaultStatusColors
if err := parseColorEnv(env+"_SUCCESS", &colors.Success); err != nil {
return nil, err
}
if err := parseColorEnv(env+"_ERROR", &colors.Error); err != nil {
return nil, err
}
newScheme[schemeKey] = colors
}
return newScheme, nil
}
// parseColorEnv parses a color from an environment variable into dst. If the
// environment variable is not set, then dst is not modified.
func parseColorEnv(env string, dst *discord.Color) error {
val := os.Getenv(env)
if val == "" {
return nil
}
if !strings.HasPrefix(val, "#") {
return fmt.Errorf("$%s: invalid color must be of format #XXXXXX", env)
}
c, err := strconv.ParseInt(strings.TrimPrefix(val, "#"), 10, 32)
if err != nil {
return errors.Wrapf(err, "$%s: invalid color", env)
}
*dst = discord.Color(c)
return nil
}
var pascalFromSnakeRe = regexp.MustCompile(`(?m)(^|_)[a-z]`)
func pascalFromSnake(str string) string {
return pascalFromSnakeRe.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.TrimPrefix(s, "_"))
})
}