Skip to content

Commit

Permalink
Fix for failed build
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
  • Loading branch information
ffjlabo committed Oct 25, 2024
1 parent 313abc4 commit 9c9aff8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
16 changes: 9 additions & 7 deletions pkg/app/pipedv1/eventwatcher/eventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"regexp/syntax"
Expand Down Expand Up @@ -386,7 +387,7 @@ func (w *watcher) execute(ctx context.Context, repo git.Repo, repoID string, eve
}
switch handler.Type {
case config.EventWatcherHandlerTypeGitUpdate:
branchName, err := w.commitFiles(ctx, latestEvent.Data, matcher.Name, handler.Config.CommitMessage, e.GitPath, handler.Config.Replacements, tmpRepo, handler.Config.MakePullRequest)
branchName, err := w.commitFiles(ctx, latestEvent, matcher.Name, handler.Config.CommitMessage, e.GitPath, handler.Config.Replacements, tmpRepo, handler.Config.MakePullRequest)

Check warning on line 390 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L390

Added line #L390 was not covered by tests
if err != nil {
w.logger.Error("failed to commit outdated files", zap.Error(err))
handledEvent := &pipedservice.ReportEventStatusesRequest_Event{
Expand Down Expand Up @@ -538,7 +539,7 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
})
continue
}
_, err := w.commitFiles(ctx, latestEvent.Data, e.Name, commitMsg, "", e.Replacements, tmpRepo, false)
_, err := w.commitFiles(ctx, latestEvent, e.Name, commitMsg, "", e.Replacements, tmpRepo, false)

Check warning on line 542 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L542

Added line #L542 was not covered by tests
if err != nil {
w.logger.Error("failed to commit outdated files", zap.Error(err))
handledEvents = append(handledEvents, &pipedservice.ReportEventStatusesRequest_Event{
Expand Down Expand Up @@ -602,7 +603,7 @@ func (w *watcher) updateValues(ctx context.Context, repo git.Repo, repoID string
}

// commitFiles commits changes if the data in Git is different from the latest event.
func (w *watcher) commitFiles(ctx context.Context, latestData, eventName, commitMsg, gitPath string, replacements []config.EventWatcherReplacement, repo git.Repo, newBranch bool) (string, error) {
func (w *watcher) commitFiles(ctx context.Context, latestEvent *model.Event, eventName, commitMsg, gitPath string, replacements []config.EventWatcherReplacement, repo git.Repo, newBranch bool) (string, error) {

Check warning on line 606 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L606

Added line #L606 was not covered by tests
// Determine files to be changed by comparing with the latest event.
changes := make(map[string][]byte, len(replacements))
for _, r := range replacements {
Expand All @@ -619,13 +620,13 @@ func (w *watcher) commitFiles(ctx context.Context, latestData, eventName, commit
path := filepath.Join(repo.GetPath(), filePath)
switch {
case r.YAMLField != "":
newContent, upToDate, err = modifyYAML(path, r.YAMLField, latestData)
newContent, upToDate, err = modifyYAML(path, r.YAMLField, latestEvent.Data)

Check warning on line 623 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L623

Added line #L623 was not covered by tests
case r.JSONField != "":
// TODO: Empower Event watcher to parse JSON format
case r.HCLField != "":
// TODO: Empower Event watcher to parse HCL format
case r.Regex != "":
newContent, upToDate, err = modifyText(path, r.Regex, latestData)
newContent, upToDate, err = modifyText(path, r.Regex, latestEvent.Data)

Check warning on line 629 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L629

Added line #L629 was not covered by tests
}
if err != nil {
return "", err
Expand All @@ -644,12 +645,13 @@ func (w *watcher) commitFiles(ctx context.Context, latestData, eventName, commit
}

args := argsTemplate{
Value: latestData,
Value: latestEvent.Data,

Check warning on line 648 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L648

Added line #L648 was not covered by tests
EventName: eventName,
}
commitMsg = parseCommitMsg(commitMsg, args)
branch := makeBranchName(newBranch, eventName, repo.GetClonedBranch())
if err := repo.CommitChanges(ctx, branch, commitMsg, newBranch, changes); err != nil {
trailers := maps.Clone(latestEvent.Contexts)
if err := repo.CommitChanges(ctx, branch, commitMsg, newBranch, changes, trailers); err != nil {

Check warning on line 654 in pkg/app/pipedv1/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/eventwatcher/eventwatcher.go#L653-L654

Added lines #L653 - L654 were not covered by tests
return "", fmt.Errorf("failed to perform git commit: %w", err)
}
w.logger.Info(fmt.Sprintf("event watcher will update values of Event %q", eventName))
Expand Down
8 changes: 4 additions & 4 deletions pkg/git/gittest/git.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (r *repo) Push(ctx context.Context, branch string) error {
}

// CommitChanges commits some changes into a branch.
func (r *repo) CommitChanges(ctx context.Context, branch, message string, newBranch bool, changes map[string][]byte) error {
func (r *repo) CommitChanges(ctx context.Context, branch, message string, newBranch bool, changes map[string][]byte, trailers map[string]string) error {
if newBranch {
if err := r.checkoutNewBranch(ctx, branch); err != nil {
return fmt.Errorf("failed to checkout new branch, branch: %v, error: %v", branch, err)
Expand All @@ -248,7 +248,7 @@ func (r *repo) CommitChanges(ctx context.Context, branch, message string, newBra
}
}
// Commit the changes.
if err := r.addCommit(ctx, message); err != nil {
if err := r.addCommit(ctx, message, trailers); err != nil {
return fmt.Errorf("failed to commit, branch: %s, error: %v", branch, err)
}
return nil
Expand Down
9 changes: 5 additions & 4 deletions pkg/git/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestChangedFiles(t *testing.T) {
err = os.WriteFile(readmeFilePath, []byte("new content"), os.ModePerm)
require.NoError(t, err)

err = r.addCommit(ctx, "Added new file")
err = r.addCommit(ctx, "Added new file", nil)
require.NoError(t, err)

headCommit, err := r.GetCommitForRev(ctx, "HEAD")
Expand Down Expand Up @@ -105,16 +105,17 @@ func TestAddCommit(t *testing.T) {
err = os.WriteFile(path, []byte("content"), os.ModePerm)
require.NoError(t, err)

err = r.addCommit(ctx, "Added new file")
err = r.addCommit(ctx, "Added new file", map[string]string{"Test-Hoge": "fuga"})
require.NoError(t, err)

err = r.addCommit(ctx, "No change")
err = r.addCommit(ctx, "No change", nil)
require.Equal(t, ErrNoChange, err)

commits, err = r.ListCommits(ctx, "")
require.NoError(t, err)
require.Equal(t, 2, len(commits))
assert.Equal(t, "Added new file", commits[0].Message)
assert.Equal(t, "Test-Hoge: fuga", commits[0].Body)
}

func TestCommitChanges(t *testing.T) {
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestCommitChanges(t *testing.T) {
"README.md": []byte("new-readme"),
"a/b/c/new.txt": []byte("new-hello"),
}
err = r.CommitChanges(ctx, "new-branch", "New commit with changes", true, changes)
err = r.CommitChanges(ctx, "new-branch", "New commit with changes", true, changes, nil)
require.NoError(t, err)

commits, err = r.ListCommits(ctx, "")
Expand Down

0 comments on commit 9c9aff8

Please sign in to comment.