@@ -330,18 +330,23 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
330330 firstRead = false
331331 }
332332 var (
333- handledEvents = make ([]* pipedservice.ReportEventStatusesRequest_Event , 0 , len (eventCfgs ))
334- outDatedEvents = make ([]* pipedservice.ReportEventStatusesRequest_Event , 0 )
335- maxTimestamp int64
336- outDatedDuration = time .Hour
337- gitUpdateEvent = false
333+ handledEvents = make ([]* pipedservice.ReportEventStatusesRequest_Event , 0 , len (eventCfgs ))
334+ outDatedEvents = make ([]* pipedservice.ReportEventStatusesRequest_Event , 0 )
335+ maxTimestamp int64
336+ outDatedDuration = time .Hour
337+ gitUpdateEvent = false
338+ newBranchs = make ([]string , 0 , len (eventCfgs ))
339+ isExistDefaultBranch bool
338340 )
339341 for _ , e := range eventCfgs {
340342 for _ , cfg := range e .Configs {
341343 var (
342344 matcher = cfg .Matcher
343345 handler = cfg .Handler
344346 )
347+ if handler .Config .MakePullRequest {
348+ isExistDefaultBranch = true
349+ }
345350 notHandledEvents := w .eventLister .ListNotHandled (matcher .Name , matcher .Labels , milestone + 1 , numToMakeOutdated )
346351 if len (notHandledEvents ) == 0 {
347352 continue
@@ -384,10 +389,10 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
384389 })
385390 continue
386391 }
387-
388392 switch handler .Type {
389393 case config .EventWatcherHandlerTypeGitUpdate :
390- if err := w .commitFiles (ctx , latestEvent .Data , matcher .Name , handler .Config .CommitMessage , e .GitPath , handler .Config .Replacements , tmpRepo , handler .Config .MakePullRequest ); err != nil {
394+ branchName , err := w .commitFiles (ctx , latestEvent .Data , matcher .Name , handler .Config .CommitMessage , e .GitPath , handler .Config .Replacements , tmpRepo , handler .Config .MakePullRequest )
395+ if err != nil {
391396 w .logger .Error ("failed to commit outdated files" , zap .Error (err ))
392397 handledEvents = append (handledEvents , & pipedservice.ReportEventStatusesRequest_Event {
393398 Id : latestEvent .Id ,
@@ -397,21 +402,7 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
397402 continue
398403 }
399404 if handler .Config .MakePullRequest {
400- retry := backoff .NewRetry (retryPushNum , backoff .NewConstant (retryPushInterval ))
401- _ , err = retry .Do (ctx , func () (interface {}, error ) {
402- err := tmpRepo .Push (ctx , tmpRepo .GetClonedBranch ())
403- return nil , err
404- })
405- if err != nil {
406- w .logger .Error ("failed to push commits" , zap .Error (err ))
407- handledEvents = append (handledEvents , & pipedservice.ReportEventStatusesRequest_Event {
408- Id : latestEvent .Id ,
409- Status : model .EventStatus_EVENT_FAILURE ,
410- StatusDescription : fmt .Sprintf ("Failed to push commits: %v" , err ),
411- })
412- continue
413- }
414-
405+ newBranchs = append (newBranchs , branchName )
415406 }
416407 handledEvents = append (handledEvents , & pipedservice.ReportEventStatusesRequest_Event {
417408 Id : latestEvent .Id ,
@@ -446,10 +437,19 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
446437 }
447438
448439 retry := backoff .NewRetry (retryPushNum , backoff .NewConstant (retryPushInterval ))
449- _ , err = retry .Do (ctx , func () (interface {}, error ) {
450- err := tmpRepo .Push (ctx , tmpRepo .GetClonedBranch ())
451- return nil , err
452- })
440+ for _ , branch := range newBranchs {
441+ _ , err = retry .Do (ctx , func () (interface {}, error ) {
442+ err := tmpRepo .Push (ctx , branch )
443+ return nil , err
444+ })
445+ }
446+ if isExistDefaultBranch {
447+ _ , err = retry .Do (ctx , func () (interface {}, error ) {
448+ err := tmpRepo .Push (ctx , repo .GetClonedBranch ())
449+ return nil , err
450+ })
451+ }
452+
453453 if err == nil {
454454 if _ , err := w .apiClient .ReportEventStatuses (ctx , & pipedservice.ReportEventStatusesRequest {Events : handledEvents }); err != nil {
455455 return fmt .Errorf ("failed to report event statuses: %w" , err )
@@ -548,7 +548,8 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
548548 })
549549 continue
550550 }
551- if err := w .commitFiles (ctx , latestEvent .Data , e .Name , commitMsg , "" , e .Replacements , tmpRepo , false ); err != nil {
551+ _ , err := w .commitFiles (ctx , latestEvent .Data , e .Name , commitMsg , "" , e .Replacements , tmpRepo , false )
552+ if err != nil {
552553 w .logger .Error ("failed to commit outdated files" , zap .Error (err ))
553554 handledEvents = append (handledEvents , & pipedservice.ReportEventStatusesRequest_Event {
554555 Id : latestEvent .Id ,
@@ -611,7 +612,7 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
611612}
612613
613614// commitFiles commits changes if the data in Git is different from the latest event.
614- func (w * watcher ) commitFiles (ctx context.Context , latestData , eventName , commitMsg , gitPath string , replacements []config.EventWatcherReplacement , repo git.Repo , newBranch bool ) error {
615+ func (w * watcher ) commitFiles (ctx context.Context , latestData , eventName , commitMsg , gitPath string , replacements []config.EventWatcherReplacement , repo git.Repo , newBranch bool ) ( string , error ) {
615616 // Determine files to be changed by comparing with the latest event.
616617 changes := make (map [string ][]byte , len (replacements ))
617618 for _ , r := range replacements {
@@ -637,19 +638,19 @@ func (w *watcher) commitFiles(ctx context.Context, latestData, eventName, commit
637638 newContent , upToDate , err = modifyText (path , r .Regex , latestData )
638639 }
639640 if err != nil {
640- return err
641+ return "" , err
641642 }
642643 if upToDate {
643644 continue
644645 }
645646
646647 if err := os .WriteFile (path , newContent , os .ModePerm ); err != nil {
647- return fmt .Errorf ("failed to write file: %w" , err )
648+ return "" , fmt .Errorf ("failed to write file: %w" , err )
648649 }
649650 changes [filePath ] = newContent
650651 }
651652 if len (changes ) == 0 {
652- return nil
653+ return "" , nil
653654 }
654655
655656 args := argsTemplate {
@@ -659,10 +660,10 @@ func (w *watcher) commitFiles(ctx context.Context, latestData, eventName, commit
659660 commitMsg = parseCommitMsg (commitMsg , args )
660661 branch := makeBranchName (newBranch , eventName , repo .GetClonedBranch ())
661662 if err := repo .CommitChanges (ctx , branch , commitMsg , newBranch , changes ); err != nil {
662- return fmt .Errorf ("failed to perform git commit: %w" , err )
663+ return "" , fmt .Errorf ("failed to perform git commit: %w" , err )
663664 }
664665 w .logger .Info (fmt .Sprintf ("event watcher will update values of Event %q" , eventName ))
665- return nil
666+ return branch , nil
666667}
667668
668669// modifyYAML returns a new YAML content as a first returned value if the value of given
0 commit comments