Skip to content

Commit 4959e52

Browse files
RajeevRanjan27Laeeqdev
authored andcommitted
feat: Add support for git hash in the app and app group overview section _revised (#4836)
* Revised the api for handelling the overview git commits part using CiArtifact table only * Removed unnecessary comments * optimized the loop and intialization * used maps instead of slices * removed unused comments * modified the GetUniqueArtifactIds * Removed error from util function * renamed the function as generateArtifactIDCommitMap
1 parent ce411f6 commit 4959e52

File tree

4 files changed

+123
-21
lines changed

4 files changed

+123
-21
lines changed

api/bean/AppView.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ type AppEnvironmentContainer struct {
138138
TeamName string `json:"teamName"`
139139
Description string `json:"description" validate:"max=40"`
140140
TotalCount int `json:"-"`
141+
Commits []string `json:"commits"`
141142
}
142143

143144
type DeploymentDetailContainer struct {
@@ -197,22 +198,24 @@ type Notes struct {
197198
}
198199

199200
type Environment struct {
200-
AppStatus string `json:"appStatus"` //this is not the status of environment , this make sense with a specific app only
201-
EnvironmentId int `json:"environmentId"`
202-
EnvironmentName string `json:"environmentName"`
203-
AppMetrics *bool `json:"appMetrics"`
204-
InfraMetrics *bool `json:"infraMetrics"`
205-
Prod bool `json:"prod"`
206-
ChartRefId int `json:"chartRefId"`
207-
LastDeployed string `json:"lastDeployed"`
208-
LastDeployedBy string `json:"lastDeployedBy"`
209-
LastDeployedImage string `json:"lastDeployedImage"`
210-
DeploymentAppDeleteRequest bool `json:"deploymentAppDeleteRequest"`
211-
Description string `json:"description" validate:"max=40"`
212-
IsVirtualEnvironment bool `json:"isVirtualEnvironment"`
213-
ClusterId int `json:"clusterId"`
214-
PipelineId int `json:"pipelineId"`
215-
LatestCdWorkflowRunnerId int `json:"latestCdWorkflowRunnerId,omitempty"`
201+
AppStatus string `json:"appStatus"` //this is not the status of environment , this make sense with a specific app only
202+
EnvironmentId int `json:"environmentId"`
203+
EnvironmentName string `json:"environmentName"`
204+
AppMetrics *bool `json:"appMetrics"`
205+
InfraMetrics *bool `json:"infraMetrics"`
206+
Prod bool `json:"prod"`
207+
ChartRefId int `json:"chartRefId"`
208+
LastDeployed string `json:"lastDeployed"`
209+
LastDeployedBy string `json:"lastDeployedBy"`
210+
LastDeployedImage string `json:"lastDeployedImage"`
211+
DeploymentAppDeleteRequest bool `json:"deploymentAppDeleteRequest"`
212+
Description string `json:"description" validate:"max=40"`
213+
IsVirtualEnvironment bool `json:"isVirtualEnvironment"`
214+
ClusterId int `json:"clusterId"`
215+
PipelineId int `json:"pipelineId"`
216+
LatestCdWorkflowRunnerId int `json:"latestCdWorkflowRunnerId,omitempty"`
217+
CiArtifactId int `json:"ciArtifactId"`
218+
Commits []string `json:"commits"`
216219
}
217220

218221
type InstanceDetail struct {

internal/sql/repository/AppListingRepository.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type AppNameTypeIdContainerDBResponse struct {
7676
}
7777

7878
type LastDeployed struct {
79+
CiArtifactId int `sql:"ci_artifact_id"`
7980
LastDeployedBy string `sql:"last_deployed_by"`
8081
LastDeployedImage string `sql:"last_deployed_image"`
8182
}
@@ -157,7 +158,7 @@ func (impl AppListingRepositoryImpl) FetchOverviewAppsByEnvironment(envId, limit
157158
func (impl AppListingRepositoryImpl) FetchLastDeployedImage(appId, envId int) (*LastDeployed, error) {
158159
var lastDeployed []*LastDeployed
159160
// we are adding a case in the query to concatenate the string "(inactive)" to the users' email id when user is inactive
160-
query := `select ca.image as last_deployed_image,
161+
query := `select ca.id as ci_artifact_id,ca.image as last_deployed_image,
161162
case
162163
when u.active = false then u.email_id || ' (inactive)'
163164
else u.email_id
@@ -604,8 +605,8 @@ func (impl AppListingRepositoryImpl) FetchOtherEnvironment(appId int) ([]*bean.E
604605
var otherEnvironments []*bean.Environment
605606
//TODO: remove infra metrics from query as it is not being used from here
606607
query := `select pcwr.pipeline_id, pcwr.last_deployed, pcwr.latest_cd_workflow_runner_id, pcwr.environment_id, pcwr.deployment_app_delete_request,
607-
e.cluster_id, e.environment_name, e.default as prod, e.description, ca.image as last_deployed_image,
608-
u.email_id as last_deployed_by, elam.app_metrics, elam.infra_metrics, ap.status as app_status
608+
e.cluster_id, e.environment_name, e.default as prod, e.description, ca.image as last_deployed_image,
609+
u.email_id as last_deployed_by, elam.app_metrics, elam.infra_metrics, ap.status as app_status,ca.id as ci_artifact_id
609610
from (select *
610611
from (select p.id as pipeline_id, p.app_id, cwr.started_on as last_deployed, cwr.triggered_by, cwr.id as latest_cd_workflow_runner_id,
611612
cw.ci_artifact_id, p.environment_id, p.deployment_app_delete_request,

pkg/app/AppListingService.go

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type AppListingServiceImpl struct {
143143
dockerRegistryIpsConfigService dockerRegistry.DockerRegistryIpsConfigService
144144
userRepository userrepository.UserRepository
145145
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService
146+
ciArtifactRepository repository.CiArtifactRepository
146147
}
147148

148149
func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository repository.AppListingRepository,
@@ -153,7 +154,7 @@ func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository re
153154
argoUserService argo.ArgoUserService, envOverrideRepository chartConfig.EnvConfigOverrideRepository,
154155
chartRepository chartRepoRepository.ChartRepository, ciPipelineRepository pipelineConfig.CiPipelineRepository,
155156
dockerRegistryIpsConfigService dockerRegistry.DockerRegistryIpsConfigService, userRepository userrepository.UserRepository,
156-
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService) *AppListingServiceImpl {
157+
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService, ciArtifactRepository repository.CiArtifactRepository) *AppListingServiceImpl {
157158
serviceImpl := &AppListingServiceImpl{
158159
Logger: Logger,
159160
appListingRepository: appListingRepository,
@@ -172,6 +173,7 @@ func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository re
172173
dockerRegistryIpsConfigService: dockerRegistryIpsConfigService,
173174
userRepository: userRepository,
174175
deployedAppMetricsService: deployedAppMetricsService,
176+
ciArtifactRepository: ciArtifactRepository,
175177
}
176178
return serviceImpl
177179
}
@@ -234,6 +236,8 @@ func (impl AppListingServiceImpl) FetchOverviewAppsByEnvironment(envId, limit, o
234236
impl.Logger.Errorw("failed to fetch environment containers", "err", err, "envId", envId)
235237
return resp, err
236238
}
239+
240+
artifactIds := make([]int, 0)
237241
for _, envContainer := range envContainers {
238242
lastDeployed, err := impl.appListingRepository.FetchLastDeployedImage(envContainer.AppId, envId)
239243
if err != nil {
@@ -243,12 +247,44 @@ func (impl AppListingServiceImpl) FetchOverviewAppsByEnvironment(envId, limit, o
243247
if lastDeployed != nil {
244248
envContainer.LastDeployedImage = lastDeployed.LastDeployedImage
245249
envContainer.LastDeployedBy = lastDeployed.LastDeployedBy
250+
envContainer.CiArtifactId = lastDeployed.CiArtifactId
251+
artifactIds = append(artifactIds, lastDeployed.CiArtifactId)
252+
}
253+
}
254+
uniqueArtifacts := getUniqueArtifacts(artifactIds)
255+
256+
artifactWithGitCommit, err := impl.generateArtifactIDCommitMap(uniqueArtifacts)
257+
if err != nil {
258+
impl.Logger.Errorw("failed to fetch Artifacts to git Triggers ", "envId", envId, "err", err)
259+
return resp, err
260+
}
261+
for _, envContainer := range envContainers {
262+
envContainer.Commits = []string{}
263+
if envContainer.CiArtifactId > 0 {
264+
if commits, ok := artifactWithGitCommit[envContainer.CiArtifactId]; ok && commits != nil {
265+
envContainer.Commits = commits
266+
}
246267
}
247268
}
248269
resp.Apps = envContainers
249270
return resp, err
250271
}
251272

273+
func getUniqueArtifacts(artifactIds []int) (uniqueArtifactIds []int) {
274+
uniqueArtifactIds = make([]int, 0)
275+
276+
uniqueArtifactMap := make(map[int]bool)
277+
278+
for _, artifactId := range artifactIds {
279+
if ok := uniqueArtifactMap[artifactId]; !ok {
280+
uniqueArtifactIds = append(uniqueArtifactIds, artifactId)
281+
uniqueArtifactMap[artifactId] = true
282+
}
283+
}
284+
285+
return uniqueArtifactIds
286+
}
287+
252288
func (impl AppListingServiceImpl) FetchAllDevtronManagedApps() ([]AppNameTypeIdContainer, error) {
253289
impl.Logger.Debug("reached at FetchAllDevtronManagedApps:")
254290
apps := make([]AppNameTypeIdContainer, 0)
@@ -770,6 +806,49 @@ func (impl AppListingServiceImpl) FetchAppStageStatus(appId int, appType int) ([
770806
return appStageStatuses, err
771807
}
772808

809+
func (impl AppListingServiceImpl) generateArtifactIDCommitMap(artifactIds []int) (ciArtifactAndGitCommitsMap map[int][]string, err error) {
810+
811+
if len(artifactIds) == 0 {
812+
impl.Logger.Errorw("error in getting the ArtifactIds", "ArtifactIds", artifactIds, "err", err)
813+
return make(map[int][]string), err
814+
}
815+
816+
artifacts, err := impl.ciArtifactRepository.GetByIds(artifactIds)
817+
if err != nil {
818+
return make(map[int][]string), err
819+
}
820+
821+
ciArtifactAndGitCommitsMap = make(map[int][]string)
822+
ciArtifactWithModificationMap := make(map[int][]repository.Modification)
823+
824+
for _, artifact := range artifacts {
825+
materialInfo, err := repository.GetCiMaterialInfo(artifact.MaterialInfo, artifact.DataSource)
826+
if err != nil {
827+
impl.Logger.Errorw("error in getting the MaterialInfo", "ArtifactId", artifact.Id, "err", err)
828+
return make(map[int][]string), err
829+
}
830+
if len(materialInfo) == 0 {
831+
continue
832+
}
833+
for _, material := range materialInfo {
834+
ciArtifactWithModificationMap[artifact.Id] = append(ciArtifactWithModificationMap[artifact.Id], material.Modifications...)
835+
}
836+
}
837+
838+
for artifactId, modifications := range ciArtifactWithModificationMap {
839+
840+
gitCommits := make([]string, 0)
841+
842+
for _, modification := range modifications {
843+
gitCommits = append(gitCommits, modification.Revision)
844+
}
845+
846+
ciArtifactAndGitCommitsMap[artifactId] = gitCommits
847+
}
848+
849+
return ciArtifactAndGitCommitsMap, nil
850+
}
851+
773852
func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, appId int) ([]*bean.Environment, error) {
774853
newCtx, span := otel.Tracer("appListingRepository").Start(ctx, "FetchOtherEnvironment")
775854
envs, err := impl.appListingRepository.FetchOtherEnvironment(appId)
@@ -793,6 +872,19 @@ func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, app
793872
impl.Logger.Errorw("error in fetching latest chart", "err", err)
794873
return envs, err
795874
}
875+
876+
ciArtifacts := make([]int, 0)
877+
for _, env := range envs {
878+
ciArtifacts = append(ciArtifacts, env.CiArtifactId)
879+
}
880+
881+
uniqueArtifacts := getUniqueArtifacts(ciArtifacts)
882+
883+
gitCommitsWithArtifacts, err := impl.generateArtifactIDCommitMap(uniqueArtifacts)
884+
if err != nil {
885+
impl.Logger.Errorw("Error in fetching the git commits of the ciArtifacts", "err", err, "ciArtifacts", ciArtifacts)
886+
return envs, err
887+
}
796888
for _, env := range envs {
797889
newCtx, span = otel.Tracer("envOverrideRepository").Start(newCtx, "FindLatestChartForAppByAppIdAndEnvId")
798890
envOverride, err := impl.envOverrideRepository.FindLatestChartForAppByAppIdAndEnvId(appId, env.EnvironmentId)
@@ -809,6 +901,12 @@ func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, app
809901
if env.AppMetrics == nil {
810902
env.AppMetrics = &appLevelAppMetrics
811903
}
904+
905+
if _, ok := gitCommitsWithArtifacts[env.CiArtifactId]; ok {
906+
env.Commits = gitCommitsWithArtifacts[env.CiArtifactId]
907+
} else {
908+
env.Commits = make([]string, 0)
909+
}
812910
env.InfraMetrics = &appLevelInfraMetrics //using default value, discarding value got from query
813911
}
814912
return envs, nil

wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)