summaryrefslogtreecommitdiff
path: root/cmd/generate/src_contribs.go
blob: 39bc04be36d934bab6629914ea017a1e2747a666 (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
package main

import (
	"fmt"
	"os"
	"strings"
	"time"

	"sigs.k8s.io/yaml"
)

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

type Contribution struct {
	ID          string
	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 := reGoogleGerritCL.FindStringSubmatch(u); m != nil && m[1] == "go-review.googlesource.com" {
			c.URLs = append(c.URLs, "https://golang.org/cl/"+m[3])
		}
	}
	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)
	}
}

const (
	statusOpen        = "open"
	statusMerged      = "merged, not yet in a release"
	statusReleasedFmt = "merged, released in %s"
)

type Forge interface {
	FetchStatus(urls []string) (string, error)
	FetchSubmittedAt(urls []string) (time.Time, error)
	FetchLastUpdated(urls []string) (time.Time, User, error)
}

var forges = []Forge{
	// highest precedence
	Gerrit{}, // must be higher than GitHub because of golang
	GitHub{},
	GitLab{},
	Forgejo{"codeberg.org"},
	PiperMail{},
	// lowest precedence
}

func (c Contribution) fetchStatus() (string, error) {
	for _, forge := range forges {
		status, err := forge.FetchStatus(c.URLs)
		if err != nil {
			return "", err
		}
		if status != "" {
			return status, nil
		}
	}
	return "", fmt.Errorf("idk how to get status for %q", c.URLs[0])
}

func (c Contribution) fetchSubmittedAt() (time.Time, error) {
	var ret time.Time
	for _, forge := range forges {
		submittedAt, err := forge.FetchSubmittedAt(c.URLs)
		if err != nil {
			return time.Time{}, err
		}
		if !submittedAt.IsZero() && (ret.IsZero() || submittedAt.Before(ret)) {
			ret = submittedAt
		}
	}
	if !ret.IsZero() {
		return ret, nil
	}
	return time.Time{}, fmt.Errorf("idk how to get created timestamp for %q", c.URLs[0])
}

func withinOneSecond(a, b time.Time) bool {
	d := a.Sub(b)
	if d < 0 {
		d = -d
	}
	return d <= time.Second
}

func (c Contribution) fetchLastUpdated() (time.Time, User, error) {
	var ret struct {
		time.Time
		User
	}
	for _, forge := range forges {
		updatedAt, updatedBy, err := forge.FetchLastUpdated(c.URLs)
		if err != nil {
			return time.Time{}, User{}, err
		}
		if !updatedAt.IsZero() && (ret.Time.IsZero() || updatedAt.After(ret.Time)) {
			ret.Time, ret.User = updatedAt, updatedBy
		}
	}
	if !ret.Time.IsZero() {
		return ret.Time, ret.User, nil
	}
	return time.Time{}, User{}, nil //fmt.Errorf("idk how to get updated timestamp for %q", c.URLs[0])
}