@@ -10,7 +10,9 @@ import (
10
10
"errors"
11
11
"fmt"
12
12
"log"
13
+ "regexp"
13
14
"sort"
15
+ "strconv"
14
16
"strings"
15
17
"sync"
16
18
"time"
@@ -95,13 +97,63 @@ func tryBotStatus(cl *maintner.GerritCL, forStaging bool) (try, done bool) {
95
97
return
96
98
}
97
99
98
- func tryWorkItem (cl * maintner.GerritCL ) * apipb.GerritTryWorkItem {
99
- return & apipb.GerritTryWorkItem {
100
+ var (
101
+ tryCommentRx = regexp .MustCompile (`(?m)^TRY=(.*)$` )
102
+ patchSetRx = regexp .MustCompile (`^Patch Set (\d{1,4}):` )
103
+ )
104
+
105
+ func tryWorkItem (cl * maintner.GerritCL , ci * gerrit.ChangeInfo ) * apipb.GerritTryWorkItem {
106
+ work := & apipb.GerritTryWorkItem {
100
107
Project : cl .Project .Project (),
101
108
Branch : strings .TrimPrefix (cl .Branch (), "refs/heads/" ),
102
109
ChangeId : cl .ChangeID (),
103
110
Commit : cl .Commit .Hash .String (),
104
111
}
112
+ if ci != nil {
113
+ if ci .CurrentRevision != "" {
114
+ // In case maintner is behind.
115
+ work .Commit = ci .CurrentRevision
116
+ work .Version = int32 (ci .Revisions [ci .CurrentRevision ].PatchSetNumber )
117
+ }
118
+ // Also include any "TRY=foo" comments (just the "foo"
119
+ // aprt) from messages that accompany Run-TryBot+1
120
+ // votes.
121
+ for _ , m := range ci .Messages {
122
+ // msg is like:
123
+ // "Patch Set 2: Run-TryBot+1\n\nTRY=foo2"
124
+ // "Patch Set 2: Run-TryBot+1 Code-Review-2"
125
+ // "Uploaded patch set 2."
126
+ // "Removed Run-TryBot+1 by Brad Fitzpatrick <[email protected] >\n"
127
+ // "Patch Set 1: Run-TryBot+1\n\nTRY=baz"
128
+ msg := m .Message
129
+ if ! strings .Contains (msg , "\n \n TRY=" ) ||
130
+ ! strings .HasPrefix (msg , "Patch Set " ) ||
131
+ ! strings .Contains (firstLine (msg ), "Run-TryBot+1" ) {
132
+ continue
133
+ }
134
+ pm := patchSetRx .FindStringSubmatch (msg )
135
+ var patchSet int
136
+ if pm != nil {
137
+ patchSet , _ = strconv .Atoi (pm [1 ])
138
+ }
139
+ if tm := tryCommentRx .FindStringSubmatch (msg ); tm != nil && patchSet > 0 {
140
+ work .TryMessage = append (work .TryMessage , & apipb.TryVoteMessage {
141
+ Message : tm [1 ],
142
+ AuthorId : m .Author .NumericID ,
143
+ Version : int32 (patchSet ),
144
+ })
145
+ }
146
+ }
147
+ }
148
+ return work
149
+ }
150
+
151
+ func firstLine (s string ) string {
152
+ if nl := strings .Index (s , "\n " ); nl < 0 {
153
+ return s
154
+ } else {
155
+ return s [:nl ]
156
+ }
105
157
}
106
158
107
159
func (s apiService ) GetRef (ctx context.Context , req * apipb.GetRefRequest ) (* apipb.GetRefResponse , error ) {
@@ -160,33 +212,45 @@ func (s apiService) GoFindTryWork(ctx context.Context, req *apipb.GoFindTryWorkR
160
212
161
213
ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
162
214
defer cancel ()
215
+
216
+ res , err := goFindTryWork (ctx , tryBotGerrit , s .c )
217
+ if err != nil {
218
+ log .Printf ("maintnerd: goFindTryWork: %v" , err )
219
+ return nil , err
220
+ }
221
+
222
+ tryCache .val = res
223
+ tryCache .forNumChanges = sumChanges
224
+
225
+ log .Printf ("maintnerd: GetTryWork: for label changes of %d, cached %d trywork items." ,
226
+ sumChanges , len (res .Waiting ))
227
+
228
+ return res , nil
229
+ }
230
+
231
+ func goFindTryWork (ctx context.Context , gerritc * gerrit.Client , maintc * maintner.Corpus ) (* apipb.GoFindTryWorkResponse , error ) {
163
232
const query = "label:Run-TryBot=1 label:TryBot-Result=0 status:open"
164
- cis , err := tryBotGerrit .QueryChanges (ctx , query , gerrit.QueryChangesOpt {
165
- Fields : []string {"CURRENT_REVISION" , "CURRENT_COMMIT" },
233
+ cis , err := gerritc .QueryChanges (ctx , query , gerrit.QueryChangesOpt {
234
+ Fields : []string {"CURRENT_REVISION" , "CURRENT_COMMIT" , "MESSAGES" },
166
235
})
167
236
if err != nil {
168
237
return nil , err
169
238
}
170
- tryCache .forNumChanges = sumChanges
171
239
172
- goProj := s . c .Gerrit ().Project ("go.googlesource.com" , "go" )
240
+ goProj := maintc .Gerrit ().Project ("go.googlesource.com" , "go" )
173
241
supportedReleases , err := supportedGoReleases (goProj )
174
242
if err != nil {
175
243
return nil , err
176
244
}
177
245
178
246
res := new (apipb.GoFindTryWorkResponse )
179
247
for _ , ci := range cis {
180
- cl := s . c .Gerrit ().Project ("go.googlesource.com" , ci .Project ).CL (int32 (ci .ChangeNumber ))
248
+ cl := maintc .Gerrit ().Project ("go.googlesource.com" , ci .Project ).CL (int32 (ci .ChangeNumber ))
181
249
if cl == nil {
182
250
log .Printf ("nil Gerrit CL %v" , ci .ChangeNumber )
183
251
continue
184
252
}
185
- work := tryWorkItem (cl )
186
- if ci .CurrentRevision != "" {
187
- // In case maintner is behind.
188
- work .Commit = ci .CurrentRevision
189
- }
253
+ work := tryWorkItem (cl , ci )
190
254
if work .Project == "go" {
191
255
// Trybot on Go repo. Set the GoVersion field based on branch name.
192
256
if work .Branch == "master" {
@@ -229,11 +293,6 @@ func (s apiService) GoFindTryWork(ctx context.Context, req *apipb.GoFindTryWorkR
229
293
sort .Slice (res .Waiting , func (i , j int ) bool {
230
294
return res .Waiting [i ].Commit < res .Waiting [j ].Commit
231
295
})
232
- tryCache .val = res
233
-
234
- log .Printf ("maintnerd: GetTryWork: for label changes of %d, cached %d trywork items." ,
235
- sumChanges , len (res .Waiting ))
236
-
237
296
return res , nil
238
297
}
239
298
0 commit comments