From dd1f408bfbaad1537aa823a66a9dafe06c5154c0 Mon Sep 17 00:00:00 2001 From: Gustavo Carvalho Date: Mon, 12 Jan 2026 10:35:36 -0300 Subject: [PATCH] fix: fix configuration --- main.go | 69 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 4030bc8..53eed2e 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "slices" + "strconv" "strings" "time" @@ -28,9 +29,14 @@ type PluginConfig struct { Organization string `mapstructure:"organization"` IncludedRepositories string `mapstructure:"included_repositories"` ExcludedRepositories string `mapstructure:"excluded_repositories"` - DeploymentLookbackDays int `mapstructure:"deployment_lookback_days"` // Number of days to look back for deployments (default: 90) - OnlyActiveDeployments bool `mapstructure:"only_active_deployments"` // Only fetch deployments that are still active (not superseded) (default: false) - IncludeFailedDeployments bool `mapstructure:"include_failed_deployments"` // Include deployments with failure/error states (default: false) + DeploymentLookbackDays string `mapstructure:"deployment_lookback_days"` // Number of days to look back for deployments (default: 90) + OnlyActiveDeployments string `mapstructure:"only_active_deployments"` // Only fetch deployments that are still active (not superseded) (default: false) + IncludeFailedDeployments string `mapstructure:"include_failed_deployments"` // Include deployments with failure/error states (default: false) + + // Parsed values (set during Configure) + deploymentLookbackDays int + onlyActiveDeployments bool + includeFailedDeployments bool } func (c *PluginConfig) Validate() error { @@ -49,6 +55,43 @@ func (c *PluginConfig) Validate() error { return nil } +func (c *PluginConfig) parseDeploymentConfig() error { + // Parse deployment lookback days (default: 90) + if c.DeploymentLookbackDays == "" { + c.deploymentLookbackDays = 90 + } else { + days, err := strconv.Atoi(c.DeploymentLookbackDays) + if err != nil { + return fmt.Errorf("invalid deployment_lookback_days: %w", err) + } + c.deploymentLookbackDays = days + } + + // Parse only active deployments (default: false) + if c.OnlyActiveDeployments == "" { + c.onlyActiveDeployments = false + } else { + active, err := strconv.ParseBool(c.OnlyActiveDeployments) + if err != nil { + return fmt.Errorf("invalid only_active_deployments: %w", err) + } + c.onlyActiveDeployments = active + } + + // Parse include failed deployments (default: false) + if c.IncludeFailedDeployments == "" { + c.includeFailedDeployments = false + } else { + include, err := strconv.ParseBool(c.IncludeFailedDeployments) + if err != nil { + return fmt.Errorf("invalid include_failed_deployments: %w", err) + } + c.includeFailedDeployments = include + } + + return nil +} + type DeploymentWithStatuses struct { Deployment *github.Deployment `json:"deployment"` Statuses []*github.DeploymentStatus `json:"statuses"` @@ -94,16 +137,12 @@ func (l *GithubReposPlugin) Configure(req *proto.ConfigureRequest) (*proto.Confi return nil, err } - // Set default deployment lookback period if not specified - if config.DeploymentLookbackDays == 0 { - config.DeploymentLookbackDays = 90 + // Parse deployment configuration from strings + if err := config.parseDeploymentConfig(); err != nil { + l.Logger.Error("Error parsing deployment config", "error", err) + return nil, err } - // Default to only active deployments (not superseded) - // Note: OnlyActiveDeployments defaults to false (zero value), so we need to check if it was explicitly set - // For now, we'll treat false as "fetch all" and true as "only active" - // IncludeFailedDeployments defaults to false, which means we skip failed deployments by default - l.config = config httpClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{ AccessToken: config.Token, @@ -392,7 +431,7 @@ func (l *GithubReposPlugin) FetchDeploymentsWithStatuses(ctx context.Context, re name := repo.GetName() // Calculate cutoff time based on configured lookback period - cutoffTime := time.Now().AddDate(0, 0, -l.config.DeploymentLookbackDays) + cutoffTime := time.Now().AddDate(0, 0, -l.config.deploymentLookbackDays) opts := &github.DeploymentsListOptions{ ListOptions: github.ListOptions{PerPage: 100, Page: 1}, @@ -440,7 +479,7 @@ func (l *GithubReposPlugin) FetchDeploymentsWithStatuses(ctx context.Context, re opts.Page = resp.NextPage } - l.Logger.Debug("Fetched deployments", "repo", repo.GetFullName(), "count", len(deploymentsWithStatuses), "lookback_days", l.config.DeploymentLookbackDays) + l.Logger.Debug("Fetched deployments", "repo", repo.GetFullName(), "count", len(deploymentsWithStatuses), "lookback_days", l.config.deploymentLookbackDays) return deploymentsWithStatuses, nil } @@ -456,13 +495,13 @@ func (l *GithubReposPlugin) shouldSkipDeployment(deployment *github.Deployment, latestState := latestStatus.GetState() // Filter inactive deployments if OnlyActiveDeployments is enabled - if l.config.OnlyActiveDeployments && latestState == "inactive" { + if l.config.onlyActiveDeployments && latestState == "inactive" { l.Logger.Trace("Skipping inactive deployment", "deployment_id", deployment.GetID(), "state", latestState) return true } // Filter failed/error deployments if IncludeFailedDeployments is false (default) - if !l.config.IncludeFailedDeployments { + if !l.config.includeFailedDeployments { if latestState == "failure" || latestState == "error" { l.Logger.Trace("Skipping failed deployment", "deployment_id", deployment.GetID(), "state", latestState) return true