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
|
package main
import (
"encoding"
"encoding/json"
"fmt"
"net/url"
"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{}
var reGoogleGerritCL = regexp.MustCompile(`https://([a-z]+-review\.googlesource\.com)/c/([^?#]+)/\+/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)
func (Gerrit) FetchStatus(urls []string) (string, error) {
return fetchPerURLStatus(urls, func(u string) (string, error) {
m := reGoogleGerritCL.FindStringSubmatch(u)
if m == nil {
return "", nil
}
authority := m[1]
projectID := m[2]
changeID := m[3]
urlStr := "https://" + authority + "/changes/" + url.PathEscape(projectID) + "~" + changeID + "?o=MESSAGES&o=DETAILED_ACCOUNTS"
var obj struct {
Status string `json:"status"`
}
if err := httpGetGerritJSON(urlStr, nil, &obj); err != nil {
return "", err
}
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-info
switch obj.Status {
case "NEW":
return "open", nil
case "MERGED":
return "merged", nil
case "ABANDONED":
return "closed", nil
}
return "", nil
})
}
func (Gerrit) FetchSubmittedAt(urls []string) (time.Time, error) {
return fetchPerURLSubmittedAt(urls, func(u string) (time.Time, error) {
m := reGoogleGerritCL.FindStringSubmatch(u)
if m == nil {
return time.Time{}, nil
}
authority := m[1]
projectID := m[2]
changeID := m[3]
urlStr := "https://" + authority + "/changes/" + url.PathEscape(projectID) + "~" + changeID + "?o=MESSAGES&o=DETAILED_ACCOUNTS"
var obj struct {
Created GerritTime `json:"created"`
}
if err := httpGetGerritJSON(urlStr, nil, &obj); err != nil {
return time.Time{}, err
}
return obj.Created.Val, nil
})
}
func (Gerrit) FetchLastUpdated(urls []string) (time.Time, User, error) {
return fetchPerURLLastUpdated(urls, func(u string) (time.Time, User, error) {
m := reGoogleGerritCL.FindStringSubmatch(u)
if m == nil {
return time.Time{}, User{}, nil
}
authority := m[1]
projectID := m[2]
changeID := m[3]
urlStr := "https://" + authority + "/changes/" + url.PathEscape(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://%s/dashboard/%d", authority, message.Author.AccountID)
break
}
}
return retUpdatedAt, retUser, nil
})
}
|