forked from justwatchcom/github-releases-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (79 loc) · 2.09 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
package main
import (
"context"
"os"
"strings"
"time"
"github.com/alexflint/go-arg"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/joho/godotenv"
"github.com/shurcooL/githubql"
"golang.org/x/oauth2"
)
// Config of env and args
type Config struct {
GithubToken string `arg:"env:GITHUB_TOKEN"`
Interval time.Duration `arg:"env:INTERVAL"`
LogLevel string `arg:"env:LOG_LEVEL"`
Repositories []string `arg:"-r,separate"`
SlackHook string `arg:"env:SLACK_HOOK"`
Weaponized bool `arg:"env:WEAPONIZED"`
}
// Token returns an oauth2 token or an error.
func (c Config) Token() *oauth2.Token {
return &oauth2.Token{AccessToken: c.GithubToken}
}
func main() {
_ = godotenv.Load()
c := Config{
Interval: time.Hour,
LogLevel: "info",
Weaponized: true,
}
arg.MustParse(&c)
logger := log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.Caller(5),
)
level.SetKey("severity")
switch strings.ToLower(c.LogLevel) {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
case "warn":
logger = level.NewFilter(logger, level.AllowWarn())
case "error":
logger = level.NewFilter(logger, level.AllowError())
default:
logger = level.NewFilter(logger, level.AllowInfo())
}
tokenSource := oauth2.StaticTokenSource(c.Token())
client := oauth2.NewClient(context.Background(), tokenSource)
checker := &Checker{
logger: logger,
client: githubql.NewClient(client),
}
tags := make(chan Repository)
go checker.Run(c.Interval, c.Repositories, tags)
slack := SlackSender{Hook: c.SlackHook}
level.Info(logger).Log("msg", "waiting for new releases")
for repository := range tags {
if c.Weaponized {
if err := slack.Send(repository); err != nil {
level.Warn(logger).Log(
"msg", "failed to send release to messenger",
"err", err,
)
continue
}
} else {
level.Info(logger).Log(
"msg", "found new release",
"owner", repository.Owner,
"name", repository.Name,
"tag", repository.Tag.Name,
)
}
}
}