Skip to content

Commit

Permalink
adapted get app summary
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-crespo-fdc committed Oct 22, 2024
1 parent 135a0d0 commit 92e6914
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 120 deletions.
12 changes: 1 addition & 11 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2455,17 +2455,7 @@ func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction
}
cache.LightweightApps = lApps
}
if shouldDelete {
lApps := make([]*api.OverviewApplication, len(cache.LightweightApps)-1)

for _, curr := range cache.LightweightApps {
if curr.Name != appName {
lApps = append(lApps, curr)
}
}
cache.LightweightApps = lApps
}


err = h.WriteOverviewCache(ctx, transaction, cache)
if err != nil {
return err
Expand Down
13 changes: 1 addition & 12 deletions services/cd-service/pkg/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,12 +2184,7 @@ func TestUpdateOverviewCache(t *testing.T) {
Team: "",
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Team: "",
},
},

EnvironmentGroups: []*api.EnvironmentGroup{},
},
},
Expand Down Expand Up @@ -2218,12 +2213,6 @@ func TestUpdateOverviewCache(t *testing.T) {
ManifestRepoUrl: "https://example.com",
},
ExpectedOverview: &api.GetOverviewResponse{
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Team: "",
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Expand Down
6 changes: 0 additions & 6 deletions services/cd-service/pkg/repository/transformer_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1727,12 +1727,6 @@ func TestCreateEnvironmentUpdatesOverview(t *testing.T) {
Team: "",
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app",
Team: "",
},
},
EnvironmentGroups: []*api.EnvironmentGroup{
&api.EnvironmentGroup{
EnvironmentGroupName: "development",
Expand Down
211 changes: 124 additions & 87 deletions services/cd-service/pkg/service/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"errors"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/logger"

"os"
"sort"
"strconv"
"strings"

api "github.com/freiheit-com/kuberpult/pkg/api/v1"
Expand Down Expand Up @@ -53,94 +56,128 @@ func (s *GitServer) GetGitTags(ctx context.Context, _ *api.GetGitTagsRequest) (*
return &api.GetGitTagsResponse{TagData: tags}, nil
}

func getProductSummaryForEnv(ctx context.Context, transaction *sql.Tx, state *repository.State, overviewService *OverviewServiceServer, envName string, appDetailsCache map[string]*api.GetAppDetailsResponse) ([]api.ProductSummary, error) {
var summaryFromEnv []api.ProductSummary
appsForEnv, err := state.GetEnvironmentApplications(ctx, transaction, envName)
if err != nil {
return nil, fmt.Errorf("unable to get apps on environment for %s: %v", envName, err)
}
for _, appName := range appsForEnv {
currentAppDetails, err := overviewService.GetAppDetails(ctx, &api.GetAppDetailsRequest{AppName: appName})
if err != nil {
return nil, fmt.Errorf("unable to get app details for app: '%s': %w", appName, err)
}
appDetailsCache[appName] = currentAppDetails
if _, ok := currentAppDetails.Deployments[envName]; !ok {
//The call state.GetEnvironmentApplications should guarantee us a deployment, but just in case it doesn't, we warn an error and continue
logger.FromContext(ctx).Sugar().Warnf("Could not find deployment information for app '%s' on environment '%s'", appName, envName)
continue
}
summaryFromEnv = append(summaryFromEnv, api.ProductSummary{
CommitId: "",
DisplayVersion: "",
Team: "",
App: appName,
Version: strconv.FormatUint(currentAppDetails.Deployments[envName].Version, 10),
Environment: envName,
})
}
return summaryFromEnv, nil
}

func (s *GitServer) GetProductSummary(ctx context.Context, in *api.GetProductSummaryRequest) (*api.GetProductSummaryResponse, error) {
//if in.Environment == nil && in.EnvironmentGroup == nil {
// return nil, fmt.Errorf("Must have an environment or environmentGroup to get the product summary for")
//}
//if in.Environment != nil && in.EnvironmentGroup != nil {
// if *in.Environment != "" && *in.EnvironmentGroup != "" {
// return nil, fmt.Errorf("Can not have both an environment and environmentGroup to get the product summary for")
// }
//}
//if in.ManifestRepoCommitHash == "" {
// return nil, fmt.Errorf("Must have a commit to get the product summary for")
//}
//response, err := s.OverviewService.GetOverview(ctx, &api.GetOverviewRequest{GitRevision: in.ManifestRepoCommitHash})
//if err != nil {
// return nil, fmt.Errorf("unable to get overview for %s: %v", in.ManifestRepoCommitHash, err)
//}
//
//var summaryFromEnv []api.ProductSummary
//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{
// CommitId: "",
// DisplayVersion: "",
// Team: "",
// App: app.Name,
// Version: strconv.FormatUint(app.Version, 10),
// Environment: *in.Environment,
// })
// }
// }
// }
// }
// if len(summaryFromEnv) == 0 {
// return &api.GetProductSummaryResponse{
// ProductSummary: 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{
// CommitId: "",
// DisplayVersion: "",
// Team: "",
// 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
// }
//}

var productVersion []*api.ProductSummary
//for _, row := range summaryFromEnv { //nolint: govet
// 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, Environment: row.Environment, Team: app.Team})
// break
// }
// }
// }
// }
//}
return &api.GetProductSummaryResponse{ProductSummary: productVersion}, nil
if in.Environment == nil && in.EnvironmentGroup == nil {
return nil, fmt.Errorf("Must have an environment or environmentGroup to get the product summary for")
}
if in.Environment != nil && in.EnvironmentGroup != nil {
if *in.Environment != "" && *in.EnvironmentGroup != "" {
return nil, fmt.Errorf("Can not have both an environment and environmentGroup to get the product summary for")
}
}
if in.ManifestRepoCommitHash == "" {
return nil, fmt.Errorf("Must have a commit to get the product summary for")
}
if s.Config.DBHandler.ShouldUseOtherTables() {
dbHandler := s.OverviewService.DBHandler
state := s.OverviewService.Repository.State()

response, err := db.WithTransactionT[api.GetProductSummaryResponse](dbHandler, ctx, db.DefaultNumRetries, true, func(ctx context.Context, transaction *sql.Tx) (*api.GetProductSummaryResponse, error) {
overview, err := s.OverviewService.GetOverview(ctx, &api.GetOverviewRequest{GitRevision: in.ManifestRepoCommitHash})
if err != nil {
return nil, fmt.Errorf("unable to get overview for %s: %v", in.ManifestRepoCommitHash, err)
}
accessedApps := make(map[string]*api.GetAppDetailsResponse) //store app details we access for later use and avoid calling the endpoint twice for each app
var summaryFromEnv []api.ProductSummary
if in.Environment != nil && *in.Environment != "" {
for _, group := range overview.EnvironmentGroups {
for _, env := range group.Environments {
if env.Name == *in.Environment {
summaryFromEnv, err = getProductSummaryForEnv(ctx, transaction, state, s.OverviewService, env.Name, accessedApps)
if err != nil {
return nil, fmt.Errorf("unable to get product summary for environment: '%s'", env.Name)
}
}
}
}
if len(summaryFromEnv) == 0 {
return &api.GetProductSummaryResponse{
ProductSummary: 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 overview.EnvironmentGroups {
if *in.EnvironmentGroup == group.EnvironmentGroupName {
for _, env := range group.Environments {
var singleEnvSummary []api.ProductSummary
singleEnvSummary, err := getProductSummaryForEnv(ctx, transaction, state, s.OverviewService, env.Name, accessedApps)
if err != nil {
return nil, fmt.Errorf("unable to get product summary for environment: '%s': %w", env.Name, err)
}
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
}
}
var productVersion []*api.ProductSummary

for _, row := range summaryFromEnv { //nolint: govet
appsForEnv, err := state.GetEnvironmentApplications(ctx, transaction, row.Environment)
if err != nil {
return nil, fmt.Errorf("unable to get environment applications for env: '%s': %w", row.Environment, err)
}
for _, app := range appsForEnv {
if row.App == app {
currentAppDetails := accessedApps[app]
if currentAppDetails == nil {
continue
}
for _, release := range currentAppDetails.Application.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, Environment: row.Environment, Team: currentAppDetails.Application.Team})
break
}
}
}
}
}
return &api.GetProductSummaryResponse{ProductSummary: productVersion}, nil
})
return response, err
}
return &api.GetProductSummaryResponse{}, nil
}

func (s *GitServer) GetCommitInfo(ctx context.Context, in *api.GetCommitInfoRequest) (*api.GetCommitInfoResponse, error) {
Expand Down
8 changes: 4 additions & 4 deletions services/cd-service/pkg/service/overview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,16 +735,16 @@ func TestOverviewService(t *testing.T) {
if len(test.Releases) != 2 {
t.Errorf("expected two releases, got %v", test.Releases)
}
if test.Releases[0].Version != 1 {
if test.Releases[1].Version != 1 {
t.Errorf("expected test release version to be 1, but got %d", test.Releases[0].Version)
}
if test.Releases[0].SourceAuthor != "example <[email protected]>" {
if test.Releases[1].SourceAuthor != "example <[email protected]>" {
t.Errorf("expected test source author to be \"example <[email protected]>\", but got %q", test.Releases[0].SourceAuthor)
}
if test.Releases[0].SourceMessage != "changed something (#678)" {
if test.Releases[1].SourceMessage != "changed something (#678)" {
t.Errorf("expected test source message to be \"changed something\", but got %q", test.Releases[0].SourceMessage)
}
if test.Releases[0].SourceCommitId != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" {
if test.Releases[1].SourceCommitId != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" {
t.Errorf("expected test source commit id to be \"deadbeef\", but got %q", test.Releases[0].SourceCommitId)
}

Expand Down

0 comments on commit 92e6914

Please sign in to comment.