Skip to content

Commit ff63dbb

Browse files
committed
apply default values in stage plugins
Signed-off-by: Rawad Hossain <rawad.hossain00@gmail.com>
1 parent 1c045f7 commit ff63dbb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/app/pipedv1/plugin/analysis/executestage/analysis.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"maps"
2323
"time"
2424

25+
"github.com/creasty/defaults"
2526
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
2627
"go.uber.org/zap"
2728
"golang.org/x/sync/errgroup"
@@ -51,9 +52,25 @@ type executor struct {
5152
previousElapsedTime time.Duration
5253
}
5354

55+
// decodeStageConfig decodes the raw JSON data and validates it.
56+
func decodeStageConfig(data json.RawMessage) (*config.AnalysisStageOptions, error) {
57+
var opts config.AnalysisStageOptions
58+
if err := json.Unmarshal(data, &opts); err != nil {
59+
return nil, fmt.Errorf("failed to unmarshal the stage config: %w", err)
60+
}
61+
if err := defaults.Set(&opts); err != nil {
62+
return nil, fmt.Errorf("failed to set default values for stage config: %w", err)
63+
}
64+
if err := opts.Validate(); err != nil {
65+
return nil, fmt.Errorf("failed to validate the stage config: %w", err)
66+
}
67+
return &opts, nil
68+
}
69+
5470
func ExecuteAnalysisStage(ctx context.Context, input *sdk.ExecuteStageInput[config.AnalysisApplicationSpec], pluginCfg *config.PluginConfig) sdk.StageStatus {
55-
stageCfg := &config.AnalysisStageOptions{}
56-
if err := json.Unmarshal(input.Request.StageConfig, stageCfg); err != nil {
71+
stageCfg, err := decodeStageConfig(input.Request.StageConfig)
72+
if err != nil {
73+
input.Client.LogPersister().Errorf("failed to decode the stage config: %v", err)
5774
return sdk.StageStatusFailure
5875
}
5976
resultStore := analysisresultstore.NewStore(input.Client, input.Logger)

pkg/app/pipedv1/plugin/wait/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020

21+
"github.com/creasty/defaults"
2122
"github.com/pipe-cd/piped-plugin-sdk-go/unit"
2223
)
2324

@@ -39,6 +40,9 @@ func decode(data json.RawMessage) (WaitStageOptions, error) {
3940
if err := json.Unmarshal(data, &opts); err != nil {
4041
return WaitStageOptions{}, fmt.Errorf("failed to unmarshal the config: %w", err)
4142
}
43+
if err := defaults.Set(&opts); err != nil {
44+
return WaitStageOptions{}, fmt.Errorf("failed to set default values for stage config: %w", err)
45+
}
4246
if err := opts.validate(); err != nil {
4347
return WaitStageOptions{}, fmt.Errorf("failed to validate the config: %w", err)
4448
}

0 commit comments

Comments
 (0)