Skip to content

Commit 2895460

Browse files
authored
chore: refactoring ci pipeline reader service (#6062)
* chore: read service for CiPipelineRepository * moved into pkg/build/pipeline
1 parent 757fba3 commit 2895460

File tree

10 files changed

+141
-24
lines changed

10 files changed

+141
-24
lines changed

Wire.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func InitializeApp() (*App, error) {
203203
apiToken.ApiTokenWireSet,
204204
webhookHelm.WebhookHelmWireSet,
205205
terminal.TerminalWireSet,
206-
build.BuildWireSet,
206+
build.WireSet,
207207
deployment2.DeploymentWireSet,
208208
argoApplication.ArgoApplicationWireSetFull,
209209
fluxApplication.FluxApplicationWireSet,

client/telemetry/TelemetryEventClientExtended.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
client "github.com/devtron-labs/devtron/api/helm-app/gRPC"
2323
repository3 "github.com/devtron-labs/devtron/pkg/build/git/gitProvider/repository"
2424
"github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
25+
ciConfig "github.com/devtron-labs/devtron/pkg/build/pipeline/read"
2526
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
2627
cron3 "github.com/devtron-labs/devtron/util/cron"
2728
"net/http"
@@ -52,7 +53,7 @@ const AppsCount int = 50
5253
type TelemetryEventClientImplExtended struct {
5354
environmentService cluster.EnvironmentService
5455
appListingRepository repository.AppListingRepository
55-
ciPipelineRepository pipelineConfig.CiPipelineRepository
56+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService
5657
pipelineRepository pipelineConfig.PipelineRepository
5758
gitProviderRepository repository3.GitProviderRepository
5859
dockerArtifactStoreRepository dockerRegistryRepository.DockerArtifactStoreRepository
@@ -71,7 +72,7 @@ func NewTelemetryEventClientImplExtended(logger *zap.SugaredLogger, client *http
7172
K8sUtil *util2.K8sServiceImpl, aCDAuthConfig *util3.ACDAuthConfig,
7273
environmentService cluster.EnvironmentService, userService user2.UserService,
7374
appListingRepository repository.AppListingRepository, PosthogClient *PosthogClient,
74-
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
75+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService, pipelineRepository pipelineConfig.PipelineRepository,
7576
gitProviderRepository repository3.GitProviderRepository, attributeRepo repository.AttributesRepository,
7677
ssoLoginService sso.SSOLoginService, appRepository app.AppRepository,
7778
ciWorkflowRepository pipelineConfig.CiWorkflowRepository, cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
@@ -89,7 +90,7 @@ func NewTelemetryEventClientImplExtended(logger *zap.SugaredLogger, client *http
8990
watcher := &TelemetryEventClientImplExtended{
9091
environmentService: environmentService,
9192
appListingRepository: appListingRepository,
92-
ciPipelineRepository: ciPipelineRepository,
93+
ciPipelineConfigReadService: ciPipelineConfigReadService,
9394
pipelineRepository: pipelineRepository,
9495
gitProviderRepository: gitProviderRepository,
9596
dockerArtifactStoreRepository: dockerArtifactStoreRepository,
@@ -239,12 +240,12 @@ func (impl *TelemetryEventClientImplExtended) SendSummaryEvent(eventType string)
239240
nonProdApps = -1
240241
}
241242

242-
ciPipelineCount, err := impl.ciPipelineRepository.FindAllPipelineCreatedCountInLast24Hour()
243+
ciPipelineCount, err := impl.ciPipelineConfigReadService.FindAllPipelineCreatedCountInLast24Hour()
243244
if err != nil && err != pg.ErrNoRows {
244245
impl.logger.Errorw("exception caught inside telemetry summary event, while retrieving ciPipelineCount, setting its value to -1", "err", err)
245246
ciPipelineCount = -1
246247
}
247-
ciPipelineDeletedCount, err := impl.ciPipelineRepository.FindAllDeletedPipelineCountInLast24Hour()
248+
ciPipelineDeletedCount, err := impl.ciPipelineConfigReadService.FindAllDeletedPipelineCountInLast24Hour()
248249
if err != nil && err != pg.ErrNoRows {
249250
impl.logger.Errorw("exception caught inside telemetry summary event, while retrieving ciPipelineDeletedCount, setting its value to -1", "err", err)
250251
ciPipelineDeletedCount = -1
@@ -314,7 +315,7 @@ func (impl *TelemetryEventClientImplExtended) SendSummaryEvent(eventType string)
314315
if err != nil {
315316
impl.logger.Errorw("exception caught inside telemetry summary event,while retrieving AppsWithDeploymentTemplateConfigured", "err", err)
316317
}
317-
payload.AppsWithCiPipelineConfigured, err = impl.ciPipelineRepository.FindNumberOfAppsWithCiPipeline(appIds)
318+
payload.AppsWithCiPipelineConfigured, err = impl.ciPipelineConfigReadService.FindNumberOfAppsWithCiPipeline(appIds)
318319
if err != nil {
319320
impl.logger.Errorw("exception caught inside telemetry summary event,while retrieving AppsWithCiPipelineConfigured", "err", err)
320321
}

internal/sql/repository/pipelineConfig/CiPipelineRepository.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ type CiPipelineScript struct {
8888
sql.AuditLog
8989
}
9090

91+
// CiPipelineRepository :
92+
// use config.CiPipelineConfigReadService instead of directly using CiPipelineRepository
9193
type CiPipelineRepository interface {
9294
sql.TransactionWrapper
9395
Save(pipeline *CiPipeline, tx *pg.Tx) error
@@ -121,7 +123,7 @@ type CiPipelineRepository interface {
121123
PipelineExistsByName(names []string) (found []string, err error)
122124
FindByName(pipelineName string) (pipeline *CiPipeline, err error)
123125
CheckIfPipelineExistsByNameAndAppId(pipelineName string, appId int) (bool, error)
124-
FindByLinkedCiCount(parentCiPipelineId int) (int, error)
126+
GetChildrenCiCount(parentCiPipelineId int) (int, error)
125127
FindByParentCiPipelineId(parentCiPipelineId int) ([]*CiPipeline, error)
126128
FindByParentIdAndType(parentCiPipelineId int, pipelineType string) ([]*CiPipeline, error)
127129

@@ -158,7 +160,7 @@ func NewCiPipelineRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger,
158160
}
159161
}
160162

161-
func (impl *CiPipelineRepositoryImpl) FindByLinkedCiCount(parentCiPipelineId int) (int, error) {
163+
func (impl *CiPipelineRepositoryImpl) GetChildrenCiCount(parentCiPipelineId int) (int, error) {
162164
return impl.dbConnection.Model((*CiPipeline)(nil)).
163165
Where("parent_ci_pipeline = ?", parentCiPipelineId).
164166
Where("active = ?", true).

pkg/build/artifacts/wire_artifacts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package artifacts
1818

1919
import "github.com/google/wire"
2020

21-
var ArtifactsWireSet = wire.NewSet(
21+
var WireSet = wire.NewSet(
2222
NewCommonArtifactServiceImpl,
2323
wire.Bind(new(CommonArtifactService), new(*CommonArtifactServiceImpl)),
2424
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package read
18+
19+
import (
20+
"errors"
21+
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
22+
"github.com/go-pg/pg"
23+
"go.uber.org/zap"
24+
)
25+
26+
type CiPipelineConfigReadService interface {
27+
FindLinkedCiCount(ciPipelineId int) (int, error)
28+
FindNumberOfAppsWithCiPipeline(appIds []int) (count int, err error)
29+
FindAllPipelineCreatedCountInLast24Hour() (pipelineCount int, err error)
30+
FindAllDeletedPipelineCountInLast24Hour() (pipelineCount int, err error)
31+
GetChildrenCiCount(parentCiPipelineId int) (int, error)
32+
}
33+
34+
type CiPipelineConfigReadServiceImpl struct {
35+
logger *zap.SugaredLogger
36+
ciPipelineRepository pipelineConfig.CiPipelineRepository
37+
}
38+
39+
func NewCiPipelineConfigReadServiceImpl(
40+
logger *zap.SugaredLogger,
41+
ciPipelineRepository pipelineConfig.CiPipelineRepository,
42+
) *CiPipelineConfigReadServiceImpl {
43+
return &CiPipelineConfigReadServiceImpl{
44+
logger: logger,
45+
ciPipelineRepository: ciPipelineRepository,
46+
}
47+
}
48+
49+
func (impl *CiPipelineConfigReadServiceImpl) FindLinkedCiCount(ciPipelineId int) (int, error) {
50+
return impl.ciPipelineRepository.FindLinkedCiCount(ciPipelineId)
51+
}
52+
53+
func (impl *CiPipelineConfigReadServiceImpl) FindNumberOfAppsWithCiPipeline(appIds []int) (count int, err error) {
54+
return impl.ciPipelineRepository.FindNumberOfAppsWithCiPipeline(appIds)
55+
}
56+
57+
func (impl *CiPipelineConfigReadServiceImpl) FindAllPipelineCreatedCountInLast24Hour() (pipelineCount int, err error) {
58+
return impl.ciPipelineRepository.FindAllPipelineCreatedCountInLast24Hour()
59+
}
60+
61+
func (impl *CiPipelineConfigReadServiceImpl) FindAllDeletedPipelineCountInLast24Hour() (pipelineCount int, err error) {
62+
return impl.ciPipelineRepository.FindAllDeletedPipelineCountInLast24Hour()
63+
}
64+
65+
func (impl *CiPipelineConfigReadServiceImpl) GetChildrenCiCount(parentCiPipelineId int) (int, error) {
66+
count, err := impl.ciPipelineRepository.GetChildrenCiCount(parentCiPipelineId)
67+
if err != nil && !errors.Is(err, pg.ErrNoRows) {
68+
impl.logger.Errorw("failed to get children ci count", "parentCiPipelineId", parentCiPipelineId, "error", err)
69+
return 0, err
70+
} else if errors.Is(err, pg.ErrNoRows) {
71+
impl.logger.Debugw("no children ci found", "parentCiPipelineId", parentCiPipelineId)
72+
return 0, nil
73+
}
74+
return count, nil
75+
}

pkg/build/pipeline/wire_pipeline.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pipeline
18+
19+
import (
20+
"github.com/devtron-labs/devtron/pkg/build/pipeline/read"
21+
"github.com/google/wire"
22+
)
23+
24+
var WireSet = wire.NewSet(
25+
read.NewCiPipelineConfigReadServiceImpl,
26+
wire.Bind(new(read.CiPipelineConfigReadService), new(*read.CiPipelineConfigReadServiceImpl)),
27+
)

pkg/build/wire_build.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ package build
1919
import (
2020
"github.com/devtron-labs/devtron/pkg/build/artifacts"
2121
"github.com/devtron-labs/devtron/pkg/build/git"
22+
"github.com/devtron-labs/devtron/pkg/build/pipeline"
2223
"github.com/google/wire"
2324
)
2425

25-
var BuildWireSet = wire.NewSet(
26-
artifacts.ArtifactsWireSet,
26+
var WireSet = wire.NewSet(
27+
artifacts.WireSet,
28+
pipeline.WireSet,
2729
git.GitWireSet,
2830
)

pkg/pipeline/BuildPipelineConfigService.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/devtron-labs/devtron/pkg/bean"
3333
"github.com/devtron-labs/devtron/pkg/build/pipeline"
3434
bean3 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
35+
"github.com/devtron-labs/devtron/pkg/build/pipeline/read"
3536
pipelineConfigBean "github.com/devtron-labs/devtron/pkg/pipeline/bean"
3637
"github.com/devtron-labs/devtron/pkg/pipeline/history"
3738
"github.com/devtron-labs/devtron/pkg/pipeline/repository"
@@ -117,6 +118,7 @@ type CiPipelineConfigServiceImpl struct {
117118
ciTemplateService CiTemplateService
118119
ciTemplateReadService pipeline.CiTemplateReadService
119120
materialRepo pipelineConfig.MaterialRepository
121+
ciPipelineConfigReadService read.CiPipelineConfigReadService
120122
ciPipelineRepository pipelineConfig.CiPipelineRepository
121123
ciConfig *types.CiCdConfig
122124
attributesService attributes.AttributesService
@@ -147,6 +149,7 @@ func NewCiPipelineConfigServiceImpl(logger *zap.SugaredLogger,
147149
materialRepo pipelineConfig.MaterialRepository,
148150
pipelineGroupRepo app2.AppRepository,
149151
pipelineRepository pipelineConfig.PipelineRepository,
152+
ciPipelineConfigReadService read.CiPipelineConfigReadService,
150153
ciPipelineRepository pipelineConfig.CiPipelineRepository,
151154
ecrConfig *EcrConfig,
152155
appWorkflowRepository appWorkflow.AppWorkflowRepository,
@@ -178,6 +181,7 @@ func NewCiPipelineConfigServiceImpl(logger *zap.SugaredLogger,
178181
materialRepo: materialRepo,
179182
appRepo: pipelineGroupRepo,
180183
pipelineRepository: pipelineRepository,
184+
ciPipelineConfigReadService: ciPipelineConfigReadService,
181185
ciPipelineRepository: ciPipelineRepository,
182186
ecrConfig: ecrConfig,
183187
appWorkflowRepository: appWorkflowRepository,
@@ -2010,8 +2014,8 @@ func (impl *CiPipelineConfigServiceImpl) DeleteCiPipeline(request *bean.CiPatchR
20102014
ciPipelineId := request.CiPipeline.Id
20112015

20122016
// check for linked ci's before deleting the ci pipeline
2013-
count, err := impl.ciPipelineRepository.FindByLinkedCiCount(ciPipelineId)
2014-
if err != nil && err != pg.ErrNoRows {
2017+
count, err := impl.ciPipelineConfigReadService.GetChildrenCiCount(ciPipelineId)
2018+
if err != nil {
20152019
impl.logger.Errorw("error in checking for any linked ci before deleting the ci pipeline", "ciPipelineId", ciPipelineId, "err", err)
20162020
return nil, err
20172021
}

pkg/pipeline/BuildPipelineSwitchService.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/devtron-labs/devtron/internal/util"
2323
"github.com/devtron-labs/devtron/pkg/bean"
2424
pipelineConfigBean "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
25+
"github.com/devtron-labs/devtron/pkg/build/pipeline/read"
2526
"github.com/devtron-labs/devtron/pkg/pipeline/adapter"
2627
"github.com/devtron-labs/devtron/pkg/pipeline/history"
2728
repository4 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
@@ -49,6 +50,7 @@ type BuildPipelineSwitchService interface {
4950

5051
type BuildPipelineSwitchServiceImpl struct {
5152
logger *zap.SugaredLogger
53+
ciPipelineConfigReadService read.CiPipelineConfigReadService
5254
ciPipelineRepository pipelineConfig.CiPipelineRepository
5355
ciCdPipelineOrchestrator CiCdPipelineOrchestrator
5456
pipelineRepository pipelineConfig.PipelineRepository
@@ -60,6 +62,7 @@ type BuildPipelineSwitchServiceImpl struct {
6062
}
6163

6264
func NewBuildPipelineSwitchServiceImpl(logger *zap.SugaredLogger,
65+
ciPipelineConfigReadService read.CiPipelineConfigReadService,
6366
ciPipelineRepository pipelineConfig.CiPipelineRepository,
6467
ciCdPipelineOrchestrator CiCdPipelineOrchestrator,
6568
pipelineRepository pipelineConfig.PipelineRepository,
@@ -70,6 +73,7 @@ func NewBuildPipelineSwitchServiceImpl(logger *zap.SugaredLogger,
7073
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository) *BuildPipelineSwitchServiceImpl {
7174
return &BuildPipelineSwitchServiceImpl{
7275
logger: logger,
76+
ciPipelineConfigReadService: ciPipelineConfigReadService,
7377
ciPipelineRepository: ciPipelineRepository,
7478
ciCdPipelineOrchestrator: ciCdPipelineOrchestrator,
7579
pipelineRepository: pipelineRepository,
@@ -355,7 +359,7 @@ func (impl *BuildPipelineSwitchServiceImpl) updateLinkedAppWorkflowMappings(tx *
355359
func (impl *BuildPipelineSwitchServiceImpl) validateSwitchPreConditions(switchFromCiPipelineId int) error {
356360

357361
// old ci_pipeline should not contain any linked ci_pipelines.
358-
linkedCiPipelines, err := impl.ciPipelineRepository.FindLinkedCiCount(switchFromCiPipelineId)
362+
linkedCiPipelines, err := impl.ciPipelineConfigReadService.FindLinkedCiCount(switchFromCiPipelineId)
359363
if err != nil {
360364
impl.logger.Errorw("error in finding the linkedCi count for the pipeline", "ciPipelineId", switchFromCiPipelineId, "err", err)
361365
return err

0 commit comments

Comments
 (0)