-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
133 lines (116 loc) · 3.4 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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"github.com/muesli/beehive/bees"
_ "github.com/muesli/beehive/bees/githubbee"
_ "github.com/muesli/beehive/bees/hellobee"
"github.com/muesli/beehive/cfg"
"github.com/muesli/beehive/reactor"
"github.com/muesli/termenv"
_ "github.com/rubiojr/commit-monitor/bees/stdoutbee"
)
// Repositories to monitor (owner, repo name)
var repos = [][]string{
{"rails", "rails"},
{"torvalds", "linux"},
{"kubernetes", "kubernetes"},
{"prometheus", "prometheus"},
{"golang", "go"},
{"LineageOS", "android_frameworks_base"},
{"rubiojr", "hello"},
}
func main() {
// configuration boilerplate using the memory backend
// so we don't touch the filesystem.
config, err := cfg.New("mem://")
if err != nil {
panic(err)
}
config.Bees = []bees.BeeConfig{}
config.Chains = []bees.Chain{}
// the stdout bee prints to stdout via the
stdoutBee := newStdoutBee()
config.Bees = append(config.Bees, stdoutBee)
// Create the Action and add it to the config
// Every chain will re-use the same action to print commits to stdout
// using the 'print' action
action := bees.Action{}
action.ID = "print-to-stdout"
action.Bee = stdoutBee.Name
action.Name = "print"
action.Options = bees.Placeholders{
{
Name: "text",
// prints something like: ** New commit in owner/repo ** for every commit
Value: formatText(`{{ Bold (Foreground "#FF0000" "**") }}`) +
" New commit in {{.repo}} " +
formatText(`{{ Bold (Foreground "#FF0000" "**") }}`) +
"\n" +
"{{.message}}\n",
Type: "string",
},
}
config.Actions = []bees.Action{action}
// Iterate over all the repositories we want to monitor
// and create a new chain that will link the 'commit' event
// to the 'print-to-stdout' action.
for _, repo := range repos {
nwo := strings.Join(repo, "/") // owner/repository
// the GitHub bee is in charge of monitoring events
// for the given repository
bee := newGitHubBee(repo[0], repo[1])
config.Bees = append(config.Bees, bee)
// Create the event
event := bees.Event{}
event.Name = "commit"
event.Bee = bee.Name
// Create the chain and add it to the existing chains
chain := bees.Chain{}
chain.Name = "commits-" + nwo
chain.Description = "Print commits for " + nwo
chain.Actions = []string{action.ID} // Action to print the commit
chain.Event = &event
chain.Filters = []string{}
config.Chains = append(config.Chains, chain)
}
// Debugging level, prints debug messages from bees
// reactor.SetLogLevel(5)
reactor.Run(config)
}
func newGitHubBee(owner, repo string) bees.BeeConfig {
options := bees.BeeOptions{
bees.BeeOption{Name: "accesstoken", Value: os.Getenv("GITHUB_TOKEN")},
bees.BeeOption{Name: "owner", Value: owner},
bees.BeeOption{Name: "repository", Value: repo},
}
bc, err := bees.NewBeeConfig(owner+"-"+repo, "githubbee", fmt.Sprintf("monitor %s/%s commits", owner, repo), options)
if err != nil {
panic(err)
}
return bc
}
func newStdoutBee() bees.BeeConfig {
options := bees.BeeOptions{}
bc, err := bees.NewBeeConfig("stdout", "stdoutbee", "test", options)
if err != nil {
panic(err)
}
return bc
}
func formatText(text string) string {
// load template helpers
f := termenv.TemplateFuncs(termenv.ColorProfile())
tpl := template.New("tpl").Funcs(f)
// parse and render
tpl, err := tpl.Parse(text)
if err != nil {
panic(err)
}
var buf bytes.Buffer
tpl.Execute(&buf, nil)
return buf.String()
}