-
Notifications
You must be signed in to change notification settings - Fork 83
/
compile.go
122 lines (102 loc) · 3.43 KB
/
compile.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
// Copyright 2018 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//go:generate packr2
package summaries
import (
"bytes"
"fmt"
"net/url"
"strings"
"text/template"
mapset "github.com/deckarep/golang-set"
"github.com/gobuffalo/packr/v2"
"github.com/google/go-github/v47/github"
"github.com/web-platform-tests/wpt.fyi/shared"
)
// nolint:gochecknoglobals // TODO: Fix gochecknoglobals lint error
var templates *template.Template
func init() {
box := packr.New("markdown templates", "./templates/")
templates = template.New("all.md").
Funcs(template.FuncMap{
"escapeMD": escapeMD,
})
for _, t := range box.List() {
tmpl := templates.New(t)
body, err := box.FindString(t)
if err != nil {
panic(err)
} else if _, err = tmpl.Parse(body); err != nil {
panic(err)
}
}
}
// escapeMD returns the escaped MD equivalent of the plain text data s.
func escapeMD(s string) string {
return strings.Replace(s, `|`, `\|`, -1)
}
// Summary is the generic interface of a summary template data type.
type Summary interface {
// GetCheckState returns the info needed to update a check.
GetCheckState() CheckState
// GetActions returns the actions that can be taken by the user.
GetActions() []*github.CheckRunAction
// GetSummary compiles the summary markdown template.
GetSummary() (string, error)
}
// CheckState represents all the status fields for updating a check.
type CheckState struct {
HostName string // The host (e.g. wpt.fyi)
TestRun *shared.TestRun // The (completed) TestRun, if applicable.
Product shared.ProductSpec
HeadSHA string
DetailsURL *url.URL
// The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.)
Status string
// Can be one of "success", "failure", "neutral", "cancelled", "timed_out", or "action_required".
// (Optional. Required if you provide a status of "completed".)
Conclusion *string
Actions []github.CheckRunAction
PRNumbers []int
}
// Name returns the check run's name, based on the product.
func (c CheckState) Name() string {
host := c.HostName
if host == "" {
host = "wpt.fyi"
}
spec := shared.ProductSpec{} // nolint:exhaustruct // TODO: Fix exhaustruct lint error
spec.BrowserName = c.Product.BrowserName
if c.Product.IsExperimental() {
spec.Labels = mapset.NewSetWith(shared.ExperimentalLabel)
}
return fmt.Sprintf("%s - %s", host, spec.String())
}
// Title returns the check run's title, based on the product.
func (c CheckState) Title() string {
return fmt.Sprintf("%s results", c.Product.DisplayName())
}
// GetCheckState returns the info in the CheckState struct.
// It's a dumb placeholder since we can't define fields on interfaces.
func (c CheckState) GetCheckState() CheckState {
return c
}
// FileIssueURL returns a URL for filing an issue in wpt.fyi repo about checks.
func (c CheckState) FileIssueURL() *url.URL {
result, _ := url.Parse("https://github.com/web-platform-tests/wpt.fyi/issues/new")
q := result.Query()
q.Set("title", "Regression checks issue")
q.Set("projects", "web-platform-tests/wpt.fyi/6")
q.Set("template", "checks.md")
q.Set("labels", "bug")
result.RawQuery = q.Encode()
return result
}
func compile(i interface{}, t string) (string, error) {
var dest bytes.Buffer
if err := templates.ExecuteTemplate(&dest, t, i); err != nil {
return "", err
}
return dest.String(), nil
}