-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (131 loc) · 3.59 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
l3 "github.com/go-joe/joe"
"github.com/go-joe/redis-memory"
"github.com/go-joe/slack-adapter"
"github.com/joho/godotenv"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
)
type L337Bot struct {
*l3.Bot
}
type Jenkins struct {
Job Details `json:"task"`
}
type Details struct {
Url string `json:"url"`
}
func main() {
// load .env file
e := godotenv.Load()
if e != nil {
log.Fatal("Error loading .env file")
}
b := &L337Bot{
Bot: l3.New("L3-Bot",
redis.Memory("localhost:6379"),
slack.Adapter(os.Getenv("SLACK_TOKEN")),
),
}
b.Respond("ping", b.Pong)
b.Respond("run jenkins job (.+)", b.ExecuteJenkinsJob)
b.Respond("remember (.+) is (.+)", b.Remember)
b.Respond("what is (.+)", b.WhatIs)
b.Respond("show keys", b.ShowKeys)
err := b.Run()
if err != nil {
b.Logger.Fatal(err.Error())
}
}
func (b *L337Bot) Pong(msg l3.Message) error {
msg.Respond("PONG")
return nil
}
func (b *L337Bot) ExecuteJenkinsJob(msg l3.Message) error {
url := fmt.Sprintf("%s/job/%s/build", os.Getenv("JENKINS_URL"), msg.Matches[0])
res, err := b.sendJenkinsRequest("POST", url, "")
if err != nil {
return err
}
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != 201 {
errorStr := fmt.Sprintf("Did not receive successful response: %v", string(body))
return errors.New(errorStr)
}
jenkinsBuildInfoUrl := res.Header.Get("Location") + "api/json"
jobUrl, err := b.getJenkinsBuildInfo(jenkinsBuildInfoUrl)
msg.Respond("Successfully triggered jenkins job!\nJob url: %s", jobUrl)
return nil
}
func (b *L337Bot) sendJenkinsRequest(method string, url string, body string) (*http.Response, error) {
req, _ := http.NewRequest(method, url, bytes.NewBuffer([]byte(body)))
req.Header.Add("Authorization", "Basic "+b.Base64Encode(os.Getenv("JENKINS_USER"), os.Getenv("JENKINS_USER_TOKEN")))
req.Header.Set("Jenkins-Crumb", os.Getenv("JENKINS_CRUMB"))
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
errMsg := fmt.Sprintf("Error sending Jenkins API request: %v", e)
return nil, errors.New(errMsg)
}
return res, nil
}
func (b *L337Bot) getJenkinsBuildInfo(url string) (string, error) {
res, err := b.sendJenkinsRequest("GET", url, "")
if err != nil {
return "", err
}
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != 200 {
errorStr := fmt.Sprintf("Did not receive successful response: %v, %v", res.StatusCode, string(body))
return "", errors.New(errorStr)
}
s := new(Jenkins)
unmarshallError := json.Unmarshal(body, &s)
if unmarshallError != nil {
log.Errorf("Unmarshalling error: %v", unmarshallError)
return "", unmarshallError
}
return s.Job.Url, nil
}
func (b *L337Bot) Base64Encode(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func (b *L337Bot) Remember(msg l3.Message) error {
key, value := msg.Matches[0], msg.Matches[1]
msg.Respond("OK, I'll remember %s is %s", key, value)
return b.Store.Set(key, value)
}
func (b *L337Bot) WhatIs(msg l3.Message) error {
key := msg.Matches[0]
var value string
ok, err := b.Store.Get(key, &value)
if err != nil {
return errors.Wrapf(err, "failed to retrieve key %q from brain", key)
}
if ok {
msg.Respond("%s is %s", key, value)
} else {
msg.Respond("I do not remember %q", key)
}
return nil
}
func (b *L337Bot) ShowKeys(msg l3.Message) error {
keys, err := b.Store.Keys()
if err != nil {
return err
}
msg.Respond("I got %d keys:", len(keys))
for i, k := range keys {
msg.Respond("%d) %q", i+1, k)
}
return nil
}