Skip to content

Skip commit and push when no replacement happens in EventWatcher #5310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions pkg/app/piped/eventwatcher/eventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
numToMakeOutdated = 10
)

var errNoChanges = errors.New("nothing to commit")

type Watcher interface {
Run(context.Context) error
}
Expand Down Expand Up @@ -336,6 +338,7 @@
outDatedDuration = time.Hour
gitUpdateEvent = false
branchHandledEvents = make(map[string][]*pipedservice.ReportEventStatusesRequest_Event, len(eventCfgs))
gitNoChangeEvents = make([]*pipedservice.ReportEventStatusesRequest_Event, 0)

Check warning on line 341 in pkg/app/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/eventwatcher/eventwatcher.go#L341

Added line #L341 was not covered by tests
)
for _, e := range eventCfgs {
for _, cfg := range e.Configs {
Expand Down Expand Up @@ -388,7 +391,8 @@
switch handler.Type {
case config.EventWatcherHandlerTypeGitUpdate:
branchName, err := w.commitFiles(ctx, latestEvent, matcher.Name, handler.Config.CommitMessage, e.GitPath, handler.Config.Replacements, tmpRepo, handler.Config.MakePullRequest)
if err != nil {
noChange := errors.Is(err, errNoChanges)
if err != nil && !noChange {

Check warning on line 395 in pkg/app/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/eventwatcher/eventwatcher.go#L394-L395

Added lines #L394 - L395 were not covered by tests
w.logger.Error("failed to commit outdated files", zap.Error(err))
handledEvent := &pipedservice.ReportEventStatusesRequest_Event{
Id: latestEvent.Id,
Expand All @@ -398,12 +402,18 @@
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)
continue
}

handledEvent := &pipedservice.ReportEventStatusesRequest_Event{
Id: latestEvent.Id,
Status: model.EventStatus_EVENT_SUCCESS,
StatusDescription: fmt.Sprintf("Successfully updated %d files in the %q repository", len(handler.Config.Replacements), repoID),
Id: latestEvent.Id,
Status: model.EventStatus_EVENT_SUCCESS,
}
if noChange {
handledEvent.StatusDescription = "Nothing to commit"
gitNoChangeEvents = append(gitNoChangeEvents, handledEvent)
} else {
handledEvent.StatusDescription = fmt.Sprintf("Successfully updated %d files in the %q repository", len(handler.Config.Replacements), repoID)
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)

Check warning on line 415 in pkg/app/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/eventwatcher/eventwatcher.go#L407-L415

Added lines #L407 - L415 were not covered by tests
}
branchHandledEvents[branchName] = append(branchHandledEvents[branchName], handledEvent)
if latestEvent.CreatedAt > maxTimestamp {
maxTimestamp = latestEvent.CreatedAt
}
Expand All @@ -428,6 +438,13 @@
return nil
}

if len(gitNoChangeEvents) > 0 {
if _, err := w.apiClient.ReportEventStatuses(ctx, &pipedservice.ReportEventStatusesRequest{Events: gitNoChangeEvents}); err != nil {
w.logger.Error("failed to report event statuses", zap.Error(err))
}
w.executionMilestoneMap.Store(repoID, maxTimestamp)

Check warning on line 445 in pkg/app/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/eventwatcher/eventwatcher.go#L441-L445

Added lines #L441 - L445 were not covered by tests
}

var responseError error
retry := backoff.NewRetry(retryPushNum, backoff.NewConstant(retryPushInterval))
for branch, events := range branchHandledEvents {
Expand Down Expand Up @@ -603,6 +620,7 @@
}

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

Check warning on line 662 in pkg/app/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/eventwatcher/eventwatcher.go#L662

Added line #L662 was not covered by tests
}

args := argsTemplate{
Expand Down
Loading