-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgithub_actions_v2.go
137 lines (121 loc) · 3.25 KB
/
github_actions_v2.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
package main
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
variant "github.com/mumoshu/variant/pkg"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"text/template"
)
var commentBody = "#### {{ .Name }}: `{{ .Command }}` completed with {{ .ExitStatus }}\n" +
"{{ if .Summary }}\n```\n{{ .Summary }}```\n{{ end -}}" +
"<details>\n\n" +
"```\n" +
"{{.Details}}" +
"```\n" +
"</details>\n"
func sendGitHubComment(name, command, exitstatus, summary, details string) (err error) {
defer func() {
if err != nil {
err = variant.NewInternalError(fmt.Errorf("unable to send a comment to GitHub Issue/PR: %v", err))
}
}()
data := map[string]string{
"Name": name,
"Command": command,
"ExitStatus": exitstatus,
"Summary": summary,
"Details": details,
}
tpl := template.New("comment")
tpl, err = tpl.Parse(commentBody)
if err != nil {
return err
}
buf := bytes.Buffer{}
if err = tpl.Execute(&buf, data); err != nil {
return fmt.Errorf("template: %v", err)
}
println("Trying to send a GitHub Issue/Pull request comment:")
println(buf.String())
if err = postGithubComment(buf.String()); err != nil {
return err
}
return nil
}
func postGithubComment(commentBody string) error {
u, err := getCommentsURL()
if err != nil {
return err
}
reqBody, err := json.Marshal(map[string]string{"body": commentBody})
req, err := http.NewRequest("POST", u, bytes.NewReader(reqBody))
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN")))
req.Header.Add("Content-Type", "application/json")
transport := &http.Transport{
TLSClientConfig: &tls.Config{},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "finished posting a github comment: code=%d, response=%s\n", resp.StatusCode, string(contents))
return nil
}
func getCommentsURL() (string, error) {
// See https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables
// This is usually /github/workflow/event.json
eventPath := os.Getenv("GITHUB_EVENT_PATH")
event, err := ioutil.ReadFile(eventPath)
if err != nil {
return "", fmt.Errorf("get comments URL: %v", err)
}
evt := struct {
PullRequest struct {
CommentsURL string `json:"comments_url"`
} `json:"pull_request"`
Issue struct {
CommentsURL string `json:"comments_url"`
} `json:"issue"`
}{}
if err := json.Unmarshal(event, &evt); err != nil {
return "", err
}
if evt.PullRequest.CommentsURL != "" {
return evt.PullRequest.CommentsURL, nil
}
if evt.Issue.CommentsURL != "" {
return evt.Issue.CommentsURL, nil
}
return "", fmt.Errorf("unable to detect issue comments URL in event.json: %s", string(event))
}
func linesScanner(reader io.Reader, log io.Writer) *bufio.Scanner {
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
buf := make([]byte, 1024)
scanner.Buffer(buf, bufio.MaxScanTokenSize)
return scanner
}
func charCount(str string, log io.Writer) int {
// Ignore ASCII color
p := regexp.MustCompile("\033" + `\[[^m]+m`)
str = p.ReplaceAllString(str, "")
return len(str)
}