github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/checks/summaries/compile.go (about)

     1  // Copyright 2018 The WPT Dashboard Project. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  //go:generate packr2
     6  
     7  package summaries
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"net/url"
    13  	"strings"
    14  	"text/template"
    15  
    16  	mapset "github.com/deckarep/golang-set"
    17  	"github.com/gobuffalo/packr/v2"
    18  
    19  	"github.com/google/go-github/v47/github"
    20  	"github.com/web-platform-tests/wpt.fyi/shared"
    21  )
    22  
    23  // nolint:gochecknoglobals // TODO: Fix gochecknoglobals lint error
    24  var templates *template.Template
    25  
    26  func init() {
    27  	box := packr.New("markdown templates", "./templates/")
    28  	templates = template.New("all.md").
    29  		Funcs(template.FuncMap{
    30  			"escapeMD": escapeMD,
    31  		})
    32  	for _, t := range box.List() {
    33  		tmpl := templates.New(t)
    34  		body, err := box.FindString(t)
    35  		if err != nil {
    36  			panic(err)
    37  		} else if _, err = tmpl.Parse(body); err != nil {
    38  			panic(err)
    39  		}
    40  	}
    41  }
    42  
    43  // escapeMD returns the escaped MD equivalent of the plain text data s.
    44  func escapeMD(s string) string {
    45  	return strings.Replace(s, `|`, `\|`, -1)
    46  }
    47  
    48  // Summary is the generic interface of a summary template data type.
    49  type Summary interface {
    50  	// GetCheckState returns the info needed to update a check.
    51  	GetCheckState() CheckState
    52  
    53  	// GetActions returns the actions that can be taken by the user.
    54  	GetActions() []*github.CheckRunAction
    55  
    56  	// GetSummary compiles the summary markdown template.
    57  	GetSummary() (string, error)
    58  }
    59  
    60  // CheckState represents all the status fields for updating a check.
    61  type CheckState struct {
    62  	HostName   string          // The host (e.g. wpt.fyi)
    63  	TestRun    *shared.TestRun // The (completed) TestRun, if applicable.
    64  	Product    shared.ProductSpec
    65  	HeadSHA    string
    66  	DetailsURL *url.URL
    67  	// The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.)
    68  	Status string
    69  	// Can be one of "success", "failure", "neutral", "cancelled", "timed_out", or "action_required".
    70  	// (Optional. Required if you provide a status of "completed".)
    71  	Conclusion *string
    72  	Actions    []github.CheckRunAction
    73  	PRNumbers  []int
    74  }
    75  
    76  // Name returns the check run's name, based on the product.
    77  func (c CheckState) Name() string {
    78  	host := c.HostName
    79  	if host == "" {
    80  		host = "wpt.fyi"
    81  	}
    82  	spec := shared.ProductSpec{} // nolint:exhaustruct // TODO: Fix exhaustruct lint error
    83  	spec.BrowserName = c.Product.BrowserName
    84  	if c.Product.IsExperimental() {
    85  		spec.Labels = mapset.NewSetWith(shared.ExperimentalLabel)
    86  	}
    87  
    88  	return fmt.Sprintf("%s - %s", host, spec.String())
    89  }
    90  
    91  // Title returns the check run's title, based on the product.
    92  func (c CheckState) Title() string {
    93  	return fmt.Sprintf("%s results", c.Product.DisplayName())
    94  }
    95  
    96  // GetCheckState returns the info in the CheckState struct.
    97  // It's a dumb placeholder since we can't define fields on interfaces.
    98  func (c CheckState) GetCheckState() CheckState {
    99  	return c
   100  }
   101  
   102  // FileIssueURL returns a URL for filing an issue in wpt.fyi repo about checks.
   103  func (c CheckState) FileIssueURL() *url.URL {
   104  	result, _ := url.Parse("https://github.com/web-platform-tests/wpt.fyi/issues/new")
   105  	q := result.Query()
   106  	q.Set("title", "Regression checks issue")
   107  	q.Set("projects", "web-platform-tests/wpt.fyi/6")
   108  	q.Set("template", "checks.md")
   109  	q.Set("labels", "bug")
   110  	result.RawQuery = q.Encode()
   111  
   112  	return result
   113  }
   114  
   115  func compile(i interface{}, t string) (string, error) {
   116  	var dest bytes.Buffer
   117  	if err := templates.ExecuteTemplate(&dest, t, i); err != nil {
   118  		return "", err
   119  	}
   120  
   121  	return dest.String(), nil
   122  }