Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1513,33 +1513,39 @@ func (handler *PipelineConfigRestHandlerImpl) ListDeploymentHistory(w http.Respo
}
token := r.Header.Get("token")
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
appIdStr := vars["appId"]
appId, err := strconv.Atoi(appIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid appId", "err", err, "appId", appIdStr)
common.HandleParameterError(w, r, "appId", appIdStr)
return
}
pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineIdStr := vars["pipelineId"]
pipelineId, err := strconv.Atoi(pipelineIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid pipelineId", "err", err, "pipelineId", pipelineIdStr)
common.HandleParameterError(w, r, "pipelineId", pipelineIdStr)
return
}

environmentId, err := strconv.Atoi(vars["environmentId"])
environmentIdStr := vars["environmentId"]
environmentId, err := strconv.Atoi(environmentIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid environmentId", "err", err, "environmentId", environmentIdStr)
common.HandleParameterError(w, r, "environmentId", environmentIdStr)
return
}

offsetQueryParam := r.URL.Query().Get("offset")
offset, err := strconv.Atoi(offsetQueryParam)
if offsetQueryParam == "" || err != nil {
//offsetQueryParam := r.URL.Query().Get("offset")
offset, err := common.ExtractPaginationParameterOrSetDefault(r, "offset", 0)
if err != nil {
handler.Logger.Errorw("request err, ListDeploymentHistory", "err", err, "appId", appId, "environmentId", environmentId, "pipelineId", pipelineId, "offset", offset)
common.WriteJsonResp(w, err, "invalid offset", http.StatusBadRequest)
return
}
sizeQueryParam := r.URL.Query().Get("size")
limit, err := strconv.Atoi(sizeQueryParam)
if sizeQueryParam == "" || err != nil {

limit, err := common.ExtractPaginationParameterOrSetDefault(r, "limit", 20)
if err != nil {
handler.Logger.Errorw("request err, ListDeploymentHistory", "err", err, "appId", appId, "environmentId", environmentId, "pipelineId", pipelineId, "sizeQueryParam", sizeQueryParam)
common.WriteJsonResp(w, err, "invalid size", http.StatusBadRequest)
return
Expand Down Expand Up @@ -1588,33 +1594,42 @@ func (handler *PipelineConfigRestHandlerImpl) GetPrePostDeploymentLogs(w http.Re
}
token := r.Header.Get("token")
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
appIdStr := vars["appId"]
appId, err := strconv.Atoi(appIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid appId", "err", err, "appId", appId)
common.HandleParameterError(w, r, "appId", appIdStr)
return
}
environmentId, err := strconv.Atoi(vars["environmentId"])
environmentIdStr := vars["environmentId"]
environmentId, err := strconv.Atoi(environmentIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid environmentId", "err", err, "environmentId", environmentId)
common.HandleParameterError(w, r, "environmentId", environmentIdStr)
return
}
pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineIdStr := vars["pipelineId"]
pipelineId, err := strconv.Atoi(pipelineIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid pipelineId", "err", err, "pipelineId", pipelineId)
common.HandleParameterError(w, r, "pipelineId", pipelineIdStr)
return
}

workflowId, err := strconv.Atoi(vars["workflowId"])
workflowRunnerIdStr := vars["workflowRunnerId"]
workflowId, err := strconv.Atoi(workflowRunnerIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid workflowId", "err", err, "workflowId", workflowId)
common.HandleParameterError(w, r, "workflowId", workflowRunnerIdStr)
return
}
followLogs := true
if ok := r.URL.Query().Has("followLogs"); ok {
followLogsStr := r.URL.Query().Get("followLogs")
follow, err := strconv.ParseBool(followLogsStr)
//follow, err := strconv.ParseBool(followLogsStr)
follow, err := common.ExtractBoolQueryParam(r, "followLogs")
if err != nil {
common.WriteJsonResp(w, err, "followLogs is not a valid bool", http.StatusBadRequest)
handler.Logger.Errorw("followLogs is not a valid bool", "err", err, "followLogs", followLogsStr)
common.HandleParameterError(w, r, "followLogs", followLogsStr)
return
}
followLogs = follow
Expand Down Expand Up @@ -1672,24 +1687,36 @@ func (handler *PipelineConfigRestHandlerImpl) FetchCdWorkflowDetails(w http.Resp
}
token := r.Header.Get("token")
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
appIdStr := vars["appId"]
appId, err := strconv.Atoi(appIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid appId", "err", err, "appId", appId)
common.HandleParameterError(w, r, "appId", appIdStr)
return
}
environmentId, err := strconv.Atoi(vars["environmentId"])
environmentIdStr := vars["environmentId"]
environmentId, err := strconv.Atoi(environmentIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid environmentId", "err", err, "environmentId", environmentId)
common.HandleParameterError(w, r, "environmentId", environmentIdStr)
return
}
pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineIdStr := vars["pipelineId"]
pipelineId, err := strconv.Atoi(pipelineIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid pipelineId", "err", err, "pipelineId", pipelineId)
common.HandleParameterError(w, r, "pipelineId", pipelineIdStr)
return
}
buildId, err := strconv.Atoi(vars["workflowRunnerId"])
workflowRunnerIdStr := vars["workflowRunnerId"]
buildId, err := strconv.Atoi(workflowRunnerIdStr)
if err != nil || buildId == 0 {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
if err != nil {
handler.Logger.Errorw("invalid workflowRunnerId", "err", err, "workflowRunnerId", workflowRunnerIdStr)
common.HandleParameterError(w, r, "workflowRunnerId", workflowRunnerIdStr)
return
}
common.HandleValidationErrors(w, r, fmt.Errorf("workflowRunnerId is required should be greater than 0, workflowRunnerId: %s", workflowRunnerIdStr))
return
}
handler.Logger.Infow("request payload, FetchCdWorkflowDetails", "err", err, "appId", appId, "environmentId", environmentId, "pipelineId", pipelineId, "buildId", buildId)
Expand Down Expand Up @@ -1724,19 +1751,25 @@ func (handler *PipelineConfigRestHandlerImpl) DownloadArtifacts(w http.ResponseW
}
token := r.Header.Get("token")
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
appIdStr := vars["appId"]
appId, err := strconv.Atoi(appIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid appId", "err", err, "appId", appId)
common.HandleParameterError(w, r, "appId", appIdStr)
return
}
pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineIdStr := vars["pipelineId"]
pipelineId, err := strconv.Atoi(pipelineIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid pipelineId", "err", err, "pipelineId", pipelineId)
common.HandleParameterError(w, r, "pipelineId", pipelineIdStr)
return
}
buildId, err := strconv.Atoi(vars["workflowRunnerId"])
workflowRunnerIdStr := vars["workflowRunnerId"]
buildId, err := strconv.Atoi(workflowRunnerIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid workflowRunnerId", "err", err, "workflowRunnerId", workflowRunnerIdStr)
common.HandleParameterError(w, r, "workflowRunnerId", workflowRunnerIdStr)
return
}
handler.Logger.Infow("request payload, DownloadArtifacts", "err", err, "appId", appId, "pipelineId", pipelineId, "buildId", buildId)
Expand Down Expand Up @@ -1784,14 +1817,19 @@ func (handler *PipelineConfigRestHandlerImpl) GetStageStatus(w http.ResponseWrit
}
token := r.Header.Get("token")
vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])

appIdStr := vars["appId"]
appId, err := strconv.Atoi(appIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid appId", "err", err, "appId", appId)
common.HandleParameterError(w, r, "appId", appIdStr)
return
}
pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineIdStr := vars["pipelineId"]
pipelineId, err := strconv.Atoi(pipelineIdStr)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Errorw("invalid pipelineId", "err", err, "pipelineId", pipelineId)
common.HandleParameterError(w, r, "pipelineId", pipelineIdStr)
return
}
handler.Logger.Infow("request payload, GetStageStatus", "err", err, "appId", appId, "pipelineId", pipelineId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (handler PipelineTriggerRestHandlerImpl) OverrideConfig(w http.ResponseWrit
err = handler.validator.Struct(overrideRequest)
if err != nil {
handler.logger.Errorw("request err, OverrideConfig", "err", err, "payload", overrideRequest)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
common.HandleValidationErrors(w, r, err)
return
}
token := r.Header.Get("token")
Expand Down Expand Up @@ -178,7 +178,7 @@ func (handler PipelineTriggerRestHandlerImpl) RotatePods(w http.ResponseWriter,
err = handler.validator.Struct(podRotateRequest)
if err != nil {
handler.logger.Errorw("validation err, RotatePods", "err", err, "payload", podRotateRequest)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
common.HandleValidationErrors(w, r, err)
return
}
token := r.Header.Get("token")
Expand Down Expand Up @@ -227,7 +227,7 @@ func (handler PipelineTriggerRestHandlerImpl) StartStopApp(w http.ResponseWriter
err = handler.validator.Struct(overrideRequest)
if err != nil {
handler.logger.Errorw("validation err, StartStopApp", "err", err, "payload", overrideRequest)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
common.HandleValidationErrors(w, r, err)
return
}
token := r.Header.Get("token")
Expand Down
42 changes: 42 additions & 0 deletions api/restHandler/common/ParamParserUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,45 @@ func ExtractBoolQueryParam(r *http.Request, paramName string) (bool, error) {

return boolValue, nil
}

// ExtractIntArrayFromQueryParam returns list of all ids in []int extracted from query param
// use this method over ExtractIntArrayQueryParam if there is list of query params
func ExtractIntArrayFromQueryParam(r *http.Request, paramName string) ([]int, error) {
queryParams := r.URL.Query()
paramValue := queryParams[paramName]
paramIntValues := make([]int, 0)
var err error
if paramValue != nil && len(paramValue) > 0 {
if strings.Contains(paramValue[0], ",") {
paramIntValues, err = convertToIntArray(paramValue[0])
} else {
paramIntValues, err = convertStringArrayToIntArray(paramValue)
}
}

return paramIntValues, err
}

func convertStringArrayToIntArray(strArr []string) ([]int, error) {
var paramValues []int
for _, item := range strArr {
paramIntValue, err := strconv.Atoi(item)
if err != nil {
return paramValues, err
}
paramValues = append(paramValues, paramIntValue)
}
return paramValues, nil
}

func ExtractPaginationParameterOrSetDefault(r *http.Request, paramName string, defaultValue int) (int, error) {
paginationParamString := r.URL.Query().Get(paramName)
if paginationParamString == "" {
return defaultValue, nil
}
paginationParam, err := strconv.Atoi(paginationParamString)
if err != nil {
return 0, err
}
return paginationParam, nil
}
Loading