-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
138 lines (116 loc) · 3.26 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/gofiber/fiber"
flog "github.com/gofiber/logger"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
var slackURL string
var tektonDashboadUrl string
type slackMessage struct {
Blocks []slack.MessageBlockType
}
func main() {
versionFlag := flag.Bool("version", false, "print version information")
flag.Parse()
if *versionFlag {
fmt.Printf("(version=%s, branch=%s, gitcommit=%s)\n", Version, Branch, GitCommit)
fmt.Printf("(go=%s, user=%s, date=%s)\n", GoVersion, BuildUser, BuildDate)
os.Exit(0)
}
logger, _ := zap.NewProduction()
defer logger.Sync()
log := logger.Sugar()
log.Infow("Startup", "version", Version)
app := fiber.New()
app.Use(flog.New())
slackURL = os.Getenv("slackURL")
if slackURL == "" {
log.Fatalw("slackURL ENV value missing")
}
tektonDashboadUrl = os.Getenv("tektonDashboadUrl")
if tektonDashboadUrl == "" {
log.Fatalw("tektonDashboadUrl ENV value missing")
}
app.Get("/", func(c *fiber.Ctx) {
c.Send("cloudlog!")
})
app.Post("/inbox", func(c *fiber.Ctx) {
var message map[string]interface{}
if err := c.BodyParser(&message); err != nil {
log.Error(err)
c.Status(500).Send(err)
return
}
log.Infow("Got an inbox message!", "message", message)
cloudEvent, err := coerceCloudEvent(message)
if err != nil {
log.Error(err)
c.Status(500).Send(err)
return
}
if err := sendSlackMessage(cloudEvent, log); err != nil {
log.Error(err)
c.Status(500).Send(err)
return
}
c.JSON(message)
})
app.Listen(3000)
}
func coerceCloudEvent(message map[string]interface{}) (te *TektonCloudEvent, err error) {
var taskRun, status, condition map[string]interface{}
var conditions []interface{}
var ok bool
var podName, reason, cMessage string
if taskRun, ok = message["taskRun"].(map[string]interface{}); !ok {
return nil, errors.New("taskRun not found in message")
}
if status, ok = taskRun["status"].(map[string]interface{}); !ok {
return nil, errors.New("status not found in taskRun")
}
if podName, ok = status["podName"].(string); !ok {
return nil, errors.New("podName not found in status")
}
if conditions, ok = status["conditions"].([]interface{}); !ok {
return nil, errors.New("conditions not found in status")
}
if condition, ok = conditions[0].(map[string]interface{}); !ok {
fmt.Println("condition not found in conditions")
}
if reason, ok = condition["reason"].(string); !ok {
fmt.Println("reason not found in condition")
}
if cMessage, ok = condition["message"].(string); !ok {
fmt.Println("message not found in condition")
}
te = &TektonCloudEvent{
Title: podName + " " + reason,
Reason: reason,
Message: cMessage,
PodName: podName,
}
return te, nil
}
type TektonCloudEvent struct {
Title string `json:"string"`
Reason string `json:"reason"`
Message string `json:"message"`
PodName string `json:"pod_name"`
}
func sendSlackMessage(cloudEvent *TektonCloudEvent, log *zap.SugaredLogger) error {
text := fmt.Sprintf("*%s*\n\n```%s```\nPodName: %s/%s", cloudEvent.Title, cloudEvent.Message, tektonDashboadUrl, cloudEvent.PodName)
log.Infow("Sending slack message!", "text", text)
msg := slack.WebhookMessage{
Text: text,
}
err := slack.PostWebhook(slackURL, &msg)
if err != nil {
return err
}
return nil
}