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
|
package main
import (
"encoding"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
)
// httpGetGerritJSON is like [httpGetJSON], but
// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
func httpGetGerritJSON(u string, hdr map[string]string, out any) error {
str, err := httpGet(u, hdr)
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
}
type Gerrit struct{}
var _ Forge = Gerrit{}
func (Gerrit) FetchStatus(urls []string) (string, error) {
return "", nil
}
func (Gerrit) FetchSubmittedAt(urls []string) (time.Time, error) {
return time.Time{}, nil
}
var reGoLangGerritCL = regexp.MustCompile(`https://go-review\.googlesource\.com/c/([^/?#]+)/\+/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
func (Gerrit) FetchLastUpdated(urls []string) (time.Time, User, error) {
for _, u := range urls {
m := reGoLangGerritCL.FindStringSubmatch(u)
if m == nil {
continue
}
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, nil, &obj); err != nil {
return time.Time{}, User{}, err
}
retUpdatedAt := obj.Updated.Val
var retUser User
for _, message := range obj.Messages {
if withinOneSecond(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
}
return time.Time{}, User{}, nil
}
|