mirror of
https://github.com/thegeeklab/github-releases-notifier.git
synced 2024-11-14 08:00:40 +00:00
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
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/githubv4"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// Config of env and args
|
|
type Config struct {
|
|
GithubToken string `arg:"env:GITHUB_TOKEN,required"`
|
|
Interval time.Duration `arg:"env:INTERVAL"`
|
|
LogLevel string `arg:"env:LOG_LEVEL"`
|
|
Repositories []string `arg:"env:GITHUB_REPOS,-r,separate"`
|
|
SlackHook string `arg:"env:SLACK_HOOK,required"`
|
|
IgnorePre bool `arg:"env:IGNORE_PRE"`
|
|
}
|
|
|
|
// 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",
|
|
}
|
|
arg.MustParse(&c)
|
|
|
|
logger := log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
|
|
logger = log.With(logger,
|
|
"ts", log.DefaultTimestampUTC,
|
|
"caller", log.Caller(5),
|
|
)
|
|
|
|
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: githubv4.NewClient(client),
|
|
}
|
|
|
|
releases := make(chan Repository)
|
|
go checker.Run(c.Interval, c.Repositories, c.IgnorePre, releases)
|
|
|
|
slack := SlackSender{Hook: c.SlackHook}
|
|
|
|
level.Info(logger).Log("msg", "waiting for new releases")
|
|
for repository := range releases {
|
|
if err := slack.Send(repository); err != nil {
|
|
level.Warn(logger).Log(
|
|
"msg", "failed to send release to messenger",
|
|
"err", err,
|
|
)
|
|
continue
|
|
}
|
|
}
|
|
}
|