diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-04-13 16:44:58 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-04-13 16:44:58 -0600 |
commit | f5d74f69a285c944eac13e78f15f4c4be6134a21 (patch) | |
tree | 0fcc29852cd36becd3fb4199855482c3aaf588bf /gen.go | |
parent | b1d829c8dd7c115779710314dbda2b034835ff1e (diff) |
wip dates
Diffstat (limited to 'gen.go')
-rw-r--r-- | gen.go | 55 |
1 files changed, 55 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 { |