Skip to content

Commit

Permalink
fix: cr
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Dec 29, 2021
1 parent 535e83d commit a35a15c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
28 changes: 10 additions & 18 deletions engine/api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ func (api *API) updateAsCodePipelineHandler() service.Handler {
if err := service.UnmarshalBody(r, &p); err != nil {
return err
}

// check pipeline name pattern
regexp := sdk.NamePatternRegex
if !regexp.MatchString(p.Name) {
return sdk.WrapError(sdk.ErrInvalidPipelinePattern, "updateAsCodePipelineHandler: pipeline name %s do not respect pattern", p.Name)
if err := p.IsValid(); err != nil {
return err
}

tx, err := api.mustDB().Begin()
Expand Down Expand Up @@ -154,13 +151,10 @@ func (api *API) updatePipelineHandler() service.Handler {

var p sdk.Pipeline
if err := service.UnmarshalBody(r, &p); err != nil {
return sdk.WrapError(err, "Cannot read body")
return err
}

// check pipeline name pattern
regexp := sdk.NamePatternRegex
if !regexp.MatchString(p.Name) {
return sdk.WrapError(sdk.ErrInvalidPipelinePattern, "updatePipelineHandler: Pipeline name %s do not respect pattern", p.Name)
if err := p.IsValid(); err != nil {
return err
}

pipelineDB, err := pipeline.LoadPipeline(ctx, api.mustDB(), key, name, true)
Expand All @@ -172,9 +166,9 @@ func (api *API) updatePipelineHandler() service.Handler {
return sdk.WithStack(sdk.ErrForbidden)
}

tx, errB := api.mustDB().Begin()
if errB != nil {
sdk.WrapError(errB, "updatePipelineHandler> Cannot start transaction")
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WrapError(err, "cannot start transaction")
}
defer tx.Rollback() // nolint

Expand Down Expand Up @@ -283,10 +277,8 @@ func (api *API) addPipelineHandler() service.Handler {
if err := service.UnmarshalBody(r, &p); err != nil {
return err
}

// check pipeline name pattern
if regexp := sdk.NamePatternRegex; !regexp.MatchString(p.Name) {
return sdk.NewErrorFrom(sdk.ErrInvalidPipelinePattern, "pipeline name %s do not respect pattern %s", p.Name, sdk.NamePattern)
if err := p.IsValid(); err != nil {
return err
}

// Check that pipeline does not already exists
Expand Down
12 changes: 6 additions & 6 deletions engine/api/pipeline/pipeline_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type ImportOptions struct {
// ParseAndImport parse an exportentities.pipeline and insert or update the pipeline in database
func ParseAndImport(ctx context.Context, db gorp.SqlExecutor, cache cache.Store, proj sdk.Project, epip exportentities.Pipeliner, u sdk.Identifiable, opts ImportOptions) (*sdk.Pipeline, []sdk.Message, error) {
//Transform payload to a sdk.Pipeline
pip, errP := epip.Pipeline()
if errP != nil {
return pip, nil, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, errP), "unable to parse pipeline")
pip, err := epip.Pipeline()
if err != nil {
return nil, nil, err
}

pip.FromRepository = opts.FromRepository
Expand All @@ -34,9 +34,9 @@ func ParseAndImport(ctx context.Context, db gorp.SqlExecutor, cache cache.Store,
}

// Check if pipeline exists
exist, errE := ExistPipeline(db, proj.ID, pip.Name)
if errE != nil {
return pip, nil, sdk.WrapError(errE, "unable to check if pipeline %v exists", pip.Name)
exist, err := ExistPipeline(db, proj.ID, pip.Name)
if err != nil {
return pip, nil, sdk.WrapError(err, "unable to check if pipeline %v exists", pip.Name)
}

done := new(sync.WaitGroup)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/pipeline_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (api *API) putImportPipelineHandler() service.Handler {
pip, allMsg, err := pipeline.ParseAndImport(ctx, tx, api.Cache, *proj, data, getAPIConsumer(ctx), pipeline.ImportOptions{Force: true, PipelineName: pipelineName})
msgListString := translate(allMsg)
if err != nil {
return sdk.NewErrorWithStack(err, sdk.NewErrorFrom(sdk.ErrInvalidPipeline, "unable to parse and import pipeline"))
return sdk.ErrorWithFallback(err, sdk.ErrWrongRequest, "unable to import pipeline")
}

if err := tx.Commit(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sdk/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (w errorWithStack) Format(s fmt.State, verb rune) {
// ErrorWithFallback returns the current error if it's not a ErrUnknownError else it returns a new ErrorWithStack
func ErrorWithFallback(err error, httpError Error, from string, args ...interface{}) error {
if ErrorIs(err, ErrUnknownError) {
return NewErrorWithStack(err, NewError(ErrWrongRequest, fmt.Errorf(from, args...)))
return NewErrorWithStack(err, NewError(httpError, fmt.Errorf(from, args...)))
}
return err
}
Expand Down
4 changes: 4 additions & 0 deletions sdk/exportentities/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func computeJob(name string, j Job) (*sdk.Job, error) {
}
job.Action.Actions = children

if err := job.Action.Requirements.IsValid(); err != nil {
return nil, err
}

return &job, nil
}

Expand Down
8 changes: 8 additions & 0 deletions sdk/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type Pipeline struct {
WorkflowAscodeHolder *Workflow `json:"workflow_ascode_holder,omitempty" cli:"-" db:"-"`
}

func (p Pipeline) IsValid() error {
// check pipeline name pattern
if regexp := NamePatternRegex; !regexp.MatchString(p.Name) {
return NewErrorFrom(ErrInvalidPipelinePattern, "pipeline name %s do not respect pattern %s", p.Name, NamePattern)
}
return nil
}

// PipelineAudit represents pipeline audit
type PipelineAudit struct {
ID int64 `json:"id" db:"id"`
Expand Down

0 comments on commit a35a15c

Please sign in to comment.