diff options
Diffstat (limited to 'gen.go')
-rw-r--r-- | gen.go | 189 |
1 files changed, 0 insertions, 189 deletions
@@ -1,189 +0,0 @@ -package main - -import ( - "bytes" - _ "embed" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "os" - "path" - "regexp" - "sigs.k8s.io/yaml" - "strings" - "time" - - "github.com/yuin/goldmark" - "html/template" -) - -type Contribs struct { - Tags map[string]TagInfo `json:"tags"` - Contributions []Contribution `json:"contributions"` -} - -type TagInfo struct { - PrettyName string `json:"prettyName"` - Desc string `json:"desc"` -} - -type Contribution struct { - URLs []string `json:"urls"` - Tags []string `json:"tags"` - SponsoredBy string `json:"sponsored-by"` - Desc string `json:"desc"` -} - -var ( - reGitHubPR = regexp.MustCompile(`^https://github.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`) - rePiperMailDate = regexp.MustCompile(`^\s*<I>([^<]+)</I>\s*$`) -) - -func (c Contribution) SubmittedAt() (time.Time, error) { - if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil { - user := m[1] - repo := m[2] - prnum := m[3] - resp, err := http.Get("https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum) - if err != nil { - return time.Time{}, err - } - if resp.StatusCode != http.StatusOK { - return time.Time{}, fmt.Errorf("unexpected HTTP status: %v", resp.Status) - } - jsonBytes, err := io.ReadAll(resp.Body) - if err != nil { - return time.Time{}, err - } - var obj struct { - CreatedAt time.Time `json:"created_at"` - } - if err := json.Unmarshal(jsonBytes, &obj); err != nil { - return time.Time{}, err - } - return obj.CreatedAt, nil - } - if strings.Contains(c.URLs[0], "/pipermail/") { - resp, err := http.Get(c.URLs[0]) - if err != nil { - return time.Time{}, err - } - if resp.StatusCode != http.StatusOK { - return time.Time{}, fmt.Errorf("unexpected HTTP status: %v", resp.Status) - } - htmlBytes, err := io.ReadAll(resp.Body) - if err != nil { - return time.Time{}, err - } - for _, line := range strings.Split(string(htmlBytes), "\n") { - if m := rePiperMailDate.FindStringSubmatch(line); m != nil { - return time.Parse(time.UnixDate, m[1]) - } - } - } - return time.Time{}, nil //fmt.Errorf("idk how to check") -} - -func ReadContribs(filename string) (Contribs, error) { - bs, err := os.ReadFile(filename) - if err != nil { - return Contribs{}, err - } - var ret Contribs - if err := yaml.UnmarshalStrict(bs, &ret); err != nil { - return Contribs{}, err - } - return ret, nil -} - -type Upstream struct { - URLs []string `json:"urls"` - Name string `json:"name"` - Desc string `json:"desc"` -} - -func ReadUpstreams(filename string) ([]Upstream, error) { - bs, err := os.ReadFile(filename) - if err != nil { - return nil, err - } - var ret []Upstream - if err := yaml.UnmarshalStrict(bs, &ret); err != nil { - return []Upstream{}, err - } - for i := range ret { - if ret[i].Name == "" { - u, err := url.Parse(ret[i].URLs[0]) - if err != nil { - return nil, err - } - _, ret[i].Name = path.Split(path.Clean(u.Path)) - } - } - return ret, nil -} - -func MarkdownToHTML(md string) (template.HTML, error) { - var html strings.Builder - if err := goldmark.Convert([]byte(md), &html); err != nil { - return template.HTML(""), err - } - return template.HTML(html.String()), nil -} - -var githubProjects = map[string]string{ - "flori/json": "ruby-json", -} - -func main() { - if err := mainWithError(); err != nil { - fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err) - os.Exit(1) - } -} - -//go:embed plan.html.tmpl -var embedPlanHTMLTmpl string - -func mainWithError() error { - contribs, err := ReadContribs("contribs.yml") - if err != nil { - return err - } - upstreams, err := ReadUpstreams("upstreams.yml") - if err != nil { - return err - } - tmpl := template.Must(template.New("plan.html"). - Funcs(template.FuncMap{ - "md2html": MarkdownToHTML, - "getUpstream": func(c Contribution) Upstream { - // First try any of the documented upstreams. - for _, cURL := range c.URLs { - for _, upstream := range upstreams { - for _, uURL := range upstream.URLs { - prefix := uURL - if !strings.HasSuffix(prefix, "/") { - prefix += "/" - } - if cURL == uURL || strings.HasPrefix(cURL, prefix) { - return upstream - } - } - } - } - return Upstream{URLs: []string{c.URLs[0]}, Name: "???"} - }, - }). - Parse(embedPlanHTMLTmpl)) - var out bytes.Buffer - if err := tmpl.Execute(&out, contribs); err != nil { - return err - } - if err := os.WriteFile("plan.html", out.Bytes(), 0666); err != nil { - return err - } - return nil -} |