diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-05-18 23:10:05 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-05-19 01:30:41 -0600 |
commit | 8b92fbf0062e1b4d6b579f4918bc67929925676e (patch) | |
tree | 8855ae7e346b9e232a3d07be2136d88cd00b765f | |
parent | c0da2a0bdaf1caad67f67bccbd982e1d4ca0fdc4 (diff) |
imworkingon: Add Gerrit last-updated detection
-rw-r--r-- | cmd/generate/gerrit.go | 54 | ||||
-rw-r--r-- | cmd/generate/src_contribs.go | 41 |
2 files changed, 93 insertions, 2 deletions
diff --git a/cmd/generate/gerrit.go b/cmd/generate/gerrit.go new file mode 100644 index 0000000..c8837fc --- /dev/null +++ b/cmd/generate/gerrit.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding" + "encoding/json" + "fmt" + "strings" + "time" +) + +// httpGetGerritJSON is like [httpGetJSON], but +// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output +func httpGetGerritJSON(u string, out any) error { + str, err := httpGet(u) + if err != nil { + return err + } + if _, body, ok := strings.Cut(str, "\n"); ok { + str = body + } + return json.Unmarshal([]byte(str), out) +} + +const GerritTimeFormat = "2006-01-02 15:04:05.000000000" + +type GerritTime struct { + Val time.Time +} + +var ( + _ fmt.Stringer = GerritTime{} + _ encoding.TextMarshaler = GerritTime{} + _ encoding.TextUnmarshaler = (*GerritTime)(nil) +) + +// String implements [fmt.Stringer]. +func (t GerritTime) String() string { + return t.Val.Format(GerritTimeFormat) +} + +// MarshalText implements [encoding.TextMarshaler]. +func (t GerritTime) MarshalText() ([]byte, error) { + return []byte(t.String()), nil +} + +// UnmarshalText implements [encoding.TextUnmarshaler]. +func (t *GerritTime) UnmarshalText(data []byte) error { + val, err := time.Parse(GerritTimeFormat, string(data)) + if err != nil { + return err + } + t.Val = val + return nil +} diff --git a/cmd/generate/src_contribs.go b/cmd/generate/src_contribs.go index f22f721..eb9d9ad 100644 --- a/cmd/generate/src_contribs.go +++ b/cmd/generate/src_contribs.go @@ -75,7 +75,7 @@ func (c *Contribution) Fill() error { } for _, u := range c.URLs { if m := reGoLangGerritCL.FindStringSubmatch(u); m != nil { - c.URLs = append(c.URLs, "https://golang.org/cl/"+m[1]) + c.URLs = append(c.URLs, "https://golang.org/cl/"+m[2]) } } return nil @@ -97,7 +97,7 @@ func classifyStatus(status string) (string, error) { } var ( - reGoLangGerritCL = regexp.MustCompile(`https://go-review\.googlesource\.com/c/[^/?#]+/\+/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`) + 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]+)(?:\?[^#]*)?(?:#.*)?$`) @@ -259,6 +259,43 @@ func (c Contribution) fetchSubmittedAt() (time.Time, error) { } func (c Contribution) fetchLastUpdated() (time.Time, User, error) { + for _, u := range c.URLs { + if m := reGoLangGerritCL.FindStringSubmatch(u); m != nil { + projectID := m[1] + changeID := m[2] + + urlStr := "https://go-review.googlesource.com/changes/" + projectID + "~" + changeID + "?o=MESSAGES&o=DETAILED_ACCOUNTS" + + var obj struct { + Updated GerritTime `json:"updated"` + Messages []struct { + Author struct { + AccountID int `json:"_account_id"` + Name string `json:"name"` + DisplayName string `json:"display_name"` + } `json:"author"` + Date GerritTime `json:"date"` + } `json:"messages"` + } + if err := httpGetGerritJSON(urlStr, &obj); err != nil { + return time.Time{}, User{}, err + } + retUpdatedAt := obj.Updated.Val + var retUser User + for _, message := range obj.Messages { + if message.Date.Val == retUpdatedAt { + if message.Author.DisplayName != "" { + retUser.Name = message.Author.DisplayName + } else { + retUser.Name = message.Author.Name + } + retUser.URL = fmt.Sprintf("https://go-review.googlesource.com/dashboard/%d", message.Author.AccountID) + break + } + } + return retUpdatedAt, retUser, nil + } + } if m := reGitHubPR.FindStringSubmatch(c.URLs[0]); m != nil { user := m[1] repo := m[2] |