-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (120 loc) · 3.34 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
151
152
153
154
155
156
package main
import (
"errors"
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/go-playground/webhooks/v6/github"
"gopkg.in/yaml.v2"
"net/http"
"os"
"strconv"
)
const (
path = "/webhooks"
)
type GitgudConfig struct {
Server struct {
Port int `yaml:"port"`
Path string `yaml:"path"`
} `yaml:"server"`
Discord struct {
Secret string `yaml:"secret"`
Channel string `yaml:"channel"`
} `yaml:"discord"`
Github struct {
Secret string `yaml:"secret"`
} `yaml:"github"`
}
func sendDiscordMessage(s *discordgo.Session, channel string, Embed *discordgo.MessageEmbed) (*discordgo.Message, error) {
return s.ChannelMessageSendEmbed(channel, Embed)
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
s.UpdateGameStatus(0, "Creepin' on your codebase")
}
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
}
func readConfigFile() (*GitgudConfig, error) {
var configFile string
flag.StringVar(&configFile, "config", "./config.yml", "The path to a valid config file.")
flag.Parse()
s, err := os.Stat(configFile)
if nil != err {
return nil, err
}
if s.IsDir() {
return nil, errors.New("file given was a directory")
}
config := &GitgudConfig{}
file, fileErr := os.OpenFile(configFile, os.O_RDONLY, 0644)
if fileErr != nil {
return nil, fileErr
}
defer file.Close()
yamlDecode := yaml.NewDecoder(file)
if yamlErr := yamlDecode.Decode(&config); yamlErr != nil {
return nil, yamlErr
}
if len(config.Server.Path) == 0 {
config.Server.Path = "/webhooks"
}
if config.Server.Port == 0 {
config.Server.Port = 3412
}
if len(config.Discord.Secret) == 0 {
return nil, errors.New("config file was missing a Discord secret")
}
if len(config.Discord.Channel) == 0 {
return nil, errors.New("config file was missing a Discord channel")
}
if len(config.Github.Secret) == 0 {
return nil, errors.New("config file was missing a Github secret")
}
return config, nil
}
func main() {
config, err := readConfigFile()
if err != nil {
println(err)
os.Exit(1)
}
discord, err := discordgo.New(config.Discord.Secret)
if err != nil {
fmt.Println("error creating Discord session: ", err)
return
}
discord.AddHandler(ready)
discord.AddHandler(guildCreate)
discord.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages
err = discord.Open()
if err != nil {
fmt.Println("error opening Discord session: ", err)
}
hook, _ := github.New(github.Options.Secret(config.Github.Secret))
http.HandleFunc(config.Server.Path, func(w http.ResponseWriter, r *http.Request) {
payload, err := hook.Parse(r, github.PushEvent, github.IssuesEvent)
if err != nil {
if err == github.ErrEventNotFound {
// ok event wasn;t one of the ones asked to be parsed
}
}
switch payload.(type) {
case github.PushPayload:
PushMessage, _ := messageForGithubPush(payload.(github.PushPayload))
if PushMessage != nil {
fmt.Println("Sent a push summary!")
sendDiscordMessage(discord, config.Discord.Channel, PushMessage)
}
break
case github.IssuesPayload:
IssueMessage, _ := messageForGithubIssue(payload.(github.IssuesPayload))
if IssueMessage != nil {
fmt.Println("Sent an issue summary!")
sendDiscordMessage(discord, config.Discord.Channel, IssueMessage)
}
break
}
})
http.ListenAndServe(":"+strconv.Itoa(config.Server.Port), nil)
discord.Close()
}