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

feat:Poll Images from ECR Container Repository Plugin #3971

Merged
merged 58 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c4f779a
trigger Ci Cron
Ashish-devtron Sep 26, 2023
3b1ffbc
changes for adding job in devtron apps
iamayushm Sep 26, 2023
52c9fbd
port-fix
iamayushm Sep 26, 2023
093a62c
main merge
Ashish-devtron Sep 26, 2023
90488cc
setting flag in pipeline db
iamayushm Sep 26, 2023
89e3756
fix ecr repo save condition
iamayushm Sep 26, 2023
e702b8c
fix corrupt data save
iamayushm Sep 26, 2023
51bd92b
adding build context git material id
iamayushm Sep 26, 2023
3955d8a
pg no rows fix
iamayushm Sep 26, 2023
4b5a07a
plugin support
Shivam-nagar23 Sep 26, 2023
b4715c7
plugin support
Shivam-nagar23 Sep 26, 2023
027e14c
main-merge
Shivam-nagar23 Sep 26, 2023
91a2301
Merge remote-tracking branch 'origin/cron-trigger' into plugin-poll-s…
Shivam-nagar23 Sep 26, 2023
4ec2bf7
trigger ci for JOB_Ci
Ashish-devtron Sep 27, 2023
65987f6
use const
Ashish-devtron Sep 27, 2023
5500856
remove only git material from ciMaterial
Ashish-devtron Sep 27, 2023
1641402
Merge branch 'main' into job-devtron-apps
iamayushm Sep 27, 2023
3f10998
Merge remote-tracking branch 'origin/cron-trigger' into cr-plugin-test
iamayushm Sep 27, 2023
affdcb0
Logging ci complete event
Shivam-nagar23 Sep 27, 2023
5f776dc
vendor-update
Shivam-nagar23 Sep 27, 2023
e50001d
icon for plugin
Shivam-nagar23 Sep 27, 2023
3196231
Merge branch 'cron-trigger' into job-devtron-apps
iamayushm Sep 27, 2023
6f1a638
get plugin Id from name
Ashish-devtron Sep 27, 2023
937dd39
revert connection.go port
Ashish-devtron Sep 27, 2023
4450ec4
sorting only latest image
Shivam-nagar23 Sep 27, 2023
77dccf4
Merge branch 'job-devtron-apps' into cr-plugin-test
iamayushm Sep 27, 2023
6566e3b
Merge branch 'cron-trigger' into cr-plugin-test
iamayushm Sep 27, 2023
898e850
logs and script addition
Shivam-nagar23 Sep 27, 2023
8157055
Merge remote-tracking branch 'origin/cr-plugin-test' into plugin-poll…
Shivam-nagar23 Sep 27, 2023
d67f4a9
script number change
Shivam-nagar23 Sep 27, 2023
f5b3754
update envDefault
Ashish-devtron Sep 27, 2023
d35a960
Merge branch 'main' into plugin-poll-support
Shivam-nagar23 Sep 27, 2023
5045b11
docker parameters
iamayushm Sep 27, 2023
71244b4
docker repo create enable
iamayushm Sep 27, 2023
0ef3cba
Merge remote-tracking branch 'origin/cr-plugin-test' into plugin-poll…
Shivam-nagar23 Sep 27, 2023
4cc8917
update envDefault
Ashish-devtron Sep 27, 2023
6d697ad
Merge branch 'plugin-poll-support' of github.com:devtron-labs/devtron…
Ashish-devtron Sep 27, 2023
0e01681
sent last fetch artifact time
Ashish-devtron Sep 27, 2023
db10a7b
change SourceControllerCronTime env
Ashish-devtron Sep 27, 2023
14a8b60
change type for cron
Ashish-devtron Sep 27, 2023
f1594f2
change query
Ashish-devtron Sep 27, 2023
8bf9892
image update
Shivam-nagar23 Sep 27, 2023
542df85
review comments
Shivam-nagar23 Sep 27, 2023
184a1e5
review comments
Shivam-nagar23 Sep 27, 2023
9f3d2fc
review comments
Shivam-nagar23 Sep 27, 2023
539d824
image plugin change
Shivam-nagar23 Sep 27, 2023
f957c9d
update down script
Shivam-nagar23 Sep 28, 2023
027ff85
pipeline type flag
iamayushm Sep 28, 2023
338f2ba
flag in app group api
iamayushm Sep 28, 2023
6169e24
Merge branch 'main' into plugin-poll-support
Shivam-nagar23 Sep 28, 2023
294b535
script update
Shivam-nagar23 Sep 28, 2023
ce303aa
comments
Shivam-nagar23 Sep 28, 2023
84ffe61
script change
Shivam-nagar23 Sep 28, 2023
bd8a516
app clone handle
iamayushm Sep 29, 2023
c525146
fixed ci pod request (#3980)
kartik-579 Sep 28, 2023
faac3e1
fixed pipelineOverride id being sent instead of pipelineId (#3984)
kartik-579 Sep 29, 2023
a47fe17
chore: added CODEOWNERS (#3966)
Shubham9t9 Sep 29, 2023
082bac2
Merge branch 'main' into plugin-poll-support
Shivam-nagar23 Sep 29, 2023
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: 4 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,10 @@ func InitializeApp() (*App, error) {
cron.NewCiStatusUpdateCronImpl,
wire.Bind(new(cron.CiStatusUpdateCron), new(*cron.CiStatusUpdateCronImpl)),

cron.GetCiTriggerCronConfig,
cron.NewCiTriggerCronImpl,
wire.Bind(new(cron.CiTriggerCron), new(*cron.CiTriggerCronImpl)),

restHandler.NewPipelineStatusTimelineRestHandlerImpl,
wire.Bind(new(restHandler.PipelineStatusTimelineRestHandler), new(*restHandler.PipelineStatusTimelineRestHandlerImpl)),

Expand Down
18 changes: 17 additions & 1 deletion api/restHandler/app/BuildPipelineRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (handler PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWri
ciConfigRequest.DockerRegistry = emptyDockerRegistry
ciConfigRequest.AppId = patchRequest.AppId
ciConfigRequest.CiBuildConfig = &bean1.CiBuildConfigBean{}
ciConfigRequest.CiBuildConfig.CiBuildType = "skip-build"
ciConfigRequest.CiBuildConfig.CiBuildType = bean1.SKIP_BUILD_BUILD_TYPE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bean1.SKIP_BUILD_BUILD_TYPE >> bean1.SKIP_BUILD_TYPE

ciConfigRequest.UserId = patchRequest.UserId
if patchRequest.CiPipeline == nil || patchRequest.CiPipeline.CiMaterial == nil {
handler.Logger.Errorw("Invalid patch ci-pipeline request", "request", patchRequest, "err", "invalid CiPipeline data")
Expand All @@ -419,6 +419,22 @@ func (handler PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWri
if app.AppType == helper.Job {
patchRequest.IsJob = true
}
if patchRequest.CiPipeline.PipelineType == bean.CI_JOB {
patchRequest.CiPipeline.IsDockerConfigOverridden = true
patchRequest.CiPipeline.DockerConfigOverride = bean.DockerConfigOverride{
DockerRegistry: ciConf.DockerRegistry,
DockerRepository: ciConf.DockerRepository,
CiBuildConfig: &bean1.CiBuildConfigBean{
Id: 0,
GitMaterialId: patchRequest.CiPipeline.CiMaterial[0].GitMaterialId,
BuildContextGitMaterialId: patchRequest.CiPipeline.CiMaterial[0].GitMaterialId,
UseRootBuildContext: false,
CiBuildType: bean1.SKIP_BUILD_BUILD_TYPE,
DockerBuildConfig: nil,
BuildPackConfig: nil,
},
}
}
createResp, err := handler.pipelineBuilder.PatchCiPipeline(&patchRequest)
if err != nil {
handler.Logger.Errorw("service err, PatchCiPipelines", "err", err, "PatchCiPipelines", patchRequest)
Expand Down
45 changes: 43 additions & 2 deletions api/router/pubsub/CiEventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package pubsub
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/caarlos0/env/v6"
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
"github.com/devtron-labs/devtron/internal/sql/repository"
Expand All @@ -28,6 +29,7 @@ import (
bean2 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
"github.com/devtron-labs/devtron/util"
"go.uber.org/zap"
"time"
)

type CiEventConfig struct {
Expand All @@ -53,6 +55,11 @@ type CiEventHandlerImpl struct {
ciEventConfig *CiEventConfig
}

type ImageDetailsFromCR struct {
ImageDetails []types.ImageDetail `json:"imageDetails"`
Region string `json:"region"`
}

type CiCompleteEvent struct {
CiProjectDetails []bean2.CiProjectDetails `json:"ciProjectDetails"`
DockerImage string `json:"dockerImage" validate:"required,image-validator"`
Expand All @@ -67,6 +74,7 @@ type CiCompleteEvent struct {
AppName string `json:"appName"`
IsArtifactUploaded bool `json:"isArtifactUploaded"`
FailureReason string `json:"failureReason"`
ImageDetailsFromCR *ImageDetailsFromCR `json:"imageDetailsFromCR"`
}

func NewCiEventHandlerImpl(logger *zap.SugaredLogger, pubsubClient *pubsub.PubSubClientServiceImpl, webhookService pipeline.WebhookService, ciEventConfig *CiEventConfig) *CiEventHandlerImpl {
Expand Down Expand Up @@ -108,10 +116,27 @@ func (impl *CiEventHandlerImpl) Subscribe() error {
ciCompleteEvent.PipelineId, "request: ", req, "error: ", err)
return
}
} else if ciCompleteEvent.ImageDetailsFromCR != nil {
if len(ciCompleteEvent.ImageDetailsFromCR.ImageDetails) > 0 {
detail := util.GetLatestImageAccToImagePushedAt(ciCompleteEvent.ImageDetailsFromCR.ImageDetails)
request, err := impl.BuildCIArtifactRequestForImageFromCR(detail, ciCompleteEvent.ImageDetailsFromCR.Region, ciCompleteEvent)
if err != nil {
impl.logger.Error("Error while creating request for pipelineID: ",
ciCompleteEvent.PipelineId, "error: ", err)
return
}
resp, err := impl.webhookService.HandleCiSuccessEvent(ciCompleteEvent.PipelineId, request, detail.ImagePushedAt)
if err != nil {
impl.logger.Error("Error while sending event for CI success for pipelineID: ",
ciCompleteEvent.PipelineId, "request: ", request, "error: ", err)
return
}
impl.logger.Debug(resp)
}

} else {
util.TriggerCIMetrics(ciCompleteEvent.Metrics, impl.ciEventConfig.ExposeCiMetrics, ciCompleteEvent.PipelineName, ciCompleteEvent.AppName)

resp, err := impl.webhookService.HandleCiSuccessEvent(ciCompleteEvent.PipelineId, req)
resp, err := impl.webhookService.HandleCiSuccessEvent(ciCompleteEvent.PipelineId, req, &time.Time{})
if err != nil {
impl.logger.Error("Error while sending event for CI success for pipelineID: ",
ciCompleteEvent.PipelineId, "request: ", req, "error: ", err)
Expand Down Expand Up @@ -195,6 +220,22 @@ func (impl *CiEventHandlerImpl) BuildCiArtifactRequest(event CiCompleteEvent) (*
return request, nil
}

func (impl *CiEventHandlerImpl) BuildCIArtifactRequestForImageFromCR(imageDetails types.ImageDetail, region string, event CiCompleteEvent) (*pipeline.CiArtifactWebhookRequest, error) {
if event.TriggeredBy == 0 {
event.TriggeredBy = 1 // system triggered event
}
request := &pipeline.CiArtifactWebhookRequest{
Image: util.ExtractEcrImage(*imageDetails.RegistryId, region, *imageDetails.RepositoryName, imageDetails.ImageTags[0]),
ImageDigest: *imageDetails.ImageDigest,
DataSource: event.DataSource,
PipelineName: event.PipelineName,
UserId: event.TriggeredBy,
WorkflowId: event.WorkflowId,
IsArtifactUploaded: event.IsArtifactUploaded,
}
return request, nil
}

func (impl *CiEventHandlerImpl) BuildCiArtifactRequestForWebhook(event CiCompleteEvent) (*pipeline.CiArtifactWebhookRequest, error) {
ciMaterialInfos := make([]repository.CiMaterialInfo, 0)
if event.MaterialType == "" {
Expand Down
5 changes: 4 additions & 1 deletion api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type MuxRouter struct {
appGroupingRouter AppGroupingRouter
rbacRoleRouter user.RbacRoleRouter
scopedVariableRouter ScopedVariableRouter
ciTriggerCron cron.CiTriggerCron
}

func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter PipelineTriggerRouter, PipelineConfigRouter PipelineConfigRouter,
Expand Down Expand Up @@ -151,7 +152,8 @@ func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter PipelineTriggerRouter, P
userTerminalAccessRouter terminal2.UserTerminalAccessRouter,
jobRouter JobRouter, ciStatusUpdateCron cron.CiStatusUpdateCron, appGroupingRouter AppGroupingRouter,
rbacRoleRouter user.RbacRoleRouter,
scopedVariableRouter ScopedVariableRouter) *MuxRouter {
scopedVariableRouter ScopedVariableRouter,
ciTriggerCron cron.CiTriggerCron) *MuxRouter {
r := &MuxRouter{
Router: mux.NewRouter(),
HelmRouter: HelmRouter,
Expand Down Expand Up @@ -222,6 +224,7 @@ func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter PipelineTriggerRouter, P
appGroupingRouter: appGroupingRouter,
rbacRoleRouter: rbacRoleRouter,
scopedVariableRouter: scopedVariableRouter,
ciTriggerCron: ciTriggerCron,
}
return r
}
Expand Down
Binary file added assets/plugin-poll-container-registry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions client/cron/CiTriggerCron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package cron

import (
"fmt"
"github.com/caarlos0/env"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/pkg/bean"
"github.com/devtron-labs/devtron/pkg/pipeline"
bean2 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
"github.com/devtron-labs/devtron/pkg/pipeline/repository"
repository3 "github.com/devtron-labs/devtron/pkg/plugin/repository"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)

type CiTriggerCron interface {
TriggerCiCron()
}

type CiTriggerCronImpl struct {
logger *zap.SugaredLogger
cron *cron.Cron
cfg *CiTriggerCronConfig
pipelineStageRepository repository.PipelineStageRepository
ciHandler pipeline.CiHandler
ciArtifactRepository repository2.CiArtifactRepository
globalPluginRepository repository3.GlobalPluginRepository
}

func NewCiTriggerCronImpl(logger *zap.SugaredLogger, cfg *CiTriggerCronConfig, pipelineStageRepository repository.PipelineStageRepository,
ciHandler pipeline.CiHandler, ciArtifactRepository repository2.CiArtifactRepository, globalPluginRepository repository3.GlobalPluginRepository) *CiTriggerCronImpl {
cron := cron.New(
cron.WithChain())
cron.Start()
impl := &CiTriggerCronImpl{
logger: logger,
cron: cron,
pipelineStageRepository: pipelineStageRepository,
ciHandler: ciHandler,
cfg: cfg,
ciArtifactRepository: ciArtifactRepository,
globalPluginRepository: globalPluginRepository,
}

_, err := cron.AddFunc(fmt.Sprintf("@every %dm", cfg.SourceControllerCronTime), impl.TriggerCiCron)
if err != nil {
logger.Errorw("error while configure cron job for ci workflow status update", "err", err)
return impl
}
return impl
}

type CiTriggerCronConfig struct {
SourceControllerCronTime int `env:"CI_TRIGGER_CRON_TIME" envDefault:"2"`
PluginName string `env:"PLUGIN_IDS" envDefault:"Pull images from container repository"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginName or PluginIds

}

func GetCiTriggerCronConfig() (*CiTriggerCronConfig, error) {
cfg := &CiTriggerCronConfig{}
err := env.Parse(cfg)
if err != nil {
fmt.Println("failed to parse ci trigger cron config: " + err.Error())
return nil, err
}
return cfg, nil
}

// UpdateCiWorkflowStatusFailedCron this function will execute periodically
func (impl *CiTriggerCronImpl) TriggerCiCron() {

plugin, err := impl.globalPluginRepository.GetPluginByName(impl.cfg.PluginName)

if err != nil || len(plugin) == 0 {
return
}

ciPipelineIds, err := impl.pipelineStageRepository.GetAllCiPipelineIdsByPluginIdAndStageType(plugin[0].Id, string(repository.PIPELINE_STAGE_TYPE_PRE_CI))
if err != nil {
return
}
//artifacts, err := impl.ciArtifactRepository.GetLatestArtifactTimeByCiPipelineIds(ciPipelineIds)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unwanted comments

//mp := make(map[int]time.Time)
//for _, artifact := range artifacts {
// mp[artifact.PipelineId] = artifact.CreatedOn
//}
for _, ciPipelineId := range ciPipelineIds {
//_, err := impl.ciHandler.FetchMaterialsByPipelineId(ciPipelineId, false)
//if err != nil {
// return
//}
var ciPipelineMaterials []bean.CiPipelineMaterial

//for _, material := range materials {
// if len(material.History) == 0 {
// return
// }
// ciPipelineMaterial := bean.CiPipelineMaterial{
// Id: material.Id,
// GitMaterialId: material.GitMaterialId,
// GitCommit: bean.GitCommit{
// Commit: material.History[0].Commit,
// },
// }
// ciPipelineMaterials = append(ciPipelineMaterials, ciPipelineMaterial)
//}
ciTriggerRequest := bean.CiTriggerRequest{
PipelineId: ciPipelineId,
CiPipelineMaterial: ciPipelineMaterials,
TriggeredBy: 1,
InvalidateCache: false,
PipelineType: bean2.CI_JOB,
}
_, err = impl.ciHandler.HandleCIManual(ciTriggerRequest)
if err != nil {
return
}
}
return
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/argoproj/argo-workflows/v3 v3.4.3
github.com/argoproj/gitops-engine v0.7.1-0.20221004132320-98ccd3d43fd9
github.com/aws/aws-sdk-go v1.44.105
github.com/aws/aws-sdk-go-v2/service/ecr v1.20.0
github.com/caarlos0/env v3.5.0+incompatible
github.com/caarlos0/env/v6 v6.7.2
github.com/casbin/casbin v1.9.1
Expand Down Expand Up @@ -114,6 +115,7 @@ require (
github.com/apparentlymart/go-textseg v1.0.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/argoproj/pkg v0.13.6 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bombsimon/logrusr/v2 v2.0.1 // indirect
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ github.com/aws/aws-sdk-go v1.44.39/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4
github.com/aws/aws-sdk-go v1.44.105 h1:UUwoD1PRKIj3ltrDUYTDQj5fOTK3XsnqolLpRTMmSEM=
github.com/aws/aws-sdk-go v1.44.105/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.21.0/go.mod h1:/RfNgGmRxI+iFOB1OeJUyxiU+9s88k3pfHvDagGEp0M=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41/go.mod h1:CrObHAuPneJBlfEJ5T3szXOUkLEThaGfvnhTf33buas=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35/go.mod h1:SJC1nEVVva1g3pHAIdCp7QsRIkMmLAgoDquQ9Rr8kYw=
github.com/aws/aws-sdk-go-v2/service/ecr v1.20.0 h1:Qw8H7V55d2P1d/a9+cLgAcdez4GtP6l30KQAeYqx9vY=
github.com/aws/aws-sdk-go-v2/service/ecr v1.20.0/go.mod h1:pGwmNL8hN0jpBfKfTbmu+Rl0bJkDhaGl+9PQLrZ4KLo=
github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ=
github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down Expand Up @@ -527,6 +534,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
Expand Down
33 changes: 33 additions & 0 deletions internal/sql/repository/CiArtifactRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package repository
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/internal/sql/repository/helper"
"github.com/devtron-labs/devtron/pkg/sql"
"strings"
"time"
Expand Down Expand Up @@ -59,6 +60,8 @@ type CiArtifactRepository interface {
GetByWfId(wfId int) (artifact *CiArtifact, err error)
GetArtifactsByCDPipeline(cdPipelineId, limit int, parentId int, parentType bean.WorkflowType) ([]*CiArtifact, error)

GetLatestArtifactTimeByCiPipelineIds(ciPipelineIds []int) ([]*CiArtifact, error)
GetLatestArtifactTimeByCiPipelineId(ciPipelineId int) (*CiArtifact, error)
GetArtifactsByCDPipelineV2(cdPipelineId int) ([]CiArtifact, error)
GetArtifactsByCDPipelineAndRunnerType(cdPipelineId int, runnerType bean.WorkflowType) ([]CiArtifact, error)
SaveAll(artifacts []*CiArtifact) error
Expand Down Expand Up @@ -236,6 +239,36 @@ func (impl CiArtifactRepositoryImpl) GetArtifactsByCDPipeline(cdPipelineId, limi
return artifactsAll, err
}

func (impl CiArtifactRepositoryImpl) GetLatestArtifactTimeByCiPipelineIds(ciPipelineIds []int) ([]*CiArtifact, error) {
artifacts := make([]*CiArtifact, 0)
query := "select cws.pipeline_id, cws.created_on from " +
"(SELECT pipeline_id, MAX(created_on) created_on " +
"FROM ci_artifact " +
"GROUP BY pipeline_id) cws " +
"where cws.pipeline_id IN (" + helper.GetCommaSepratedString(ciPipelineIds) + "); "

_, err := impl.dbConnection.Query(&artifacts, query)
if err != nil {
return nil, err
}
return artifacts, nil
}

func (impl CiArtifactRepositoryImpl) GetLatestArtifactTimeByCiPipelineId(ciPipelineId int) (*CiArtifact, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put comments

artifacts := &CiArtifact{}
query := "select cws.pipeline_id, cws.created_on from " +
"(SELECT pipeline_id, MAX(created_on) created_on " +
"FROM ci_artifact " +
"GROUP BY pipeline_id) cws " +
"where cws.pipeline_id = ? ; "

_, err := impl.dbConnection.Query(artifacts, query, ciPipelineId)
if err != nil {
return nil, err
}
return artifacts, nil
}

func (impl CiArtifactRepositoryImpl) GetArtifactsByCDPipelineAndRunnerType(cdPipelineId int, runnerType bean.WorkflowType) ([]CiArtifact, error) {
var artifactsA []CiArtifact
var artifactsAB []CiArtifact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type CiPipeline struct {
ParentCiPipeline int `sql:"parent_ci_pipeline"`
ScanEnabled bool `sql:"scan_enabled,notnull"`
IsDockerConfigOverridden bool `sql:"is_docker_config_overridden, notnull"`
PipelineType string `sql:"ci_pipeline_type"`
sql.AuditLog
CiPipelineMaterials []*CiPipelineMaterial
CiTemplate *CiTemplate
Expand Down
Loading
Loading