Skip to content

Commit

Permalink
Add event context (#5295)
Browse files Browse the repository at this point in the history
* Add contexts to the RegisterEventRequest

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add contexts to model.Event

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Store event context in Control Plane

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Add trailers when commiting on event watcher

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

* Fix for failed build

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>

---------

Signed-off-by: Yoshiki Fujikane <ffjlabo@gmail.com>
Signed-off-by: pipecd-bot <pipecd.dev@gmail.com>
  • Loading branch information
ffjlabo authored and pipecd-bot committed Nov 7, 2024
1 parent eb98030 commit 310b7d2
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 371 deletions.
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 @@ import (
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 @@ func newRegisterCommand(root *command) *cobra.Command {
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")

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("data")
Expand All @@ -61,9 +63,10 @@ func (r *register) run(ctx context.Context, input cli.Input) error {
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,
}

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 @@ 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)
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)
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) {
// 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)
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)
}
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,
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 {
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 @@ 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)
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)
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) {
// 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)
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)
}
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,
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 {
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 @@ func (a *API) RegisterEvent(ctx context.Context, req *apiservice.RegisterEventRe
Name: req.Name,
Data: req.Data,
Labels: req.Labels,
Contexts: req.Contexts,
EventKey: model.MakeEventKey(req.Name, req.Labels),
ProjectId: key.ProjectId,
Status: model.EventStatus_EVENT_NOT_HANDLED,
Expand Down
Loading

0 comments on commit 310b7d2

Please sign in to comment.