Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 32 additions & 5 deletions internal/sql/repository/app/AppRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package app

import (
"fmt"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/pkg/team"
"github.com/go-pg/pg"
Expand Down Expand Up @@ -58,7 +59,8 @@ type AppRepository interface {
FindAppAndProjectByAppName(appName string) (*App, error)
GetConnection() *pg.DB
FindAllMatchesByAppName(appName string) ([]*App, error)
FindIdsByTeamIds(teamIds []int) ([]int, error)
FindIdsByTeamIdsAndTeamNames(teamIds []int, teamNames []string) ([]int, error)
FindIdsByNames(appNames []string) ([]int, error)
}

const DevtronApp = "DevtronApp"
Expand Down Expand Up @@ -250,12 +252,37 @@ func (repo AppRepositoryImpl) FindAllMatchesByAppName(appName string) ([]*App, e
return apps, err
}

func (repo AppRepositoryImpl) FindIdsByTeamIds(teamIds []int) ([]int, error) {
func (repo AppRepositoryImpl) FindIdsByTeamIdsAndTeamNames(teamIds []int, teamNames []string) ([]int, error) {
var ids []int
query := "select id from app where team_id in (?) and active = ?;"
_, err := repo.dbConnection.Query(&ids, query, pg.In(teamIds), true)
var err error
if len(teamIds) == 0 && len(teamNames) == 0 {
err = fmt.Errorf("invalid input arguments, no projectIds or projectNames to get apps")
return nil, err
}
if len(teamIds) > 0 && len(teamNames) > 0 {
query := `select app.id from app inner join team on team.id=app.team_id where team.active=? and app.active=?
and (team.id in (?) or team.name in (?));`
_, err = repo.dbConnection.Query(&ids, query, true, true, pg.In(teamIds), pg.In(teamNames))
} else if len(teamIds) > 0 {
query := "select id from app where team_id in (?) and active=?;"
_, err = repo.dbConnection.Query(&ids, query, pg.In(teamIds), true)
} else if len(teamNames) > 0 {
query := "select app.id from app inner join team on team.id=app.team_id where team.name in (?) and team.active=? and app.active=?;"
_, err = repo.dbConnection.Query(&ids, query, pg.In(teamNames), true, true)
}
if err != nil {
repo.logger.Errorw("error in getting appIds by teamIds and teamNames", "err", err, "teamIds", teamIds, "teamNames", teamNames)
return nil, err
}
return ids, err
}

func (repo AppRepositoryImpl) FindIdsByNames(appNames []string) ([]int, error) {
var ids []int
query := "select id from app where app_name in (?) and active=?;"
_, err := repo.dbConnection.Query(&ids, query, pg.In(appNames), true)
if err != nil {
repo.logger.Errorw("error in getting appIds by teamIds", "err", err, "teamIds", teamIds)
repo.logger.Errorw("error in getting appIds by names", "err", err, "names", appNames)
return nil, err
}
return ids, err
Expand Down
30 changes: 22 additions & 8 deletions pkg/bulkAction/BulkUpdateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,31 +1284,45 @@ func (impl BulkUpdateServiceImpl) BulkBuildTrigger(request *BulkApplicationForEn

func (impl BulkUpdateServiceImpl) GetBulkActionImpactedPipelinesAndWfs(dto *CdBulkActionRequestDto) ([]*pipelineConfig.Pipeline, []int, []int, error) {
var err error
if len(dto.EnvIds) == 0 || (len(dto.AppIds) == 0 && len(dto.ProjectIds) == 0) {
//invalid payload, envIds are must and either of appIds or projectIds are must
if (len(dto.EnvIds) == 0 && len(dto.EnvNames) == 0) || ((len(dto.AppIds) == 0 && len(dto.AppNames) == 0) && (len(dto.ProjectIds) == 0 && len(dto.ProjectNames) == 0)) {
//invalid payload, envIds or envNames are must and at least one of appIds, appNames, projectIds, projectNames is must
return nil, nil, nil, &util.ApiError{Code: "400", HttpStatusCode: 400, UserMessage: "invalid payload, can not get pipelines for this filter"}
}

if len(dto.ProjectIds) > 0 {
appIdsInProjects, err := impl.appRepository.FindIdsByTeamIds(dto.ProjectIds)
if len(dto.ProjectIds) > 0 || len(dto.ProjectNames) > 0 {
appIdsInProjects, err := impl.appRepository.FindIdsByTeamIdsAndTeamNames(dto.ProjectIds, dto.ProjectNames)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting appIds by projectIds", "err", err, "projectIds", dto.ProjectIds)
impl.logger.Errorw("error in getting appIds by projectIds and projectNames", "err", err, "projectIds", dto.ProjectIds, "projectNames", dto.ProjectNames)
return nil, nil, nil, err
}
dto.AppIds = append(dto.AppIds, appIdsInProjects...)
}
var impactedWfIds []int
var impactedPipelineIds []int
var impactedCiPipelineIds []int
if len(dto.AppIds) > 0 && len(dto.EnvIds) > 0 {
if (len(dto.AppIds) > 0 || len(dto.AppNames) > 0) && (len(dto.EnvIds) > 0 || len(dto.EnvNames) > 0) {
if len(dto.AppNames) > 0 {
appIdsByNames, err := impl.appRepository.FindIdsByNames(dto.AppNames)
if err != nil {
impl.logger.Errorw("error in getting appIds by names", "err", err, "names", dto.AppNames)
return nil, nil, nil, err
}
dto.AppIds = append(dto.AppIds, appIdsByNames...)
}
if len(dto.EnvNames) > 0 {
envIdsByNames, err := impl.environmentRepository.FindIdsByNames(dto.EnvNames)
if err != nil {
impl.logger.Errorw("error in getting envIds by names", "err", err, "names", dto.EnvNames)
return nil, nil, nil, err
}
dto.EnvIds = append(dto.EnvIds, envIdsByNames...)
}
if !dto.DeleteWfAndCiPipeline {
//getting pipeline IDs for app level deletion request
impactedPipelineIds, err = impl.pipelineRepository.FindIdsByAppIdsAndEnvironmentIds(dto.AppIds, dto.EnvIds)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting cd pipelines by appIds and envIds", "err", err)
return nil, nil, nil, err
}

} else {
//getting all workflows in given apps which do not have pipelines of other than given environments
appWfs, err := impl.appWorkflowRepository.FindAllWfsHavingCdPipelinesFromSpecificEnvsOnly(dto.EnvIds, dto.AppIds)
Expand Down
3 changes: 3 additions & 0 deletions pkg/bulkAction/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ const (
type CdBulkActionRequestDto struct {
Action CdBulkAction `json:"action"`
EnvIds []int `json:"envIds"`
EnvNames []string `json:"envNames"`
AppIds []int `json:"appIds"`
AppNames []string `json:"appNames"`
ProjectIds []int `json:"projectIds"`
ProjectNames []string `json:"projectNames"`
DeleteWfAndCiPipeline bool `json:"deleteWfAndCiPipeline"`
ForceDelete bool `json:"forceDelete"`
UserId int32 `json:"-"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/repository/EnvironmentRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type EnvironmentRepository interface {
FindOneByNamespaceAndClusterId(namespace string, clusterId int) (*Environment, error)
FindByClusterIdAndNamespace(namespaceClusterPair []*ClusterNamespacePair) ([]*Environment, error)
FindByClusterIds(clusterIds []int) ([]*Environment, error)
FindIdsByNames(envNames []string) ([]int, error)
}

func NewEnvironmentRepositoryImpl(dbConnection *pg.DB) *EnvironmentRepositoryImpl {
Expand Down Expand Up @@ -242,3 +243,10 @@ func (repo EnvironmentRepositoryImpl) MarkEnvironmentDeleted(deleteReq *Environm
func (repo EnvironmentRepositoryImpl) GetConnection() (dbConnection *pg.DB) {
return repo.dbConnection
}

func (repo EnvironmentRepositoryImpl) FindIdsByNames(envNames []string) ([]int, error) {
var ids []int
query := "select id from environment where environment_name in (?) and active=?;"
_, err := repo.dbConnection.Query(&ids, query, pg.In(envNames), true)
return ids, err
}
15 changes: 15 additions & 0 deletions specs/bulk-env-delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,31 @@ components:
items:
type: integer
description: ids of all environments whose pipelines are to be included in action
envNames:
type: array
items:
type: integer
description: names of all environments whose pipelines are to be included in action
appIds:
type: array
items:
type: integer
description: ids of all apps for which the environment is to be included
appNames:
type: array
items:
type: integer
description: names of all apps for which the environment is to be included
projectIds:
type: array
items:
type: integer
description: ids of all projects for which the environment is to be included
projectNames:
type: array
items:
type: integer
description: names of all projects for which the environment is to be included
CdPipelineImpactedObjectDto:
type: object
properties:
Expand Down