diff --git a/pkg/app/pipedv1/plugin/analysis/executestage/analysis.go b/pkg/app/pipedv1/plugin/analysis/executestage/analysis.go index de6cb7d1c9..1a5000eb19 100644 --- a/pkg/app/pipedv1/plugin/analysis/executestage/analysis.go +++ b/pkg/app/pipedv1/plugin/analysis/executestage/analysis.go @@ -22,6 +22,7 @@ import ( "maps" "time" + "github.com/creasty/defaults" sdk "github.com/pipe-cd/piped-plugin-sdk-go" "go.uber.org/zap" "golang.org/x/sync/errgroup" @@ -51,9 +52,25 @@ type executor struct { previousElapsedTime time.Duration } +// decodeStageConfig decodes the raw JSON data and validates it. +func decodeStageConfig(data json.RawMessage) (*config.AnalysisStageOptions, error) { + var opts config.AnalysisStageOptions + if err := json.Unmarshal(data, &opts); err != nil { + return nil, fmt.Errorf("failed to unmarshal the stage config: %w", err) + } + if err := defaults.Set(&opts); err != nil { + return nil, fmt.Errorf("failed to set default values for stage config: %w", err) + } + if err := opts.Validate(); err != nil { + return nil, fmt.Errorf("failed to validate the stage config: %w", err) + } + return &opts, nil +} + func ExecuteAnalysisStage(ctx context.Context, input *sdk.ExecuteStageInput[config.AnalysisApplicationSpec], pluginCfg *config.PluginConfig) sdk.StageStatus { - stageCfg := &config.AnalysisStageOptions{} - if err := json.Unmarshal(input.Request.StageConfig, stageCfg); err != nil { + stageCfg, err := decodeStageConfig(input.Request.StageConfig) + if err != nil { + input.Client.LogPersister().Errorf("failed to decode the stage config: %v", err) return sdk.StageStatusFailure } resultStore := analysisresultstore.NewStore(input.Client, input.Logger) diff --git a/pkg/app/pipedv1/plugin/wait/options.go b/pkg/app/pipedv1/plugin/wait/options.go index bb6dd6899d..8e79ca5124 100644 --- a/pkg/app/pipedv1/plugin/wait/options.go +++ b/pkg/app/pipedv1/plugin/wait/options.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" + "github.com/creasty/defaults" "github.com/pipe-cd/piped-plugin-sdk-go/unit" ) @@ -39,6 +40,9 @@ func decode(data json.RawMessage) (WaitStageOptions, error) { if err := json.Unmarshal(data, &opts); err != nil { return WaitStageOptions{}, fmt.Errorf("failed to unmarshal the config: %w", err) } + if err := defaults.Set(&opts); err != nil { + return WaitStageOptions{}, fmt.Errorf("failed to set default values for stage config: %w", err) + } if err := opts.validate(); err != nil { return WaitStageOptions{}, fmt.Errorf("failed to validate the config: %w", err) }