Skip to content

Commit

Permalink
chore: refactoring image tagging reader service (#6063)
Browse files Browse the repository at this point in the history
* chore: refactoring image tagging reader service

* added licencing comment

* moved method GetImageTaggingServiceConfig in read service

* moved ImageTaggingService.go

* fix: TestImageTaggingService unit test cases

* added mock file

* fix: TestImageTaggingService unit test cases
  • Loading branch information
Ash-exp authored Nov 15, 2024
1 parent 2895460 commit 64f31bb
Show file tree
Hide file tree
Showing 18 changed files with 1,227 additions and 195 deletions.
7 changes: 4 additions & 3 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import (
"github.com/devtron-labs/devtron/pkg/asyncProvider"
"github.com/devtron-labs/devtron/pkg/attributes"
"github.com/devtron-labs/devtron/pkg/build"
"github.com/devtron-labs/devtron/pkg/build/artifacts/imageTagging"
pipeline6 "github.com/devtron-labs/devtron/pkg/build/pipeline"
"github.com/devtron-labs/devtron/pkg/bulkAction"
"github.com/devtron-labs/devtron/pkg/chart"
Expand Down Expand Up @@ -209,7 +210,7 @@ func InitializeApp() (*App, error) {
fluxApplication.FluxApplicationWireSet,
eventProcessor.EventProcessorWireSet,
workflow3.WorkflowWireSet,

imageTagging.WireSet,
devtronResource.DevtronResourceWireSet,
// -------wireset end ----------
// -------
Expand Down Expand Up @@ -446,8 +447,8 @@ func InitializeApp() (*App, error) {
// session.NewK8sClient,
repository8.NewImageTaggingRepositoryImpl,
wire.Bind(new(repository8.ImageTaggingRepository), new(*repository8.ImageTaggingRepositoryImpl)),
pipeline.NewImageTaggingServiceImpl,
wire.Bind(new(pipeline.ImageTaggingService), new(*pipeline.ImageTaggingServiceImpl)),
imageTagging.NewImageTaggingServiceImpl,
wire.Bind(new(imageTagging.ImageTaggingService), new(*imageTagging.ImageTaggingServiceImpl)),
argocdServer.NewVersionServiceImpl,
wire.Bind(new(argocdServer.VersionService), new(*argocdServer.VersionServiceImpl)),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/devtron/pkg/build/artifacts/imageTagging"
"github.com/devtron-labs/devtron/internal/sql/constants"
bean2 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -1110,7 +1111,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetBuildHistory(w http.ResponseWri
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
return
}
appTags, err := handler.imageTaggingService.GetUniqueTagsByAppId(ciPipeline.AppId)
appTags, err := handler.imageTaggingReadService.GetUniqueTagsByAppId(ciPipeline.AppId)
if err != nil {
handler.Logger.Errorw("service err, GetTagsByAppId", "err", err, "appId", ciPipeline.AppId)
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
Expand All @@ -1120,7 +1121,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetBuildHistory(w http.ResponseWri

prodEnvExists, err := handler.imageTaggingService.GetProdEnvFromParentAndLinkedWorkflow(ciPipeline.Id)
resp.TagsEditable = prodEnvExists && triggerAccess
resp.HideImageTaggingHardDelete = handler.imageTaggingService.GetImageTaggingServiceConfig().HideImageTaggingHardDelete
resp.HideImageTaggingHardDelete = handler.imageTaggingService.IsHardDeleteHidden()
if err != nil {
handler.Logger.Errorw("service err, GetProdEnvFromParentAndLinkedWorkflow", "err", err, "ciPipelineId", ciPipeline.Id)
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
Expand Down Expand Up @@ -2072,11 +2073,11 @@ func (handler *PipelineConfigRestHandlerImpl) CreateUpdateImageTagging(w http.Re
return
}
req.ExternalCi = externalCi
//pass it to service layer
// pass it to the service layer
resp, err := handler.imageTaggingService.CreateOrUpdateImageTagging(ciPipelineId, appId, artifactId, int(userId), req)
if err != nil {
if err.Error() == pipeline.DuplicateTagsInAppError {
appReleaseTags, err1 := handler.imageTaggingService.GetUniqueTagsByAppId(appId)
if err.Error() == imageTagging.DuplicateTagsInAppError {
appReleaseTags, err1 := handler.imageTaggingReadService.GetUniqueTagsByAppId(appId)
if err1 != nil {
handler.Logger.Errorw("error occurred in getting unique tags in app", "err", err1, "appId", appId)
err = err1
Expand Down Expand Up @@ -2112,7 +2113,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetImageTaggingData(w http.Respons

externalCi, ciPipelineId, appId, err := handler.extractCipipelineMetaForImageTags(artifactId)
if err != nil {
handler.Logger.Errorw("error occurred in fetching extractCipipelineMetaForImageTags by artifact Id ", "err", err, "artifactId", artifactId)
handler.Logger.Errorw("error occurred in fetching extract ci pipeline metadata for ImageTags by artifact id", "err", err, "artifactId", artifactId)
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusInternalServerError)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetArtifactsByCDPipeline(w http.Re
return
}

appTags, err := handler.imageTaggingService.GetUniqueTagsByAppId(pipeline.AppId)
appTags, err := handler.imageTaggingReadService.GetUniqueTagsByAppId(pipeline.AppId)
if err != nil {
handler.Logger.Errorw("service err, GetTagsByAppId", "err", err, "appId", pipeline.AppId)
common.WriteJsonResp(w, err, ciArtifactResponse, http.StatusInternalServerError)
Expand All @@ -1364,7 +1364,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetArtifactsByCDPipeline(w http.Re

prodEnvExists, err := handler.imageTaggingService.GetProdEnvByCdPipelineId(pipeline.Id)
ciArtifactResponse.TagsEditable = prodEnvExists && triggerAccess
ciArtifactResponse.HideImageTaggingHardDelete = handler.imageTaggingService.GetImageTaggingServiceConfig().HideImageTaggingHardDelete
ciArtifactResponse.HideImageTaggingHardDelete = handler.imageTaggingService.IsHardDeleteHidden()
if err != nil {
handler.Logger.Errorw("service err, GetProdEnvByCdPipelineId", "err", err, "cdPipelineId", pipeline.Id)
common.WriteJsonResp(w, err, ciArtifactResponse, http.StatusInternalServerError)
Expand Down Expand Up @@ -1597,7 +1597,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetArtifactsForRollback(w http.Res
common.WriteJsonResp(w, err, "unable to fetch artifacts", http.StatusInternalServerError)
return
}
appTags, err := handler.imageTaggingService.GetUniqueTagsByAppId(app.Id)
appTags, err := handler.imageTaggingReadService.GetUniqueTagsByAppId(app.Id)
if err != nil {
handler.Logger.Errorw("service err, GetTagsByAppId", "err", err, "appId", app.Id)
common.WriteJsonResp(w, err, ciArtifactResponse, http.StatusInternalServerError)
Expand All @@ -1608,7 +1608,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetArtifactsForRollback(w http.Res

prodEnvExists, err := handler.imageTaggingService.GetProdEnvByCdPipelineId(cdPipelineId)
ciArtifactResponse.TagsEditable = prodEnvExists && triggerAccess
ciArtifactResponse.HideImageTaggingHardDelete = handler.imageTaggingService.GetImageTaggingServiceConfig().HideImageTaggingHardDelete
ciArtifactResponse.HideImageTaggingHardDelete = handler.imageTaggingService.IsHardDeleteHidden()
if err != nil {
handler.Logger.Errorw("service err, GetProdEnvByCdPipelineId", "err", err, "cdPipelineId", app.Id)
common.WriteJsonResp(w, err, ciArtifactResponse, http.StatusInternalServerError)
Expand Down Expand Up @@ -1723,7 +1723,7 @@ func (handler *PipelineConfigRestHandlerImpl) ListDeploymentHistory(w http.Respo
return
}

appTags, err := handler.imageTaggingService.GetUniqueTagsByAppId(appId)
appTags, err := handler.imageTaggingReadService.GetUniqueTagsByAppId(appId)
if err != nil {
handler.Logger.Errorw("service err, GetTagsByAppId", "err", err, "appId", appId)
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
Expand All @@ -1733,7 +1733,7 @@ func (handler *PipelineConfigRestHandlerImpl) ListDeploymentHistory(w http.Respo

prodEnvExists, err := handler.imageTaggingService.GetProdEnvByCdPipelineId(pipelineId)
resp.TagsEdiatable = prodEnvExists
resp.HideImageTaggingHardDelete = handler.imageTaggingService.GetImageTaggingServiceConfig().HideImageTaggingHardDelete
resp.HideImageTaggingHardDelete = handler.imageTaggingService.IsHardDeleteHidden()
if err != nil {
handler.Logger.Errorw("service err, GetProdEnvFromParentAndLinkedWorkflow", "err", err, "cdPipelineId", pipelineId)
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/pkg/build/git/gitProvider/read"
"github.com/devtron-labs/devtron/pkg/build/artifacts/imageTagging"
imageTaggingRead "github.com/devtron-labs/devtron/pkg/build/artifacts/imageTagging/read"
gitProviderRead "github.com/devtron-labs/devtron/pkg/build/git/gitProvider/read"
bean3 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
"github.com/devtron-labs/devtron/pkg/chart/gitOpsConfig"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics"
Expand Down Expand Up @@ -121,9 +123,10 @@ type PipelineConfigRestHandlerImpl struct {
materialRepository pipelineConfig.MaterialRepository
policyService security2.PolicyService
scanResultRepository security.ImageScanResultRepository
gitProviderReadService read.GitProviderReadService
gitProviderReadService gitProviderRead.GitProviderReadService
argoUserService argo.ArgoUserService
imageTaggingService pipeline.ImageTaggingService
imageTaggingReadService imageTaggingRead.ImageTaggingReadService
imageTaggingService imageTagging.ImageTaggingService
deploymentTemplateService generateManifest.DeploymentTemplateService
pipelineRestHandlerEnvConfig *PipelineRestHandlerEnvConfig
ciArtifactRepository repository.CiArtifactRepository
Expand All @@ -143,7 +146,8 @@ func NewPipelineRestHandlerImpl(pipelineBuilder pipeline.PipelineBuilder, Logger
ciHandler pipeline.CiHandler,
validator *validator.Validate,
gitSensorClient gitSensor.Client,
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
ciPipelineRepository pipelineConfig.CiPipelineRepository,
pipelineRepository pipelineConfig.PipelineRepository,
enforcerUtil rbac.EnforcerUtil,
dockerRegistryConfig pipeline.DockerRegistryConfig,
cdHandler pipeline.CdHandler,
Expand All @@ -153,12 +157,13 @@ func NewPipelineRestHandlerImpl(pipelineBuilder pipeline.PipelineBuilder, Logger
materialRepository pipelineConfig.MaterialRepository, policyService security2.PolicyService,
scanResultRepository security.ImageScanResultRepository,
argoUserService argo.ArgoUserService, ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository,
imageTaggingService pipeline.ImageTaggingService,
imageTaggingReadService imageTaggingRead.ImageTaggingReadService,
imageTaggingService imageTagging.ImageTaggingService,
ciArtifactRepository repository.CiArtifactRepository,
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService,
chartRefService chartRef.ChartRefService,
ciCdPipelineOrchestrator pipeline.CiCdPipelineOrchestrator,
gitProviderReadService read.GitProviderReadService) *PipelineConfigRestHandlerImpl {
gitProviderReadService gitProviderRead.GitProviderReadService) *PipelineConfigRestHandlerImpl {
envConfig := &PipelineRestHandlerEnvConfig{}
err := env.Parse(envConfig)
if err != nil {
Expand Down Expand Up @@ -189,6 +194,7 @@ func NewPipelineRestHandlerImpl(pipelineBuilder pipeline.PipelineBuilder, Logger
scanResultRepository: scanResultRepository,
argoUserService: argoUserService,
ciPipelineMaterialRepository: ciPipelineMaterialRepository,
imageTaggingReadService: imageTaggingReadService,
imageTaggingService: imageTaggingService,
deploymentTemplateService: deploymentTemplateService,
pipelineRestHandlerEnvConfig: envConfig,
Expand Down
Loading

0 comments on commit 64f31bb

Please sign in to comment.