-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmarkdown.go
152 lines (138 loc) · 3.4 KB
/
markdown.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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
"golang.org/x/tools/cover"
)
var reportTmp = template.Must(template.New("report").Parse(`
# go-cover-view
{{range .}}
<details> <summary> {{.FileName}} </summary>
{{.Report}}
</details>
{{end}}
`))
type markdownRenderer struct{}
var _ renderer = (*markdownRenderer)(nil)
func (r *markdownRenderer) Render(w io.Writer, profiles []*cover.Profile, path string) error {
results, err := getMarkdownReports(profiles, path)
if err != nil {
return err
}
return reportTmp.ExecuteTemplate(w, "report", results)
}
type markdownReport struct {
FileName string
Report string
}
func newMarkdownReport(fileName string, lines []string) *markdownReport {
return &markdownReport{
FileName: fileName,
Report: buildReport(lines),
}
}
func getMarkdownReports(profiles []*cover.Profile, path string) ([]*markdownReport, error) {
diffs, err := getDiffs()
if err != nil {
return nil, err
}
reports := make([]*markdownReport, 0, len(profiles))
for _, profile := range profiles {
lines, err := getLines(profile, path)
if err != nil {
return nil, err
}
if gitDiffOnly {
if containsDiff(profile.FileName, path, diffs) {
reports = append(reports, newMarkdownReport(profile.FileName, lines))
}
continue
}
reports = append(reports, newMarkdownReport(profile.FileName, lines))
}
return reports, nil
}
func buildReport(lines []string) string {
var b strings.Builder
fmt.Fprintln(&b)
fmt.Fprintln(&b, "```")
for _, line := range lines {
fmt.Fprintln(&b, line)
}
fmt.Fprintln(&b)
fmt.Fprintln(&b, "```")
return b.String()
}
func upsertGitHubPullRequestComment(profiles []*cover.Profile, path string) error {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
if eventPath == "" {
return errors.New("env: GITHUB_EVENT_PATH is missing")
}
f, err := os.Open(eventPath)
if err != nil {
return err
}
defer f.Close()
var event github.PullRequestEvent
if err := json.NewDecoder(f).Decode(&event); err != nil {
return err
}
ctx := context.Background()
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return errors.New("env: GITHUB_TOKEN is missing")
}
var buf bytes.Buffer
r := &markdownRenderer{}
if err := r.Render(&buf, profiles, path); err != nil {
return err
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(ctx, ts)
gc := github.NewClient(httpClient)
pr := event.GetPullRequest()
_repo := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
if len(_repo) != 2 {
return fmt.Errorf("invalid env: GITHUB_REPOSITORY=%v", _repo)
}
owner := _repo[0]
repo := _repo[1]
comments, _, err := gc.Issues.ListComments(ctx, owner, repo, pr.GetNumber(), nil)
if err != nil {
return err
}
var commentID int64
for _, c := range comments {
u := c.GetUser()
if u.GetLogin() == "github-actions[bot]" && u.GetType() == "Bot" && strings.Contains(c.GetBody(), "# go-cover-view") {
commentID = c.GetID()
break
}
}
body := buf.String()
if commentID == 0 {
_, _, err := gc.Issues.CreateComment(ctx, owner, repo, pr.GetNumber(), &github.IssueComment{
Body: &body,
})
if err != nil {
return err
}
} else {
_, _, err := gc.Issues.EditComment(ctx, owner, repo, commentID, &github.IssueComment{
Body: &body,
})
if err != nil {
return err
}
}
return nil
}