-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (161 loc) · 4.11 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
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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/antchfx/jsonquery"
)
func main() {
webhook := os.Getenv("webhook")
github_context := os.Getenv("github_context")
context, err := NewContext(webhook, github_context)
if err != nil {
log.Fatal(err)
}
message := buildMessage(context)
body := strings.NewReader(message)
_, err = http.Post(context.Webhook, "Content-type: application/json", body)
if err != nil {
log.Fatal(err)
}
}
type Context struct {
Webhook string
TriggeringEvent string
Branch string
Author string
Commit string
CommitUrl string
WorkflowName string
WorkflowUrl string
JobsUrl string
}
func NewContext(Webhook, github_context string) (*Context, error) {
jq, err := jsonquery.Parse(strings.NewReader(github_context))
if err != nil {
return nil, err
}
TriggeringEvent := jsonquery.FindOne(jq, "event/workflow_run/event").FirstChild.Data
Branch := jsonquery.FindOne(jq, "event/workflow_run/head_branch").FirstChild.Data
Author := jsonquery.FindOne(jq, "actor").FirstChild.Data
Commit := jsonquery.FindOne(jq, "sha").FirstChild.Data
repository := jsonquery.FindOne(jq, "repository").FirstChild.Data
CommitUrl := fmt.Sprintf("https://github.com/%s/commit/%s", repository, Commit)
WorkflowName := jsonquery.FindOne(jq, "event/workflow_run/name").FirstChild.Data
WorkflowUrl := jsonquery.FindOne(jq, "event/workflow_run/html_url").FirstChild.Data
JobsUrl := jsonquery.FindOne(jq, "event/workflow_run/jobs_url").FirstChild.Data
return &Context{
Webhook,
TriggeringEvent,
Branch,
Author,
Commit,
CommitUrl,
WorkflowName,
WorkflowUrl,
JobsUrl,
}, nil
}
func buildMessage(context *Context) string {
title := fmt.Sprintf("CI Failed For Branch: %s", context.Branch)
header := fmt.Sprintf(`{
"type" : "section",
"text" : {
"type": "mrkdwn",
"text": "*%s*\n %s"
}
},`, title, getMention(context))
section := buildSection(context)
message := fmt.Sprintf(`{"blocks" : [ %s ], "attachments":[{ "color": "#a60021", "blocks": [ %s ] }]}`, header, section)
return message
}
func buildSection(context *Context) string {
section := `{"type": "section", "fields":[`
failed_action := fmt.Sprintf(`
{
"type": "mrkdwn",
"text": "*Failed Action*\n%s"
},
`, context.WorkflowName)
section += failed_action
failed_job := fmt.Sprintf(`
{
"type": "mrkdwn",
"text": "*Failed Job*\n%s"
},
`, getFailedJob(context.JobsUrl))
section += failed_job
commit := fmt.Sprintf(`
{
"type": "mrkdwn",
"text": "*Commit*\n<%s|%s>"
},
`, context.CommitUrl, context.Commit[:6])
section += commit
action_url := fmt.Sprintf(`
{
"type": "mrkdwn",
"text": "*Workflow Url*\n%s"
}
`, context.WorkflowUrl)
section += action_url
section += `]}`
return section
}
func getMention(context *Context) string {
branch := strings.ToLower(context.Branch)
if branch == "main" || branch == "master" {
return "<!channel>"
} else if context.TriggeringEvent == "pull_request" || context.TriggeringEvent == "push" {
return getAuthorSlackID(context.Author)
}
return ""
}
func getAuthorSlackID(author string) string {
path := os.Getenv("users_path")
file, err := os.Open(path)
if err != nil {
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineArr := strings.Split(scanner.Text(), ":")
if len(lineArr) == 2 {
if lineArr[0] == author {
return fmt.Sprintf("<@%s>", lineArr[1])
}
}
}
os.Exit(0) // exit the program, to prevent any notification from unknown authors' PRs
return ""
}
func getFailedJob(url string) string {
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
return ""
}
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return ""
}
jq, err := jsonquery.Parse(strings.NewReader(string(bodyBytes)))
if err != nil {
fmt.Println(err)
return ""
}
jobs := jsonquery.FindOne(jq, "jobs").ChildNodes()
for _, job := range jobs {
status := job.SelectElement("conclusion").FirstChild.Data
if status == "failure" {
return job.SelectElement("name").FirstChild.Data
}
}
return ""
}