diff options
-rw-r--r-- | gen.go | 55 | ||||
-rw-r--r-- | plan.html.tmpl | 1 |
2 files changed, 56 insertions, 0 deletions
@@ -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*<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 { 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 @@ <a href="#tag-{{ $tag }}">#{{ $tag }}</a> {{/* */}} {{- end }} </div> + <div class="contrib-submitted-at">{{ $contrib.SubmittedAt }}</div> <div class="contrib-desc">{{ $contrib.Desc | md2html }}</div> </article> {{- end }} |