Skip to content

Commit

Permalink
refactor(commit-message): use complete repo str (#56)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico <rainbowstack@gmail.com>
  • Loading branch information
bluebrown committed May 24, 2024
1 parent 9e596db commit 2a6bc83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
Binary file modified e2e/gitea/testdata/repo.tar.gz
Binary file not shown.
11 changes: 11 additions & 0 deletions e2e/gitea/testdata/repo/stage/argocdapp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: argo-helm-app
spec:
source:
helm:
valuesObject:
image:
tag: "v1" # kobold: tag: ^1; type: semver; part: tag; context: localhost:8080/library/busybox

42 changes: 26 additions & 16 deletions krm/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

func DefaultNodeHandler(_, currentRef, nextRef string, opts Options) (string, error) {
func DefaultNodeHandler(_, currentRef, nextRef string, opts Options) (string, Change, error) {
if currentRef == nextRef {
return currentRef, nil
return currentRef, Change{}, nil
}

fullRef := currentRef
Expand All @@ -19,40 +19,47 @@ func DefaultNodeHandler(_, currentRef, nextRef string, opts Options) (string, er
}
oldRef, err := name.ParseReference(fullRef)
if err != nil {
return currentRef, err
return currentRef, Change{}, err
}

newRef, _, err := ParseImageRefWithDigest(nextRef)
if err != nil {
return currentRef, err
return currentRef, Change{}, err
}

if oldRef.Context().Name() != newRef.Context().Name() {
return currentRef, nil
return currentRef, Change{}, nil
}

ok, err := MatchTag(newRef.Identifier(), opts)
if err != nil {
return currentRef, err
return currentRef, Change{}, err
}

if !ok {
return currentRef, nil
return currentRef, Change{}, nil
}

if _, err := name.ParseReference(nextRef); err != nil {
return currentRef, err
return currentRef, Change{}, err
}

c := Change{
Description: fmt.Sprintf("update image ref %q to %q", currentRef, nextRef),
Registry: newRef.Context().RegistryStr(),
Repo: newRef.Context().RepositoryStr(),
}

if opts.Part == "tag" {
return newRef.Identifier(), nil
return newRef.Identifier(), c, nil
}
return nextRef, nil

return nextRef, c, nil
}

var CommentPrefix = "# kobold:"

type NodeHandler func(key, currentRef, nextRef string, opts Options) (string, error)
type NodeHandler func(key, currentRef, nextRef string, opts Options) (string, Change, error)

type ImageRefUpdateFilter struct {
handler NodeHandler
Expand All @@ -63,6 +70,7 @@ type ImageRefUpdateFilter struct {

type Change struct {
Description string
Registry string
Repo string
}

Expand Down Expand Up @@ -96,23 +104,25 @@ func (i *ImageRefUpdateFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error
}

originalValue := mn.Value.YNode().Value
lastChange := Change{}

for _, imageRef := range i.imageRefs {
v, err := i.handler(mn.Key.YNode().Value, mn.Value.YNode().Value, imageRef, opts)
v, change, err := i.handler(mn.Key.YNode().Value, mn.Value.YNode().Value, imageRef, opts)
if err != nil {
i.Warnings = append(i.Warnings, fmt.Sprintf("failed to update image ref %q: %v", imageRef, err))
continue
}
mn.Value.YNode().Value = v
lastChange = change
}

newValue := mn.Value.YNode().Value
if opts.Context != "" {
newValue = fmt.Sprintf("%s:%s", opts.Context, newValue)
}

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)
i.Changes = append(i.Changes, lastChange)
}

return nil
Expand Down

0 comments on commit 2a6bc83

Please sign in to comment.