Skip to content

Commit 5898f42

Browse files
authored
Skip commit and push when no replacement happens in EventWatcher (#5310)
* fix: skip push if no diff exist Signed-off-by: t-kikuc <[email protected]> * modify message Signed-off-by: t-kikuc <[email protected]> * refactor: use noChange variable Signed-off-by: t-kikuc <[email protected]> --------- Signed-off-by: t-kikuc <[email protected]>
1 parent 6b43655 commit 5898f42

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

pkg/app/piped/eventwatcher/eventwatcher.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const (
5656
numToMakeOutdated = 10
5757
)
5858

59+
var errNoChanges = errors.New("nothing to commit")
60+
5961
type Watcher interface {
6062
Run(context.Context) error
6163
}
@@ -336,6 +338,7 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
336338
outDatedDuration = time.Hour
337339
gitUpdateEvent = false
338340
branchHandledEvents = make(map[string][]*pipedservice.ReportEventStatusesRequest_Event, len(eventCfgs))
341+
gitNoChangeEvents = make([]*pipedservice.ReportEventStatusesRequest_Event, 0)
339342
)
340343
for _, e := range eventCfgs {
341344
for _, cfg := range e.Configs {
@@ -388,7 +391,8 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
388391
switch handler.Type {
389392
case config.EventWatcherHandlerTypeGitUpdate:
390393
branchName, err := w.commitFiles(ctx, latestEvent, matcher.Name, handler.Config.CommitMessage, e.GitPath, handler.Config.Replacements, tmpRepo, handler.Config.MakePullRequest)
391-
if err != nil {
394+
noChange := errors.Is(err, errNoChanges)
395+
if err != nil && !noChange {
392396
w.logger.Error("failed to commit outdated files", zap.Error(err))
393397
handledEvent := &pipedservice.ReportEventStatusesRequest_Event{
394398
Id: latestEvent.Id,
@@ -398,12 +402,18 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
398402
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)
399403
continue
400404
}
405+
401406
handledEvent := &pipedservice.ReportEventStatusesRequest_Event{
402-
Id: latestEvent.Id,
403-
Status: model.EventStatus_EVENT_SUCCESS,
404-
StatusDescription: fmt.Sprintf("Successfully updated %d files in the %q repository", len(handler.Config.Replacements), repoID),
407+
Id: latestEvent.Id,
408+
Status: model.EventStatus_EVENT_SUCCESS,
409+
}
410+
if noChange {
411+
handledEvent.StatusDescription = "Nothing to commit"
412+
gitNoChangeEvents = append(gitNoChangeEvents, handledEvent)
413+
} else {
414+
handledEvent.StatusDescription = fmt.Sprintf("Successfully updated %d files in the %q repository", len(handler.Config.Replacements), repoID)
415+
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)
405416
}
406-
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)
407417
if latestEvent.CreatedAt > maxTimestamp {
408418
maxTimestamp = latestEvent.CreatedAt
409419
}
@@ -428,6 +438,13 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
428438
return nil
429439
}
430440

441+
if len(gitNoChangeEvents) > 0 {
442+
if _, err := w.apiClient.ReportEventStatuses(ctx, &pipedservice.ReportEventStatusesRequest{Events: gitNoChangeEvents}); err != nil {
443+
w.logger.Error("failed to report event statuses", zap.Error(err))
444+
}
445+
w.executionMilestoneMap.Store(repoID, maxTimestamp)
446+
}
447+
431448
var responseError error
432449
retry := backoff.NewRetry(retryPushNum, backoff.NewConstant(retryPushInterval))
433450
for branch, events := range branchHandledEvents {
@@ -603,6 +620,7 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
603620
}
604621

605622
// commitFiles commits changes if the data in Git is different from the latest event.
623+
// If there are no changes to commit, it returns errNoChanges.
606624
func (w *watcher) commitFiles(ctx context.Context, latestEvent *model.Event, eventName, commitMsg, gitPath string, replacements []config.EventWatcherReplacement, repo git.Repo, newBranch bool) (string, error) {
607625
// Determine files to be changed by comparing with the latest event.
608626
changes := make(map[string][]byte, len(replacements))
@@ -641,7 +659,7 @@ func (w *watcher) commitFiles(ctx context.Context, latestEvent *model.Event, eve
641659
changes[filePath] = newContent
642660
}
643661
if len(changes) == 0 {
644-
return "", nil
662+
return "", errNoChanges
645663
}
646664

647665
args := argsTemplate{

0 commit comments

Comments
 (0)