Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetProductSummary endpoint to allow envGroups as well as environments #1181

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion pkg/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ message GetGitTagsResponse {

message GetProductSummaryRequest {
string commit_hash = 1;
string environment = 2;
optional string environment = 2;
optional string environmentGroup = 3;
}

message GetProductSummaryResponse {
Expand All @@ -35,6 +36,7 @@ message ProductSummary {
string version = 2;
string commit_id = 3;
string displayVersion = 4;
string environment = 5;
}

message TagData {
Expand Down
60 changes: 44 additions & 16 deletions services/cd-service/pkg/service/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ func (s *TagsServer) GetGitTags(ctx context.Context, in *api.GetGitTagsRequest)
}

func (s *TagsServer) GetProductSummary(ctx context.Context, in *api.GetProductSummaryRequest) (*api.GetProductSummaryResponse, error) {
if in.Environment == "" {
return nil, fmt.Errorf("Must have an environment to get the product summary for")
if in.Environment == nil && in.EnvironmentGroup == nil {
return nil, fmt.Errorf("Must have an environment or enviorenmentGroup to get the product summary for")
gsandok marked this conversation as resolved.
Show resolved Hide resolved
}
if in.Environment != nil && in.EnvironmentGroup != nil {
if *in.Environment != "" && *in.EnvironmentGroup != "" {
return nil, fmt.Errorf("Can not have both an environment and enviorenmentGroup to get the product summary for")
gsandok marked this conversation as resolved.
Show resolved Hide resolved
}
}
if in.CommitHash == "" {
return nil, fmt.Errorf("Must have a commit to get the product summary for")
Expand All @@ -51,32 +56,55 @@ func (s *TagsServer) GetProductSummary(ctx context.Context, in *api.GetProductSu
if err != nil {
return nil, fmt.Errorf("unable to get overview for %s: %v", in.CommitHash, err)
}

var summaryFromEnv []api.ProductSummary
for _, group := range response.EnvironmentGroups {
for _, env := range group.Environments {
if env.Name == in.Environment {
for _, app := range env.Applications {
summaryFromEnv = append(summaryFromEnv, api.ProductSummary{App: app.Name, Version: strconv.FormatUint(app.Version, 10)})
if in.Environment != nil && *in.Environment != "" {
for _, group := range response.EnvironmentGroups {
for _, env := range group.Environments {
if env.Name == *in.Environment {
for _, app := range env.Applications {
summaryFromEnv = append(summaryFromEnv, api.ProductSummary{App: app.Name, Version: strconv.FormatUint(app.Version, 10), Environment: *in.Environment})
}
}
}
}
if len(summaryFromEnv) == 0 {
return nil, nil
}
sort.Slice(summaryFromEnv, func(i, j int) bool {
a := summaryFromEnv[i].App
b := summaryFromEnv[j].App
return a < b
})
} else {
for _, group := range response.EnvironmentGroups {
if *in.EnvironmentGroup == group.EnvironmentGroupName {
for _, env := range group.Environments {
var singleEnvSummary []api.ProductSummary
for _, app := range env.Applications {
singleEnvSummary = append(singleEnvSummary, api.ProductSummary{App: app.Name, Version: strconv.FormatUint(app.Version, 10), Environment: env.Name})
}
sort.Slice(singleEnvSummary, func(i, j int) bool {
a := singleEnvSummary[i].App
b := singleEnvSummary[j].App
return a < b
})
summaryFromEnv = append(summaryFromEnv, singleEnvSummary...)
}
}
}
if len(summaryFromEnv) == 0 {
return nil, nil
}
}
if len(summaryFromEnv) == 0 {
return nil, fmt.Errorf("environment %s not found", in.Environment)
}
sort.Slice(summaryFromEnv, func(i, j int) bool {
a := summaryFromEnv[i].App
b := summaryFromEnv[j].App
return a < b
})

var productVersion []*api.ProductSummary
for _, row := range summaryFromEnv {
for _, app := range response.Applications {
if row.App == app.Name {
for _, release := range app.Releases {
if strconv.FormatUint(release.Version, 10) == row.Version {
productVersion = append(productVersion, &api.ProductSummary{App: row.App, Version: row.Version, CommitId: release.SourceCommitId, DisplayVersion: release.DisplayVersion})
productVersion = append(productVersion, &api.ProductSummary{App: row.App, Version: row.Version, CommitId: release.SourceCommitId, DisplayVersion: release.DisplayVersion, Environment: row.Environment})
break
}
}
Expand Down
97 changes: 85 additions & 12 deletions services/cd-service/pkg/service/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,39 @@ import (
"testing"

"github.com/freiheit-com/kuberpult/pkg/api"
"github.com/freiheit-com/kuberpult/pkg/ptr"
"github.com/freiheit-com/kuberpult/pkg/testutil"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/config"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository"
)

func TestGetProductOverview(t *testing.T) {
var dev = "dev"
tcs := []struct {
Name string
givenEnv string
givenEnv *string
givenEnvGroup *string
expectedProductSummary []api.ProductSummary
expectedErr error
Setup []repository.Transformer
}{
{
Name: "get Product Overview with no env",
expectedErr: fmt.Errorf("Must have an environment to get the product summary for"),
Name: "get Product Overview with no env or envGroup",
expectedErr: fmt.Errorf("Must have an environment or enviorenmentGroup to get the product summary for"),
gsandok marked this conversation as resolved.
Show resolved Hide resolved
},
{
Name: "get Product Overview with no commitHash",
givenEnv: "testing",
givenEnv: ptr.FromString("testing"),
expectedErr: fmt.Errorf("Must have a commit to get the product summary for"),
},
{
Name: "get Product Overview as expected",
givenEnv: "development",
Name: "get Product Overview with both env and envGroup",
givenEnv: ptr.FromString("testing"),
givenEnvGroup: ptr.FromString("testingGroup"),
expectedErr: fmt.Errorf("Can not have both an environment and enviorenmentGroup to get the product summary for"),
gsandok marked this conversation as resolved.
Show resolved Hide resolved
},
{
Name: "get Product Overview as expected with env",
givenEnv: ptr.FromString("development"),
Setup: []repository.Transformer{
&repository.CreateEnvironment{
Environment: "development",
Expand All @@ -55,7 +62,7 @@ func TestGetProductOverview(t *testing.T) {
Latest: true,
},
ArgoCd: nil,
EnvironmentGroup: &dev,
EnvironmentGroup: ptr.FromString("dev"),
},
},
&repository.CreateApplicationVersion{
Expand All @@ -79,7 +86,73 @@ func TestGetProductOverview(t *testing.T) {
},
{
Name: "invalid environment used",
givenEnv: "staging",
givenEnv: ptr.FromString("staging"),
Setup: []repository.Transformer{
&repository.CreateEnvironment{
Environment: "development",
Config: config.EnvironmentConfig{
Upstream: &config.EnvironmentConfigUpstream{
Latest: true,
},
ArgoCd: nil,
EnvironmentGroup: ptr.FromString("dev"),
},
},
&repository.CreateApplicationVersion{
Application: "test",
Manifests: map[string]string{
"development": "dev",
},
SourceAuthor: "example <example@example.com>",
SourceCommitId: "testing25",
SourceMessage: "changed something (#678)",
SourceRepoUrl: "testing@testing.com/abc",
DisplayVersion: "v1.0.2",
},
&repository.DeployApplicationVersion{
Application: "test",
Environment: "development",
Version: 1,
},
},
expectedProductSummary: []api.ProductSummary{},
},
{
Name: "get Product Overview as expected with envGroup",
givenEnvGroup: ptr.FromString("dev"),
Setup: []repository.Transformer{
&repository.CreateEnvironment{
Environment: "development",
Config: config.EnvironmentConfig{
Upstream: &config.EnvironmentConfigUpstream{
Latest: true,
},
ArgoCd: nil,
EnvironmentGroup: ptr.FromString("dev"),
},
},
&repository.CreateApplicationVersion{
Application: "test",
Manifests: map[string]string{
"development": "dev",
},
SourceAuthor: "example <example@example.com>",
SourceCommitId: "testing25",
SourceMessage: "changed something (#678)",
SourceRepoUrl: "testing@testing.com/abc",
DisplayVersion: "v1.0.2",
},
&repository.DeployApplicationVersion{
Application: "test",
Environment: "development",
Version: 1,
},
},
expectedProductSummary: []api.ProductSummary{{App: "test", Version: "1", DisplayVersion: "v1.0.2", CommitId: "testing25"}},
},
{
Name: "invalid envGroup used",
givenEnvGroup: ptr.FromString("notDev"),
Setup: []repository.Transformer{
&repository.CreateEnvironment{
Environment: "development",
Expand All @@ -88,7 +161,7 @@ func TestGetProductOverview(t *testing.T) {
Latest: true,
},
ArgoCd: nil,
EnvironmentGroup: &dev,
EnvironmentGroup: ptr.FromString("dev"),
},
},
&repository.CreateApplicationVersion{
Expand All @@ -108,7 +181,7 @@ func TestGetProductOverview(t *testing.T) {
Version: 1,
},
},
expectedErr: fmt.Errorf("environment staging not found"),
expectedProductSummary: []api.ProductSummary{},
},
}
for _, tc := range tcs {
Expand All @@ -128,7 +201,7 @@ func TestGetProductOverview(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %s", err)
}
productSummary, err := sv.GetProductSummary(testutil.MakeTestContext(), &api.GetProductSummaryRequest{CommitHash: ov.GitRevision, Environment: tc.givenEnv})
productSummary, err := sv.GetProductSummary(testutil.MakeTestContext(), &api.GetProductSummaryRequest{CommitHash: ov.GitRevision, Environment: tc.givenEnv, EnvironmentGroup: tc.givenEnvGroup})
if err != nil && tc.expectedErr == nil {
t.Fatalf("expected no error, but got [%s]: %v", ov.GitRevision, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('Product Version Data', () => {
environmentName: 'tester2',
tags: [{ commitId: '123', tag: 'refs/tags/dummyTag' }],
expectedDropDown: 'dummyTag',
productSummary: [{ app: 'testing-app', version: '4', commitId: '123', displayVersion: 'v1.2.3' }],
productSummary: [
{ app: 'testing-app', version: '4', commitId: '123', displayVersion: 'v1.2.3', environment: 'dev' },
],
},
{
name: 'table to be displayed with multiple rows of data',
Expand All @@ -51,8 +53,8 @@ describe('Product Version Data', () => {
],
expectedDropDown: 'dummyTag',
productSummary: [
{ app: 'testing-app', version: '4', commitId: '123', displayVersion: 'v1.2.3' },
{ app: 'tester', version: '10', commitId: '4565', displayVersion: '' },
{ app: 'testing-app', version: '4', commitId: '123', displayVersion: 'v1.2.3', environment: 'dev' },
{ app: 'tester', version: '10', commitId: '4565', displayVersion: '', environment: 'dev' },
],
},
];
Expand Down
Loading