Repository navigation
Expand file tree
/
Copy pathgithub_slack.go
More file actions
193 lines (163 loc) · 5.74 KB
/
Copy pathgithub_slack.go
File metadata and controls
193 lines (163 loc) · 5.74 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Package notifiers provides Slack notification formatting for GitHub and
// Okta events.
package notifiers
import (
"context"
"fmt"
"github.com/cruxstack/github-ops-app/internal/errors"
"github.com/cruxstack/github-ops-app/internal/github"
"github.com/cruxstack/github-ops-app/internal/okta"
"github.com/slack-go/slack"
)
// NotifyPRBypass sends a Slack notification when branch protection is
// bypassed.
func (s *SlackNotifier) NotifyPRBypass(ctx context.Context, result *github.PRComplianceResult, repoFullName string) error {
if result.PR == nil {
return fmt.Errorf("%w: pr result missing", errors.ErrMissingPRData)
}
prURL := ""
prTitle := "unknown pr"
prNumber := 0
mergedBy := "unknown"
if result.PR.HTMLURL != nil {
prURL = *result.PR.HTMLURL
}
if result.PR.Title != nil {
prTitle = *result.PR.Title
}
if result.PR.Number != nil {
prNumber = *result.PR.Number
}
if result.PR.MergedBy != nil && result.PR.MergedBy.Login != nil {
mergedBy = *result.PR.MergedBy.Login
}
blocks := []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject("plain_text", "🚨 Branch Protection Bypassed", false, false),
),
slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*PR #%d*: %s", prNumber, prTitle), false, false),
nil, nil,
),
}
detailsFields := []*slack.TextBlockObject{
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*Repository*\n%s", repoFullName), false, false),
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*Branch*\n%s", result.BaseBranch), false, false),
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*Merged by*\n%s", mergedBy), false, false),
}
if result.UserHasBypass {
detailsFields = append(detailsFields,
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*User Permission*\n%s", result.UserBypassReason), false, false),
)
}
blocks = append(blocks, slack.NewSectionBlock(nil, detailsFields, nil))
if len(result.Violations) > 0 {
violationText := "*Violations:*\n"
for _, v := range result.Violations {
violationText += fmt.Sprintf("• %s\n", v.Description)
}
blocks = append(blocks, slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn", violationText, false, false),
nil, nil,
))
}
buttons := slack.NewActionBlock(
"actions",
slack.NewButtonBlockElement("view_pr", "view", slack.NewTextBlockObject("plain_text", "View PR", false, false)).WithStyle(slack.StylePrimary).WithURL(prURL),
)
blocks = append(blocks, buttons)
_, _, err := s.client.PostMessageContext(
ctx,
s.channel,
slack.MsgOptionBlocks(blocks...),
slack.MsgOptionText(fmt.Sprintf("branch protection bypassed on pr #%d", prNumber), false),
)
if err != nil {
return fmt.Errorf("failed to post pr bypass notification to slack: %w", err)
}
return nil
}
// NotifyOktaSync sends a Slack notification with Okta sync results.
func (s *SlackNotifier) NotifyOktaSync(ctx context.Context, reports []*okta.SyncReport) error {
if len(reports) == 0 {
return nil
}
blocks := []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject("plain_text", "✅ Okta Group Sync Complete", false, false),
),
}
for _, report := range reports {
sectionText := fmt.Sprintf("*Rule:* %s\n*Okta Group:* %s\n*GitHub Team:* %s", report.Rule, report.OktaGroup, report.GitHubTeam)
if len(report.MembersAdded) > 0 {
sectionText += fmt.Sprintf("\n*Added:* %d members", len(report.MembersAdded))
}
if len(report.MembersRemoved) > 0 {
sectionText += fmt.Sprintf("\n*Removed:* %d members", len(report.MembersRemoved))
}
if len(report.MembersSkippedExternal) > 0 {
sectionText += fmt.Sprintf("\n*Skipped (External):* %d members", len(report.MembersSkippedExternal))
}
if len(report.MembersSkippedNoGHUsername) > 0 {
sectionText += fmt.Sprintf("\n*Skipped (No GitHub Username):* %d members", len(report.MembersSkippedNoGHUsername))
}
if len(report.Errors) > 0 {
sectionText += fmt.Sprintf("\n*Errors:* %d", len(report.Errors))
}
blocks = append(blocks, slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn", sectionText, false, false),
nil, nil,
))
blocks = append(blocks, slack.NewDividerBlock())
}
_, _, err := s.client.PostMessageContext(
ctx,
s.channel,
slack.MsgOptionBlocks(blocks...),
slack.MsgOptionText("okta group sync completed", false),
)
if err != nil {
return fmt.Errorf("failed to post okta sync notification to slack: %w", err)
}
return nil
}
// NotifyOrphanedUsers sends a Slack notification about organization members
// not in any synced teams.
func (s *SlackNotifier) NotifyOrphanedUsers(ctx context.Context, report *okta.OrphanedUsersReport) error {
if report == nil || len(report.OrphanedUsers) == 0 {
return nil
}
blocks := []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject("plain_text", "⚠️ Orphaned GitHub Users Detected", false, false),
),
slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn",
fmt.Sprintf("Found *%d* organization member(s) not in any Okta-synced GitHub teams:", len(report.OrphanedUsers)),
false, false),
nil, nil,
),
}
userList := ""
for _, user := range report.OrphanedUsers {
userList += fmt.Sprintf("• `%s`\n", user)
}
blocks = append(blocks, slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn", userList, false, false),
nil, nil,
))
blocks = append(blocks, slack.NewContextBlock(
"context",
slack.NewTextBlockObject("mrkdwn", "_These users may need to be added to Okta groups or removed from the organization._", false, false),
))
_, _, err := s.client.PostMessageContext(
ctx,
s.channel,
slack.MsgOptionBlocks(blocks...),
slack.MsgOptionText(fmt.Sprintf("orphaned github users detected: %d users", len(report.OrphanedUsers)), false),
)
if err != nil {
return fmt.Errorf("failed to post orphaned users notification to slack: %w", err)
}
return nil
}