@@ -70,6 +70,12 @@ type Repo struct {
70
70
71
71
// Templates
72
72
ProposalTemplate []byte
73
+
74
+ // Temporary caches
75
+ // a local git clone of remoteOrg/remoteRepo
76
+ gitRepo * git.Repo
77
+ // all open pull requests for remoteOrg/remoteRepo
78
+ allPRs []* github.PullRequest
73
79
}
74
80
75
81
// New returns a new repo client configured to use the the normal os.Stdxxx and Filesystem
@@ -179,10 +185,8 @@ func (r *Repo) SetGitHubToken(tokenFile string) error {
179
185
if err != nil {
180
186
return err
181
187
}
182
-
183
188
r .Token = strings .Trim (string (token ), "\n \r " )
184
189
}
185
-
186
190
return nil
187
191
}
188
192
@@ -199,10 +203,7 @@ func (r *Repo) getProposalTemplate() ([]byte, error) {
199
203
}
200
204
201
205
func (r * Repo ) findLocalKEPMeta (sig string ) ([]string , error ) {
202
- sigPath := filepath .Join (
203
- r .ProposalPath ,
204
- sig ,
205
- )
206
+ sigPath := filepath .Join (r .ProposalPath , sig )
206
207
207
208
keps := []string {}
208
209
@@ -234,6 +235,10 @@ func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
234
235
235
236
if info .Name () == ProposalMetadataFilename {
236
237
logrus .Debugf ("adding %s as KEP metadata" , info .Name ())
238
+ path , err = filepath .Rel (r .BasePath , path )
239
+ if err != nil {
240
+ return err
241
+ }
237
242
keps = append (keps , path )
238
243
return filepath .SkipDir
239
244
}
@@ -263,65 +268,88 @@ func (r *Repo) LoadLocalKEPs(sig string) ([]*api.Proposal, error) {
263
268
logrus .Debugf ("loading the following local KEPs: %v" , files )
264
269
265
270
var allKEPs []* api.Proposal
266
- for _ , k := range files {
267
- if filepath .Ext (k ) == ".yaml" {
268
- kep , err := r .loadKEPFromYaml (k )
269
- if err != nil {
270
- return nil , errors .Wrapf (
271
- err ,
272
- "reading KEP %s from yaml" ,
273
- k ,
274
- )
275
- }
276
-
277
- allKEPs = append (allKEPs , kep )
271
+ for _ , kepYamlPath := range files {
272
+ kep , err := r .loadKEPFromYaml (r .BasePath , kepYamlPath )
273
+ if err != nil {
274
+ return nil , errors .Wrapf (
275
+ err ,
276
+ "reading KEP %s from yaml" ,
277
+ kepYamlPath ,
278
+ )
278
279
}
280
+
281
+ allKEPs = append (allKEPs , kep )
279
282
}
280
283
281
284
logrus .Debugf ("returning %d local KEPs" , len (allKEPs ))
282
285
283
286
return allKEPs , nil
284
287
}
285
288
286
- func (r * Repo ) loadKEPPullRequests (sig string ) ([]* api.Proposal , error ) {
289
+ func (r * Repo ) LoadLocalKEP (sig , name string ) (* api.Proposal , error ) {
290
+ kepPath := filepath .Join (
291
+ ProposalPathStub ,
292
+ sig ,
293
+ name ,
294
+ ProposalMetadataFilename ,
295
+ )
296
+
297
+ _ , err := os .Stat (kepPath )
298
+ if err != nil {
299
+ return nil , errors .Wrapf (err , "getting file info for %s" , kepPath )
300
+ }
301
+
302
+ return r .loadKEPFromYaml (r .BasePath , kepPath )
303
+ }
304
+
305
+ func (r * Repo ) LoadPullRequestKEPs (sig string ) ([]* api.Proposal , error ) {
306
+ // Initialize github client
307
+ logrus .Debugf ("Initializing github client to load PRs for sig: %v" , sig )
287
308
var auth * http.Client
288
309
ctx := context .Background ()
289
310
if r .Token != "" {
290
311
ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : r .Token })
291
312
auth = oauth2 .NewClient (ctx , ts )
292
313
}
293
-
294
314
gh := github .NewClient (auth )
295
- allPulls := []* github.PullRequest {}
296
- opt := & github.PullRequestListOptions {
297
- ListOptions : github.ListOptions {
298
- PerPage : 100 ,
299
- },
300
- }
301
315
302
- for {
303
- pulls , resp , err := gh .PullRequests .List (
304
- ctx ,
305
- remoteOrg ,
306
- remoteRepo ,
307
- opt ,
308
- )
309
- if err != nil {
310
- return nil , err
311
- }
316
+ // Fetch list of all PRs if none exists
317
+ if r .allPRs == nil {
318
+ logrus .Debugf ("Initializing list of all PRs for %v/%v" , remoteOrg , remoteRepo )
319
+ r .allPRs = []* github.PullRequest {}
312
320
313
- allPulls = append (allPulls , pulls ... )
314
- if resp .NextPage == 0 {
315
- break
321
+ opt := & github.PullRequestListOptions {
322
+ ListOptions : github.ListOptions {
323
+ PerPage : 100 ,
324
+ },
316
325
}
317
326
318
- opt .Page = resp .NextPage
327
+ for {
328
+ pulls , resp , err := gh .PullRequests .List (
329
+ ctx ,
330
+ remoteOrg ,
331
+ remoteRepo ,
332
+ opt ,
333
+ )
334
+ if err != nil {
335
+ return nil , err
336
+ }
337
+
338
+ r .allPRs = append (r .allPRs , pulls ... )
339
+ if resp .NextPage == 0 {
340
+ break
341
+ }
342
+
343
+ opt .Page = resp .NextPage
344
+ }
319
345
}
320
346
321
- kepPRs := make ([]* github.PullRequest , 10 )
322
- for _ , pr := range allPulls {
347
+ // Find KEP PRs for the given sig
348
+ kepPRs := []* github.PullRequest {}
349
+ sigLabel := strings .Replace (sig , "-" , "/" , 1 )
350
+ logrus .Debugf ("Searching list of %v PRs for %v/%v with labels: [%v, %v]" , len (r .allPRs ), remoteOrg , remoteRepo , sigLabel , proposalLabel )
351
+ for _ , pr := range r .allPRs {
323
352
foundKind , foundSIG := false , false
324
- sigLabel := strings .Replace (sig , "-" , "/" , 1 )
325
353
326
354
for _ , l := range pr .Labels {
327
355
if * l .Name == proposalLabel {
@@ -337,31 +365,37 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
337
365
continue
338
366
}
339
367
368
+ logrus .Debugf ("Found #%v" , pr .GetHTMLURL ())
369
+
340
370
kepPRs = append (kepPRs , pr )
341
371
}
372
+ logrus .Debugf ("Found %v PRs for %v/%v with labels: [%v, %v]" , len (kepPRs ), remoteOrg , remoteRepo , sigLabel , proposalLabel )
342
373
343
374
if len (kepPRs ) == 0 {
344
375
return nil , nil
345
376
}
346
377
347
- // Pull a temporary clone of the repo
348
- g , err := git .NewClient ()
349
- if err != nil {
350
- return nil , err
351
- }
378
+ // Pull a temporary clone of the repo if none already exists
379
+ if r .gitRepo == nil {
380
+ g , err := git .NewClient ()
381
+ if err != nil {
382
+ return nil , err
383
+ }
352
384
353
- g .SetCredentials ("" , func () []byte { return []byte {} })
354
- g .SetRemote (krgh .GitHubURL )
385
+ g .SetCredentials ("" , func () []byte { return []byte {} })
386
+ g .SetRemote (krgh .GitHubURL )
355
387
356
- repo , err := g .Clone (remoteOrg , remoteRepo )
357
- if err != nil {
358
- return nil , err
388
+ r .gitRepo , err = g .Clone (remoteOrg , remoteRepo )
389
+ if err != nil {
390
+ return nil , err
391
+ }
359
392
}
360
393
361
394
// read out each PR, and create a Proposal for each KEP that is
362
395
// touched by a PR. This may result in multiple versions of the same KEP.
363
396
var allKEPs []* api.Proposal
364
397
for _ , pr := range kepPRs {
398
+ logrus .Debugf ("Getting list of files for %v" , pr .GetHTMLURL ())
365
399
files , _ , err := gh .PullRequests .ListFiles (
366
400
context .Background (),
367
401
remoteOrg ,
@@ -395,14 +429,20 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
395
429
continue
396
430
}
397
431
398
- err = repo .CheckoutPullRequest (pr .GetNumber ())
432
+ err = r . gitRepo .CheckoutPullRequest (pr .GetNumber ())
399
433
if err != nil {
400
434
return nil , err
401
435
}
402
436
403
437
// read all these KEPs
404
438
for k := range kepNames {
405
- kep , err := r .ReadKEP (sig , k )
439
+ kepPath := filepath .Join (
440
+ ProposalPathStub ,
441
+ sig ,
442
+ k ,
443
+ ProposalMetadataFilename ,
444
+ )
445
+ kep , err := r .loadKEPFromYaml (r .gitRepo .Directory (), kepPath )
406
446
if err != nil {
407
447
logrus .Warnf ("error reading KEP %v: %v" , k , err )
408
448
} else {
@@ -415,26 +455,13 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
415
455
return allKEPs , nil
416
456
}
417
457
418
- func (r * Repo ) ReadKEP (sig , name string ) (* api.Proposal , error ) {
419
- kepPath := filepath .Join (
420
- r .ProposalPath ,
421
- sig ,
422
- name ,
423
- ProposalMetadataFilename ,
424
- )
425
-
426
- _ , err := os .Stat (kepPath )
427
- if err != nil {
428
- return nil , errors .Wrapf (err , "getting file info for %s" , kepPath )
429
- }
430
-
431
- return r .loadKEPFromYaml (kepPath )
432
- }
433
-
434
- func (r * Repo ) loadKEPFromYaml (kepPath string ) (* api.Proposal , error ) {
435
- b , err := ioutil .ReadFile (kepPath )
458
+ // loadKEPFromYaml will return a Proposal from a kep.yaml at the given kepPath
459
+ // within the given repoPath, or an error if the Proposal is invalid
460
+ func (r * Repo ) loadKEPFromYaml (repoPath , kepPath string ) (* api.Proposal , error ) {
461
+ fullKEPPath := filepath .Join (repoPath , kepPath )
462
+ b , err := ioutil .ReadFile (fullKEPPath )
436
463
if err != nil {
437
- return nil , fmt .Errorf ("unable to read KEP metadata: %s" , err )
464
+ return nil , fmt .Errorf ("unable to read KEP metadata for %s: %w" , fullKEPPath , err )
438
465
}
439
466
440
467
var p api.Proposal
@@ -444,24 +471,22 @@ func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
444
471
}
445
472
446
473
p .Name = filepath .Base (filepath .Dir (kepPath ))
474
+ prrApprovalPath := filepath .Join (repoPath , ProposalPathStub , PRRApprovalPathStub )
447
475
448
476
// Read the PRR approval file and add any listed PRR approvers in there
449
477
// to the PRR approvers list in the KEP. this is a hack while we transition
450
478
// away from PRR approvers listed in kep.yaml
451
479
handler := r .PRRHandler
452
- err = kepval .ValidatePRR (& p , handler , r . PRRApprovalPath )
480
+ err = kepval .ValidatePRR (& p , handler , prrApprovalPath )
453
481
if err != nil {
454
482
logrus .Errorf (
455
483
"%v" ,
456
484
errors .Wrapf (err , "validating PRR for %s" , p .Name ),
457
485
)
458
486
} else {
459
- prrPath := filepath .Dir (kepPath )
460
- prrPath = filepath .Dir (prrPath )
461
- sig := filepath .Base (prrPath )
462
- prrPath = filepath .Join (
463
- filepath .Dir (prrPath ),
464
- PRRApprovalPathStub ,
487
+ sig := filepath .Base (filepath .Dir (filepath .Dir (kepPath )))
488
+ prrPath := filepath .Join (
489
+ prrApprovalPath ,
465
490
sig ,
466
491
p .Number + ".yaml" ,
467
492
)
@@ -484,7 +509,7 @@ func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
484
509
if err != nil {
485
510
logrus .Errorf (
486
511
"%v" ,
487
- errors . Wrapf ( err , "getting PRR approver for %s stage " , p .Stage ),
512
+ fmt . Errorf ( "getting PRR approver for stage '%s' for kep %s: %w " , p .Stage , fullKEPPath , err ),
488
513
)
489
514
}
490
515
0 commit comments