-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
103 lines (78 loc) · 2.65 KB
/
bot.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
package main
import (
"context"
"fmt"
"log"
"strconv"
"time"
"github.com/google/go-github/v35/github"
"github.com/jspc-bots/bottom"
"github.com/lrstanley/girc"
)
type Bot struct {
bottom bottom.Bottom
github *github.Client
}
type handlerFunc func(groups [][]byte) error
func New(user, password, server string, verify bool, gh *github.Client) (b Bot, err error) {
b.github = gh
b.bottom, err = bottom.New(user, password, server, verify)
if err != nil {
return
}
b.bottom.Client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
c.Cmd.Join(Chan)
})
router := bottom.NewRouter()
router.AddRoute(`run\s+([\w\-\_]+)\/([\w\-\_\.]+)\s+(\S+)\s+for\s+(\S+)`, b.trigger)
router.AddRoute(`run\s+([\w\-\_]+)\/([\w\-\_\.]+)\s+(\S+)$`, b.trigger)
b.bottom.Middlewares.Push(router)
return
}
func (b Bot) trigger(sender, channel string, groups []string) (err error) {
if len(groups) < 4 && len(groups) > 5 {
return fmt.Errorf("somehow ended up with %d groups, expected at least 3, at most 4", len(groups))
}
// groups[0] is the full string
owner := groups[1]
repo := groups[2]
workflow := groups[3]
var ref string
if len(groups) == 5 {
ref = groups[4]
} else {
ref = "main"
}
b.bottom.Client.Cmd.Messagef(channel, "Running workflow %q on %q/%q (ref: %q)", workflow, owner, repo, ref)
// Use an intermediate error here to avoid accidentally returning
// the wrong error
if w, scErr := strconv.Atoi(workflow); scErr == nil {
// Got a workflow ID, not a filename
_, err = b.github.Actions.CreateWorkflowDispatchEventByID(context.Background(), owner, repo, int64(w), github.CreateWorkflowDispatchEventRequest{Ref: ref})
return
}
_, err = b.github.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), owner, repo, workflow, github.CreateWorkflowDispatchEventRequest{Ref: ref})
if err != nil {
b.bottom.Client.Cmd.Messagef(channel, "I got this error from github :/ %v", err)
} else {
b.bottom.Client.Cmd.Message(channel, "Job triggered (seemingly)")
}
return
}
func (b Bot) processNotifications() (err error) {
log.Print("loading notifications")
notifications, _, err := b.github.Activity.ListNotifications(context.Background(), &github.NotificationListOptions{All: false})
if err != nil {
return
}
b.github.Activity.MarkNotificationsRead(context.Background(), time.Now())
log.Printf("loaded %d notifications", len(notifications))
for _, n := range notifications {
// We're only tracking CI activity rib.githubt now
if *n.Reason != "ci_activity" {
continue
}
b.bottom.Client.Cmd.Messagef(Chan, "%s: %q (see: %s/actions)", *n.Repository.FullName, *n.Subject.Title, *n.Repository.HTMLURL)
}
return
}