Skip to content

Commit

Permalink
feat: Include updated images in commit message (#49)
Browse files Browse the repository at this point in the history
resolve: #49
  • Loading branch information
tsmalls93 authored Apr 22, 2024
1 parent ab14a59 commit aab019c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 15 deletions.
30 changes: 24 additions & 6 deletions krm/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ type NodeHandler func(key, currentRef, nextRef string, opts Options) (string, er
type ImageRefUpdateFilter struct {
handler NodeHandler
imageRefs []string
Changes []string
Changes []Change
Warnings []string
}

type Change struct {
Description string
Repo string
}

// create a new krm filter. The filter will traverse all nodes and invoke the
// handler if a map node with a line comment matching the CommentPrefix is
// found. The handler is responsible for determining if the node should be
Expand Down Expand Up @@ -93,17 +98,30 @@ func (i *ImageRefUpdateFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error
}
mn.Value.YNode().Value = v
}

if originalValue != mn.Value.YNode().Value {
i.Changes = append(i.Changes, fmt.Sprintf("%s: %q -> %q", mn.Key.YNode().Value, originalValue, mn.Value.YNode().Value))
newValue := mn.Value.YNode().Value
if originalValue != newValue {
description := fmt.Sprintf("%s: %q -> %q", mn.Key.YNode().Value, originalValue, newValue)
repo := GetRepoName(newValue)
change := Change{description, repo}
i.Changes = append(i.Changes, change)
}

return nil
})

return nodes, err
}

func GetRepoName(image string) (result string) {
result = ""
s := strings.LastIndex(image, "/")
newS := image[s+1:]
e := strings.Index(newS, ":")
if e == -1 {
return newS
}
result = newS[:e]
return result
}

func ParseImageRefWithDigest(s string) (name.Reference, string, error) {
rawRef, digest, _ := strings.Cut(s, "@")
// NOTE: not checking ok here, to allow the user to use refs without digest
Expand Down
2 changes: 1 addition & 1 deletion krm/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/kio"
)

func Pipeline(ctx context.Context, pkg string, refs ...string) ([]string, []string, error) {
func Pipeline(ctx context.Context, pkg string, refs ...string) ([]Change, []string, error) {
rw := &kio.LocalPackageReadWriter{
PackageFileName: ".krmignore",
PackagePath: pkg,
Expand Down
7 changes: 4 additions & 3 deletions plugin/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/bluebrown/kobold/krm"
"github.com/bluebrown/kobold/store/model"
"go.starlark.net/starlark"
)
Expand All @@ -19,7 +20,7 @@ func NewPostHookRunner() *PostHookRunner {
}
}

func (runner *PostHookRunner) Run(group model.TaskGroup, msg string, changes []string, warnings []string) error {
func (runner *PostHookRunner) Run(group model.TaskGroup, msg string, changes []krm.Change, warnings []string) error {
if group.PostHook == nil {
return nil
}
Expand All @@ -36,7 +37,7 @@ func (runner *PostHookRunner) Run(group model.TaskGroup, msg string, changes []s
return nil
}

func (runner *PostHookRunner) args(group model.TaskGroup, msg string, changes []string, warnings []string) starlark.Tuple {
func (runner *PostHookRunner) args(group model.TaskGroup, msg string, changes []krm.Change, warnings []string) starlark.Tuple {
title, body, ok := strings.Cut(msg, "\n")
if !ok {
title = msg
Expand All @@ -60,7 +61,7 @@ func (runner *PostHookRunner) args(group model.TaskGroup, msg string, changes []

ch := starlark.NewList([]starlark.Value{})
for _, c := range changes {
if err := ch.Append(starlark.String(c)); err != nil {
if err := ch.Append(starlark.String(c.Description)); err != nil {
panic(err)
}
}
Expand Down
18 changes: 14 additions & 4 deletions task/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

// the task handler is the final point of execution. After decoding, debouncing
// and aggregating the events, this handler is resonbible for the actual work.
// and aggregating the events, this handler is responsible for the actual work.
func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner HookRunner) ([]string, error) {
var (
changes []string
changes []krm.Change
warnings []string
msg string
)
Expand All @@ -43,8 +43,10 @@ func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner
g.DestBranch.String = g.RepoUri.Ref
g.DestBranch.Valid = true
}

msg = "chore(kobold): update image refs"
msg, err = GetCommitMessage(changes)
if err != nil {
msg = "chore(kobold): Update image refs"
}

if err := git.Publish(ctx, cache, g.DestBranch.String, msg); err != nil {
return nil, fmt.Errorf("git publish: %w", err)
Expand All @@ -63,6 +65,14 @@ func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner
return warnings, nil
}

func GetCommitMessage(changes []krm.Change) (string, error) {
msg := "chore(kobold): Update"
for _, change := range changes {
msg += " " + change.Repo
}
return msg, nil
}

var _ Handler = KoboldHandler

func PrintHandler(_ context.Context, _ string, g model.TaskGroup, _ HookRunner) ([]string, error) {
Expand Down
62 changes: 62 additions & 0 deletions task/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package task

import (
"testing"

"github.com/bluebrown/kobold/krm"
)

func TestGetCommitMessage(t *testing.T) {
type args struct {
changes []krm.Change
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "single image",
args: args{
changes: []krm.Change{
{
Description: "busybox:1.0.0 -> busybox:1.0.1",
Repo: "busybox",
},
},
},
want: "chore(kobold): Update busybox",
wantErr: false,
},
{
name: "multiple images",
args: args{
changes: []krm.Change{
{
Description: "busybox:1.0.0 -> busybox:1.0.1",
Repo: "busybox",
},
{
Description: "somerepo:2.0.0 -> somerepo:2.0.1",
Repo: "somerepo",
},
},
},
want: "chore(kobold): Update busybox somerepo",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetCommitMessage(tt.args.changes)
if (err != nil) != tt.wantErr {
t.Errorf("GetCommitMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetCommitMessage() got = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/bluebrown/kobold/krm"
"github.com/bluebrown/kobold/store/model"
)

Expand All @@ -21,7 +22,7 @@ type DecoderRunner interface {
}

type HookRunner interface {
Run(group model.TaskGroup, msg string, changes []string, warnings []string) error
Run(group model.TaskGroup, msg string, changes []krm.Change, warnings []string) error
}

type Handler func(ctx context.Context, hostPath string, g model.TaskGroup, hook HookRunner) ([]string, error)
Expand Down

0 comments on commit aab019c

Please sign in to comment.