Skip to content

Commit a8ca5b0

Browse files
fix: duplicate name in charts and chart Group (#2942)
* Duplicate name in Charts and Chart Group * Bad Request * Error message * Status Code Update
1 parent 72353ed commit a8ca5b0

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

api/appStore/InstalledAppRestHandler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,16 @@ func (handler *InstalledAppRestHandlerImpl) DeployBulk(w http.ResponseWriter, r
295295
}
296296
//RBAC block ends here
297297

298+
visited := make(map[string]bool)
299+
298300
for _, item := range request.ChartGroupInstallChartRequest {
301+
if visited[item.AppName] {
302+
handler.Logger.Errorw("service err, CreateInstalledApp", "err", err, "payload", request)
303+
common.WriteJsonResp(w, errors.New("duplicate appName found"), nil, http.StatusBadRequest)
304+
return
305+
} else {
306+
visited[item.AppName] = true
307+
}
299308
isChartRepoActive, err := handler.appStoreDeploymentService.IsChartRepoActive(item.AppStoreVersion)
300309
if err != nil {
301310
handler.Logger.Errorw("service err, CreateInstalledApp", "err", err, "payload", request)

api/restHandler/ChartGroupRestHandler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ func (impl *ChartGroupRestHandlerImpl) CreateChartGroup(w http.ResponseWriter, r
9999
res, err := impl.ChartGroupService.CreateChartGroup(&request)
100100
if err != nil {
101101
impl.Logger.Errorw("service err, CreateChartGroup", "err", err, "payload", request)
102-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
102+
statusCode := http.StatusInternalServerError
103+
if service.AppNameAlreadyExistsError == err.Error() {
104+
statusCode = http.StatusBadRequest
105+
}
106+
common.WriteJsonResp(w, err, nil, statusCode)
103107
return
104108
}
105109
common.WriteJsonResp(w, err, res, http.StatusOK)
@@ -312,7 +316,7 @@ func (impl *ChartGroupRestHandlerImpl) GetChartGroupListMin(w http.ResponseWrite
312316
common.WriteJsonResp(w, err, res, http.StatusOK)
313317
}
314318

315-
func(impl *ChartGroupRestHandlerImpl) DeleteChartGroup(w http.ResponseWriter, r *http.Request){
319+
func (impl *ChartGroupRestHandlerImpl) DeleteChartGroup(w http.ResponseWriter, r *http.Request) {
316320
userId, err := impl.userAuthService.GetLoggedInUser(r)
317321
if userId == 0 || err != nil {
318322
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
@@ -350,4 +354,4 @@ func(impl *ChartGroupRestHandlerImpl) DeleteChartGroup(w http.ResponseWriter, r
350354
return
351355
}
352356
common.WriteJsonResp(w, err, CHART_GROUP_DELETE_SUCCESS_RESP, http.StatusOK)
353-
}
357+
}

internal/sql/repository/app/AppRepository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (repo AppRepositoryImpl) CheckAppExists(appNames []string) ([]*App, error)
135135
err := repo.dbConnection.
136136
Model(&apps).
137137
Where("app_name in (?)", pg.In(appNames)).
138+
Where("active = ?", true).
138139
Select()
139140
return apps, err
140141
}

pkg/appStore/deployment/repository/ChartGroup.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type ChartGroupReposotory interface {
5353
FindById(chartGroupId int) (*ChartGroup, error)
5454
GetAll(max int) ([]*ChartGroup, error)
5555
MarkChartGroupDeleted(chartGroupId int, tx *pg.Tx) error
56+
FindByName(chartGroupName string) (bool, error)
5657
}
5758

5859
func (impl *ChartGroupReposotoryImpl) Save(model *ChartGroup) (*ChartGroup, error) {
@@ -105,3 +106,12 @@ func (impl *ChartGroupReposotoryImpl) MarkChartGroupDeleted(chartGroupId int, tx
105106
Update()
106107
return err
107108
}
109+
110+
func (impl *ChartGroupReposotoryImpl) FindByName(chartGroupName string) (bool, error) {
111+
var ChartGroup ChartGroup
112+
exists, err := impl.dbConnection.Model(&ChartGroup).Where("name = ?", chartGroupName).
113+
Where("deleted = ?", false).
114+
Exists()
115+
116+
return exists, err
117+
}

pkg/appStore/deployment/service/ChartGroupService.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package service
1919

2020
import (
21+
"errors"
2122
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
2223
"github.com/devtron-labs/devtron/pkg/appStore/deployment/repository"
2324
appStoreValuesRepository "github.com/devtron-labs/devtron/pkg/appStore/values/repository"
@@ -69,7 +70,7 @@ type ChartGroupList struct {
6970
Groups []*ChartGroupBean `json:"groups,omitempty"`
7071
}
7172
type ChartGroupBean struct {
72-
Name string `json:"name,omitempty" validate:"name-component,max=200"`
73+
Name string `json:"name,omitempty" validate:"name-component,max=200,min=5"`
7374
Description string `json:"description,omitempty"`
7475
Id int `json:"id,omitempty"`
7576
ChartGroupEntries []*ChartGroupEntryBean `json:"chartGroupEntries,omitempty"`
@@ -108,8 +109,21 @@ type InstalledChart struct {
108109
InstalledAppId int `json:"installedAppId,omitempty"`
109110
}
110111

112+
const AppNameAlreadyExistsError = "A chart with this name already exist"
113+
111114
func (impl *ChartGroupServiceImpl) CreateChartGroup(req *ChartGroupBean) (*ChartGroupBean, error) {
112115
impl.Logger.Debugw("chart group create request", "req", req)
116+
117+
exist, err := impl.chartGroupRepository.FindByName(req.Name)
118+
if err != nil {
119+
impl.Logger.Errorw("error in creating chart group", "req", req, "err", err)
120+
return nil, err
121+
}
122+
if exist {
123+
impl.Logger.Errorw("Chart with this name already exist", "req", req, "err", err)
124+
return nil, errors.New(AppNameAlreadyExistsError)
125+
}
126+
113127
chartGrouModel := &repository.ChartGroup{
114128
Name: req.Name,
115129
Description: req.Description,

0 commit comments

Comments
 (0)