@@ -126,14 +126,16 @@ func (w *watcher) Run(ctx context.Context) error {
126
126
127
127
workingDir , err := os .MkdirTemp ("" , "event-watcher" )
128
128
if err != nil {
129
- return fmt .Errorf ("failed to create the working directory: %w" , err )
129
+ w .logger .Error ("failed to create the working directory" , zap .Error (err ))
130
+ return err
130
131
}
131
132
defer os .RemoveAll (workingDir )
132
133
w .workingDir = workingDir
133
134
134
135
for _ , r := range w .config .Repositories {
135
136
repo , err := w .cloneRepo (ctx , r )
136
137
if err != nil {
138
+ w .logger .Error ("failed to clone repository" , zap .String ("repo-id" , r .RepoID ), zap .Error (err ))
137
139
return err
138
140
}
139
141
defer repo .Clean ()
@@ -303,11 +305,13 @@ func (w *watcher) run(ctx context.Context, repo git.Repo, repoCfg config.PipedRe
303
305
func (w * watcher ) cloneRepo (ctx context.Context , repoCfg config.PipedRepository ) (git.Repo , error ) {
304
306
dst , err := os .MkdirTemp (w .workingDir , repoCfg .RepoID )
305
307
if err != nil {
306
- return nil , fmt .Errorf ("failed to create a new temporary directory: %w" , err )
308
+ w .logger .Error ("failed to create a new temporary directory" , zap .Error (err ))
309
+ return nil , err
307
310
}
308
311
repo , err := w .gitClient .Clone (ctx , repoCfg .RepoID , repoCfg .Remote , repoCfg .Branch , dst )
309
312
if err != nil {
310
- return nil , fmt .Errorf ("failed to clone repository %s: %w" , repoCfg .RepoID , err )
313
+ w .logger .Error ("failed to clone repository" , zap .String ("repo-id" , repoCfg .RepoID ), zap .Error (err ))
314
+ return nil , err
311
315
}
312
316
return repo , nil
313
317
}
@@ -317,11 +321,13 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
317
321
// Copy the repo to another directory to modify local file to avoid reverting previous changes.
318
322
tmpDir , err := os .MkdirTemp (w .workingDir , "repo" )
319
323
if err != nil {
320
- return fmt .Errorf ("failed to create a new temporary directory: %w" , err )
324
+ w .logger .Error ("failed to create a new temporary directory" , zap .Error (err ))
325
+ return err
321
326
}
322
327
tmpRepo , err := repo .CopyToModify (filepath .Join (tmpDir , "tmp-repo" ))
323
328
if err != nil {
324
- return fmt .Errorf ("failed to copy the repository to the temporary directory: %w" , err )
329
+ w .logger .Error ("failed to copy the repository to the temporary directory" , zap .Error (err ))
330
+ return err
325
331
}
326
332
// nolint: errcheck
327
333
defer tmpRepo .Clean ()
@@ -368,7 +374,8 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
368
374
Labels : matcher .Labels ,
369
375
})
370
376
if err != nil {
371
- return fmt .Errorf ("failed to get the latest event: %w" , err )
377
+ w .logger .Error ("failed to get the latest event" , zap .Error (err ))
378
+ return err
372
379
}
373
380
// The case where the latest event has already been handled.
374
381
if resp .Event .CreatedAt > latestEvent .CreatedAt {
@@ -429,7 +436,8 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
429
436
}
430
437
if len (outDatedEvents ) > 0 {
431
438
if _ , err := w .apiClient .ReportEventStatuses (ctx , & pipedservice.ReportEventStatusesRequest {Events : outDatedEvents }); err != nil {
432
- return fmt .Errorf ("failed to report event statuses: %w" , err )
439
+ w .logger .Error ("failed to report event statuses" , zap .Error (err ))
440
+ return err
433
441
}
434
442
w .logger .Info (fmt .Sprintf ("successfully made %d events OUTDATED" , len (outDatedEvents )))
435
443
}
@@ -449,8 +457,11 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
449
457
retry := backoff .NewRetry (retryPushNum , backoff .NewConstant (retryPushInterval ))
450
458
for branch , events := range branchHandledEvents {
451
459
_ , err = retry .Do (ctx , func () (interface {}, error ) {
452
- err := tmpRepo .Push (ctx , branch )
453
- return nil , err
460
+ if err := tmpRepo .Push (ctx , branch ); err != nil {
461
+ w .logger .Error ("failed to push commits" , zap .String ("repo-id" , repoID ), zap .String ("branch" , branch ), zap .Error (err ))
462
+ return nil , err
463
+ }
464
+ return nil , nil
454
465
})
455
466
456
467
if err == nil {
@@ -493,11 +504,13 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
493
504
// Copy the repo to another directory to modify local file to avoid reverting previous changes.
494
505
tmpDir , err := os .MkdirTemp (w .workingDir , "repo" )
495
506
if err != nil {
496
- return fmt .Errorf ("failed to create a new temporary directory: %w" , err )
507
+ w .logger .Error ("failed to create a new temporary directory" , zap .Error (err ))
508
+ return err
497
509
}
498
510
tmpRepo , err := repo .CopyToModify (filepath .Join (tmpDir , "tmp-repo" ))
499
511
if err != nil {
500
- return fmt .Errorf ("failed to copy the repository to the temporary directory: %w" , err )
512
+ w .logger .Error ("failed to copy the repository to the temporary directory" , zap .Error (err ))
513
+ return err
501
514
}
502
515
defer tmpRepo .Clean ()
503
516
@@ -536,7 +549,8 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
536
549
Labels : e .Labels ,
537
550
})
538
551
if err != nil {
539
- return fmt .Errorf ("failed to get the latest event: %w" , err )
552
+ w .logger .Error ("failed to get the latest event" , zap .Error (err ))
553
+ return err
540
554
}
541
555
// The case where the latest event has already been handled.
542
556
if resp .Event .CreatedAt > latestEvent .CreatedAt {
@@ -577,7 +591,8 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
577
591
}
578
592
if len (outDatedEvents ) > 0 {
579
593
if _ , err := w .apiClient .ReportEventStatuses (ctx , & pipedservice.ReportEventStatusesRequest {Events : outDatedEvents }); err != nil {
580
- return fmt .Errorf ("failed to report event statuses: %w" , err )
594
+ w .logger .Error ("failed to report event statuses" , zap .Error (err ))
595
+ return err
581
596
}
582
597
w .logger .Info (fmt .Sprintf ("successfully made %d events OUTDATED" , len (outDatedEvents )))
583
598
}
@@ -587,12 +602,16 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
587
602
588
603
retry := backoff .NewRetry (retryPushNum , backoff .NewConstant (retryPushInterval ))
589
604
_ , err = retry .Do (ctx , func () (interface {}, error ) {
590
- err := tmpRepo .Push (ctx , tmpRepo .GetClonedBranch ())
591
- return nil , err
605
+ if err := tmpRepo .Push (ctx , tmpRepo .GetClonedBranch ()); err != nil {
606
+ w .logger .Error ("failed to push commits" , zap .String ("repo-id" , repoID ), zap .String ("branch" , tmpRepo .GetClonedBranch ()), zap .Error (err ))
607
+ return nil , err
608
+ }
609
+ return nil , nil
592
610
})
593
611
if err == nil {
594
612
if _ , err := w .apiClient .ReportEventStatuses (ctx , & pipedservice.ReportEventStatusesRequest {Events : handledEvents }); err != nil {
595
- return fmt .Errorf ("failed to report event statuses: %w" , err )
613
+ w .logger .Error ("failed to report event statuses" , zap .Error (err ))
614
+ return err
596
615
}
597
616
w .milestoneMap .Store (repoID , maxTimestamp )
598
617
return nil
@@ -613,10 +632,12 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
613
632
handledEvents [i ].StatusDescription = fmt .Sprintf ("Failed to push changed files: %v" , err )
614
633
}
615
634
if _ , err := w .apiClient .ReportEventStatuses (ctx , & pipedservice.ReportEventStatusesRequest {Events : handledEvents }); err != nil {
616
- return fmt .Errorf ("failed to report event statuses: %w" , err )
635
+ w .logger .Error ("failed to report event statuses: %w" , zap .Error (err ))
636
+ return err
617
637
}
618
638
w .milestoneMap .Store (repoID , maxTimestamp )
619
- return fmt .Errorf ("failed to push commits: %w" , err )
639
+ w .logger .Error ("failed to push commits" , zap .Error (err ))
640
+ return err
620
641
}
621
642
622
643
// commitFiles commits changes if the data in Git is different from the latest event.
@@ -647,14 +668,16 @@ func (w *watcher) commitFiles(ctx context.Context, latestEvent *model.Event, eve
647
668
newContent , upToDate , err = modifyText (path , r .Regex , latestEvent .Data )
648
669
}
649
670
if err != nil {
671
+ w .logger .Error ("failed to modify file" , zap .Error (err ))
650
672
return "" , err
651
673
}
652
674
if upToDate {
653
675
continue
654
676
}
655
677
656
678
if err := os .WriteFile (path , newContent , os .ModePerm ); err != nil {
657
- return "" , fmt .Errorf ("failed to write file: %w" , err )
679
+ w .logger .Error ("failed to write file" , zap .Error (err ))
680
+ return "" , err
658
681
}
659
682
changes [filePath ] = newContent
660
683
}
@@ -670,7 +693,12 @@ func (w *watcher) commitFiles(ctx context.Context, latestEvent *model.Event, eve
670
693
branch := makeBranchName (newBranch , eventName , repo .GetClonedBranch ())
671
694
trailers := maps .Clone (latestEvent .Contexts )
672
695
if err := repo .CommitChanges (ctx , branch , commitMsg , newBranch , changes , trailers ); err != nil {
673
- return "" , fmt .Errorf ("failed to perform git commit: %w" , err )
696
+ w .logger .Error ("failed to perform git commit" ,
697
+ zap .String ("branch" , branch ),
698
+ zap .Bool ("make-new-branch" , newBranch ),
699
+ zap .Int ("changed-files" , len (changes )),
700
+ zap .Error (err ))
701
+ return "" , err
674
702
}
675
703
w .logger .Info (fmt .Sprintf ("event watcher will update values of Event %q" , eventName ))
676
704
return branch , nil
0 commit comments