/
handle_status.go
105 lines (89 loc) · 2.65 KB
/
handle_status.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
package gh
import (
"net/http"
"time"
"github.com/go-faster/errors"
"github.com/go-faster/sdk/zctx"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
"github.com/labstack/echo/v4"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type StatusWebhook struct {
Meta StatusMeta `json:"meta"`
Page StatusPage `json:"page"`
ComponentUpdate ComponentUpdate `json:"component_update"`
Component StatusComponent `json:"component"`
}
type StatusMeta struct {
Unsubscribe string `json:"unsubscribe"`
Documentation string `json:"documentation"`
}
type StatusPage struct {
ID string `json:"id"`
StatusIndicator string `json:"status_indicator"`
StatusDescription string `json:"status_description"`
}
type ComponentUpdate struct {
CreatedAt time.Time `json:"created_at"`
NewStatus string `json:"new_status"`
OldStatus string `json:"old_status"`
ID string `json:"id"`
ComponentID string `json:"component_id"`
}
type StatusComponent struct {
CreatedAt time.Time `json:"created_at"`
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
}
func formatStatus(s StatusWebhook) message.StyledTextOption {
formatter := func(eb *entity.Builder) error {
eb.Plain("GitHub ")
eb.Bold(s.Component.Name)
eb.Plain(" status changed to ")
eb.Bold(s.Component.Status)
return nil
}
return styling.Custom(formatter)
}
func (w *Webhook) handleStatus(c echo.Context) error {
// Handle Atlassian status webhook.
//
// See https://support.atlassian.com/statuspage/docs/enable-webhook-notifications/
// GitHub status page: https://www.githubstatus.com
ctx := c.Request().Context()
ctx, span := w.tracer.Start(ctx, "github.status")
defer span.End()
var s StatusWebhook
if err := c.Bind(&s); err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return errors.Wrap(err, "bind")
}
if s.Component.Name == "" {
// Incident update, ignoring.
zctx.From(ctx).Debug("Ignoring incident update")
return c.String(http.StatusOK, "ok")
}
span.AddEvent("StatusWebhook",
trace.WithAttributes(
attribute.String("component_name", s.Component.Name),
attribute.String("component_status", s.Component.Status),
),
)
// Notify telegram group.
p, err := w.notifyPeer(ctx)
if err != nil {
return errors.Wrap(err, "peer")
}
if _, err := w.sender.To(p).
NoWebpage().
StyledText(ctx, formatStatus(s)); err != nil {
return errors.Wrap(err, "send")
}
return c.String(http.StatusOK, "ok")
}