Skip to content
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
9 changes: 6 additions & 3 deletions pkg/app/piped/appconfigreporter/appconfigreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,13 @@ func (r *Reporter) readApplicationInfo(repoDir, appDirRelPath, cfgFilename strin
if spec.Name == "" {
return nil, fmt.Errorf("missing application name")
}
kind, ok := cfg.Kind.ToApplicationKind()
if !ok {
return nil, fmt.Errorf("%q is not application config kind", cfg.Kind)
}
return &model.ApplicationInfo{
Name: spec.Name,
// TODO: Convert Kind string into dedicated type
//Kind: cfg.Kind,
Name: spec.Name,
Kind: kind,
Labels: spec.Labels,
Path: appDirRelPath,
ConfigFilename: cfgFilename,
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/creasty/defaults"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -67,6 +68,18 @@ const (
KindEventWatcher Kind = "EventWatcher"
)

// ToApplicationKind converts itself into model.ApplicationKind if it's for an application config.
// Return false as a second returned value if it's not an application config.
func (k Kind) ToApplicationKind() (model.ApplicationKind, bool) {
upper := strings.ToUpper(string(k))
kind := strings.TrimSuffix(upper, "APP")
appKind, ok := model.ApplicationKind_value[kind]
if !ok {
return -1, false
}
return model.ApplicationKind(appKind), true
}

var (
ErrNotFound = errors.New("not found")
)
Expand Down
31 changes: 31 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/pipe-cd/pipe/pkg/model"
)

func TestUnmarshalConfig(t *testing.T) {
Expand Down Expand Up @@ -79,3 +81,32 @@ func TestUnmarshalConfig(t *testing.T) {
func newBoolPointer(v bool) *bool {
return &v
}

func TestKind_ToApplicationKind(t *testing.T) {
testcases := []struct {
name string
k Kind
want model.ApplicationKind
wantOk bool
}{
{
name: "App config",
k: KindKubernetesApp,
want: model.ApplicationKind_KUBERNETES,
wantOk: true,
},
{
name: "Not an app config",
k: KindPiped,
want: -1,
wantOk: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
got, gotOk := tc.k.ToApplicationKind()
assert.Equal(t, tc.want, got)
assert.Equal(t, tc.wantOk, gotOk)
})
}
}