-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(db): Write git author in esl metadata and pass it to transforme…
…rs (#1700) The cd-services writes git author name & email in the metadata field of esl json. The manifest-export service reads it and passes it to transformers, and uses it for creating git commits.
- Loading branch information
Showing
6 changed files
with
323 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package db | |
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
@@ -1434,6 +1435,107 @@ func TestReadWriteEnvironment(t *testing.T) { | |
}) | ||
} | ||
} | ||
func TestReadWriteEslEvent(t *testing.T) { | ||
const envName = "dev" | ||
const appName = "my-app" | ||
const lockId = "ui-v2-ke1up" | ||
const message = "test" | ||
const authorName = "testauthor" | ||
const authorEmail = "[email protected]" | ||
const evtType = EvtCreateApplicationVersion | ||
expectedJson, _ := json.Marshal(map[string]interface{}{ | ||
"env": envName, | ||
"app": appName, | ||
"lockId": lockId, | ||
"message": message, | ||
"metadata": map[string]string{ | ||
"authorEmail": authorEmail, | ||
"authorName": authorName, | ||
}, | ||
}) | ||
expectedJsonWithoutMetadata, _ := json.Marshal(map[string]interface{}{ | ||
"env": envName, | ||
"app": appName, | ||
"lockId": lockId, | ||
"message": message, | ||
"metadata": map[string]string{ | ||
"authorEmail": "", | ||
"authorName": "", | ||
}, | ||
}) | ||
|
||
tcs := []struct { | ||
Name string | ||
EventType EventType | ||
EventData *map[string]string | ||
EventMetadata ESLMetadata | ||
ExpectedEsl EslEventRow | ||
}{ | ||
{ | ||
Name: "Write and read", | ||
EventType: evtType, | ||
EventData: &map[string]string{ | ||
"env": envName, | ||
"app": appName, | ||
"lockId": lockId, | ||
"message": message, | ||
}, | ||
EventMetadata: ESLMetadata{ | ||
AuthorName: authorName, | ||
AuthorEmail: authorEmail, | ||
}, | ||
ExpectedEsl: EslEventRow{ | ||
EventType: evtType, | ||
EventJson: string(expectedJson), | ||
}, | ||
}, | ||
{ | ||
Name: "Write and read with nil metadata", | ||
EventType: evtType, | ||
EventData: &map[string]string{ | ||
"env": envName, | ||
"app": appName, | ||
"lockId": lockId, | ||
"message": message, | ||
}, | ||
ExpectedEsl: EslEventRow{ | ||
EventType: evtType, | ||
EventJson: string(expectedJsonWithoutMetadata), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
ctx := testutil.MakeTestContext() | ||
dbHandler := setupDB(t) | ||
err := dbHandler.WithTransaction(ctx, func(ctx context.Context, transaction *sql.Tx) error { | ||
err := dbHandler.DBWriteEslEventInternal(ctx, tc.EventType, transaction, tc.EventData, tc.EventMetadata) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
actual, err := dbHandler.DBReadEslEventInternal(ctx, transaction, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if diff := cmp.Diff(tc.ExpectedEsl.EventType, actual.EventType); diff != "" { | ||
t.Fatalf("event type mismatch (-want, +got):\n%s", diff) | ||
} | ||
if diff := cmp.Diff(tc.ExpectedEsl.EventJson, actual.EventJson); diff != "" { | ||
t.Fatalf("event json mismatch (-want, +got):\n%s", diff) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatalf("transaction error: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestReadWriteAllEnvironments(t *testing.T) { | ||
type TestCase struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,6 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/freiheit-com/kuberpult/pkg/argocd" | ||
"github.com/freiheit-com/kuberpult/pkg/config" | ||
"github.com/freiheit-com/kuberpult/pkg/db" | ||
"github.com/freiheit-com/kuberpult/pkg/mapper" | ||
time2 "github.com/freiheit-com/kuberpult/pkg/time" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
@@ -37,6 +32,12 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"github.com/freiheit-com/kuberpult/pkg/argocd" | ||
"github.com/freiheit-com/kuberpult/pkg/config" | ||
"github.com/freiheit-com/kuberpult/pkg/db" | ||
"github.com/freiheit-com/kuberpult/pkg/mapper" | ||
time2 "github.com/freiheit-com/kuberpult/pkg/time" | ||
|
||
"github.com/freiheit-com/kuberpult/pkg/grpc" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
|
@@ -546,8 +547,8 @@ func CombineArray(others []*TransformerResult) *TransformerResult { | |
return r | ||
} | ||
|
||
func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx, transformers ...Transformer) (*TransformerResult, *TransformerBatchApplyError) { | ||
commitMsg, state, changes, applyErr := r.ApplyTransformersInternal(ctx, transaction, transformers...) | ||
func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx, transformer Transformer) (*TransformerResult, *TransformerBatchApplyError) { | ||
commitMsg, state, changes, applyErr := r.ApplyTransformersInternal(ctx, transaction, transformer) | ||
if applyErr != nil { | ||
return nil, applyErr | ||
} | ||
|
@@ -565,10 +566,16 @@ func (r *repository) ApplyTransformers(ctx context.Context, transaction *sql.Tx, | |
When: time.Now(), | ||
} | ||
|
||
// TODO this will be handled in Ref SRX-PA568W | ||
transformerMetadata := transformer.GetMetadata() | ||
if transformerMetadata.AuthorEmail == "" || transformerMetadata.AuthorName == "" { | ||
return nil, &TransformerBatchApplyError{ | ||
TransformerError: fmt.Errorf("transformer metadata is empty"), | ||
Index: -1, | ||
} | ||
} | ||
user := auth.User{ | ||
Email: "[email protected]", | ||
Name: "invalid", | ||
Email: transformerMetadata.AuthorEmail, | ||
Name: transformerMetadata.AuthorName, | ||
DexAuthContext: nil, | ||
} | ||
|
||
|
Oops, something went wrong.