Skip to content

Commit

Permalink
Merge pull request #1835 from keboola/petr-hosek-PSGO-664
Browse files Browse the repository at this point in the history
CLI: Fix ignoring branch name for diff
  • Loading branch information
hosekpeter committed Jul 1, 2024
2 parents 6c25648 + 8855283 commit 4f1bc71
Show file tree
Hide file tree
Showing 44 changed files with 269 additions and 52 deletions.
65 changes: 54 additions & 11 deletions internal/pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ import (
"github.com/keboola/keboola-as-code/internal/pkg/utils/strhelper"
)

type Option func(c *Config)

type Config struct {
allowTargetEnv bool
}

func WithIgnoreBranchName(allowTargetEnv bool) Option {
return func(c *Config) {
c.allowTargetEnv = allowTargetEnv
}
}

type typeName string

type Differ struct {
objects model.ObjectStates
results []*Result // diff results
typeCache map[typeName][]*reflecthelper.StructField // reflection cache
errors errors.MultiError
objects model.ObjectStates
results []*Result // diff results
typeCache map[typeName][]*reflecthelper.StructField // reflection cache
errors errors.MultiError
allowTargetEnv bool // option
}

type ResultState int
Expand Down Expand Up @@ -58,10 +71,17 @@ type Results struct {
Objects model.ObjectStates
}

func NewDiffer(objects model.ObjectStates) *Differ {
func NewDiffer(objects model.ObjectStates, option ...Option) *Differ {
config := Config{}

for _, o := range option {
o(&config)
}

return &Differ{
objects: objects,
typeCache: make(map[typeName][]*reflecthelper.StructField),
objects: objects,
allowTargetEnv: config.allowTargetEnv,
typeCache: make(map[typeName][]*reflecthelper.StructField),
}
}

Expand Down Expand Up @@ -149,11 +169,23 @@ func (d *Differ) diffState(state model.ObjectState) (*Result, error) {
}

// Diff
if remoteState.Kind().IsBranch() {
result = d.resultFn(result, state, diffFields, remoteValues, localValues, d.newBranchOptions)
} else {
result = d.resultFn(result, state, diffFields, remoteValues, localValues, d.newOptions)
}

return result, nil
}

