|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/antchfx/jsonquery" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + webhook := os.Getenv("webhook") |
| 17 | + github_context := os.Getenv("github_context") |
| 18 | + context, err := NewContext(webhook, github_context) |
| 19 | + if err != nil { |
| 20 | + log.Fatal(err) |
| 21 | + } |
| 22 | + message := buildMessage(context) |
| 23 | + body := strings.NewReader(message) |
| 24 | + _, err = http.Post(context.Webhook, "Content-type: application/json", body) |
| 25 | + if err != nil { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +type Context struct { |
| 31 | + Webhook string |
| 32 | + TriggeringEvent string |
| 33 | + Branch string |
| 34 | + Author string |
| 35 | + Commit string |
| 36 | + CommitUrl string |
| 37 | + WorkflowName string |
| 38 | + WorkflowUrl string |
| 39 | + JobsUrl string |
| 40 | +} |
| 41 | + |
| 42 | +func NewContext(Webhook, github_context string) (*Context, error) { |
| 43 | + jq, err := jsonquery.Parse(strings.NewReader(github_context)) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + TriggeringEvent := jsonquery.FindOne(jq, "event/workflow_run/event").FirstChild.Data |
| 49 | + Branch := jsonquery.FindOne(jq, "event/workflow_run/head_branch").FirstChild.Data |
| 50 | + Author := jsonquery.FindOne(jq, "actor").FirstChild.Data |
| 51 | + Commit := jsonquery.FindOne(jq, "sha").FirstChild.Data |
| 52 | + repository := jsonquery.FindOne(jq, "repository").FirstChild.Data |
| 53 | + CommitUrl := fmt.Sprintf("https://github.com/%s/commit/%s", repository, Commit) |
| 54 | + WorkflowName := jsonquery.FindOne(jq, "event/workflow_run/name").FirstChild.Data |
| 55 | + WorkflowUrl := jsonquery.FindOne(jq, "event/workflow_run/html_url").FirstChild.Data |
| 56 | + JobsUrl := jsonquery.FindOne(jq, "event/workflow_run/jobs_url").FirstChild.Data |
| 57 | + |
| 58 | + return &Context{ |
| 59 | + Webhook, |
| 60 | + TriggeringEvent, |
| 61 | + Branch, |
| 62 | + Author, |
| 63 | + Commit, |
| 64 | + CommitUrl, |
| 65 | + WorkflowName, |
| 66 | + WorkflowUrl, |
| 67 | + JobsUrl, |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +func buildMessage(context *Context) string { |
| 72 | + title := fmt.Sprintf("CI Failed For Branch: %s", context.Branch) |
| 73 | + header := fmt.Sprintf(`{ |
| 74 | + "type" : "section", |
| 75 | + "text" : { |
| 76 | + "type": "mrkdwn", |
| 77 | + "text": "*%s*\n %s" |
| 78 | + } |
| 79 | + },`, title, getMention(context)) |
| 80 | + section := buildSection(context) |
| 81 | + message := fmt.Sprintf(`{"blocks" : [ %s ], "attachments":[{ "color": "#a60021", "blocks": [ %s ] }]}`, header, section) |
| 82 | + return message |
| 83 | +} |
| 84 | + |
| 85 | +func buildSection(context *Context) string { |
| 86 | + section := `{"type": "section", "fields":[` |
| 87 | + |
| 88 | + failed_action := fmt.Sprintf(` |
| 89 | + { |
| 90 | + "type": "mrkdwn", |
| 91 | + "text": "*Failed Action*\n%s" |
| 92 | + }, |
| 93 | + `, context.WorkflowName) |
| 94 | + section += failed_action |
| 95 | + |
| 96 | + failed_job := fmt.Sprintf(` |
| 97 | + { |
| 98 | + "type": "mrkdwn", |
| 99 | + "text": "*Failed Job*\n%s" |
| 100 | + }, |
| 101 | + `, getFailedJob(context.JobsUrl)) |
| 102 | + section += failed_job |
| 103 | + |
| 104 | + commit := fmt.Sprintf(` |
| 105 | + { |
| 106 | + "type": "mrkdwn", |
| 107 | + "text": "*Commit*\n<%s|%s>" |
| 108 | + }, |
| 109 | + `, context.CommitUrl, context.Commit[:6]) |
| 110 | + section += commit |
| 111 | + |
| 112 | + action_url := fmt.Sprintf(` |
| 113 | + { |
| 114 | + "type": "mrkdwn", |
| 115 | + "text": "*Workflow Url*\n%s" |
| 116 | + } |
| 117 | + `, context.WorkflowUrl) |
| 118 | + section += action_url |
| 119 | + |
| 120 | + section += `]}` |
| 121 | + return section |
| 122 | +} |
| 123 | + |
| 124 | +func getMention(context *Context) string { |
| 125 | + branch := strings.ToLower(context.Branch) |
| 126 | + if branch == "main" || branch == "master" { |
| 127 | + return "<!channel>" |
| 128 | + } else if context.TriggeringEvent == "pull_request" || context.TriggeringEvent == "push" { |
| 129 | + return getAuthorSlackID(context.Author) |
| 130 | + } |
| 131 | + return "" |
| 132 | +} |
| 133 | + |
| 134 | +func getAuthorSlackID(author string) string { |
| 135 | + path := os.Getenv("users_path") |
| 136 | + file, err := os.Open(path) |
| 137 | + if err != nil { |
| 138 | + return "" |
| 139 | + } |
| 140 | + defer file.Close() |
| 141 | + scanner := bufio.NewScanner(file) |
| 142 | + for scanner.Scan() { |
| 143 | + lineArr := strings.Split(scanner.Text(), ":") |
| 144 | + if len(lineArr) == 2 { |
| 145 | + if lineArr[0] == author { |
| 146 | + return fmt.Sprintf("<@%s>", lineArr[1]) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + os.Exit(0) // exit the program, to prevent any notification from unknown authors' PRs |
| 151 | + return "" |
| 152 | +} |
| 153 | + |
| 154 | +func getFailedJob(url string) string { |
| 155 | + res, err := http.Get(url) |
| 156 | + if err != nil { |
| 157 | + fmt.Println(err) |
| 158 | + return "" |
| 159 | + } |
| 160 | + bodyBytes, err := io.ReadAll(res.Body) |
| 161 | + |
| 162 | + if err != nil { |
| 163 | + fmt.Println(err) |
| 164 | + return "" |
| 165 | + } |
| 166 | + |
| 167 | + jq, err := jsonquery.Parse(strings.NewReader(string(bodyBytes))) |
| 168 | + if err != nil { |
| 169 | + fmt.Println(err) |
| 170 | + return "" |
| 171 | + } |
| 172 | + |
| 173 | + jobs := jsonquery.FindOne(jq, "jobs").ChildNodes() |
| 174 | + for _, job := range jobs { |
| 175 | + status := job.SelectElement("conclusion").FirstChild.Data |
| 176 | + if status == "failure" { |
| 177 | + return job.SelectElement("name").FirstChild.Data |
| 178 | + } |
| 179 | + } |
| 180 | + return "" |
| 181 | +} |
0 commit comments