From f5d74f69a285c944eac13e78f15f4c4be6134a21 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sat, 13 Apr 2024 16:44:58 -0600 Subject: wip dates --- gen.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ plan.html.tmpl | 1 + 2 files changed, 56 insertions(+) diff --git a/gen.go b/gen.go index dbddd3b..f82847e 100644 --- a/gen.go +++ b/gen.go @@ -3,12 +3,17 @@ 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" @@ -31,6 +36,56 @@ type Contribution struct { Desc string `json:"desc"` } +var ( + reGitHubPR = regexp.MustCompile(`^https://github.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`) + rePiperMailDate = regexp.MustCompile(`^\s*([^<]+)\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 { diff --git a/plan.html.tmpl b/plan.html.tmpl index 70f7720..54d9431 100644 --- a/plan.html.tmpl +++ b/plan.html.tmpl @@ -125,6 +125,7 @@ #{{ $tag }} {{/* */}} {{- end }} +
{{ $contrib.SubmittedAt }}
{{ $contrib.Desc | md2html }}
{{- end }} -- cgit v1.2.3-54-g00ecf