Skip to content
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

Add event context #5295

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions pkg/app/pipectl/cmd/event/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
type register struct {
root *command

name string
data string
labels map[string]string
name string
data string
labels map[string]string
contexts map[string]string
}

func newRegisterCommand(root *command) *cobra.Command {
Expand All @@ -46,6 +47,7 @@
cmd.Flags().StringVar(&r.name, "name", r.name, "The name of event.")
cmd.Flags().StringVar(&r.data, "data", r.data, "The string value of event data.")
cmd.Flags().StringToStringVar(&r.labels, "labels", r.labels, "The list of labels for event. Format: key=value,key2=value2")
cmd.Flags().StringToStringVar(&r.contexts, "contexts", r.contexts, "The list of the values for the event context. Format: key=value,key2=value2")

Check warning on line 50 in pkg/app/pipectl/cmd/event/register.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/event/register.go#L50

Added line #L50 was not covered by tests

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("data")
Expand All @@ -61,9 +63,10 @@
defer cli.Close()

req := &apiservice.RegisterEventRequest{
Name: r.name,
Data: r.data,
Labels: r.labels,
Name: r.name,
Data: r.data,
Labels: r.labels,
Contexts: r.contexts,

Check warning on line 69 in pkg/app/pipectl/cmd/event/register.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/event/register.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}

res, err := cli.RegisterEvent(ctx, req)
Expand Down
16 changes: 9 additions & 7 deletions pkg/app/piped/eventwatcher/eventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"context"
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"regexp/syntax"
Expand Down Expand Up @@ -386,7 +387,7 @@
}
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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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 @@
})
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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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 @@
}

// 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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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 @@
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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L629 was not covered by tests
}
if err != nil {
return "", err
Expand All @@ -644,12 +645,13 @@
}

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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/piped/eventwatcher/eventwatcher.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/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
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 @@
"context"
"errors"
"fmt"
"maps"
"os"
"path/filepath"
"regexp/syntax"
Expand Down Expand Up @@ -386,7 +387,7 @@
}
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 @@
})
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 @@
}

// 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 @@
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 @@
}

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
1 change: 1 addition & 0 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@
Name: req.Name,
Data: req.Data,
Labels: req.Labels,
Contexts: req.Contexts,

Check warning on line 795 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L795

Added line #L795 was not covered by tests
EventKey: model.MakeEventKey(req.Name, req.Labels),
ProjectId: key.ProjectId,
Status: model.EventStatus_EVENT_NOT_HANDLED,
Expand Down
Loading
Loading