-
Notifications
You must be signed in to change notification settings - Fork 0
/
reviewbot.go
93 lines (72 loc) · 1.51 KB
/
reviewbot.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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/containrrr/shoutrrr"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/rickar/cal/v2"
"github.com/rickar/cal/v2/dk"
"github.com/robfig/cron"
)
func main() {
c := cron.New()
_ = c.AddFunc("0 45 8,10,12,14 * * *", run)
c.Start()
select {}
}
func run() {
c := workCalendar()
if !c.IsWorkday(time.Now()) {
return
}
edges, count, err := reviewRequests()
if err != nil {
panic(err)
}
if count <= 0 {
return
}
str, err := format(edges, count)
if err != nil {
panic(err)
}
err = send(str)
if err != nil {
panic(err)
}
}
func workCalendar() *cal.BusinessCalendar {
c := cal.NewBusinessCalendar()
c.AddHoliday(dk.Holidays...)
//nolint:exhaustivestruct
c.AddHoliday(&cal.Holiday{
Month: time.December,
Day: 24, //nolint:gomnd
Func: cal.CalcDayOfMonth,
})
//nolint:exhaustivestruct
c.AddHoliday(&cal.Holiday{
Month: time.December,
Day: 31, //nolint:gomnd
Func: cal.CalcDayOfMonth,
})
return c
}
func send(message string) error {
services := os.Getenv("NOTIFY")
notify, err := shoutrrr.CreateSender(strings.Split(services, ",")...)
if err != nil {
return fmt.Errorf("error creating notification sender(s): %w", err)
}
t := time.Now()
params := types.Params{
"topic": fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day()),
}
errs := notify.Send(message, ¶ms)
if len(errs) > 0 {
return fmt.Errorf("error creating notification sender(s): %v", errs) //nolint:goerr113
}
return nil
}