1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"time"
"sigs.k8s.io/yaml"
)
type Contribution struct {
URLs []string `json:"urls"`
Tags []string `json:"tags"`
SponsoredBy string `json:"sponsored-by"`
Desc string `json:"desc"`
SubmittedAt time.Time `json:"submitted-at"`
}
func ReadContribs(filename string) ([]Contribution, error) {
bs, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var ret []Contribution
if err := yaml.UnmarshalStrict(bs, &ret); err != nil {
return nil, err
}
for i := range ret {
contrib := ret[i]
if err := contrib.Fill(); err != nil {
return nil, err
}
ret[i] = contrib
}
return ret, nil
}
func (c *Contribution) Fill() error {
var err error
if c.SubmittedAt.IsZero() {
c.SubmittedAt, err = c.getSubmittedAt()
if err != nil {
return err
}
}
return nil
}
var (
reGitHubPR = regexp.MustCompile(`^https://github.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
rePiperMailDate = regexp.MustCompile(`^\s*<I>([^<]+)</I>\s*$`)
)
func (c Contribution) getSubmittedAt() (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{}, fmt.Errorf("idk how to get timestamps for %q", c.URLs[0])
}
|