func (d *Differ) resultFn(result *Result, state model.ObjectState, diffFields []*reflecthelper.StructField, remoteValues, localValues reflect.Value, opts func(r *Reporter) cmp.Options) *Result {
state.RemoteState()
for _, field := range diffFields {
reporter := d.diffValues(
state,
remoteValues.FieldByName(field.StructField.Name).Interface(),
localValues.FieldByName(field.StructField.Name).Interface(),
opts,
)
diffStr := reporter.String()
if len(diffStr) > 0 {
Expand All @@ -169,16 +201,27 @@ func (d *Differ) diffState(state model.ObjectState) (*Result, error) {
} else {
result.State = ResultEqual
}

return result, nil
return result
}

func (d *Differ) diffValues(objectState model.ObjectState, remoteValue, localValue any) *Reporter {
func (d *Differ) diffValues(objectState model.ObjectState, remoteValue, localValue any, opts func(r *Reporter) cmp.Options) *Reporter {
reporter := newReporter(objectState, d.objects)
cmp.Diff(remoteValue, localValue, d.newOptions(reporter))
cmp.Diff(remoteValue, localValue, opts(reporter))
return reporter
}

func (d *Differ) newBranchOptions(reporter *Reporter) cmp.Options {
options := d.newOptions(reporter)
options = append(options, cmp.Transformer("name", func(s string) string {
if d.allowTargetEnv {
return ""
}

return s
}))
return options
}

func (d *Differ) newOptions(reporter *Reporter) cmp.Options {
return cmp.Options{
cmp.Reporter(reporter),
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func TestDiffRelations(t *testing.T) {
assert.NoError(t, projectState.Set(objectState))

differ := NewDiffer(projectState)
reporter := differ.diffValues(objectState, objectState.Remote.Relations, objectState.Local.Relations)
reporter := differ.diffValues(objectState, objectState.Remote.Relations, objectState.Local.Relations, differ.newOptions)
expected := `
- manifest side relation mocked key "foo"
+ manifest side relation mocked key "bar"
Expand Down Expand Up @@ -454,7 +454,7 @@ func TestDiffTransformation(t *testing.T) {

// Transformation
differ := NewDiffer(projectState)
reporter := differ.diffValues(configState, configState.Remote.Transformation, configState.Local.Transformation)
reporter := differ.diffValues(configState, configState.Remote.Transformation, configState.Local.Transformation, differ.newOptions)
expected := `
blocks/001-my-block:
- # Block 1
Expand All @@ -472,7 +472,7 @@ func TestDiffTransformation(t *testing.T) {
assert.Equal(t, strings.Trim(expected, "\n"), reporter.String())

// SharedCode link
reporter = differ.diffValues(configState, configState.Remote.SharedCode, configState.Local.SharedCode)
reporter = differ.diffValues(configState, configState.Remote.SharedCode, configState.Local.SharedCode, differ.newOptions)
expected = `
- (null)
+ 12345
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestDiffSharedCode(t *testing.T) {

// Transformation
differ := NewDiffer(projectState)
reporter := differ.diffValues(configRowState, configRowState.Remote.SharedCode, configRowState.Local.SharedCode)
reporter := differ.diffValues(configRowState, configRowState.Remote.SharedCode, configRowState.Local.SharedCode, differ.newOptions)
expected := `
- SELECT 4;
+ SELECT 1;
Expand Down Expand Up @@ -681,7 +681,7 @@ func TestDiffOrchestration(t *testing.T) {
assert.NoError(t, projectState.Set(configState))

differ := NewDiffer(projectState)
reporter := differ.diffValues(configState, configState.Remote.Orchestration, configState.Local.Orchestration)
reporter := differ.diffValues(configState, configState.Remote.Orchestration, configState.Local.Orchestration, differ.newOptions)
expected := `
phases/001-phase:
# 001 Phase
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestDiffMap(t *testing.T) {
assert.NoError(t, projectState.Set(configState))

differ := NewDiffer(projectState)
reporter := differ.diffValues(configState, configState.Remote.Content, configState.Local.Content)
reporter := differ.diffValues(configState, configState.Remote.Content, configState.Local.Content, differ.newOptions)
expected := `
foo.bar:
- "value"
Expand Down
4 changes: 0 additions & 4 deletions internal/pkg/plan/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ func NewPlan(diffResults *diff.Results, allowTargetEnv bool) (*diffop.Plan, erro
if result.ChangedFields.String() == "relations" && !result.ChangedFields.Get("relations").HasPath("InAPI") {
continue
}

if allowTargetEnv && result.ChangedFields.Has("name") {
continue
}
plan.Add(result, diffop.ActionSaveRemote)
case diff.ResultOnlyInLocal:
plan.Add(result, diffop.ActionSaveRemote)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/operation/project/sync/diff/create/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type dependencies interface {
Telemetry() telemetry.Telemetry
}

func Run(ctx context.Context, o Options, d dependencies) (results *diff.Results, err error) {
func Run(ctx context.Context, o Options, d dependencies, opts ...diff.Option) (results *diff.Results, err error) {
ctx, span := d.Telemetry().Tracer().Start(ctx, "keboola.go.operation.project.sync.diff.create")
defer span.End(&err)

differ := diff.NewDiffer(o.Objects)
differ := diff.NewDiffer(o.Objects, opts...)
results, err = differ.Diff()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/operation/project/sync/diff/printdiff/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Run(ctx context.Context, projectState *project.State, o Options, d dependen
logger := d.Logger()

// Diff
results, err = createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d)
results, err = createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d, diff.WithIgnoreBranchName(projectState.ProjectManifest().AllowTargetENV()))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/operation/project/sync/pull/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/keboola/go-client/pkg/keboola"

"github.com/keboola/keboola-as-code/internal/pkg/diff"
"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/plan/pull"
"github.com/keboola/keboola-as-code/internal/pkg/project"
Expand Down Expand Up @@ -49,7 +50,7 @@ func Run(ctx context.Context, projectState *project.State, o Options, d dependen
logger := d.Logger()

// Diff
results, err := createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d)
results, err := createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d, diff.WithIgnoreBranchName(projectState.ProjectManifest().AllowTargetENV()))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/operation/project/sync/push/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/keboola/go-client/pkg/keboola"

"github.com/keboola/keboola-as-code/internal/pkg/diff"
"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/plan/push"
"github.com/keboola/keboola-as-code/internal/pkg/project"
Expand Down Expand Up @@ -65,7 +66,7 @@ func Run(ctx context.Context, projectState *project.State, o Options, d dependen
}

// Diff
results, err := createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d)
results, err := createDiff.Run(ctx, createDiff.Options{Objects: projectState}, d, diff.WithIgnoreBranchName(projectState.ProjectManifest().AllowTargetENV()))
if err != nil {
return err
}
Expand Down
44 changes: 18 additions & 26 deletions pkg/lib/operation/template/sync/diff/create/operation.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package create

import (
"context"
// type Options struct {
// Objects model.ObjectStates
//}
//
// type dependencies interface {
// Telemetry() telemetry.Telemetry
//}

"github.com/keboola/keboola-as-code/internal/pkg/diff"
"github.com/keboola/keboola-as-code/internal/pkg/model"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry"
)

type Options struct {
Objects model.ObjectStates
}

type dependencies interface {
Telemetry() telemetry.Telemetry
}

func Run(ctx context.Context, o Options, d dependencies) (results *diff.Results, err error) {
ctx, span := d.Telemetry().Tracer().Start(ctx, "keboola.go.operation.template.sync.diff.create")
defer span.End(&err)

differ := diff.NewDiffer(o.Objects)
results, err = differ.Diff()
if err != nil {
return nil, err
}
return results, nil
}
// func Run(ctx context.Context, o Options, d dependencies) (results *diff.Results, err error) {
// ctx, span := d.Telemetry().Tracer().Start(ctx, "keboola.go.operation.template.sync.diff.create")
// defer span.End(&err)
//
// differ := diff.NewDiffer(o.Objects)
// results, err = differ.Diff()
// if err != nil {
// return nil, err
// }
// return results, nil
//}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/cli/allow-target-env/push-changed-config-values/args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
push --storage-api-token %%TEST_KBC_STORAGE_API_TOKEN%%
2 changes: 2 additions & 0 deletions test/cli/allow-target-env/push-changed-config-values/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KBC_PROJECT_ID=%%TEST_KBC_PROJECT_ID%%
KBC_BRANCH_ID=%%TEST_BRANCH_MAIN_ID%%
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"branches": [
{
"branch": {
"name": "Main",
"description": "",
"isDefault": true
},
"configs": [
{
"componentId": "keboola.data-apps",
"name": "Data-App",
"description": "test fixture",
"changeDescription": "Updated from #KeboolaCLI",
"configuration": {
"foo": "modified"
},
"rows": [],
"isDisabled": false
},
{
"componentId": "ex-generic-v2",
"name": "empty",
"description": "test fixture",
"configuration": {},
"rows": [],
"isDisabled": false
}
]
}
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Overriding the project ID by the environment variable KBC_PROJECT_ID=%s
Overriding the branch ID by the environment variable KBC_BRANCH_ID=%s
Plan for "push" operation:
* C main/app/keboola.data-apps/data-app | changed: configuration, name
× C main/extractor/ex-generic-v2/empty - SKIPPED
Skipped remote objects deletion, use "--force" to delete them.
Push done.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"version": 2,
"project": {
"id": 1,
"apiHost": "%%TEST_KBC_STORAGE_API_HOST%%"
},
"allowTargetEnv": true,
"sortBy": "path",
"naming": {
"branch": "{branch_name}",
"config": "{component_type}/{component_id}/{config_name}",
"configRow": "rows/{config_row_name}",
"schedulerConfig": "schedules/{config_name}",
"sharedCodeConfig": "_shared/{target_component_id}",
"sharedCodeConfigRow": "codes/{config_row_name}",
"variablesConfig": "variables",
"variablesValuesRow": "values/{config_row_name}",
"dataAppConfig": "app/{component_id}/{config_name}"
},
"allowedBranches": [
"2"
],
"ignoredComponents": [],
"templates": {
"repositories": [
{
"type": "git",
"name": "keboola",
"url": "https://github.com/keboola/keboola-as-code-templates.git",
"ref": "main"
}
]
},
"branches": [
{
"id": 2,
"path": "main"
}
],
"configurations": [
{
"branchId": 2,
"componentId": "keboola.data-apps",
"id": "%%TEST_BRANCH_MAIN_CONFIG_DATA_APP_ID%%",
"path": "app/keboola.data-apps/data-app",
"rows": []
}
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "modified"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Data-App",
"isDisabled": false
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Main",
"isDefault": true
}
Loading

0 comments on commit 4f1bc71

Please sign in to comment.