summaryrefslogtreecommitdiff
path: root/cmd/generate/src_contribs.go
blob: 6db676407fa122a5842d150747d41db77a1d93e0 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main

import (
	"fmt"
	"net/url"
	"os"
	"regexp"
	"strings"
	"time"

	"sigs.k8s.io/yaml"
)

type User struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

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"`
	LastUpdatedBy User      `json:"last-updated-by"`
	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, c.LastUpdatedBy, err = c.fetchLastUpdated()
		if err != nil {
			return err
		}
	}
	if c.Status == "" {
		c.Status, err = c.fetchStatus()
		if err != nil {
			return err
		}
	}
	c.StatusClass, err = classifyStatus(c.Status)
	if err != nil {
		return err
	}
	for _, u := range c.URLs {
		if m := reGoLangGerritCL.FindStringSubmatch(u); m != nil {
			c.URLs = append(c.URLs, "https://golang.org/cl/"+m[1])
		}
	}
	return nil
}

func classifyStatus(status string) (string, error) {
	switch {
	case strings.Contains(status, "released") || strings.Contains(status, "deployed"):
		return "released", nil
	case strings.Contains(status, "merged"):
		return "merged", nil
	case strings.Contains(status, "open"):
		return "open", nil
	case strings.Contains(status, "closed") || strings.Contains(status, "locked"):
		return "closed", nil
	default:
		return "", fmt.Errorf("unrecognized status string: %q", status)
	}
}

var (
	reGoLangGerritCL = regexp.MustCompile(`https://go-review\.googlesource\.com/c/[^/?#]+/\+/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
	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*$`)
)

const (
	statusOpen        = "open"
	statusMerged      = "merged, not yet in a release"
	statusReleasedFmt = "merged, released in %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"`
			MergeCommitSha string `json:"merge_commit_sha"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return "", err
		}
		ret := obj.State
		if obj.Merged {
			ret = statusMerged
			tag, err := getGitTagThatContainsAll("https://github.com/"+user+"/"+repo, obj.MergeCommitSha)
			if err != nil {
				return "", err
			}
			if tag != "" {
				ret = fmt.Sprintf(statusReleasedFmt, tag)
			}
		}

		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"`
			MergeCommitSha  string `json:"merge_commit_sha"`
			SquashCommitSha string `json:"squash_commit_sha"`
		}
		if err := httpGetJSON(urlStr, &obj); err != nil {
			return "", err
		}

		ret := obj.State
		if ret == "opened" {
			ret = statusOpen
		}

		if ret == "merged" {
			ret = statusMerged
			var mergeCommit string
			if obj.MergeCommitSha != "" {
				mergeCommit = obj.MergeCommitSha
			}
			if obj.SquashCommitSha != "" {
				mergeCommit = obj.SquashCommitSha
			}
			if mergeCommit != "" {
				tag, err := getGitTagThatContainsAll("https://"+authority+"/"+projectID+".git", mergeCommit)
				if err != nil {
					return "", err
				}
				if tag != "" {
					ret = fmt.Sprintf(statusReleasedFmt, tag)
				}
			}
		}

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

				gitURL = "https://github.com/" + user + "/" + repo
				gitCommits = append(gitCommits, hash)
			}
		}
		if len(gitCommits) > 0 {
			ret := statusMerged
			tag, err := getGitTagThatContainsAll(gitURL, gitCommits...)
			if err != nil {
				return "", err
			}
			if tag != "" {
				ret = fmt.Sprintf(statusReleasedFmt, tag)
			}
			return ret, 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) fetchLastUpdated() (time.Time, User, error) {
	if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil {
		user := m[1]
		repo := m[2]
		prnum := m[3]

		var obj struct {
			UpdatedAt time.Time `json:"updated_at"`
			MergedAt  time.Time `json:"merged_at"`
			MergedBy  struct {
				Login   string `json:"login"`
				HTMLURL string `json:"html_url"`
			} `json:"merged_by"`
		}
		if err := httpGetJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum, &obj); err != nil {
			return time.Time{}, User{}, err
		}

		retUpdatedAt := obj.UpdatedAt
		var retUser User

		if obj.MergedAt == retUpdatedAt {
			retUser.Name = obj.MergedBy.Login
			retUser.URL = obj.MergedBy.HTMLURL
		}
		if retUser == (User{}) {
			// "normal" comments
			var comments []struct {
				UpdatedAt time.Time `json:"updated_at"`
				User      struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"user"`
			}
			if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", &comments, githubPagination); err != nil {
				return time.Time{}, User{}, err
			}
			for _, comment := range comments {
				if comment.UpdatedAt == retUpdatedAt || comment.UpdatedAt.Add(1*time.Second) == retUpdatedAt {
					retUser.Name = comment.User.Login
					retUser.URL = comment.User.HTMLURL
					break
				}
			}
		}
		if retUser == (User{}) {
			// comments on a specific part of the diff
			var reviewComments []struct {
				UpdatedAt time.Time `json:"updated_at"`
				User      struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"user"`
			}
			if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", &reviewComments, githubPagination); err != nil {
				return time.Time{}, User{}, err
			}
			for _, comment := range reviewComments {
				if comment.UpdatedAt == retUpdatedAt {
					retUser.Name = comment.User.Login
					retUser.URL = comment.User.HTMLURL
					break
				}
			}
		}
		if retUser == (User{}) {
			var events []struct {
				CreatedAt time.Time `json:"created_at"`
				Actor     struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"actor"`
			}
			if err := httpGetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", &events); err != nil {
				return time.Time{}, User{}, err
			}
			for _, event := range events {
				if event.CreatedAt == retUpdatedAt {
					retUser.Name = event.Actor.Login
					retUser.URL = event.Actor.HTMLURL
					break
				}
			}
		}

		return retUpdatedAt, retUser, 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{}, User{}, err
		}
		return obj.UpdatedAt, User{}, 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{}, User{}, 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, User{}, nil
	}

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