Skip to content

Commit 02a13e3

Browse files
authored
chore: refactoring mandatory plugin v2 (#6056)
* chore: refactoring mandatory plugin v2 * updated error message * added migration script for mandatory plugin * added; unit test caes * fix: backward compactibility issue * chore: rename import alias
1 parent 7a7d84b commit 02a13e3

29 files changed

+789
-303
lines changed

App.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ import (
4747
)
4848

4949
type App struct {
50-
MuxRouter *router.MuxRouter
51-
Logger *zap.SugaredLogger
52-
SSE *sse.SSE
53-
Enforcer *casbin.SyncedEnforcer
54-
EnforcerV2 *casbinv2.SyncedEnforcer
55-
server *http.Server
56-
db *pg.DB
57-
posthogClient *telemetry.PosthogClient
58-
centralEventProcessor *eventProcessor.CentralEventProcessor
50+
MuxRouter *router.MuxRouter
51+
Logger *zap.SugaredLogger
52+
SSE *sse.SSE
53+
Enforcer *casbin.SyncedEnforcer
54+
EnforcerV2 *casbinv2.SyncedEnforcer
55+
server *http.Server
56+
db *pg.DB
57+
posthogClient *telemetry.PosthogClient
58+
// eventProcessor.CentralEventProcessor is used to register event processors
59+
centralEventProcessor *eventProcessor.CentralEventProcessor // do not remove this.
5960
// used for local dev only
6061
serveTls bool
6162
sessionManager2 *authMiddleware.SessionManager
@@ -85,7 +86,7 @@ func NewApp(router *router.MuxRouter,
8586
Logger: Logger,
8687
SSE: sse,
8788
Enforcer: enforcer,
88-
EnforcerV2: enforcerV2,
89+
EnforcerV2: enforcerV2,
8990
db: db,
9091
serveTls: false,
9192
sessionManager2: sessionManager2,

api/restHandler/CoreAppRestHandler.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"errors"
2323
"fmt"
2424
app2 "github.com/devtron-labs/devtron/api/restHandler/app/pipeline/configure"
25-
bean3 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
25+
appWorkflowBean "github.com/devtron-labs/devtron/pkg/appWorkflow/bean"
26+
pipelineBean "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
2627
"net/http"
2728
"strconv"
2829
"strings"
@@ -197,7 +198,7 @@ func (handler CoreAppRestHandlerImpl) GetAppAllDetail(w http.ResponseWriter, r *
197198

198199
//get/build app workflows starts
199200
//using empty workflow name because it is optional, if not provided then workflows will be fetched on the basis of app
200-
wfCloneRequest := &appWorkflow.WorkflowCloneRequest{AppId: appId}
201+
wfCloneRequest := &appWorkflowBean.WorkflowCloneRequest{AppId: appId}
201202
appWorkflows, err, statusCode := handler.buildAppWorkflows(wfCloneRequest)
202203
if err != nil {
203204
common.WriteJsonResp(w, err, nil, statusCode)
@@ -626,24 +627,24 @@ func (handler CoreAppRestHandlerImpl) buildAppEnvironmentDeploymentTemplate(appI
626627
}
627628

628629
// validate and build workflows
629-
func (handler CoreAppRestHandlerImpl) buildAppWorkflows(request *appWorkflow.WorkflowCloneRequest) ([]*appBean.AppWorkflow, error, int) {
630+
func (handler CoreAppRestHandlerImpl) buildAppWorkflows(request *appWorkflowBean.WorkflowCloneRequest) ([]*appBean.AppWorkflow, error, int) {
630631
handler.logger.Debugw("Getting app detail - workflows", "appId", request.AppId)
631-
var workflowsList []appWorkflow.AppWorkflowDto
632+
var workflowsList []appWorkflowBean.AppWorkflowDto
632633
var err error
633634
if len(request.WorkflowName) != 0 {
634635
workflow, err := handler.appWorkflowService.FindAppWorkflowByName(request.WorkflowName, request.AppId)
635636
if err != nil {
636637
handler.logger.Errorw("error in fetching workflow by name", "err", err, "workflowName", request.WorkflowName, "appId", request.AppId)
637638
return nil, err, http.StatusInternalServerError
638639
}
639-
workflowsList = []appWorkflow.AppWorkflowDto{workflow}
640+
workflowsList = []appWorkflowBean.AppWorkflowDto{workflow}
640641
} else if request.WorkflowId > 0 {
641642
workflow, err := handler.appWorkflowService.FindAppWorkflowById(request.WorkflowId, request.AppId)
642643
if err != nil {
643644
handler.logger.Errorw("error in fetching workflow by id", "err", err, "workflowName", request.WorkflowName, "appId", request.AppId)
644645
return nil, err, http.StatusInternalServerError
645646
}
646-
workflowsList = []appWorkflow.AppWorkflowDto{workflow}
647+
workflowsList = []appWorkflowBean.AppWorkflowDto{workflow}
647648
} else {
648649
workflowsList, err = handler.appWorkflowService.FindAppWorkflows(request.AppId)
649650
if err != nil {
@@ -1323,9 +1324,9 @@ func (handler CoreAppRestHandlerImpl) createDockerConfig(appId int, dockerConfig
13231324
dockerBuildConfig := dockerConfig.DockerBuildConfig
13241325
if dockerBuildConfig != nil {
13251326
dockerConfig.CheckoutPath = dockerBuildConfig.GitCheckoutPath
1326-
dockerConfig.CiBuildConfig = &bean3.CiBuildConfigBean{
1327-
CiBuildType: bean3.SELF_DOCKERFILE_BUILD_TYPE,
1328-
DockerBuildConfig: &bean3.DockerBuildConfig{
1327+
dockerConfig.CiBuildConfig = &pipelineBean.CiBuildConfigBean{
1328+
CiBuildType: pipelineBean.SELF_DOCKERFILE_BUILD_TYPE,
1329+
DockerBuildConfig: &pipelineBean.DockerBuildConfig{
13291330
DockerfilePath: dockerBuildConfig.DockerfileRelativePath,
13301331
DockerBuildOptions: dockerBuildConfig.DockerBuildOptions,
13311332
Args: dockerBuildConfig.Args,
@@ -1546,7 +1547,7 @@ func (handler CoreAppRestHandlerImpl) createWorkflows(ctx context.Context, appId
15461547
//Creating CI pipeline starts
15471548
ciPipeline, err := handler.createCiPipeline(appId, userId, workflowId, workflow.CiPipeline)
15481549
if err != nil {
1549-
if err.Error() == bean3.PIPELINE_NAME_ALREADY_EXISTS_ERROR {
1550+
if err.Error() == pipelineBean.PIPELINE_NAME_ALREADY_EXISTS_ERROR {
15501551
handler.logger.Errorw("service err, DeleteAppWorkflow ", "err", err)
15511552
return err, http.StatusBadRequest
15521553
}
@@ -1683,7 +1684,7 @@ func (handler CoreAppRestHandlerImpl) createCiPipeline(appId int, userId int32,
16831684
ParentCiPipeline: ciPipelineData.ParentCiPipeline,
16841685
ParentAppId: ciPipelineData.ParentAppId,
16851686
LinkedCount: ciPipelineData.LinkedCount,
1686-
PipelineType: bean3.PipelineType(ciPipelineData.PipelineType),
1687+
PipelineType: pipelineBean.PipelineType(ciPipelineData.PipelineType),
16871688
},
16881689
}
16891690

@@ -2330,7 +2331,7 @@ func (handler CoreAppRestHandlerImpl) GetAppWorkflow(w http.ResponseWriter, r *h
23302331

23312332
//get/build app workflows starts
23322333
//using empty workflow name because it is optional, if not provided then workflows will be fetched on the basis of app
2333-
wfCloneRequest := &appWorkflow.WorkflowCloneRequest{AppId: appId}
2334+
wfCloneRequest := &appWorkflowBean.WorkflowCloneRequest{AppId: appId}
23342335
appWorkflows, err, statusCode := handler.buildAppWorkflows(wfCloneRequest)
23352336
if err != nil {
23362337
common.WriteJsonResp(w, err, nil, statusCode)
@@ -2378,7 +2379,7 @@ func (handler CoreAppRestHandlerImpl) GetAppWorkflowAndOverridesSample(w http.Re
23782379
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
23792380
return
23802381
}
2381-
wfCloneRequest := &appWorkflow.WorkflowCloneRequest{AppId: appId}
2382+
wfCloneRequest := &appWorkflowBean.WorkflowCloneRequest{AppId: appId}
23822383
workflowName := r.URL.Query().Get("workflowName")
23832384
wfCloneRequest.WorkflowName = workflowName
23842385
environmentIdStr := r.URL.Query().Get("environmentId")

api/restHandler/GlobalPluginRestHandler.go

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import (
2828
"github.com/devtron-labs/devtron/pkg/plugin/bean"
2929
"github.com/devtron-labs/devtron/util/rbac"
3030
"github.com/gorilla/mux"
31+
"github.com/gorilla/schema"
3132
"go.uber.org/zap"
33+
"io"
3234
"net/http"
3335
"strconv"
36+
"strings"
3437
)
3538

3639
type GlobalPluginRestHandler interface {
@@ -310,12 +313,13 @@ func (handler *GlobalPluginRestHandlerImpl) GetAllUniqueTags(w http.ResponseWrit
310313

311314
func (handler *GlobalPluginRestHandlerImpl) GetPluginDetailByIds(w http.ResponseWriter, r *http.Request) {
312315
token := r.Header.Get("token")
313-
appId, err := common.ExtractIntQueryParam(w, r, "appId", 0)
316+
request, err := handler.getPluginDetailsRequestDto(r)
314317
if err != nil {
318+
common.WriteJsonResp(w, err, err.Error(), http.StatusBadRequest)
315319
return
316320
}
317321

318-
ok, err := handler.IsUserAuthorized(token, appId)
322+
ok, err := handler.IsUserAuthorized(token, request.AppId)
319323
if err != nil {
320324
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
321325
return
@@ -325,15 +329,9 @@ func (handler *GlobalPluginRestHandlerImpl) GetPluginDetailByIds(w http.Response
325329
return
326330
}
327331

328-
pluginIds, parentPluginIds, fetchAllVersionDetails, err := handler.extractQueryParamsForPluginDetail(r)
332+
pluginDetail, err := handler.globalPluginService.GetPluginDetailV2(request)
329333
if err != nil {
330-
common.WriteJsonResp(w, err, err.Error(), http.StatusBadRequest)
331-
return
332-
}
333-
334-
pluginDetail, err := handler.globalPluginService.GetPluginDetailV2(pluginIds, parentPluginIds, fetchAllVersionDetails)
335-
if err != nil {
336-
handler.logger.Errorw("error in getting plugin detail", "pluginIds", pluginIds, "parentPluginIds", parentPluginIds, "fetchAllVersionDetails", fetchAllVersionDetails, "err", err)
334+
handler.logger.Errorw("error in getting plugin detail", "request", request, "err", err)
337335
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
338336
return
339337
}
@@ -376,22 +374,27 @@ func (handler *GlobalPluginRestHandlerImpl) getListFilterFromQueryParam(w http.R
376374
return listFilter, nil
377375
}
378376

379-
func (handler *GlobalPluginRestHandlerImpl) extractQueryParamsForPluginDetail(r *http.Request) ([]int, []int, bool, error) {
380-
pluginIds, parentPluginIds := make([]int, 0), make([]int, 0)
381-
382-
pluginIds, err := common.ExtractIntArrayFromQueryParam(r, "pluginId")
383-
if err != nil {
384-
return nil, nil, false, errors.New("invalid pluginId")
385-
}
386-
parentPluginIds, err = common.ExtractIntArrayFromQueryParam(r, "parentPluginId")
387-
if err != nil {
388-
return nil, nil, false, errors.New("invalid parentPluginId")
389-
}
390-
fetchAllVersionDetails, err := common.ExtractBoolQueryParam(r, "fetchAllVersionDetails")
391-
if err != nil {
392-
return nil, nil, fetchAllVersionDetails, errors.New("invalid fetchAllVersionDetails value")
377+
func (handler *GlobalPluginRestHandlerImpl) getPluginDetailsRequestDto(r *http.Request) (bean.GlobalPluginDetailsRequest, error) {
378+
request := bean.GlobalPluginDetailsRequest{}
379+
jsonDecoder := json.NewDecoder(r.Body)
380+
err := jsonDecoder.Decode(&request)
381+
if err != nil && err != io.EOF {
382+
handler.logger.Errorw("request err, CreateOrUpdateGlobalPolicy", "err", err, "payload", request)
383+
return request, err
384+
} else if err == io.EOF {
385+
var schemaDecoder = schema.NewDecoder()
386+
schemaDecoder.IgnoreUnknownKeys(true)
387+
err = schemaDecoder.Decode(&request, r.URL.Query())
388+
if err != nil {
389+
handler.logger.Errorw("error in parsing query param", "err", err)
390+
return request, err
391+
}
392+
parentPluginIdentifiers := strings.Split(request.ParentPluginIdentifier, ",")
393+
for _, identifiers := range parentPluginIdentifiers {
394+
request.ParentPluginIdentifiers = append(request.ParentPluginIdentifiers, strings.TrimSpace(identifiers))
395+
}
393396
}
394-
return pluginIds, parentPluginIds, fetchAllVersionDetails, nil
397+
return request, nil
395398
}
396399

397400
func (handler *GlobalPluginRestHandlerImpl) IsUserAuthorized(token string, appId int) (bool, error) {
@@ -465,21 +468,31 @@ func (handler *GlobalPluginRestHandlerImpl) CreatePlugin(w http.ResponseWriter,
465468

466469
func (handler *GlobalPluginRestHandlerImpl) GetAllPluginMinData(w http.ResponseWriter, r *http.Request) {
467470
token := r.Header.Get("token")
468-
appId, err := common.ExtractIntQueryParam(w, r, "appId", 0)
471+
v := r.URL.Query()
472+
var schemaDecoder = schema.NewDecoder()
473+
schemaDecoder.IgnoreUnknownKeys(true)
474+
queryParams := bean.PluginDetailsMinQuery{}
475+
err := schemaDecoder.Decode(&queryParams, v)
469476
if err != nil {
477+
handler.logger.Errorw("error in parsing query param", "err", err)
478+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
470479
return
471480
}
472-
ok, err := handler.IsUserAuthorized(token, appId)
481+
ok, err := handler.IsUserAuthorized(token, queryParams.AppId)
473482
if err != nil {
483+
handler.logger.Errorw("error in verifying rbac", "appId", queryParams.AppId, "err", err)
474484
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
475485
return
476486
}
477487
if !ok {
478488
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
479489
return
480490
}
481-
482-
pluginDetail, err := handler.globalPluginService.GetAllPluginMinData()
491+
if !queryParams.IsValidPluginType() {
492+
common.WriteJsonResp(w, fmt.Errorf("invalid query param 'type'"), "invalid query param 'type'", http.StatusBadRequest)
493+
return
494+
}
495+
pluginDetail, err := handler.globalPluginService.GetAllPluginMinData(queryParams.GetPluginType())
483496
if err != nil {
484497
handler.logger.Errorw("error in getting all unique tags", "err", err)
485498
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

0 commit comments

Comments
 (0)