summaryrefslogtreecommitdiff
path: root/cmd/generate/src_contribs.go
blob: b47cf896cd047b7f30c6046607302c28a6016989 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main

import (
	"fmt"
	"net/url"
	"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"`
	LastUpdatedAt time.Time `json:"last-updated-at"`
	Status        string    `json:"status"`
	StatusClass   string    `json:"-"`
}

func ReadContribs(filename string) ([]Contribution, error) {
	bs, err := os.ReadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("contribs: %q: %w", filename, err)
	}
	var ret []Contribution
	if err := yaml.UnmarshalStrict(bs, &ret); err != nil {
		return nil, fmt.Errorf("contribs: %q: %w", filename, err)
	}
	for i := range ret {
		contrib := ret[i]
		if err := contrib.Fill(); err != nil {
			return nil, fmt.Errorf("contribs: %q: %w", filename, err)
		}
		ret[i] = contrib
	}
	return ret, nil
}

func (c *Contribution) Fill() error {
	var err error
	if c.SubmittedAt.IsZero() {
		c.SubmittedAt, err = c.fetchSubmittedAt()
		if err != nil {
			return err
		}
	}
	if c.LastUpdatedAt.IsZero() {
		c.LastUpdatedAt, err = c.fetchLastUpdatedAt()
		if err != nil {
			return err
		}
	}
	if c.Status == "" {
		c.Status, err = c.fetchStatus()
		if err != nil {
			return err
		}
	}
	switch {
	case strings.Contains(c.Status, "released") || strings.Contains(c.Status, "deployed"):
		c.StatusClass = "released"
	case strings.Contains(c.Status, "merged"):
		c.StatusClass = "merged"
	case strings.Contains(c.Status, "open"):
		c.StatusClass = "open"
	case strings.Contains(c.Status, "closed") || strings.Contains(c.Status, "locked"):
		c.StatusClass = "closed"
	default:
		return fmt.Errorf("unrecognized status string: %q", c.Status)
	}
	return nil
}

var (
	reGitHubPR      = regexp.MustCompile(`^https://github.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
	reGitHubCommit  = regexp.MustCompile(`^https://github.com/([^/?#]+)/([^/?#]+)/commit/([0-9a-f]+)(?:\?[^#]*)?(?:#.*)?$`)
	reGitLabMR      = regexp.MustCompile(`^https://([^/]+)/([^?#]+)/-/merge_requests/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
	rePiperMailDate = regexp.MustCompile(`^\s*<I>([^<]+)</I>\s*$`)
)

func (c Contribution) fetchStatus() (string, error) {
	if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil {
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			// State values are "open" and "closed".
			State  string `json:"state"`
			Merged bool   `json:"merged"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return "", err
		}
		ret := obj.State
		if obj.Merged {
			ret = "merged"
		}
		return ret, nil
	}
	if m := reGitLabMR.FindStringSubmatch(c.URLs[0]); m != nil {
		authority := m[1]
		projectID := m[2]
		mrnum := m[3]

		urlStr := "https://" + authority + "/api/v4/projects/" + url.QueryEscape(projectID) + "/merge_requests/" + mrnum

		var obj struct {
			// State values are "opened", "closed", "locked", and "merged".
			State string `json:"state"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return "", err
		}

		ret := obj.State
		if ret == "opened" {
			ret = "open"
		}
		return ret, nil
	}
	if len(c.URLs) > 1 {
		for _, u := range c.URLs[1:] {
			if reGitHubCommit.MatchString(u) {
				return "merged", nil
			}
		}
	}
	return "", fmt.Errorf("idk how to get status for %q", c.URLs[0])
}

func (c Contribution) fetchSubmittedAt() (time.Time, error) {
	if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil {
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			CreatedAt time.Time `json:"created_at"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return time.Time{}, err
		}
		return obj.CreatedAt, nil
	}
	if m := reGitLabMR.FindStringSubmatch(c.URLs[0]); m != nil {
		authority := m[1]
		projectID := m[2]
		mrnum := m[3]

		urlStr := "https://" + authority + "/api/v4/projects/" + url.QueryEscape(projectID) + "/merge_requests/" + mrnum

		var obj struct {
			CreatedAt time.Time `json:"created_at"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return time.Time{}, err
		}
		return obj.CreatedAt, nil
	}
	if strings.Contains(c.URLs[0], "/pipermail/") {
		htmlStr, err := httpGet(c.URLs[0])
		if err != nil {
			return time.Time{}, err
		}
		for _, line := range strings.Split(htmlStr, "\n") {
			if m := rePiperMailDate.FindStringSubmatch(line); m != nil {
				return time.Parse(time.UnixDate, m[1])
			}
		}
	}
	return time.Time{}, fmt.Errorf("idk how to get created timestamp for %q", c.URLs[0])
}

func (c Contribution) fetchLastUpdatedAt() (time.Time, error) {
	if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil {
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			UpdatedAt time.Time `json:"updated_at"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return time.Time{}, err
		}
		return obj.UpdatedAt, nil
	}
	if m := reGitLabMR.FindStringSubmatch(c.URLs[0]); m != nil {
		authority := m[1]
		projectID := m[2]
		mrnum := m[3]

		urlStr := "https://" + authority + "/api/v4/projects/" + url.QueryEscape(projectID) + "/merge_requests/" + mrnum

		var obj struct {
			UpdatedAt time.Time `json:"updated_at"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return time.Time{}, err
		}
		return obj.UpdatedAt, nil
	}

	var ret time.Time
	if len(c.URLs) > 1 {
		for _, u := range c.URLs[1:] {
			if m := reGitHubCommit.FindStringSubmatch(u); m != nil {
				user := m[1]
				repo := m[2]
				hash := m[3]

				urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/commits/" + hash
				var obj struct {
					Commit struct {
						Author struct {
							Date time.Time `json:"date"`
						} `json:"author"`
						Committer struct {
							Date time.Time `json:"date"`
						} `json:"committer"`
					} `json:"commit"`
				}
				if err := httpGetJSON(urlStr, &obj); err != nil {
					return time.Time{}, err
				}
				if obj.Commit.Author.Date.After(ret) {
					ret = obj.Commit.Author.Date
				}
				if obj.Commit.Committer.Date.After(ret) {
					ret = obj.Commit.Committer.Date
				}
			}
		}
	}
	if !ret.IsZero() {
		return ret, nil
	}

	return time.Time{}, nil //fmt.Errorf("idk how to get updated timestamp for %q", c.URLs[0])
}