Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into refactoring-template-…
Browse files Browse the repository at this point in the history
…gitOps-appStore
  • Loading branch information
subhashish-devtron committed Feb 2, 2024
2 parents 5272d9e + 30618a2 commit 7b8b017
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions api/restHandler/app/BuildPipelineRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/devtron-labs/devtron/pkg/pipeline/types"
resourceGroup "github.com/devtron-labs/devtron/pkg/resourceGroup"
util2 "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/response"
"github.com/go-pg/pg"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel"
Expand All @@ -51,6 +52,7 @@ type DevtronAppBuildRestHandler interface {
TriggerCiPipeline(w http.ResponseWriter, r *http.Request)
GetCiPipelineMin(w http.ResponseWriter, r *http.Request)
GetCIPipelineById(w http.ResponseWriter, r *http.Request)
GetCIPipelineByPipelineId(w http.ResponseWriter, r *http.Request)
HandleWorkflowWebhook(w http.ResponseWriter, r *http.Request)
GetBuildLogs(w http.ResponseWriter, r *http.Request)
FetchWorkflowDetails(w http.ResponseWriter, r *http.Request)
Expand Down Expand Up @@ -1170,6 +1172,69 @@ func (handler PipelineConfigRestHandlerImpl) GetCIPipelineById(w http.ResponseWr
common.WriteJsonResp(w, err, ciPipeline, http.StatusOK)
}

func (handler PipelineConfigRestHandlerImpl) GetCIPipelineByPipelineId(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
var ciPipelineId int
var err error
v := r.URL.Query()
pipelineId := v.Get("pipelineId")
if len(pipelineId) != 0 {
ciPipelineId, err = strconv.Atoi(pipelineId)
if err != nil {
handler.Logger.Errorw("request err, GetCIPipelineByPipelineId", "err", err, "pipelineIdParam", pipelineId)
response.WriteResponse(http.StatusBadRequest, "please send valid pipelineId", w, errors.New("pipelineId id invalid"))
return
}
} else {
response.WriteResponse(http.StatusBadRequest, "please send valid pipelineId", w, errors.New("pipelineId id invalid"))
return
}

handler.Logger.Infow("request payload, GetCIPipelineByPipelineId", "pipelineId", pipelineId)

ciPipeline, err := handler.pipelineBuilder.GetCiPipelineById(ciPipelineId)
if err != nil {
handler.Logger.Infow("service error, GetCIPipelineById", "err", err, "pipelineId", pipelineId)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}

app, err := handler.pipelineBuilder.GetApp(ciPipeline.AppId)
if err != nil {
handler.Logger.Infow("service error, GetCIPipelineByPipelineId", "err", err, "appId", ciPipeline.AppId, "pipelineId", pipelineId)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
ciPipeline.AppName = app.AppName
ciPipeline.AppType = app.AppType

resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(app.Id)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
return
}

pipelineData, err := handler.pipelineRepository.FindActiveByAppIdAndPipelineId(ciPipeline.AppId, ciPipelineId)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
var environmentIds []int
for _, pipeline := range pipelineData {
environmentIds = append(environmentIds, pipeline.EnvironmentId)
}
if handler.appWorkflowService.CheckCdPipelineByCiPipelineId(ciPipelineId) {
for _, envId := range environmentIds {
envObject := handler.enforcerUtil.GetEnvRBACNameByCiPipelineIdAndEnvId(ciPipelineId, envId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceEnvironment, casbin.ActionUpdate, envObject); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
return
}
}
}
common.WriteJsonResp(w, err, ciPipeline, http.StatusOK)
}

func (handler PipelineConfigRestHandlerImpl) CreateMaterial(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
decoder := json.NewDecoder(r.Body)
Expand Down
1 change: 1 addition & 0 deletions api/router/PipelineConfigRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (router PipelineConfigRouterImpl) initPipelineConfigRouter(configRouter *mu
configRouter.Path("/env").HandlerFunc(router.restHandler.EnvConfigOverrideUpdate).Methods("PUT")
configRouter.Path("/env/{appId}/{environmentId}/{chartRefId}").HandlerFunc(router.restHandler.GetEnvConfigOverride).Methods("GET")

configRouter.Path("/ci-pipeline").HandlerFunc(router.restHandler.GetCIPipelineByPipelineId).Methods("GET")
configRouter.Path("/ci-pipeline").HandlerFunc(router.restHandler.CreateCiConfig).Methods("POST")
configRouter.Path("/ci-pipeline/{appId}").HandlerFunc(router.restHandler.GetCiPipeline).Methods("GET")
configRouter.Path("/external-ci/{appId}").HandlerFunc(router.restHandler.GetExternalCi).Methods("GET")
Expand Down
2 changes: 2 additions & 0 deletions pkg/bean/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type CiPipeline struct {
ParentCiPipeline int `json:"parentCiPipeline"`
ParentAppId int `json:"parentAppId"`
AppId int `json:"appId"`
AppName string `json:"appName,omitempty"`
AppType helper.AppType `json:"appType,omitempty"`
ExternalCiConfig ExternalCiConfig `json:"externalCiConfig"`
CiMaterial []*CiMaterial `json:"ciMaterial,omitempty" validate:"dive,min=1"`
Name string `json:"name,omitempty" validate:"name-component,max=100"` //name suffix of corresponding pipeline. required, unique, validation corresponding to gocd pipelineName will be applicable
Expand Down

0 comments on commit 7b8b017

Please sign in to comment.