This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gerrit.go
168 lines (144 loc) · 3.95 KB
/
gerrit.go
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
package gerrit
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const gerritURL = "https://go-review.googlesource.com/changes/?q=status:merged&O=12&n=100"
// TODO: this type contains more details than necessary for previous Twitter functionality.
type storedCL struct {
Tweeted bool `datastore:"Tweeted,noindex"`
URL string `datastore:"URL,noindex"`
Message string `datastore:"Message,noindex"`
CrawledAt time.Time `datastore:"CrawledAt"`
}
type GerritCL struct {
Project string `json:"project"`
ChangeID string `json:"change_id"`
Number int `json:"_number"`
Subject string `json:"subject"`
Branch string `json:"branch"`
CurrentRevision string `json:"current_revision"`
Revisions map[string]struct {
Commit struct {
Subject string `json:"subject"`
Message string `json:"message"`
} `json:"commit"`
} `json:"revisions"`
}
func (cl *GerritCL) Link() string {
return fmt.Sprintf("https://golang.org/cl/%d/", cl.Number)
}
func (cl *GerritCL) Message() string {
subject := cl.Subject
if cl.Project != "go" {
subject = fmt.Sprintf("[%s] %s", cl.Project, subject)
}
return subject
}
// Gerrit tracks merged CLs.
type Gerrit struct {
store Store
http *http.Client
logf func(message string, args ...interface{})
notify func(GerritCL) bool
lastID int
}
// Store persists information about CLs that have been handled.
type Store interface {
LatestNumber(context.Context) (int, error)
Put(_ context.Context, number int, _ storedCL) error
Exists(_ context.Context, number int) (bool, error)
}
// ErrNotFound should be returned by Store implementations when CL number
// doesn't exist.
var ErrNotFound = errors.New("CL not found")
// New creates an initializes an instance of Gerrit.
func New(ctx context.Context, s Store, http *http.Client, logf func(message string, args ...interface{}), notify func(GerritCL) bool) (*Gerrit, error) {
lastID, err := s.LatestNumber(ctx)
switch err {
case nil:
case ErrNotFound:
lastID = -1
default:
return nil, fmt.Errorf("loading last ID from datastore: %v", err)
}
return &Gerrit{
store: s,
http: http,
logf: logf,
notify: notify,
lastID: lastID,
}, nil
}
// Poll checks for new merged CLs and calls notify for each CL.
func (g *Gerrit) Poll(ctx context.Context) {
req, err := http.NewRequest("GET", gerritURL, nil)
if err != nil {
g.logf("failed to build GET request to %q: %v\n", gerritURL, err)
return
}
req.Header.Add("User-Agent", "Gophers Slack bot")
req = req.WithContext(ctx)
resp, err := g.http.Do(req)
if err != nil {
g.logf("failed to get data from Gerrit: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
g.logf("got non-200 code: %d from gerrit api", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
g.logf("reading body: %v", err)
return
}
// Gerrit prefixes responses with `)]}'`
// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
body = bytes.TrimPrefix(body, []byte(")]}'"))
var cls []GerritCL
err = json.Unmarshal(body, &cls)
if err != nil {
g.logf("unmarshaling response: %v\n", err)
return
}
// The change output is sorted by the last update time, most recently updated to oldest updated.
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
for i, cl := range cls {
if cl.Number == g.lastID {
cls = cls[:i]
break
}
}
for i := len(cls) - 1; i >= 0; i-- {
cl := cls[i]
exists, err := g.store.Exists(ctx, cl.Number)
if err != nil {
g.logf("checking whether CL shown: %v", err)
continue
}
if exists {
continue
}
err = g.store.Put(ctx, cl.Number, storedCL{
URL: cl.Link(),
Message: cl.Message(),
CrawledAt: time.Now(),
})
if err != nil {
g.logf("saving CL to datastore: %v", err)
return
}
if !g.notify(cl) {
return
}
g.lastID = cl.Number
}
}