From ca4f0ee998fee2381df938761ab45388e5c97b38 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 22:30:41 +0530 Subject: [PATCH 01/33] add format section in .golangci-lint and format code --- .golangci.yml | 21 +++++++ backend/bootstrap/main.go | 7 +-- backend/ci_backends/ci_backends.go | 2 +- backend/ci_backends/github_actions.go | 2 +- backend/config/config.go | 3 +- backend/controllers/activity.go | 3 +- backend/controllers/billing.go | 5 +- backend/controllers/cache.go | 4 +- backend/controllers/github.go | 25 ++++---- backend/controllers/github_api.go | 1 - backend/controllers/helpers.go | 3 +- backend/controllers/orgs.go | 6 +- backend/controllers/policies.go | 4 -- backend/controllers/policies_api.go | 1 - backend/controllers/projects.go | 11 +--- backend/controllers/repos.go | 1 - backend/locking/backend_locking.go | 1 + backend/main.go | 1 + backend/middleware/basic.go | 1 - backend/middleware/headers.go | 3 +- backend/middleware/jwt.go | 14 ++--- backend/middleware/noop.go | 5 +- backend/middleware/webhook.go | 3 +- backend/models/orgs.go | 11 ++-- backend/models/runs.go | 4 +- backend/models/scheduler.go | 8 ++- backend/models/setup.go | 7 ++- backend/models/storage.go | 61 +++++++------------ backend/models/storage_dashboard.go | 3 +- backend/models/storage_digger_jobs.go | 7 +-- backend/segment/segment.go | 4 +- backend/service_clients/projects_service.go | 3 +- backend/services/messages.go | 2 +- backend/services/repos.go | 9 +-- backend/services/scheduler.go | 6 +- backend/tasks/runs.go | 2 +- backend/tasks/runs_test.go | 6 +- backend/utils/ai.go | 4 +- backend/utils/allowlist_test.go | 4 +- backend/utils/bitbucket.go | 10 +-- backend/utils/github.go | 15 +++-- backend/utils/github_test.go | 3 +- backend/utils/gitlab.go | 8 +-- backend/utils/graphs.go | 2 +- backend/utils/graphs_test.go | 14 +---- backend/utils/pr_comment.go | 2 +- .../projects_refesh_main.go | 6 +- cli/cmd/digger/default.go | 11 ++-- cli/cmd/digger/main.go | 4 +- cli/cmd/digger/main_test.go | 11 ++-- cli/cmd/digger/root.go | 23 ++++--- cli/cmd/digger/run_spec.go | 3 +- cli/pkg/core/drift/drift.go | 2 +- cli/pkg/digger/digger.go | 23 +++---- cli/pkg/digger/digger_test.go | 35 +++++------ cli/pkg/digger/io.go | 3 +- cli/pkg/drift/Provider.go | 3 +- cli/pkg/drift/slack.go | 2 +- cli/pkg/drift/slack_test.go | 3 +- cli/pkg/github/github.go | 10 +-- cli/pkg/github/models/models.go | 1 + cli/pkg/integration/integration_test.go | 14 ++--- cli/pkg/spec/manual.go | 9 ++- cli/pkg/spec/spec.go | 10 +-- cli/pkg/usage/usage.go | 15 +++-- cli/pkg/usage/usage_test.go | 3 +- cli/pkg/utils/io.go | 3 +- cli_e2e/plan_storage_test.go | 5 +- dgctl/cmd/exec.go | 24 +++----- dgctl/cmd/root.go | 2 +- dgctl/main.go | 3 +- dgctl/utils/gitio.go | 2 +- ee/backend/ci_backends/bitbucket_pipeline.go | 3 +- ee/backend/ci_backends/buildkite.go | 5 +- ee/backend/ci_backends/gitlab_pipeline.go | 5 +- ee/backend/ci_backends/provider.go | 5 +- ee/backend/controllers/artefacts.go | 8 +-- ee/backend/controllers/bitbucket.go | 10 +-- ee/backend/controllers/github.go | 10 +-- ee/backend/controllers/gitlab.go | 24 ++++---- ee/backend/controllers/spec.go | 20 +++--- ee/backend/main.go | 8 +-- ee/backend/providers/github/providers.go | 12 ++-- ee/cli/cmd/digger/default.go | 9 +-- ee/cli/cmd/digger/main.go | 6 +- ee/cli/cmd/digger/main_test.go | 13 ++-- ee/cli/cmd/digger/root.go | 25 ++++---- ee/cli/cmd/digger/run_spec.go | 3 +- ee/cli/pkg/comment_updater/provider.go | 4 +- ee/cli/pkg/comment_updater/updater.go | 9 ++- ee/cli/pkg/drift/github_issue.go | 5 +- ee/cli/pkg/drift/provider.go | 3 +- ee/cli/pkg/github/providers.go | 7 ++- ee/cli/pkg/gitlab/gitlab.go | 9 ++- ee/cli/pkg/policy/policy.go | 14 ++--- ee/cli/pkg/policy/policy_test.go | 3 +- ee/cli/pkg/policy/providers.go | 13 ++-- ee/cli/pkg/vcs/providers.go | 3 +- ee/drift/controllers/ci_jobs.go | 10 +-- ee/drift/controllers/drift.go | 5 +- ee/drift/controllers/notifications.go | 9 +-- ee/drift/main.go | 11 ++-- ee/drift/middleware/job_token.go | 5 +- ee/drift/middleware/middleware.go | 6 +- ee/drift/middleware/webhooks.go | 3 +- ee/drift/services/spec.go | 9 +-- ee/drift/tasks/github.go | 9 +-- ee/drift/utils/github.go | 20 +++--- libs/backendapi/backend.go | 9 +-- libs/backendapi/diggerapi.go | 25 +++----- libs/backendapi/mocks.go | 12 ++-- libs/ci/azure/azure.go | 32 +++++----- libs/ci/azure/azure_test.go | 4 +- libs/ci/bitbucket/bitbucket.go | 31 ++++------ libs/ci/ci.go | 14 ++--- libs/ci/generic/events.go | 11 ++-- libs/ci/github/github.go | 43 +++++++------ libs/ci/github/github_test.go | 5 +- libs/ci/github/mocks.go | 21 ++++--- libs/ci/gitlab/gitlab.go | 26 ++++---- libs/ci/gitlab/gitlab_test.go | 3 +- libs/ci/gitlab/webhooks.go | 2 +- libs/ci/mocks.go | 15 ++--- libs/comment_utils/reporting/core.go | 2 +- libs/comment_utils/reporting/reporting.go | 3 +- .../comment_utils/reporting/reporting_test.go | 19 +++--- libs/comment_utils/summary/provider.go | 1 + libs/comment_utils/summary/updater.go | 10 ++- libs/crypto/decrypt.go | 2 +- libs/digger_config/config.go | 14 +++-- libs/digger_config/converters.go | 2 - libs/digger_config/digger_config.go | 19 +++--- libs/digger_config/digger_config_test.go | 20 +++--- .../terragrunt/atlantis/generate.go | 22 +++---- .../terragrunt/atlantis/parse_hcl.go | 3 +- .../terragrunt/atlantis/parse_locals.go | 8 +-- .../terragrunt/atlantis/parse_tf.go | 7 ++- libs/digger_config/utils.go | 2 +- libs/digger_config/utils_test.go | 5 +- libs/digger_config/yaml.go | 3 +- libs/execution/execution.go | 16 ++--- libs/execution/execution_test.go | 3 +- libs/execution/io_writer_filtered_test.go | 4 +- libs/execution/runners.go | 8 +-- libs/execution/terragrunt.go | 3 +- libs/execution/test_utils.go | 7 +-- libs/git_utils/clone_utils.go | 4 +- libs/iac_utils/iac_utils.go | 7 ++- libs/iac_utils/pulumi.go | 8 +-- libs/iac_utils/terraform.go | 11 ++-- libs/iac_utils/terraform_test.go | 2 - libs/license/license.go | 3 +- libs/license/license_test.go | 3 +- libs/locking/aws/dynamo_locking_test.go | 1 + libs/locking/aws/envprovider/envprovider.go | 4 +- libs/locking/azure/storage_account.go | 4 +- libs/locking/gcp/gcp_lock_test.go | 2 +- libs/locking/locking.go | 4 +- libs/locking/locking_test.go | 3 +- libs/locking/utils.go | 1 + libs/orchestrator/mock.go | 15 ++--- libs/policy/core.go | 12 ++-- libs/policy/mocks.go | 9 ++- libs/policy/policy.go | 38 +++++------- libs/policy/policy_test.go | 33 +++++----- libs/policy/providers.go | 5 +- libs/scheduler/aws.go | 12 ++-- libs/scheduler/aws_test.go | 61 +++++++++---------- libs/scheduler/convert.go | 2 +- libs/scheduler/jobs.go | 7 ++- libs/scheduler/json_models.go | 2 +- libs/scheduler/json_models_test.go | 7 ++- libs/scheduler/utils.go | 12 ++-- libs/spec/payloads.go | 4 +- libs/spec/providers.go | 6 +- libs/spec/variables_provider.go | 3 +- libs/spec/variables_provider_test.go | 7 +-- libs/storage/aws_plan_storage.go | 2 +- libs/storage/aws_plan_storage_test.go | 3 +- libs/storage/azure_plan_storage.go | 12 ++-- libs/storage/gcp_plan_storage.go | 9 ++- libs/storage/io.go | 7 +-- libs/storage/io_test.go | 13 ++-- libs/storage/mocks.go | 11 ++-- libs/storage/plan_storage.go | 22 +++---- libs/storage/storage.go | 8 +-- next/ci_backends/ci_backends.go | 2 +- next/ci_backends/github_actions.go | 5 +- next/ci_backends/provider.go | 3 +- next/controllers/drift.go | 13 ++-- next/controllers/github.go | 38 ++++++------ next/controllers/github_after_merge.go | 11 ++-- next/controllers/projects.go | 12 ++-- next/controllers/runs.go | 14 ++--- next/controllers/static.go | 3 +- next/dbmodels/scheduler.go | 19 +++--- next/dbmodels/setup.go | 7 +-- next/dbmodels/storage.go | 52 ++++++---------- next/main.go | 17 +++--- next/middleware/job_token_auth.go | 7 ++- next/middleware/middleware.go | 6 +- next/middleware/supabase_cookie_auth.go | 7 ++- next/middleware/webhooks.go | 3 +- next/services/config.go | 6 +- next/services/drift.go | 5 +- next/services/runs.go | 5 +- next/services/scheduler.go | 20 +++--- next/services/spec.go | 9 +-- next/supa/supa.go | 3 +- next/utils/crontab.go | 3 +- next/utils/crontab_test.go | 4 +- next/utils/github.go | 27 ++++---- 212 files changed, 968 insertions(+), 1023 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..aedeaa770 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,21 @@ +# See for configurations: https://golangci-lint.run/usage/configuration/ +version: 2 +# See: https://golangci-lint.run/usage/formatters/ +formatters: + default: none + enable: + - gofmt # https://pkg.go.dev/cmd/gofmt + - gofumpt # https://github.com/mvdan/gofumpt + - goimports # https://pkg.go.dev/golang.org/x/tools/cmd/goimports + + settings: + gofmt: + simplify: true # Simplify code: gofmt with `-s` option. + + gofumpt: + # Module path which contains the source code being formatted. + # Default: "" + module-path: github.com/diggerhq/digger # Should match with module in go.mod + # Choose whether to use the extra rules. + # Default: false + extra-rules: true diff --git a/backend/bootstrap/main.go b/backend/bootstrap/main.go index e4203afa9..31af388f7 100644 --- a/backend/bootstrap/main.go +++ b/backend/bootstrap/main.go @@ -11,6 +11,7 @@ import ( "path/filepath" "runtime" "runtime/pprof" + "time" "github.com/diggerhq/digger/backend/config" "github.com/diggerhq/digger/backend/segment" @@ -18,8 +19,6 @@ import ( pprof_gin "github.com/gin-contrib/pprof" sloggin "github.com/samber/slog-gin" - "time" - "github.com/diggerhq/digger/backend/controllers" "github.com/diggerhq/digger/backend/middleware" "github.com/diggerhq/digger/backend/models" @@ -38,7 +37,7 @@ func setupProfiler(r *gin.Engine) { pprof_gin.Register(r) // Create profiles directory if it doesn't exist - if err := os.MkdirAll("/tmp/profiles", 0755); err != nil { + if err := os.MkdirAll("/tmp/profiles", 0o755); err != nil { slog.Error("Failed to create profiles directory", "error", err) panic(err) } @@ -115,7 +114,7 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController slog.Error("Sentry initialization failed", "error", err) } - //database migrations + // database migrations models.ConnectDatabase() r := gin.Default() diff --git a/backend/ci_backends/ci_backends.go b/backend/ci_backends/ci_backends.go index e366951a7..1af25f427 100644 --- a/backend/ci_backends/ci_backends.go +++ b/backend/ci_backends/ci_backends.go @@ -6,7 +6,7 @@ import ( ) type CiBackend interface { - TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error + TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error GetWorkflowUrl(spec spec.Spec) (string, error) } diff --git a/backend/ci_backends/github_actions.go b/backend/ci_backends/github_actions.go index 528d17e97..abc26f298 100644 --- a/backend/ci_backends/github_actions.go +++ b/backend/ci_backends/github_actions.go @@ -16,7 +16,7 @@ type GithubActionCi struct { Client *github.Client } -func (g GithubActionCi) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { +func (g GithubActionCi) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { slog.Info("TriggerGithubWorkflow", "repoOwner", spec.VCS.RepoOwner, "repoName", spec.VCS.RepoName, "commentId", spec.CommentId) client := g.Client specBytes, err := json.Marshal(spec) diff --git a/backend/config/config.go b/backend/config/config.go index 99e9c25f7..709f7a8e8 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -1,11 +1,12 @@ package config import ( - "github.com/spf13/cast" "os" "strings" "time" + "github.com/spf13/cast" + "github.com/spf13/viper" ) diff --git a/backend/controllers/activity.go b/backend/controllers/activity.go index 567c36713..a48ba3e7f 100644 --- a/backend/controllers/activity.go +++ b/backend/controllers/activity.go @@ -3,11 +3,12 @@ package controllers import ( "errors" "fmt" + "net/http" + "github.com/diggerhq/digger/backend/middleware" "github.com/diggerhq/digger/backend/models" "github.com/gin-gonic/gin" "gorm.io/gorm" - "net/http" ) func GetActivity(c *gin.Context) { diff --git a/backend/controllers/billing.go b/backend/controllers/billing.go index 79943e451..261637f07 100644 --- a/backend/controllers/billing.go +++ b/backend/controllers/billing.go @@ -2,12 +2,13 @@ package controllers import ( "errors" + "log/slog" + "net/http" + "github.com/diggerhq/digger/backend/middleware" "github.com/diggerhq/digger/backend/models" "github.com/gin-gonic/gin" "gorm.io/gorm" - "log/slog" - "net/http" ) func BillingStatusApi(c *gin.Context) { diff --git a/backend/controllers/cache.go b/backend/controllers/cache.go index 8f519fd03..a9749ee89 100644 --- a/backend/controllers/cache.go +++ b/backend/controllers/cache.go @@ -2,13 +2,14 @@ package controllers import ( "fmt" - "github.com/diggerhq/digger/libs/git_utils" "log/slog" "net/http" "os" "path" "strings" + "github.com/diggerhq/digger/libs/git_utils" + "github.com/diggerhq/digger/backend/models" "github.com/diggerhq/digger/backend/utils" dg_configuration "github.com/diggerhq/digger/libs/digger_config" @@ -78,7 +79,6 @@ func (d DiggerController) UpdateRepoCache(c *gin.Context) { } return nil }) - if err != nil { slog.Error("Could not load digger config", "error", err) return diff --git a/backend/controllers/github.go b/backend/controllers/github.go index 3fa638900..08e4d9132 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/diggerhq/digger/libs/git_utils" "log/slog" "math/rand" "net/http" @@ -20,6 +19,8 @@ import ( "strings" "time" + "github.com/diggerhq/digger/libs/git_utils" + "github.com/diggerhq/digger/backend/ci_backends" config2 "github.com/diggerhq/digger/backend/config" "github.com/diggerhq/digger/backend/locking" @@ -289,7 +290,7 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) { }) } -func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisation string, ghRepoName string, ghRepoUrl string, installationId int64, appId int64, defaultBranch string, cloneUrl string) (*models.Repo, *models.Organisation, error) { +func createOrGetDiggerRepoForGithubRepo(ghRepoFullName, ghRepoOrganisation, ghRepoName, ghRepoUrl string, installationId, appId int64, defaultBranch, cloneUrl string) (*models.Repo, *models.Organisation, error) { slog.Info("Creating or getting Digger repo for GitHub repo", slog.Group("githubRepo", slog.String("fullName", ghRepoFullName), @@ -416,7 +417,6 @@ func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent, a loadProjectsOnPush := os.Getenv("DIGGER_LOAD_PROJECTS_ON_PUSH") if loadProjectsOnPush == "true" { - if strings.HasSuffix(ref, defaultBranch) { slog.Debug("Loading projects from GitHub repo (push event)", "loadProjectsOnPush", loadProjectsOnPush, "ref", ref, "defaultBranch", defaultBranch) err := services.LoadProjectsFromGithubRepo(gh, strconv.FormatInt(installationId, 10), repoFullName, repoOwner, repoName, cloneURL, defaultBranch) @@ -737,7 +737,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not handle commentId: %v", err)) } - var aiSummaryCommentId = "" + aiSummaryCommentId := "" if config.Reporting.AiSummary { slog.Info("Creating AI summary comment", "prNumber", prNumber) aiSummaryComment, err := ghService.PublishComment(prNumber, "AI Summary will be posted here after completion") @@ -894,7 +894,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR return nil } -func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string, changedFiles []string) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { +func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName, cloneUrl, branch string, changedFiles []string) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { slog.Info("Getting Digger config for branch", slog.Group("repository", slog.String("fullName", repoFullName), @@ -944,7 +944,6 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6 } return nil }) - if err != nil { slog.Error("Error cloning and loading config", "repoFullName", repoFullName, @@ -969,7 +968,7 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6 } // TODO: Refactor this func to receive ghService as input -func getDiggerConfigForPR(gh utils.GithubClientProvider, orgId uint, prLabels []string, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], *string, *string, []string, error) { +func getDiggerConfigForPR(gh utils.GithubClientProvider, orgId uint, prLabels []string, installationId int64, repoFullName, repoOwner, repoName, cloneUrl string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], *string, *string, []string, error) { slog.Info("Getting Digger config for PR", slog.Group("repository", slog.String("fullName", repoFullName), @@ -1130,7 +1129,7 @@ func retrieveConfigFromCache(orgId uint, repoFullName string) (string, *dg_confi return repoCache.DiggerYmlStr, &config, &projectsGraph, nil } -func GetRepoByInstllationId(installationId int64, repoOwner string, repoName string) (*models.Repo, error) { +func GetRepoByInstllationId(installationId int64, repoOwner, repoName string) (*models.Repo, error) { slog.Debug("Getting repo by installation ID", "installationId", installationId, "repoOwner", repoOwner, @@ -1544,7 +1543,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu return fmt.Errorf("comment reporter error: %v", err) } - var aiSummaryCommentId = "" + aiSummaryCommentId := "" if config.Reporting.AiSummary { slog.Info("Creating AI summary comment", "issueNumber", issueNumber) aiSummaryComment, err := ghService.PublishComment(issueNumber, "AI Summary will be posted here after completion") @@ -1725,7 +1724,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu return nil } -func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporterManager utils.CommentReporterManager, config *dg_configuration.DiggerConfig, defaultBranch string, ghService *dg_github.GithubService, repoOwner string, repoName string, commitSha *string, issueNumber int, branch *string) error { +func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporterManager utils.CommentReporterManager, config *dg_configuration.DiggerConfig, defaultBranch string, ghService *dg_github.GithubService, repoOwner, repoName string, commitSha *string, issueNumber int, branch *string) error { if !strings.HasPrefix(*payload.Comment.Body, "digger generate") { return nil } @@ -2009,7 +2008,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte return nil } -func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, repoOwner string, repoName string, batchId *uuid.UUID, prNumber int, prService ci.PullRequestService, gh utils.GithubClientProvider) error { +func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName, repoOwner, repoName string, batchId *uuid.UUID, prNumber int, prService ci.PullRequestService, gh utils.GithubClientProvider) error { slog.Info("Triggering Digger jobs for batch", "batchId", batchId, slog.Group("repository", @@ -2323,7 +2322,7 @@ jobs: func (d DiggerController) GithubAppCallbackPage(c *gin.Context) { installationId := c.Request.URL.Query()["installation_id"][0] - //setupAction := c.Request.URL.Query()["setup_action"][0] + // setupAction := c.Request.URL.Query()["setup_action"][0] code := c.Request.URL.Query()["code"][0] appId := c.Request.URL.Query().Get("state") @@ -2663,7 +2662,7 @@ func (d DiggerController) GithubReposPage(c *gin.Context) { // why this validation is needed: https://roadie.io/blog/avoid-leaking-github-org-data/ // validation based on https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app , step 3 -func validateGithubCallback(githubClientProvider utils.GithubClientProvider, clientId string, clientSecret string, code string, installationId int64) (bool, *github.Installation, error) { +func validateGithubCallback(githubClientProvider utils.GithubClientProvider, clientId, clientSecret, code string, installationId int64) (bool, *github.Installation, error) { slog.Debug("Validating GitHub callback", "clientId", clientId, "installationId", installationId, diff --git a/backend/controllers/github_api.go b/backend/controllers/github_api.go index ea58ea056..8bc29ec44 100644 --- a/backend/controllers/github_api.go +++ b/backend/controllers/github_api.go @@ -49,7 +49,6 @@ func LinkGithubInstallationToOrgApi(c *gin.Context) { } link, err := models.DB.GetGithubAppInstallationLink(installationId) - if err != nil { slog.Error("Could not get installation link", "installationId", installationId, "error", err) c.JSON(http.StatusInternalServerError, gin.H{"status": "Could not get installation link"}) diff --git a/backend/controllers/helpers.go b/backend/controllers/helpers.go index 4d1e7aa38..3c6bcef6e 100644 --- a/backend/controllers/helpers.go +++ b/backend/controllers/helpers.go @@ -1,8 +1,9 @@ package controllers import ( - "github.com/gin-gonic/gin" "net/http" + + "github.com/gin-gonic/gin" ) func Home(c *gin.Context) { diff --git a/backend/controllers/orgs.go b/backend/controllers/orgs.go index cd386e704..3c47335eb 100644 --- a/backend/controllers/orgs.go +++ b/backend/controllers/orgs.go @@ -4,13 +4,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/diggerhq/digger/backend/middleware" - "gorm.io/gorm" "log/slog" "net/http" "os" "strings" + "github.com/diggerhq/digger/backend/middleware" + "gorm.io/gorm" + "github.com/diggerhq/digger/backend/models" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" @@ -180,7 +181,6 @@ func AssociateTenantIdToDiggerOrg(c *gin.Context) { slog.Debug("Processing JWT claims", "name", nameStr, "tenantId", tenantIdStr) org, err := models.DB.GetOrganisation(tenantId) - if err != nil { slog.Error("Failed to get organisation by tenantId", "tenantId", tenantIdStr, "error", err) c.AbortWithStatus(http.StatusInternalServerError) diff --git a/backend/controllers/policies.go b/backend/controllers/policies.go index fed19a455..d3a84778f 100644 --- a/backend/controllers/policies.go +++ b/backend/controllers/policies.go @@ -176,7 +176,6 @@ func upsertPolicyForOrg(c *gin.Context, policyType string) { Type: policyType, Policy: string(policyData), }).Error - if err != nil { slog.Error("Error creating policy", "organisation", organisation, "policyType", policyType, "error", err) c.String(http.StatusInternalServerError, "Error creating policy") @@ -317,7 +316,6 @@ func IssueAccessTokenForOrg(c *gin.Context) { OrganisationID: org.ID, Type: models.AccessPolicyType, }).Error - if err != nil { slog.Error("Error creating token", "orgId", org.ID, "error", err) c.String(http.StatusInternalServerError, "Unexpected error") @@ -329,7 +327,6 @@ func IssueAccessTokenForOrg(c *gin.Context) { } func loadDiggerConfig(configYaml *dg_configuration.DiggerConfigYaml) (*dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { - err := dg_configuration.ValidateDiggerConfigYaml(configYaml, "loaded config") if err != nil { slog.Error("Error validating config", "error", err) @@ -343,7 +340,6 @@ func loadDiggerConfig(configYaml *dg_configuration.DiggerConfigYaml) (*dg_config } err = dg_configuration.ValidateDiggerConfig(config) - if err != nil { slog.Error("Error validating converted config", "error", err) return nil, nil, fmt.Errorf("error validating config: %v", err) diff --git a/backend/controllers/policies_api.go b/backend/controllers/policies_api.go index 19b04f0e6..fce8fb131 100644 --- a/backend/controllers/policies_api.go +++ b/backend/controllers/policies_api.go @@ -98,7 +98,6 @@ func PolicyOrgUpsertApi(c *gin.Context) { Type: policyType, Policy: policyData, }).Error - if err != nil { slog.Error("Error creating policy", "organisationId", organisationId, "orgId", org.ID, "policyType", policyType, "error", err) c.String(http.StatusInternalServerError, "Error creating policy") diff --git a/backend/controllers/projects.go b/backend/controllers/projects.go index 3a68fa324..02765df86 100644 --- a/backend/controllers/projects.go +++ b/backend/controllers/projects.go @@ -49,7 +49,6 @@ func ListProjectsApi(c *gin.Context) { Where("projects.organisation_id = ?", org.ID). Order("name"). Find(&projects).Error - if err != nil { slog.Error("Error fetching projects", "organisationId", organisationId, "orgId", org.ID, "error", err) c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database") @@ -269,7 +268,6 @@ func FindProjectsForOrg(c *gin.Context) { Order("repos.repo_full_name"). Order("name"). Find(&projects).Error - if err != nil { slog.Error("Error fetching projects for organisation", "orgId", org.ID, @@ -428,7 +426,6 @@ func ReportProjectsForRepo(c *gin.Context) { var repo models.Repo err = models.DB.GormDB.Where("name = ? AND organisation_id = ?", repoName, orgId).First(&repo).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Info("Repository not found, creating new one", @@ -443,7 +440,6 @@ func ReportProjectsForRepo(c *gin.Context) { } err = models.DB.GormDB.Create(&repo).Error - if err != nil { slog.Error("Error creating repository", "repoName", repoName, @@ -489,7 +485,6 @@ func ReportProjectsForRepo(c *gin.Context) { } err = models.DB.GormDB.Create(&project).Error - if err != nil { slog.Error("Error creating project", "projectName", request.Name, @@ -559,7 +554,6 @@ func RunHistoryForProject(c *gin.Context) { var repo models.Repo err = models.DB.GormDB.Where("name = ? AND organisation_id = ?", repoName, orgId).First(&repo).Error - if err != nil { slog.Error("Error fetching repository", "repoName", repoName, @@ -573,7 +567,6 @@ func RunHistoryForProject(c *gin.Context) { var project models.Project err = models.DB.GormDB.Where("name = ? AND repo_id = ? AND organisation_id = ?", projectName, repo.ID, org.ID).First(&project).Error - if err != nil { slog.Error("Error fetching project", "projectName", projectName, @@ -587,7 +580,6 @@ func RunHistoryForProject(c *gin.Context) { var runHistory []models.ProjectRun err = models.DB.GormDB.Where("project_id = ?", project.ID).Find(&runHistory).Error - if err != nil { slog.Error("Error fetching run history", "projectId", project.ID, @@ -637,7 +629,6 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { var request SetJobStatusRequest err := c.BindJSON(&request) - if err != nil { slog.Error("Error binding JSON request", "jobId", jobId, "error", err) c.JSON(http.StatusBadRequest, gin.H{"error": "Error binding JSON"}) @@ -1406,7 +1397,7 @@ func CreateTerraformOutputsSummary(gh utils.GithubClientProvider, batch *models. "jobCount", len(jobs), ) - var terraformOutputs = "" + terraformOutputs := "" for _, job := range jobs { var jobSpec orchestrator_scheduler.JobJson err := json.Unmarshal(job.SerializedJobSpec, &jobSpec) diff --git a/backend/controllers/repos.go b/backend/controllers/repos.go index 01be84dfc..9a00eef26 100644 --- a/backend/controllers/repos.go +++ b/backend/controllers/repos.go @@ -36,7 +36,6 @@ func ListReposApi(c *gin.Context) { Where("repos.organisation_id = ?", org.ID). Order("name"). Find(&repos).Error - if err != nil { slog.Error("Error fetching repos", "organisationId", organisationId, "orgId", org.ID, "error", err) c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database") diff --git a/backend/locking/backend_locking.go b/backend/locking/backend_locking.go index a54c8aad5..7139bd3da 100644 --- a/backend/locking/backend_locking.go +++ b/backend/locking/backend_locking.go @@ -3,6 +3,7 @@ package locking import ( "errors" "fmt" + "github.com/diggerhq/digger/backend/models" "gorm.io/gorm" ) diff --git a/backend/main.go b/backend/main.go index 4c3baaf45..e1eda4af5 100644 --- a/backend/main.go +++ b/backend/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "fmt" + "github.com/diggerhq/digger/backend/bootstrap" "github.com/diggerhq/digger/backend/ci_backends" "github.com/diggerhq/digger/backend/config" diff --git a/backend/middleware/basic.go b/backend/middleware/basic.go index a5c69554d..225126bd6 100644 --- a/backend/middleware/basic.go +++ b/backend/middleware/basic.go @@ -12,7 +12,6 @@ import ( ) func HttpBasicWebAuth() gin.HandlerFunc { - return func(c *gin.Context) { slog.Info("Restricting access with HTTP Basic Auth") username := os.Getenv("HTTP_BASIC_AUTH_USERNAME") diff --git a/backend/middleware/headers.go b/backend/middleware/headers.go index 3818d44c0..f77bb0949 100644 --- a/backend/middleware/headers.go +++ b/backend/middleware/headers.go @@ -1,8 +1,9 @@ package middleware import ( - "github.com/gin-gonic/gin" "net/http" + + "github.com/gin-gonic/gin" ) func HeadersApiAuth() gin.HandlerFunc { diff --git a/backend/middleware/jwt.go b/backend/middleware/jwt.go index 62e1b3266..dd6059ef0 100644 --- a/backend/middleware/jwt.go +++ b/backend/middleware/jwt.go @@ -175,7 +175,6 @@ func SecretCodeAuth() gin.HandlerFunc { } return []byte(os.Getenv("WEBHOOK_SECRET")), nil }) - if err != nil { slog.Error("Error parsing webhook secret", "error", err) c.String(http.StatusForbidden, "Invalid x-webhook-secret header provided") @@ -262,7 +261,6 @@ func JWTBearerTokenAuth(auth services.Auth) gin.HandlerFunc { } return publicKey, nil }) - if err != nil { slog.Error("Error while parsing token", "error", err) c.String(http.StatusForbidden, "Authorization header is invalid") @@ -323,8 +321,10 @@ func CORSMiddleware() gin.HandlerFunc { } } -const ORGANISATION_ID_KEY = "organisation_ID" -const ORGANISATION_SOURCE_KEY = "organisation_Source" -const USER_ID_KEY = "user_ID" -const ACCESS_LEVEL_KEY = "access_level" -const JOB_TOKEN_KEY = "job_token" +const ( + ORGANISATION_ID_KEY = "organisation_ID" + ORGANISATION_SOURCE_KEY = "organisation_Source" + USER_ID_KEY = "user_ID" + ACCESS_LEVEL_KEY = "access_level" + JOB_TOKEN_KEY = "job_token" +) diff --git a/backend/middleware/noop.go b/backend/middleware/noop.go index 4142b4519..391113b33 100644 --- a/backend/middleware/noop.go +++ b/backend/middleware/noop.go @@ -1,11 +1,12 @@ package middleware import ( - "github.com/diggerhq/digger/backend/models" - "github.com/gin-gonic/gin" "log/slog" "net/http" "strings" + + "github.com/diggerhq/digger/backend/models" + "github.com/gin-gonic/gin" ) func NoopWebAuth() gin.HandlerFunc { diff --git a/backend/middleware/webhook.go b/backend/middleware/webhook.go index b109f0a76..bdedae092 100644 --- a/backend/middleware/webhook.go +++ b/backend/middleware/webhook.go @@ -1,10 +1,11 @@ package middleware import ( - "github.com/gin-gonic/gin" "net/http" "os" "strings" + + "github.com/gin-gonic/gin" ) func InternalApiAuth() gin.HandlerFunc { diff --git a/backend/models/orgs.go b/backend/models/orgs.go index ad5d9fbb6..94d0a5065 100644 --- a/backend/models/orgs.go +++ b/backend/models/orgs.go @@ -92,9 +92,11 @@ const ( type DriftStatus string -const DriftStatusNewDrift DriftStatus = "new drift" -const DriftStatusNoDrift DriftStatus = "no drift" -const DriftStatusAcknowledgeDrift DriftStatus = "acknowledged drift" +const ( + DriftStatusNewDrift DriftStatus = "new drift" + DriftStatusNoDrift DriftStatus = "no drift" + DriftStatusAcknowledgeDrift DriftStatus = "acknowledged drift" +) type Project struct { gorm.Model @@ -151,11 +153,12 @@ func (p *Project) MapToJsonStruct() interface{} { DriftTerraformPlan: p.DriftTerraformPlan, LastActivityTimestamp: p.UpdatedAt.String(), LastActivityAuthor: "unknown", - //LastActivityStatus: string(status), + // LastActivityStatus: string(status), IsGenerated: p.IsGenerated, IsInMainBranch: p.IsInMainBranch, } } + func (r *Repo) MapToJsonStruct() interface{} { OrganisationName := func() string { if r.Organisation == nil { diff --git a/backend/models/runs.go b/backend/models/runs.go index 0a58de0b9..f20e275bc 100644 --- a/backend/models/runs.go +++ b/backend/models/runs.go @@ -66,7 +66,7 @@ type DiggerRunStage struct { } type SerializedRunStage struct { - //DiggerRunId uint `json:"digger_run_id"` + // DiggerRunId uint `json:"digger_run_id"` DiggerJobId string `json:"digger_job_id"` Status orchestrator_scheduler.DiggerJobStatus `json:"status"` ProjectName string `json:"project_name"` @@ -126,7 +126,7 @@ func (r DiggerRunStage) MapToJsonStruct() (*SerializedRunStage, error) { serialized := &SerializedRunStage{ DiggerJobId: job.DiggerJobID, Status: job.Status, - //ProjectName: r.Run.ProjectName, + // ProjectName: r.Run.ProjectName, WorkflowRunUrl: job.WorkflowRunUrl, ResourcesCreated: job.DiggerJobSummary.ResourcesCreated, ResourcesUpdated: job.DiggerJobSummary.ResourcesUpdated, diff --git a/backend/models/scheduler.go b/backend/models/scheduler.go index a77844fa0..810c72adf 100644 --- a/backend/models/scheduler.go +++ b/backend/models/scheduler.go @@ -19,9 +19,11 @@ type DiggerJobParentLink struct { type DiggerVCSType string -const DiggerVCSGithub DiggerVCSType = "github" -const DiggerVCSGitlab DiggerVCSType = "gitlab" -const DiggerVCSBitbucket DiggerVCSType = "bitbucket" +const ( + DiggerVCSGithub DiggerVCSType = "github" + DiggerVCSGitlab DiggerVCSType = "gitlab" + DiggerVCSBitbucket DiggerVCSType = "bitbucket" +) type DiggerBatch struct { gorm.Model diff --git a/backend/models/setup.go b/backend/models/setup.go index 8a3103d85..53b385071 100644 --- a/backend/models/setup.go +++ b/backend/models/setup.go @@ -1,13 +1,14 @@ package models import ( + "log/slog" + "os" + "time" + sloggorm "github.com/imdatngo/slog-gorm/v2" "gorm.io/driver/postgres" _ "gorm.io/driver/postgres" "gorm.io/gorm" - "log/slog" - "os" - "time" ) type Database struct { diff --git a/backend/models/storage.go b/backend/models/storage.go index 5bdf92e8f..fd2956a3a 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -35,7 +35,6 @@ func (db *Database) GetProjectsFromContext(c *gin.Context, orgIdKey string) ([]P Joins("INNER JOIN repos ON projects.repo_id = repos.id"). Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", loggedInOrganisationId).Find(&projects).Error - if err != nil { slog.Error("error fetching projects from database", "error", err) return nil, false @@ -46,7 +45,6 @@ func (db *Database) GetProjectsFromContext(c *gin.Context, orgIdKey string) ([]P } func (db *Database) GetProjectsRemainingInFreePLan(orgId uint) (uint, uint, uint, error) { - var countOfMonitoredProjects int64 err := db.GormDB.Model(&Project{}).Where("organisation_id = ? AND drift_enabled = ?", orgId, true).Count(&countOfMonitoredProjects).Error if err != nil { @@ -73,7 +71,6 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]Repo err := db.GormDB.Preload("Organisation"). Joins("INNER JOIN organisations ON repos.organisation_id = organisations.id"). Where("repos.organisation_id = ?", loggedInOrganisationId).Find(&repos).Error - if err != nil { slog.Error("error fetching repos from database", "error", err) return nil, false @@ -100,7 +97,6 @@ func (db *Database) GetPoliciesFromContext(c *gin.Context, orgIdKey string) ([]P Joins("LEFT JOIN repos ON projects.repo_id = repos.id"). Joins("LEFT JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", loggedInOrganisationId).Find(&policies).Error - if err != nil { slog.Error("error fetching policies from database", "error", err) return nil, false @@ -120,7 +116,6 @@ func (db *Database) GetProjectRunsForOrg(orgId int) ([]ProjectRun, error) { Joins("INNER JOIN repos ON projects.repo_id = repos.id"). Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", orgId).Order("created_at desc").Limit(100).Find(&runs).Error - if err != nil { slog.Error("error fetching project runs from database", "error", err, "organisationId", orgId) return nil, fmt.Errorf("unknown error occurred while fetching database, %v", err) @@ -166,7 +161,6 @@ func (db *Database) GetProjectByRunId(c *gin.Context, runId uint, orgIdKey strin Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", loggedInOrganisationId). Where("project_runs.id = ?", runId).First(&projectRun).Error - if err != nil { slog.Error("error fetching project run from database", "error", err, @@ -196,7 +190,6 @@ func (db *Database) GetProjectByProjectId(c *gin.Context, projectId uint, orgIdK Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", loggedInOrganisationId). Where("projects.id = ?", projectId).First(&project).Error - if err != nil { slog.Error("error fetching project from database", "error", err, @@ -216,7 +209,6 @@ func (db *Database) GetProject(projectId uint) (*Project, error) { err := db.GormDB.Preload("Organisation"). Where("id = ?", projectId). First(&project).Error - if err != nil { slog.Error("error fetching project from database", "error", err, @@ -229,7 +221,7 @@ func (db *Database) GetProject(projectId uint) (*Project, error) { // GetProjectByName return project for specified org and repo // if record doesn't exist return nil -func (db *Database) GetProjectByName(orgId any, repoFullName string, name string) (*Project, error) { +func (db *Database) GetProjectByName(orgId any, repoFullName, name string) (*Project, error) { slog.Info("getting project by name", slog.Group("project", "orgId", orgId, @@ -243,7 +235,6 @@ func (db *Database) GetProjectByName(orgId any, repoFullName string, name string Where("projects.organisation_id = ?", orgId). Where("repo_full_name = ?", repoFullName). Where("projects.name = ?", name).First(&project).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Debug("project not found", @@ -278,7 +269,6 @@ func (db *Database) GetProjectByRepo(orgId any, repo *Repo) ([]Project, error) { Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", orgId). Where("repos.id = ?", repo.ID).Find(&projects).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Debug("no projects found for repo", @@ -321,7 +311,6 @@ func (db *Database) GetPolicyByPolicyId(c *gin.Context, policyId uint, orgIdKey Joins("INNER JOIN organisations ON projects.organisation_id = organisations.id"). Where("projects.organisation_id = ?", loggedInOrganisationId). Where("policies.id = ?", policyId).First(&policy).Error - if err != nil { slog.Error("error fetching policy from database", "error", err, @@ -346,7 +335,6 @@ func (db *Database) GetDefaultRepo(c *gin.Context, orgIdKey string) (*Repo, bool err := db.GormDB.Preload("Organisation"). Joins("INNER JOIN organisations ON repos.organisation_id = organisations.id"). Where("organisations.id = ?", loggedInOrganisationId).First(&repo).Error - if err != nil { slog.Error("error fetching default repo from database", "error", err, @@ -369,7 +357,6 @@ func (db *Database) GetRepo(orgIdKey any, repoName string) (*Repo, error) { err := db.GormDB.Preload("Organisation"). Joins("INNER JOIN organisations ON repos.organisation_id = organisations.id"). Where("organisations.id = ? AND repos.name=?", orgIdKey, repoName).First(&repo).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Debug("repo not found", @@ -401,7 +388,6 @@ func (db *Database) GetRepoByFullName(orgIdKey any, repoFullName string) (*Repo, err := db.GormDB.Preload("Organisation"). Joins("INNER JOIN organisations ON repos.organisation_id = organisations.id"). Where("organisations.id = ? AND repos.repo_full_name=?", orgIdKey, repoFullName).First(&repo).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Debug("repo not found", @@ -440,13 +426,12 @@ func (db *Database) GetRepoByInstallationIdAndRepoFullName(installationId int64, } // GetRepoById returns digger repo by organisationId and repo name (diggerhq-digger) -func (db *Database) GetRepoById(orgIdKey any, repoId any) (*Repo, error) { +func (db *Database) GetRepoById(orgIdKey, repoId any) (*Repo, error) { var repo Repo err := db.GormDB.Preload("Organisation"). Joins("INNER JOIN organisations ON repos.organisation_id = organisations.id"). Where("organisations.id = ? AND repos.ID=?", orgIdKey, repoId).First(&repo).Error - if err != nil { slog.Error("failed to find digger repo by id", "error", err, "orgId", orgIdKey, "repoId", repoId) return nil, err @@ -455,7 +440,7 @@ func (db *Database) GetRepoById(orgIdKey any, repoId any) (*Repo, error) { } // GithubRepoAdded handles github drift that github repo has been added to the app installation -func (db *Database) GithubRepoAdded(installationId int64, appId int64, login string, accountId int64, repoFullName string) (*GithubAppInstallation, error) { +func (db *Database) GithubRepoAdded(installationId, appId int64, login string, accountId int64, repoFullName string) (*GithubAppInstallation, error) { // check if item exist already item := &GithubAppInstallation{} result := db.GormDB.Where("github_installation_id = ? AND repo=? AND github_app_id=?", installationId, repoFullName, appId).First(item) @@ -486,7 +471,7 @@ func (db *Database) GithubRepoAdded(installationId int64, appId int64, login str return item, nil } -func (db *Database) GithubRepoRemoved(installationId int64, appId int64, repoFullName string) (*GithubAppInstallation, error) { +func (db *Database) GithubRepoRemoved(installationId, appId int64, repoFullName string) (*GithubAppInstallation, error) { item := &GithubAppInstallation{} err := db.GormDB.Where("github_installation_id = ? AND status=? AND github_app_id=? AND repo=?", installationId, GithubAppInstallActive, appId, repoFullName).First(item).Error if err != nil { @@ -575,7 +560,7 @@ func (db *Database) GetGithubAppInstallationLink(installationId int64) (*GithubA return &link, nil } -func (db *Database) CreateVCSConnection(name string, vcsType DiggerVCSType, githubId int64, ClientID string, ClientSecretEncrypted string, WebhookSecretEncrypted string, PrivateKeyEncrypted string, PrivateKeyBase64Encrypted string, Org string, url string, bitbucketAccessTokenEnc string, bitbucketWebhookSecretEnc string, gitlabWebhookSecret string, gitlabAccessToken string, orgId uint) (*VCSConnection, error) { +func (db *Database) CreateVCSConnection(name string, vcsType DiggerVCSType, githubId int64, ClientID, ClientSecretEncrypted, WebhookSecretEncrypted, PrivateKeyEncrypted, PrivateKeyBase64Encrypted, Org, url, bitbucketAccessTokenEnc, bitbucketWebhookSecretEnc, gitlabWebhookSecret, gitlabAccessToken string, orgId uint) (*VCSConnection, error) { app := VCSConnection{ Name: name, VCSType: vcsType, @@ -705,7 +690,7 @@ func (db *Database) MakeGithubAppInstallationLinkInactive(link *GithubAppInstall return link, nil } -func (db *Database) CreateDiggerJobLink(diggerJobId string, repoFullName string) (*GithubDiggerJobLink, error) { +func (db *Database) CreateDiggerJobLink(diggerJobId, repoFullName string) (*GithubDiggerJobLink, error) { link := GithubDiggerJobLink{Status: DiggerJobLinkCreated, DiggerJobId: diggerJobId, RepoFullName: repoFullName} result := db.GormDB.Save(&link) if result.Error != nil { @@ -736,7 +721,7 @@ func (db *Database) GetDiggerJobLink(diggerJobId string) (*GithubDiggerJobLink, return &link, nil } -func (db *Database) UpdateDiggerJobLink(diggerJobId string, repoFullName string, githubJobId int64) (*GithubDiggerJobLink, error) { +func (db *Database) UpdateDiggerJobLink(diggerJobId, repoFullName string, githubJobId int64) (*GithubDiggerJobLink, error) { jobLink := GithubDiggerJobLink{} // check if there is already a link to another org, and throw an error in this case result := db.GormDB.Where("digger_job_id = ? AND repo_full_name=? ", diggerJobId, repoFullName).Find(&jobLink) @@ -788,7 +773,7 @@ func (db *Database) GetDiggerBatch(batchId *uuid.UUID) (*DiggerBatch, error) { return batch, nil } -func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, diggerConfig string, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutputs bool, coverAllImpactedProjects bool, VCSConnectionId *uint) (*DiggerBatch, error) { +func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, PRNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutputs, coverAllImpactedProjects bool, VCSConnectionId *uint) (*DiggerBatch, error) { uid := uuid.New() batch := &DiggerBatch{ ID: uid, @@ -876,8 +861,10 @@ func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte, wor } workflowUrl := "#" - job := &DiggerJob{DiggerJobID: jobId, Status: scheduler.DiggerJobCreated, - BatchID: &batchIdStr, SerializedJobSpec: serializedJob, DiggerJobSummary: *summary, WorkflowRunUrl: &workflowUrl, WorkflowFile: workflowFile} + job := &DiggerJob{ + DiggerJobID: jobId, Status: scheduler.DiggerJobCreated, + BatchID: &batchIdStr, SerializedJobSpec: serializedJob, DiggerJobSummary: *summary, WorkflowRunUrl: &workflowUrl, WorkflowFile: workflowFile, + } result = db.GormDB.Save(job) if result.Error != nil { return nil, result.Error @@ -896,7 +883,6 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ err := db.GormDB.Preload("PlanStage").Preload("ApplyStage"). Where("project_name = ? AND repo_id = ?", projectName, repoId).Order("created_at desc").Find(&runs).Error - if err != nil { slog.Error("error fetching digger runs for project", "error", err, @@ -912,7 +898,7 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ return runs, nil } -func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId uint, ProjectName string, RunType RunType, planStageId *uint, applyStageId *uint) (*DiggerRun, error) { +func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId, DiggerConfig string, GithubInstallationId int64, RepoId uint, ProjectName string, RunType RunType, planStageId, applyStageId *uint) (*DiggerRun, error) { dr := &DiggerRun{ Triggertype: Triggertype, PrNumber: &PrNumber, @@ -993,7 +979,7 @@ func (db *Database) GetDiggerRun(id uint) (*DiggerRun, error) { return dr, nil } -func (db *Database) CreateDiggerRunQueueItem(diggerRunId uint, projectId uint) (*DiggerRunQueueItem, error) { +func (db *Database) CreateDiggerRunQueueItem(diggerRunId, projectId uint) (*DiggerRunQueueItem, error) { drq := &DiggerRunQueueItem{ DiggerRunId: diggerRunId, ProjectId: projectId, @@ -1121,7 +1107,7 @@ WHERE return runqueuesWithData, nil } -func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated uint, resourcesUpdated uint, resourcesDeleted uint) (*DiggerJob, error) { +func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated, resourcesUpdated, resourcesDeleted uint) (*DiggerJob, error) { diggerJob, err := db.GetDiggerJob(diggerJobId) if err != nil { slog.Error("could not get digger job for summary update", @@ -1334,7 +1320,7 @@ func (db *Database) GetDiggerJobParentLinksByParentId(parentId *string) ([]Digge return jobParentLinks, nil } -func (db *Database) CreateDiggerJobParentLink(parentJobId string, jobId string) error { +func (db *Database) CreateDiggerJobParentLink(parentJobId, jobId string) error { jobParentLink := DiggerJobParentLink{ParentDiggerJobId: parentJobId, DiggerJobId: jobId} result := db.GormDB.Create(&jobParentLink) if result.Error != nil { @@ -1388,7 +1374,7 @@ func (db *Database) GetOrganisation(tenantId any) (*Organisation, error) { return org, nil } -func (db *Database) CreateUser(email string, externalSource string, externalId string, orgId uint, username string) (*User, error) { +func (db *Database) CreateUser(email, externalSource, externalId string, orgId uint, username string) (*User, error) { user := &User{ Email: email, ExternalId: externalId, @@ -1413,7 +1399,7 @@ func (db *Database) CreateUser(email string, externalSource string, externalId s return user, nil } -func (db *Database) CreateOrganisation(name string, externalSource string, tenantId string) (*Organisation, error) { +func (db *Database) CreateOrganisation(name, externalSource, tenantId string) (*Organisation, error) { org := &Organisation{Name: name, ExternalSource: externalSource, ExternalId: tenantId} result := db.GormDB.Save(org) if result.Error != nil { @@ -1432,7 +1418,7 @@ func (db *Database) CreateOrganisation(name string, externalSource string, tenan return org, nil } -func (db *Database) CreateProject(name string, directory string, org *Organisation, repoFullName string, isGenerated bool, isInMainBranch bool) (*Project, error) { +func (db *Database) CreateProject(name, directory string, org *Organisation, repoFullName string, isGenerated, isInMainBranch bool) (*Project, error) { project := &Project{ Name: name, Directory: directory, @@ -1477,7 +1463,7 @@ func (db *Database) UpdateProject(project *Project) error { return nil } -func (db *Database) CreateRepo(name string, repoFullName string, repoOrganisation string, repoName string, repoUrl string, org *Organisation, diggerConfig string, installationId int64, githubAppId int64, defaultBranch string, cloneUrl string) (*Repo, error) { +func (db *Database) CreateRepo(name, repoFullName, repoOrganisation, repoName, repoUrl string, org *Organisation, diggerConfig string, installationId, githubAppId int64, defaultBranch, cloneUrl string) (*Repo, error) { var repo Repo // check if repo exist already, do nothing in this case result := db.GormDB.Where("name = ? AND organisation_id=?", name, org.ID).Find(&repo) @@ -1628,7 +1614,7 @@ func (db *Database) GetJobArtefact(jobTokenId uint) (*JobArtefact, error) { return &artefact, nil } -func (db *Database) CreateGithubAppInstallation(installationId int64, githubAppId int64, login string, accountId int, repoFullName string) (*GithubAppInstallation, error) { +func (db *Database) CreateGithubAppInstallation(installationId, githubAppId int64, login string, accountId int, repoFullName string) (*GithubAppInstallation, error) { installation := &GithubAppInstallation{ GithubInstallationId: installationId, GithubAppId: githubAppId, @@ -1695,7 +1681,6 @@ func (db *Database) RefreshProjectsFromRepo(orgId string, config configuration.D } return nil }) - if err != nil { return fmt.Errorf("error while updating projects from config: %v", err) } @@ -1746,7 +1731,7 @@ func (db *Database) GetDiggerLock(resource string) (*DiggerLock, error) { return lock, nil } -func (db *Database) UpsertRepoCache(orgId uint, repoFullName string, diggerYmlStr string, diggerConfig configuration.DiggerConfig) (*RepoCache, error) { +func (db *Database) UpsertRepoCache(orgId uint, repoFullName, diggerYmlStr string, diggerConfig configuration.DiggerConfig) (*RepoCache, error) { var repoCache RepoCache configMarshalled, err := json.Marshal(diggerConfig) @@ -1817,7 +1802,6 @@ func (db *Database) GetRepoCache(orgId uint, repoFullName string) (*RepoCache, e err := db.GormDB. Where("org_id = ? AND repo_full_name = ?", orgId, repoFullName).First(&repoCache).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { slog.Debug("repo cache not found", @@ -1858,5 +1842,4 @@ func (db *Database) GetDiggerBatchesForPR(repoFullName string, prNumber int) ([] "jobCount", len(batches)) return batches, nil - } diff --git a/backend/models/storage_dashboard.go b/backend/models/storage_dashboard.go index 83d66b4b0..3eff17a50 100644 --- a/backend/models/storage_dashboard.go +++ b/backend/models/storage_dashboard.go @@ -1,8 +1,9 @@ package models import ( - "github.com/diggerhq/digger/libs/scheduler" "time" + + "github.com/diggerhq/digger/libs/scheduler" ) func (db *Database) GetRepoCount(orgID uint) (int64, error) { diff --git a/backend/models/storage_digger_jobs.go b/backend/models/storage_digger_jobs.go index 076434273..bfa7bcabe 100644 --- a/backend/models/storage_digger_jobs.go +++ b/backend/models/storage_digger_jobs.go @@ -4,10 +4,11 @@ import ( "encoding/json" "errors" "fmt" + "log" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "gorm.io/gorm" - "log" ) func (db *Database) GetDiggerCiJob(diggerJobId string) (*DiggerJob, error) { @@ -15,7 +16,6 @@ func (db *Database) GetDiggerCiJob(diggerJobId string) (*DiggerJob, error) { var ciJob DiggerJob err := db.GormDB.Preload("Batch").Where("digger_job_id = ?", diggerJobId).First(&ciJob).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fmt.Errorf("ci job not found") @@ -27,8 +27,7 @@ func (db *Database) GetDiggerCiJob(diggerJobId string) (*DiggerJob, error) { return &ciJob, nil } -func (db *Database) CreateCiJobFromSpec(spec spec.Spec, runName string, projectName string, batchId string) (*DiggerJob, error) { - +func (db *Database) CreateCiJobFromSpec(spec spec.Spec, runName, projectName, batchId string) (*DiggerJob, error) { marshalledJobSpec, err := json.Marshal(spec.Job) if err != nil { log.Printf("failed to marshal job: %v", err) diff --git a/backend/segment/segment.go b/backend/segment/segment.go index bf7b85e62..af49b5fb9 100644 --- a/backend/segment/segment.go +++ b/backend/segment/segment.go @@ -28,7 +28,7 @@ func CloseClient() { client.Close() } -func IdentifyClient(userId string, userFullName string, username string, email string, organisationName string, organisationId string, userPlan string) { +func IdentifyClient(userId, userFullName, username, email, organisationName, organisationId, userPlan string) { if client == nil { return } @@ -45,7 +45,7 @@ func IdentifyClient(userId string, userFullName string, username string, email s }) } -func Track(userId string, action string) { +func Track(userId, action string) { if client == nil { return } diff --git a/backend/service_clients/projects_service.go b/backend/service_clients/projects_service.go index cec8c7719..f69f2f628 100644 --- a/backend/service_clients/projects_service.go +++ b/backend/service_clients/projects_service.go @@ -39,8 +39,7 @@ type QueuedResponse struct { Queued string `json:"queued"` } -func TriggerProjectsRefreshService(cloneUrl string, branch string, githubToken string, repoFullName string, orgId string) (*MachineResponse, error) { - +func TriggerProjectsRefreshService(cloneUrl, branch, githubToken, repoFullName, orgId string) (*MachineResponse, error) { slog.Debug("awaiting machine fetch") // Prepare machine configuration diff --git a/backend/services/messages.go b/backend/services/messages.go index 5c3bc2f09..397bb392d 100644 --- a/backend/services/messages.go +++ b/backend/services/messages.go @@ -10,7 +10,7 @@ import ( /// Messages are stored to the session and displayed only once -func addMessage(c *gin.Context, message string, key string) { +func addMessage(c *gin.Context, message, key string) { session := sessions.Default(c) var messages []string sessionMessages := session.Get(key) diff --git a/backend/services/repos.go b/backend/services/repos.go index dce68940f..87bec5b84 100644 --- a/backend/services/repos.go +++ b/backend/services/repos.go @@ -2,15 +2,16 @@ package services import ( "fmt" - "github.com/diggerhq/digger/backend/models" - "github.com/diggerhq/digger/backend/service_clients" - utils3 "github.com/diggerhq/digger/backend/utils" "log/slog" "strconv" "strings" + + "github.com/diggerhq/digger/backend/models" + "github.com/diggerhq/digger/backend/service_clients" + utils3 "github.com/diggerhq/digger/backend/utils" ) -func LoadProjectsFromGithubRepo(gh utils3.GithubClientProvider, installationId string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) error { +func LoadProjectsFromGithubRepo(gh utils3.GithubClientProvider, installationId, repoFullName, repoOwner, repoName, cloneUrl, branch string) error { installationId64, err := strconv.ParseInt(installationId, 10, 64) if err != nil { slog.Error("failed to convert installation id %v to int64", "insallationId", installationId) diff --git a/backend/services/scheduler.go b/backend/services/scheduler.go index 62cc21d4f..11804ee54 100644 --- a/backend/services/scheduler.go +++ b/backend/services/scheduler.go @@ -16,7 +16,7 @@ import ( "github.com/google/uuid" ) -func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *models.DiggerJob, repoFullName string, repoOwner string, repoName string, workflowFileName string, gh utils.GithubClientProvider) error { +func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *models.DiggerJob, repoFullName, repoOwner, repoName, workflowFileName string, gh utils.GithubClientProvider) error { slog.Info("Job completed", "parentJobId", parentJob.DiggerJobID) jobLinksForParent, err := models.DB.GetDiggerJobParentLinksByParentId(&parentJob.DiggerJobID) @@ -69,7 +69,7 @@ func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *mo return nil } -func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob, gh utils.GithubClientProvider) error { +func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname, repoOwner, repoName string, batchId *uuid.UUID, job *models.DiggerJob, gh utils.GithubClientProvider) error { maxConcurrencyForBatch := config.DiggerConfig.GetInt("max_concurrency_per_batch") if maxConcurrencyForBatch == 0 { @@ -118,7 +118,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner return nil } -func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error { +func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, repoFullname, repoOwner, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error { slog.Info("Triggering job", "jobId", job.DiggerJobID, slog.Group("repository", diff --git a/backend/tasks/runs.go b/backend/tasks/runs.go index 1981e9216..9f2fc0956 100644 --- a/backend/tasks/runs.go +++ b/backend/tasks/runs.go @@ -83,7 +83,7 @@ func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.Pull slog.Info("checking plan status", runContext) // Check the status of the batch - batchStatus := orchestrator_scheduler.BatchJobSucceeded //dr.PlanStage.Batch.Status + batchStatus := orchestrator_scheduler.BatchJobSucceeded // dr.PlanStage.Batch.Status approvalRequired := true // if failed then go straight to failed diff --git a/backend/tasks/runs_test.go b/backend/tasks/runs_test.go index 2ec8a88d4..3e710f4b0 100644 --- a/backend/tasks/runs_test.go +++ b/backend/tasks/runs_test.go @@ -16,10 +16,9 @@ import ( "gorm.io/gorm" ) -type MockCiBackend struct { -} +type MockCiBackend struct{} -func (m MockCiBackend) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { +func (m MockCiBackend) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { return nil } @@ -153,5 +152,4 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) { diggerRunRefreshed, _ := models.DB.GetDiggerRun(diggerRun.ID) assert.Equal(t, testParam.NextExpectedStatus, diggerRunRefreshed.Status) } - } diff --git a/backend/utils/ai.go b/backend/utils/ai.go index 17b641d3f..48442066b 100644 --- a/backend/utils/ai.go +++ b/backend/utils/ai.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func GenerateTerraformCode(appCode string, generationEndpoint string, apiToken string) (string, error) { +func GenerateTerraformCode(appCode, generationEndpoint, apiToken string) (string, error) { slog.Debug("Generating Terraform code", "endpoint", generationEndpoint, "codeLength", len(appCode), @@ -89,7 +89,7 @@ func GenerateTerraformCode(appCode string, generationEndpoint string, apiToken s return response.Result, nil } -func GetAiSummaryFromTerraformPlans(plans string, summaryEndpoint string, apiToken string) (string, error) { +func GetAiSummaryFromTerraformPlans(plans, summaryEndpoint, apiToken string) (string, error) { slog.Debug("Generating AI summary for Terraform plans", "endpoint", summaryEndpoint, "plansLength", len(plans), diff --git a/backend/utils/allowlist_test.go b/backend/utils/allowlist_test.go index 00613609e..b61926a92 100644 --- a/backend/utils/allowlist_test.go +++ b/backend/utils/allowlist_test.go @@ -1,9 +1,10 @@ package utils import ( - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestExtractRepoName(t *testing.T) { @@ -29,5 +30,4 @@ func TestRepoAllowList(t *testing.T) { url = "http://gitlab.com/diggerdev/digger-demo.git" allowed = IsInRepoAllowList(url) assert.True(t, allowed) - } diff --git a/backend/utils/bitbucket.go b/backend/utils/bitbucket.go index 7a14ab5ce..b78de133d 100644 --- a/backend/utils/bitbucket.go +++ b/backend/utils/bitbucket.go @@ -2,12 +2,13 @@ package utils import ( "fmt" - "github.com/diggerhq/digger/libs/git_utils" "log/slog" "net/http" "os" "path" + "github.com/diggerhq/digger/libs/git_utils" + orchestrator_bitbucket "github.com/diggerhq/digger/libs/ci/bitbucket" dg_configuration "github.com/diggerhq/digger/libs/digger_config" "github.com/dominikbraun/graph" @@ -25,7 +26,7 @@ func (b BitbucketClientProvider) NewClient(token string) (*bitbucket.Client, err return client, nil } -func GetBitbucketService(bb BitbucketProvider, token string, repoOwner string, repoName string, prNumber int) (*orchestrator_bitbucket.BitbucketAPI, error) { +func GetBitbucketService(bb BitbucketProvider, token, repoOwner, repoName string, prNumber int) (*orchestrator_bitbucket.BitbucketAPI, error) { slog.Debug("Creating Bitbucket service", slog.Group("repository", slog.String("owner", repoOwner), @@ -34,7 +35,7 @@ func GetBitbucketService(bb BitbucketProvider, token string, repoOwner string, r "prNumber", prNumber, ) - //token := os.Getenv("DIGGER_BITBUCKET_ACCESS_TOKEN") + // token := os.Getenv("DIGGER_BITBUCKET_ACCESS_TOKEN") //client, err := bb.NewClient(token) //if err != nil { @@ -54,7 +55,7 @@ func GetBitbucketService(bb BitbucketProvider, token string, repoOwner string, r return &service, nil } -func GetDiggerConfigForBitbucketBranch(bb BitbucketProvider, token string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string, prNumber int) (string, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { +func GetDiggerConfigForBitbucketBranch(bb BitbucketProvider, token, repoFullName, repoOwner, repoName, cloneUrl, branch string, prNumber int) (string, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { slog.Info("Getting Digger config for Bitbucket branch", slog.Group("repository", slog.String("fullName", repoFullName), @@ -118,7 +119,6 @@ func GetDiggerConfigForBitbucketBranch(bb BitbucketProvider, token string, repoF } return nil }) - if err != nil { slog.Error("Error cloning and loading config", "repoFullName", repoFullName, diff --git a/backend/utils/github.go b/backend/utils/github.go index c45f7a568..c45575bf3 100644 --- a/backend/utils/github.go +++ b/backend/utils/github.go @@ -19,8 +19,7 @@ import ( ) // just a wrapper around github client to be able to use mocks -type DiggerGithubRealClientProvider struct { -} +type DiggerGithubRealClientProvider struct{} type DiggerGithubClientMockProvider struct { MockedHTTPClient *net.Client @@ -28,7 +27,7 @@ type DiggerGithubClientMockProvider struct { type GithubClientProvider interface { NewClient(netClient *net.Client) (*github.Client, error) - Get(githubAppId int64, installationId int64) (*github.Client, *string, error) + Get(githubAppId, installationId int64) (*github.Client, *string, error) FetchCredentials(githubAppId string) (string, string, string, string, error) } @@ -37,7 +36,7 @@ func (gh DiggerGithubRealClientProvider) NewClient(netClient *net.Client) (*gith return ghClient, nil } -func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubRealClientProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { slog.Debug("Getting GitHub client", "githubAppId", githubAppId, "installationId", installationId, @@ -110,7 +109,7 @@ func (gh DiggerGithubClientMockProvider) NewClient(netClient *net.Client) (*gith return ghClient, nil } -func (gh DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubClientMockProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { ghClient, _ := gh.NewClient(gh.MockedHTTPClient) token := "token" return ghClient, &token, nil @@ -135,12 +134,12 @@ func GetGithubClient(gh GithubClientProvider, installationId int64, repoFullName return ghClient, token, err } -func GetGithubClientFromAppId(gh GithubClientProvider, installationId int64, githubAppId int64, repoFullName string) (*github.Client, *string, error) { +func GetGithubClientFromAppId(gh GithubClientProvider, installationId, githubAppId int64, repoFullName string) (*github.Client, *string, error) { ghClient, token, err := gh.Get(githubAppId, installationId) return ghClient, token, err } -func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) { +func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName string) (*github2.GithubService, *string, error) { ghClient, token, err := GetGithubClient(gh, installationId, repoFullName) if err != nil { slog.Error("Failed to create GitHub client", @@ -245,7 +244,7 @@ func GetGithubHostname() string { return githubHostname } -func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (int64, string, error) { +func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner, repoName, diggerJobID string) (int64, string, error) { slog.Debug("Looking for workflow for job", "diggerJobId", diggerJobID, slog.Group("repository", diff --git a/backend/utils/github_test.go b/backend/utils/github_test.go index 51c0510c8..a4620d10e 100644 --- a/backend/utils/github_test.go +++ b/backend/utils/github_test.go @@ -1,10 +1,11 @@ package utils import ( - "github.com/diggerhq/digger/libs/git_utils" "os" "testing" + "github.com/diggerhq/digger/libs/git_utils" + "github.com/stretchr/testify/assert" ) diff --git a/backend/utils/gitlab.go b/backend/utils/gitlab.go index 78c9b8aff..cd40ef5f4 100644 --- a/backend/utils/gitlab.go +++ b/backend/utils/gitlab.go @@ -2,11 +2,12 @@ package utils import ( "fmt" - "github.com/diggerhq/digger/libs/git_utils" "log/slog" "os" "path" + "github.com/diggerhq/digger/libs/git_utils" + orchestrator_gitlab "github.com/diggerhq/digger/libs/ci/gitlab" dg_configuration "github.com/diggerhq/digger/libs/digger_config" "github.com/dominikbraun/graph" @@ -32,7 +33,7 @@ func (g GitlabClientProvider) NewClient(token string) (*gitlab.Client, error) { } } -func GetGitlabService(gh GitlabProvider, projectId int, repoName string, repoFullName string, prNumber int, discussionId string) (*orchestrator_gitlab.GitLabService, error) { +func GetGitlabService(gh GitlabProvider, projectId int, repoName, repoFullName string, prNumber int, discussionId string) (*orchestrator_gitlab.GitLabService, error) { slog.Debug("Getting GitLab service", slog.Group("repository", slog.String("name", repoName), @@ -68,7 +69,7 @@ func GetGitlabService(gh GitlabProvider, projectId int, repoName string, repoFul return &service, nil } -func GetDiggerConfigForBranchGitlab(gh GitlabProvider, projectId int, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string, prNumber int, discussionId string) (string, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { +func GetDiggerConfigForBranchGitlab(gh GitlabProvider, projectId int, repoFullName, repoOwner, repoName, cloneUrl, branch string, prNumber int, discussionId string) (string, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { slog.Info("Getting Digger config for GitLab branch", slog.Group("repository", slog.String("fullName", repoFullName), @@ -142,7 +143,6 @@ func GetDiggerConfigForBranchGitlab(gh GitlabProvider, projectId int, repoFullNa } return nil }) - if err != nil { slog.Error("Failed to clone and load config", "projectId", projectId, diff --git a/backend/utils/graphs.go b/backend/utils/graphs.go index 8c2f9991b..a2d0b866a 100644 --- a/backend/utils/graphs.go +++ b/backend/utils/graphs.go @@ -15,7 +15,7 @@ import ( ) // ConvertJobsToDiggerJobs jobs is map with project name as a key and a Job as a value -func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner string, repoName string, repoFullName string, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutput bool, coverAllImpactedProjects bool, VCSConnectionId *uint) (*uuid.UUID, map[string]*models.DiggerJob, error) { +func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner, repoName, repoFullName, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutput, coverAllImpactedProjects bool, VCSConnectionId *uint) (*uuid.UUID, map[string]*models.DiggerJob, error) { slog.Info("Converting jobs to Digger jobs", "jobType", jobType, "vcsType", vcsType, diff --git a/backend/utils/graphs_test.go b/backend/utils/graphs_test.go index df83e7106..a3238a7a8 100644 --- a/backend/utils/graphs_test.go +++ b/backend/utils/graphs_test.go @@ -1,8 +1,9 @@ package utils import ( - configuration "github.com/diggerhq/digger/libs/digger_config" "testing" + + configuration "github.com/diggerhq/digger/libs/digger_config" ) func TestImpactedProjectsOnlyGraph(t *testing.T) { @@ -17,19 +18,16 @@ func TestImpactedProjectsOnlyGraph(t *testing.T) { impactedProjects := map[string]configuration.Project{"p1": p1, "p2": p2, "p3": p3} dg, err := configuration.CreateProjectDependencyGraph(projects) - if err != nil { t.Errorf("Error creating dependency graph: %v", err) } newGraph, err := ImpactedProjectsOnlyGraph(dg, impactedProjects) - if err != nil { t.Errorf("Error creating impacted projects only graph: %v", err) } adjMap, err := newGraph.AdjacencyMap() - if err != nil { t.Errorf("Error getting adjacency map: %v", err) } @@ -63,19 +61,16 @@ func TestImpactedProjectsOnlyGraph2(t *testing.T) { impactedProjects := map[string]configuration.Project{"p1": p1, "p2": p2, "p3": p3, "p5": p5} dg, err := configuration.CreateProjectDependencyGraph(projects) - if err != nil { t.Errorf("Error creating dependency graph: %v", err) } newGraph, err := ImpactedProjectsOnlyGraph(dg, impactedProjects) - if err != nil { t.Errorf("Error creating impacted projects only graph: %v", err) } adjMap, err := newGraph.AdjacencyMap() - if err != nil { t.Errorf("Error getting adjacency map: %v", err) } @@ -113,19 +108,16 @@ func TestImpactedProjectsOnlyGraph3(t *testing.T) { impactedProjects := map[string]configuration.Project{"p1": p1, "p3": p3, "p6": p6} dg, err := configuration.CreateProjectDependencyGraph(projects) - if err != nil { t.Errorf("Error creating dependency graph: %v", err) } newGraph, err := ImpactedProjectsOnlyGraph(dg, impactedProjects) - if err != nil { t.Errorf("Error creating impacted projects only graph: %v", err) } adjMap, err := newGraph.AdjacencyMap() - if err != nil { t.Errorf("Error getting adjacency map: %v", err) } @@ -159,7 +151,6 @@ func TestTraverseGraphVisitAllParentsFirst(t *testing.T) { projects := []configuration.Project{p1, p2, p3, p4, p5, p6, p7, p8} dg, err := configuration.CreateProjectDependencyGraph(projects) - if err != nil { t.Errorf("Error creating dependency graph: %v", err) } @@ -173,7 +164,6 @@ func TestTraverseGraphVisitAllParentsFirst(t *testing.T) { } err = TraverseGraphVisitAllParentsFirst(dg, visit) - if err != nil { t.Errorf("Error traversing graph: %v", err) } diff --git a/backend/utils/pr_comment.go b/backend/utils/pr_comment.go index 563f7e32c..568d2624c 100644 --- a/backend/utils/pr_comment.go +++ b/backend/utils/pr_comment.go @@ -96,7 +96,7 @@ func InitCommentReporter(prService ci.PullRequestService, prNumber int, commentM return nil, fmt.Errorf("count not initialize comment reporter: %v", err) } - //commentId, err := strconv.ParseInt(fmt.Sprintf("%v", comment.Id), 10, 64) + // commentId, err := strconv.ParseInt(fmt.Sprintf("%v", comment.Id), 10, 64) if err != nil { slog.Error("Could not convert comment ID to int64", "commentId", comment.Id, "error", err) return nil, fmt.Errorf("could not convert to int64, %v", err) diff --git a/background/projects-refresh-service/projects_refesh_main.go b/background/projects-refresh-service/projects_refesh_main.go index 0623237c5..e8fdb0c5d 100644 --- a/background/projects-refresh-service/projects_refesh_main.go +++ b/background/projects-refresh-service/projects_refesh_main.go @@ -2,11 +2,12 @@ package main import ( "fmt" + "log/slog" + "os" + "github.com/diggerhq/digger/backend/models" dg_configuration "github.com/diggerhq/digger/libs/digger_config" utils3 "github.com/diggerhq/digger/libs/git_utils" - "log/slog" - "os" ) func init() { @@ -64,5 +65,4 @@ func main() { slog.Error("error while cloning repo: %v", err) os.Exit(1) } - } diff --git a/cli/cmd/digger/default.go b/cli/cmd/digger/default.go index 737029a09..d16f08f8f 100644 --- a/cli/cmd/digger/default.go +++ b/cli/cmd/digger/default.go @@ -3,6 +3,10 @@ package main import ( "encoding/json" "fmt" + "log/slog" + "os" + "runtime/debug" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/drift" "github.com/diggerhq/digger/cli/pkg/github" @@ -13,13 +17,9 @@ import ( "github.com/diggerhq/digger/libs/policy" lib_spec "github.com/diggerhq/digger/libs/spec" "github.com/spf13/cobra" - "log/slog" - "os" - "runtime/debug" ) func initLogger() { - logLevel := os.Getenv("DIGGER_LOG_LEVEL") var level slog.Leveler if logLevel == "DEBUG" { @@ -34,7 +34,6 @@ func initLogger() { })) slog.SetDefault(logger) - } var defaultCmd = &cobra.Command{ @@ -70,7 +69,7 @@ var defaultCmd = &cobra.Command{ usage.ReportErrorAndExit(spec.VCS.Actor, "Successfully ran spec", 0) } - var logLeader = "Unknown CI" + logLeader := "Unknown CI" ci := digger.DetectCI() switch ci { diff --git a/cli/cmd/digger/main.go b/cli/cmd/digger/main.go index 3eeed4bb3..fed76f12f 100644 --- a/cli/cmd/digger/main.go +++ b/cli/cmd/digger/main.go @@ -2,8 +2,9 @@ package main import ( "fmt" - "github.com/diggerhq/digger/cli/pkg/usage" "os" + + "github.com/diggerhq/digger/cli/pkg/usage" ) /* @@ -27,5 +28,4 @@ func main() { if err := rootCmd.Execute(); err != nil { usage.ReportErrorAndExit("", fmt.Sprintf("Error occurred during command exec: %v", err), 8) } - } diff --git a/cli/cmd/digger/main_test.go b/cli/cmd/digger/main_test.go index 6b3e5352f..88be2296c 100644 --- a/cli/cmd/digger/main_test.go +++ b/cli/cmd/digger/main_test.go @@ -1,15 +1,17 @@ package main import ( + "log" + "testing" + "github.com/diggerhq/digger/libs/backendapi" "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/ci/generic" "github.com/diggerhq/digger/libs/comment_utils/reporting" - "github.com/diggerhq/digger/libs/comment_utils/summary" + comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary" "github.com/diggerhq/digger/libs/locking" "github.com/diggerhq/digger/libs/policy" "github.com/diggerhq/digger/libs/storage" - "log" "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/github/models" @@ -19,8 +21,6 @@ import ( "github.com/google/go-github/v61/github" - "testing" - "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -875,7 +875,6 @@ var githubInvalidContextJson = `{ ` func TestGitHubNewPullRequestContext(t *testing.T) { - actionContext, err := models.GetGitHubContext(githubContextNewPullRequestJson) context := actionContext.ToEventPackage() @@ -1020,7 +1019,7 @@ func TestGitHubTestPRCommandCaseInsensitivity(t *testing.T) { var impactedProjects []configuration.Project impactedProjects = make([]configuration.Project, 1) impactedProjects[0] = project - var requestedProject = project + requestedProject := project workflows := make(map[string]configuration.Workflow, 1) workflows["default"] = configuration.Workflow{} jobs, _, err := generic.ConvertIssueCommentEventToJobs("", "", 0, "digger plan", impactedProjects, &requestedProject, workflows, "prbranch", "main") diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index 0936f7e3c..143c06c33 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -2,6 +2,11 @@ package main import ( "fmt" + "log/slog" + "net/http" + "os" + "time" + "github.com/diggerhq/digger/cli/pkg/utils" "github.com/diggerhq/digger/libs/backendapi" "github.com/diggerhq/digger/libs/ci" @@ -11,10 +16,6 @@ import ( locking2 "github.com/diggerhq/digger/libs/locking" core_policy "github.com/diggerhq/digger/libs/policy" "github.com/spf13/cobra" - "log/slog" - "net/http" - "os" - "time" ) type RunConfig struct { @@ -76,10 +77,12 @@ func (r *RunConfig) GetServices() (*ci.PullRequestService, *ci.OrgService, *repo return &prService, &orgService, &reporter, nil } -var PolicyChecker core_policy.Checker -var BackendApi backendapi.Api -var ReportStrategy reporting.ReportStrategy -var lock locking2.Lock +var ( + PolicyChecker core_policy.Checker + BackendApi backendapi.Api + ReportStrategy reporting.ReportStrategy + lock locking2.Lock +) func PreRun(cmd *cobra.Command, args []string) { if cmd.Name() == "run_spec" { @@ -88,9 +91,9 @@ func PreRun(cmd *cobra.Command, args []string) { hostName := os.Getenv("DIGGER_HOSTNAME") token := os.Getenv("DIGGER_TOKEN") - //orgName := os.Getenv("DIGGER_ORGANISATION") + // orgName := os.Getenv("DIGGER_ORGANISATION") BackendApi = backendapi.NewBackendApi(hostName, token) - //PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token) + // PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token) if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" { ReportStrategy = &reporting.CommentPerRunStrategy{ diff --git a/cli/cmd/digger/run_spec.go b/cli/cmd/digger/run_spec.go index f6988da36..6cb674284 100644 --- a/cli/cmd/digger/run_spec.go +++ b/cli/cmd/digger/run_spec.go @@ -3,6 +3,8 @@ package main import ( "encoding/json" "fmt" + "strings" + spec2 "github.com/diggerhq/digger/cli/pkg/spec" "github.com/diggerhq/digger/cli/pkg/usage" comment_summary "github.com/diggerhq/digger/libs/comment_utils/summary" @@ -10,7 +12,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" - "strings" ) var viperRunSpec *viper.Viper diff --git a/cli/pkg/core/drift/drift.go b/cli/pkg/core/drift/drift.go index f33fc78aa..510aa301f 100644 --- a/cli/pkg/core/drift/drift.go +++ b/cli/pkg/core/drift/drift.go @@ -1,5 +1,5 @@ package drift type Notification interface { - Send(projectName string, plan string) error + Send(projectName, plan string) error } diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index 42dc0542b..90368fc64 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -45,7 +45,6 @@ func (ci CIName) String() string { } func DetectCI() CIName { - notEmpty := func(key string) bool { return os.Getenv(key) != "" } @@ -63,10 +62,9 @@ func DetectCI() CIName { return Azure } return None - } -func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgService ci.OrgService, lock locking2.Lock, reporter reporting.Reporter, planStorage storage.PlanStorage, policyChecker policy.Checker, commentUpdater comment_updater.CommentUpdater, backendApi backendapi.Api, jobId string, reportFinalStatusToBackend bool, reportTerraformOutput bool, prCommentId string, workingDir string) (bool, bool, error) { +func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgService ci.OrgService, lock locking2.Lock, reporter reporting.Reporter, planStorage storage.PlanStorage, policyChecker policy.Checker, commentUpdater comment_updater.CommentUpdater, backendApi backendapi.Api, jobId string, reportFinalStatusToBackend, reportTerraformOutput bool, prCommentId, workingDir string) (bool, bool, error) { defer reporter.Flush() slog.Debug("Variable info", "TF_PLUGIN_CACHE_DIR", os.Getenv("TF_PLUGIN_CACHE_DIR")) @@ -85,7 +83,6 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic for _, command := range job.Commands { allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, &prService, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, job.RequestedBy, []string{}) - if err != nil { return false, false, fmt.Errorf("error checking policy: %v", err) } @@ -177,7 +174,7 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic return allAppliesSuccess, atLeastOneApply, nil } -func reportPolicyError(projectName string, command string, requestedBy string, reporter reporting.Reporter) string { +func reportPolicyError(projectName, command, requestedBy string, reporter reporting.Reporter) string { msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command) if reporter.SupportsMarkdown() { _, _, err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) @@ -193,11 +190,10 @@ func reportPolicyError(projectName string, command string, requestedBy string, r return msg } -func run(command string, job orchestrator.Job, policyChecker policy.Checker, orgService ci.OrgService, SCMOrganisation string, SCMrepository string, PRNumber *int, requestedBy string, reporter reporting.Reporter, lock locking2.Lock, prService ci.PullRequestService, projectNamespace string, workingDir string, planStorage storage.PlanStorage, appliesPerProject map[string]bool) (*execution.DiggerExecutorResult, string, error) { +func run(command string, job orchestrator.Job, policyChecker policy.Checker, orgService ci.OrgService, SCMOrganisation, SCMrepository string, PRNumber *int, requestedBy string, reporter reporting.Reporter, lock locking2.Lock, prService ci.PullRequestService, projectNamespace, workingDir string, planStorage storage.PlanStorage, appliesPerProject map[string]bool) (*execution.DiggerExecutorResult, string, error) { slog.Info("Running command for project", "command", command, "project name", job.ProjectName, "project workflow", job.ProjectWorkflow) allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, &prService, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, requestedBy, []string{}) - if err != nil { return nil, "error checking policy", fmt.Errorf("error checking policy: %v", err) } @@ -323,7 +319,6 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org } planReportMessage = planReportMessage + strings.Join(preformattedMessaged, "
") _, _, err = reporter.Report(planReportMessage, planPolicyFormatter) - if err != nil { slog.Error("Failed to report plan.", "error", err) } @@ -426,7 +421,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org applySummary, applyPerformed, output, err := diggerExecutor.Apply() if err != nil { - //TODO reuse executor error handling + // TODO reuse executor error handling slog.Error("Failed to Run digger apply command.", "error", err) err := prService.SetStatus(*job.PullRequestNumber, "failure", job.ProjectName+"/apply") if err != nil { @@ -458,7 +453,6 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org slog.Error("Failed to send usage report.", "error", err) } _, err = diggerExecutor.Destroy() - if err != nil { slog.Error("Failed to Run digger destroy command.", "error", err) msg := fmt.Sprintf("failed to run digger destroy command: %v", err) @@ -520,7 +514,7 @@ func reportApplyMergeabilityError(reporter reporting.Reporter) string { return comment } -func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, plan string) { +func reportTerraformPlanOutput(reporter reporting.Reporter, projectId, plan string) { var formatter func(string) string if reporter.SupportsMarkdown() { @@ -580,7 +574,6 @@ func RunJob( for _, command := range job.Commands { allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, nil, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, nil, requestedBy, []string{}) - if err != nil { return fmt.Errorf("error checking policy: %v", err) } @@ -733,7 +726,7 @@ func RunJob( return nil } -func runDriftDetection(policyChecker policy.Checker, SCMOrganisation string, SCMrepository string, projectName string, requestedBy string, eventName string, diggerExecutor execution.Executor, notification *core_drift.Notification) (string, error) { +func runDriftDetection(policyChecker policy.Checker, SCMOrganisation, SCMrepository, projectName, requestedBy, eventName string, diggerExecutor execution.Executor, notification *core_drift.Notification) (string, error) { err := usage.SendUsageRecord(requestedBy, eventName, "drift-detect") if err != nil { slog.Error("Failed to send usage report.", "error", err) @@ -776,7 +769,7 @@ func runDriftDetection(policyChecker policy.Checker, SCMOrganisation string, SCM func SortedCommandsByDependency(project []orchestrator.Job, dependencyGraph *graph.Graph[string, config.Project]) []orchestrator.Job { var sortedCommands []orchestrator.Job - sortedGraph, err := graph.StableTopologicalSort(*dependencyGraph, func(s string, s2 string) bool { + sortedGraph, err := graph.StableTopologicalSort(*dependencyGraph, func(s, s2 string) bool { return s < s2 }) if err != nil { @@ -805,7 +798,6 @@ func MergePullRequest(ciService ci.PullRequestService, prNumber int, mergeStrate if !isMerged { combinedStatus, err := ciService.GetCombinedPullRequestStatus(prNumber) - if err != nil { log.Fatalf("failed to get combined status, %v", err) } @@ -815,7 +807,6 @@ func MergePullRequest(ciService ci.PullRequestService, prNumber int, mergeStrate } prIsMergeable, err := ciService.IsMergeable(prNumber) - if err != nil { log.Fatalf("failed to check if PR is mergeable, %v", err) } diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index 7b89c2e62..17764af99 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -30,7 +30,7 @@ type MockCommandRunner struct { Commands []RunInfo } -func (m *MockCommandRunner) Run(workDir string, shell string, commands []string, envs map[string]string) (string, string, error) { +func (m *MockCommandRunner) Run(workDir, shell string, commands []string, envs map[string]string) (string, string, error) { m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + strings.Join(commands, " "), time.Now()}) return "", "", nil } @@ -72,7 +72,7 @@ type MockPRManager struct { Commands []RunInfo } -func (m *MockPRManager) GetUserTeams(organisation string, user string) ([]string, error) { +func (m *MockPRManager) GetUserTeams(organisation, user string) ([]string, error) { return []string{}, nil } @@ -101,16 +101,16 @@ func (m *MockPRManager) ListIssues() ([]*ci.Issue, error) { return nil, nil } -func (m *MockPRManager) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (m *MockPRManager) PublishIssue(title, body string, labels *[]string) (int64, error) { m.Commands = append(m.Commands, RunInfo{"PublishComment", body, time.Now()}) return 0, nil } -func (m *MockPRManager) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (m *MockPRManager) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (m *MockPRManager) SetStatus(prNumber int, status string, statusContext string) error { +func (m *MockPRManager) SetStatus(prNumber int, status, statusContext string) error { m.Commands = append(m.Commands, RunInfo{"SetStatus", strconv.Itoa(prNumber) + " " + status + " " + statusContext, time.Now()}) return nil } @@ -150,7 +150,7 @@ func (m *MockPRManager) GetComments(prNumber int) ([]ci.Comment, error) { return []ci.Comment{}, nil } -func (m *MockPRManager) EditComment(prNumber int, id string, comment string) error { +func (m *MockPRManager) EditComment(prNumber int, id, comment string) error { m.Commands = append(m.Commands, RunInfo{"EditComment", id + " " + comment, time.Now()}) return nil } @@ -159,7 +159,7 @@ func (m *MockPRManager) DeleteComment(id string) error { return nil } -func (m *MockPRManager) CreateCommentReaction(id string, reaction string) error { +func (m *MockPRManager) CreateCommentReaction(id, reaction string) error { m.Commands = append(m.Commands, RunInfo{"EditComment", id + " " + reaction, time.Now()}) return nil } @@ -169,10 +169,9 @@ func (m *MockPRManager) GetBranchName(prNumber int) (string, string, error) { return "", "", nil } -func (m *MockPRManager) SetOutput(prNumber int, key string, value string) error { +func (m *MockPRManager) SetOutput(prNumber int, key, value string) error { m.Commands = append(m.Commands, RunInfo{"SetOutput", strconv.Itoa(prNumber), time.Now()}) return nil - } type MockProjectLock struct { @@ -203,7 +202,7 @@ type MockZipper struct { Commands []RunInfo } -func (m *MockZipper) GetFileFromZip(zipFile string, filename string) (string, error) { +func (m *MockZipper) GetFileFromZip(zipFile, filename string) (string, error) { m.Commands = append(m.Commands, RunInfo{"GetFileFromZip", zipFile + " " + filename, time.Now()}) return "plan", nil } @@ -212,22 +211,22 @@ type MockPlanStorage struct { Commands []RunInfo } -func (m *MockPlanStorage) StorePlanFile(fileContents []byte, artifactName string, fileName string) error { +func (m *MockPlanStorage) StorePlanFile(fileContents []byte, artifactName, fileName string) error { m.Commands = append(m.Commands, RunInfo{"StorePlanFile", artifactName, time.Now()}) return nil } -func (m *MockPlanStorage) RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) { +func (m *MockPlanStorage) RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) { m.Commands = append(m.Commands, RunInfo{"RetrievePlan", localPlanFilePath, time.Now()}) return nil, nil } -func (m *MockPlanStorage) DeleteStoredPlan(artifactName string, storedPlanFilePath string) error { +func (m *MockPlanStorage) DeleteStoredPlan(artifactName, storedPlanFilePath string) error { m.Commands = append(m.Commands, RunInfo{"DeleteStoredPlan", storedPlanFilePath, time.Now()}) return nil } -func (m *MockPlanStorage) PlanExists(artifactName string, storedPlanFilePath string) (bool, error) { +func (m *MockPlanStorage) PlanExists(artifactName, storedPlanFilePath string) (bool, error) { m.Commands = append(m.Commands, RunInfo{"PlanExists", storedPlanFilePath, time.Now()}) return false, nil } @@ -252,7 +251,6 @@ func (m MockPlanPathProvider) LocalPlanFilePath() string { } func TestCorrectCommandExecutionWhenApplying(t *testing.T) { - commandRunner := &MockCommandRunner{} terraformExecutor := &MockTerraformExecutor{} prManager := &MockPRManager{} @@ -302,7 +300,6 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) { } func TestCorrectCommandExecutionWhenDestroying(t *testing.T) { - commandRunner := &MockCommandRunner{} terraformExecutor := &MockTerraformExecutor{} prManager := &MockPRManager{} @@ -384,7 +381,7 @@ func TestCorrectCommandExecutionWhenPlanning(t *testing.T) { IacUtils: iac_utils.TerraformUtils{}, } - os.WriteFile(planPathProvider.LocalPlanFilePath(), []byte{123}, 0644) + os.WriteFile(planPathProvider.LocalPlanFilePath(), []byte{123}, 0o644) defer os.Remove(planPathProvider.LocalPlanFilePath()) executor.Plan() @@ -478,11 +475,10 @@ func TestSortedCommandByDependency(t *testing.T) { assert.Equal(t, "project4", sortedCommands[1].ProjectName) assert.Equal(t, "project2", sortedCommands[2].ProjectName) assert.Equal(t, "project1", sortedCommands[3].ProjectName) - } func TestParseWorkspace(t *testing.T) { - var commentTests = []struct { + commentTests := []struct { in string out string err bool @@ -508,5 +504,4 @@ func TestParseWorkspace(t *testing.T) { } } } - } diff --git a/cli/pkg/digger/io.go b/cli/pkg/digger/io.go index 214b4116a..30b0cffc0 100644 --- a/cli/pkg/digger/io.go +++ b/cli/pkg/digger/io.go @@ -2,9 +2,10 @@ package digger import ( "fmt" + "log" + "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/scheduler" - "log" ) func UpdateAggregateStatus(batch *scheduler.SerializedBatch, prService ci.PullRequestService) error { diff --git a/cli/pkg/drift/Provider.go b/cli/pkg/drift/Provider.go index a1949a31a..7345498df 100644 --- a/cli/pkg/drift/Provider.go +++ b/cli/pkg/drift/Provider.go @@ -2,9 +2,10 @@ package drift import ( "fmt" + "os" + core_drift "github.com/diggerhq/digger/cli/pkg/core/drift" "github.com/diggerhq/digger/libs/ci" - "os" ) type DriftNotificationProvider interface { diff --git a/cli/pkg/drift/slack.go b/cli/pkg/drift/slack.go index a344c2777..cfb1e3dd9 100644 --- a/cli/pkg/drift/slack.go +++ b/cli/pkg/drift/slack.go @@ -40,7 +40,7 @@ func SplitCodeBlocks(message string) []string { return res } -func (slack SlackNotification) Send(projectName string, plan string) error { +func (slack SlackNotification) Send(projectName, plan string) error { message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan) httpClient := &http.Client{} type SlackMessage struct { diff --git a/cli/pkg/drift/slack_test.go b/cli/pkg/drift/slack_test.go index 47466bfdf..5656dd6f1 100644 --- a/cli/pkg/drift/slack_test.go +++ b/cli/pkg/drift/slack_test.go @@ -1,10 +1,11 @@ package drift import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestSlackSplitLargerMessage(t *testing.T) { diff --git a/cli/pkg/github/github.go b/cli/pkg/github/github.go index 0ce2ad8e8..96965ca18 100644 --- a/cli/pkg/github/github.go +++ b/cli/pkg/github/github.go @@ -3,6 +3,10 @@ package github import ( "errors" "fmt" + "log/slog" + "os" + "strings" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/drift" github_models "github.com/diggerhq/digger/cli/pkg/github/models" @@ -21,9 +25,6 @@ import ( "github.com/google/go-github/v61/github" "github.com/samber/lo" "gopkg.in/yaml.v3" - "log/slog" - "os" - "strings" ) func initLogger() { @@ -55,7 +56,7 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh hostName := os.Getenv("DIGGER_HOSTNAME") token := os.Getenv("DIGGER_TOKEN") orgName := os.Getenv("DIGGER_ORGANISATION") - var policyChecker, _ = policyCheckerProvider.Get(hostName, token, orgName) + policyChecker, _ := policyCheckerProvider.Get(hostName, token, orgName) ghToken := os.Getenv("GITHUB_TOKEN") if ghToken == "" { @@ -281,7 +282,6 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh jobs, coversAllImpactedProjects, err = dg_github.ConvertGithubPullRequestEventToJobs(&prEvent, impactedProjects, requestedProject, *diggerConfig, true) } else if commentEvent, ok := ghEvent.(github.IssueCommentEvent); ok { prBranchName, _, err := githubPrService.GetBranchName(*commentEvent.Issue.Number) - if err != nil { usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Error while retrieving default branch from Issue: %v", err), 6) } diff --git a/cli/pkg/github/models/models.go b/cli/pkg/github/models/models.go index 4c7d04522..56a744773 100644 --- a/cli/pkg/github/models/models.go +++ b/cli/pkg/github/models/models.go @@ -35,6 +35,7 @@ func (g *GithubAction) ToEventPackage() models.EventPackage { Repository: g.Repository, } } + func (g *GithubAction) UnmarshalJSON(data []byte) error { type Alias GithubAction aux := struct { diff --git a/cli/pkg/integration/integration_test.go b/cli/pkg/integration/integration_test.go index 9c45bf4a2..405249adf 100644 --- a/cli/pkg/integration/integration_test.go +++ b/cli/pkg/integration/integration_test.go @@ -2,18 +2,19 @@ package integration import ( "context" - "github.com/diggerhq/digger/libs/ci/generic" - "github.com/diggerhq/digger/libs/execution" - "github.com/diggerhq/digger/libs/locking" - "github.com/diggerhq/digger/libs/locking/aws" - "github.com/diggerhq/digger/libs/storage" "log" "math/rand" "os" "testing" "time" - "github.com/diggerhq/digger/libs/comment_utils/summary" + "github.com/diggerhq/digger/libs/ci/generic" + "github.com/diggerhq/digger/libs/execution" + "github.com/diggerhq/digger/libs/locking" + "github.com/diggerhq/digger/libs/locking/aws" + "github.com/diggerhq/digger/libs/storage" + + comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary" "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/github/models" @@ -690,7 +691,6 @@ func TestUnLock(t *testing.T) { } func TestNonExistentGitHubEvent(t *testing.T) { - unknownEventContext := githubContextUnknownEventJson _, err := models.GetGitHubContext(unknownEventContext) println(err.Error()) diff --git a/cli/pkg/spec/manual.go b/cli/pkg/spec/manual.go index 49f20ec5c..ec92cdb33 100644 --- a/cli/pkg/spec/manual.go +++ b/cli/pkg/spec/manual.go @@ -2,6 +2,10 @@ package spec import ( "fmt" + "log/slog" + "os" + "os/exec" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/usage" "github.com/diggerhq/digger/cli/pkg/utils" @@ -12,9 +16,6 @@ import ( "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "github.com/diggerhq/digger/libs/storage" - "log/slog" - "os" - "os/exec" ) func RunSpecManualCommand( @@ -28,7 +29,6 @@ func RunSpecManualCommand( PlanStorageProvider spec.PlanStorageProvider, commentUpdaterProvider comment_summary.CommentUpdaterProvider, ) error { - job, err := jobProvider.GetJob(spec.Job) if err != nil { usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get job: %v", err), 1) @@ -37,7 +37,6 @@ func RunSpecManualCommand( lock, err := lockProvider.GetLock(spec.Lock) if err != nil { usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get job: %v", err), 1) - } //prService, err := vcsProvider.GetPrService(spec.VCS) diff --git a/cli/pkg/spec/spec.go b/cli/pkg/spec/spec.go index 822d1a579..b31e4f756 100644 --- a/cli/pkg/spec/spec.go +++ b/cli/pkg/spec/spec.go @@ -2,6 +2,11 @@ package spec import ( "fmt" + "log/slog" + "os" + "os/exec" + "time" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/usage" backend2 "github.com/diggerhq/digger/libs/backendapi" @@ -9,10 +14,6 @@ import ( "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "github.com/samber/lo" - "log/slog" - "os" - "os/exec" - "time" ) func reportError(spec spec.Spec, backendApi backend2.Api, message string, err error) { @@ -36,7 +37,6 @@ func RunSpec( variablesProvider spec.VariablesProvider, commentUpdaterProvider comment_summary.CommentUpdaterProvider, ) error { - backendApi, err := backedProvider.GetBackendApi(spec.Backend) if err != nil { slog.Error("could not get backend api", "error", err) diff --git a/cli/pkg/usage/usage.go b/cli/pkg/usage/usage.go index 7325f4315..c2ed2708a 100644 --- a/cli/pkg/usage/usage.go +++ b/cli/pkg/usage/usage.go @@ -5,14 +5,17 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" - configuration "github.com/diggerhq/digger/libs/digger_config" "log/slog" "net/http" "os" + + configuration "github.com/diggerhq/digger/libs/digger_config" ) -var telemetry = true -var source = "unknown" +var ( + telemetry = true + source = "unknown" +) type UsageRecord struct { UserId interface{} `json:"userid"` @@ -21,7 +24,7 @@ type UsageRecord struct { Token string `json:"token"` } -func SendUsageRecord(repoOwner string, eventName string, action string) error { +func SendUsageRecord(repoOwner, eventName, action string) error { h := sha256.New() h.Write([]byte(repoOwner)) sha := h.Sum(nil) @@ -35,7 +38,7 @@ func SendUsageRecord(repoOwner string, eventName string, action string) error { return sendPayload(payload) } -func SendLogRecord(repoOwner string, message string) error { +func SendLogRecord(repoOwner, message string) error { h := sha256.New() h.Write([]byte(repoOwner)) sha := h.Sum(nil) @@ -112,7 +115,7 @@ func init() { } } -func ReportErrorAndExit(repoOwner string, message string, exitCode int) { +func ReportErrorAndExit(repoOwner, message string, exitCode int) { if exitCode == 0 { slog.Info(message) } else { diff --git a/cli/pkg/usage/usage_test.go b/cli/pkg/usage/usage_test.go index 5e7555a01..8c086a0b1 100644 --- a/cli/pkg/usage/usage_test.go +++ b/cli/pkg/usage/usage_test.go @@ -1,9 +1,10 @@ package usage import ( - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/assert" ) // util function for testing of send usage record diff --git a/cli/pkg/utils/io.go b/cli/pkg/utils/io.go index 70d764f2a..5a65bdd84 100644 --- a/cli/pkg/utils/io.go +++ b/cli/pkg/utils/io.go @@ -8,8 +8,7 @@ import ( "path/filepath" ) -func ExtractZip(zipFilePath string, outDir string) error { - +func ExtractZip(zipFilePath, outDir string) error { // Open the zip file zipReader, err := zip.OpenReader(zipFilePath) if err != nil { diff --git a/cli_e2e/plan_storage_test.go b/cli_e2e/plan_storage_test.go index f8601912e..f2e1dfbb9 100644 --- a/cli_e2e/plan_storage_test.go +++ b/cli_e2e/plan_storage_test.go @@ -2,13 +2,14 @@ package cli_e2e import ( "fmt" - "github.com/diggerhq/digger/libs/locking/gcp" - storage2 "github.com/diggerhq/digger/libs/storage" "log" "os" "strings" "testing" + "github.com/diggerhq/digger/libs/locking/gcp" + storage2 "github.com/diggerhq/digger/libs/storage" + "github.com/stretchr/testify/assert" ) diff --git a/dgctl/cmd/exec.go b/dgctl/cmd/exec.go index e2259c63e..9540a0230 100644 --- a/dgctl/cmd/exec.go +++ b/dgctl/cmd/exec.go @@ -8,13 +8,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/diggerhq/digger/dgctl/utils" - "github.com/diggerhq/digger/libs/backendapi" - orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" - "github.com/diggerhq/digger/libs/spec" - "github.com/google/go-github/v61/github" - "github.com/spf13/pflag" - "github.com/spf13/viper" "io" "log" "net/http" @@ -25,6 +18,14 @@ import ( "strings" "time" + "github.com/diggerhq/digger/dgctl/utils" + "github.com/diggerhq/digger/libs/backendapi" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" + "github.com/diggerhq/digger/libs/spec" + "github.com/google/go-github/v61/github" + "github.com/spf13/pflag" + "github.com/spf13/viper" + "github.com/diggerhq/digger/libs/digger_config" "github.com/spf13/cobra" ) @@ -82,7 +83,7 @@ func GetUrlContents(url string) (string, error) { return content, nil } -func GetSpec(diggerUrl string, authToken string, command string, actor string, projectMarshalled string, diggerConfigMarshalled string, repoFullName string) ([]byte, error) { +func GetSpec(diggerUrl, authToken, command, actor, projectMarshalled, diggerConfigMarshalled, repoFullName string) ([]byte, error) { payload := spec.GetSpecPayload{ Command: command, RepoFullName: repoFullName, @@ -104,7 +105,6 @@ func GetSpec(diggerUrl string, authToken string, command string, actor string, p } req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) - if err != nil { return nil, fmt.Errorf("error while creating request: %v", err) } @@ -114,7 +114,6 @@ func GetSpec(diggerUrl string, authToken string, command string, actor string, p client := http.DefaultClient resp, err := client.Do(req) - if err != nil { return nil, fmt.Errorf("error while sending request: %v", err) } @@ -131,7 +130,7 @@ func GetSpec(diggerUrl string, authToken string, command string, actor string, p return body, nil } -func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (*int64, *int64, *string, error) { +func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner, repoName, diggerJobID string) (*int64, *int64, *string, error) { timeFilter := time.Now().Add(-5 * time.Minute) runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), repoOwner, repoName, &github.ListWorkflowRunsOptions{ Created: ">=" + timeFilter.Format(time.RFC3339), @@ -160,7 +159,6 @@ func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, } func cleanupDiggerOutput(output string) string { - startingDelimiter := "<========= DIGGER RUNNING IN MANUAL MODE =========>" endingDelimiter := "<========= DIGGER COMPLETED =========>" @@ -332,7 +330,6 @@ var execCmd = &cobra.Command{ log.Printf("streaming logs from remote job:") logsContent, err := GetUrlContents(logs.String()) - if err != nil { log.Printf("error while fetching logs: %v", err) os.Exit(1) @@ -360,5 +357,4 @@ func init() { } rootCmd.AddCommand(execCmd) - } diff --git a/dgctl/cmd/root.go b/dgctl/cmd/root.go index d0f76d928..a9c68e029 100644 --- a/dgctl/cmd/root.go +++ b/dgctl/cmd/root.go @@ -53,5 +53,5 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. - //rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/dgctl/main.go b/dgctl/main.go index c7dc1d1a9..d3c648f23 100644 --- a/dgctl/main.go +++ b/dgctl/main.go @@ -16,9 +16,10 @@ limitations under the License. package main import ( - "github.com/diggerhq/digger/dgctl/cmd" "log" "os" + + "github.com/diggerhq/digger/dgctl/cmd" ) func init() { diff --git a/dgctl/utils/gitio.go b/dgctl/utils/gitio.go index c4f10b150..9efae8c02 100644 --- a/dgctl/utils/gitio.go +++ b/dgctl/utils/gitio.go @@ -17,7 +17,7 @@ func ArchiveGitRepo(sourcePath string) (string, error) { return "", fmt.Errorf("failed to create temp dir: %v", err) } // Create the temp directory - if err := os.MkdirAll(tempDir, 0755); err != nil { + if err := os.MkdirAll(tempDir, 0o755); err != nil { return "", fmt.Errorf("failed to create temp directory: %w", err) } diff --git a/ee/backend/ci_backends/bitbucket_pipeline.go b/ee/backend/ci_backends/bitbucket_pipeline.go index 83930b152..1a8679825 100644 --- a/ee/backend/ci_backends/bitbucket_pipeline.go +++ b/ee/backend/ci_backends/bitbucket_pipeline.go @@ -3,6 +3,7 @@ package ci_backends import ( "encoding/json" "fmt" + orchestrator_bitbucket "github.com/diggerhq/digger/libs/ci/bitbucket" "github.com/diggerhq/digger/libs/spec" ) @@ -14,7 +15,7 @@ type BitbucketPipelineCI struct { Branch string } -func (bbp BitbucketPipelineCI) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { +func (bbp BitbucketPipelineCI) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { specBytes, err := json.Marshal(spec) if err != nil { return fmt.Errorf("could not serialize spec: %v", err) diff --git a/ee/backend/ci_backends/buildkite.go b/ee/backend/ci_backends/buildkite.go index bda487327..0febcf587 100644 --- a/ee/backend/ci_backends/buildkite.go +++ b/ee/backend/ci_backends/buildkite.go @@ -2,6 +2,7 @@ package ci_backends import ( "encoding/json" + "github.com/buildkite/go-buildkite/v3/buildkite" "github.com/diggerhq/digger/libs/spec" ) @@ -12,8 +13,7 @@ type BuildkiteCi struct { Pipeline string } -func (b BuildkiteCi) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { - +func (b BuildkiteCi) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { specBytes, err := json.Marshal(spec) client := b.Client _, _, err = client.Builds.Create(b.Org, b.Pipeline, &buildkite.CreateBuild{ @@ -29,7 +29,6 @@ func (b BuildkiteCi) TriggerWorkflow(spec spec.Spec, runName string, vcsToken st }) return err - } func (b BuildkiteCi) GetWorkflowUrl(spec spec.Spec) (string, error) { diff --git a/ee/backend/ci_backends/gitlab_pipeline.go b/ee/backend/ci_backends/gitlab_pipeline.go index 05ca09ad1..78b217626 100644 --- a/ee/backend/ci_backends/gitlab_pipeline.go +++ b/ee/backend/ci_backends/gitlab_pipeline.go @@ -3,9 +3,10 @@ package ci_backends import ( "encoding/json" "fmt" + "strconv" + "github.com/diggerhq/digger/libs/spec" "github.com/xanzy/go-gitlab" - "strconv" ) type GitlabPipelineCI struct { @@ -23,7 +24,7 @@ type GitlabPipelineCI struct { GitlabDiscussionId string } -func (gl GitlabPipelineCI) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { +func (gl GitlabPipelineCI) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { specBytes, err := json.Marshal(spec) if err != nil { return fmt.Errorf("could not serialize spec: %v", err) diff --git a/ee/backend/ci_backends/provider.go b/ee/backend/ci_backends/provider.go index abac53968..c1ff6e1ce 100644 --- a/ee/backend/ci_backends/provider.go +++ b/ee/backend/ci_backends/provider.go @@ -2,11 +2,12 @@ package ci_backends import ( "fmt" + "log" + "os" + "github.com/buildkite/go-buildkite/v3/buildkite" "github.com/diggerhq/digger/backend/ci_backends" "github.com/diggerhq/digger/backend/utils" - "log" - "os" ) type EEBackendProvider struct{} diff --git a/ee/backend/controllers/artefacts.go b/ee/backend/controllers/artefacts.go index f4d359d9a..b7054c4f9 100644 --- a/ee/backend/controllers/artefacts.go +++ b/ee/backend/controllers/artefacts.go @@ -2,13 +2,14 @@ package controllers import ( "fmt" + "io" + "log" + "net/http" + "github.com/diggerhq/digger/backend/middleware" "github.com/diggerhq/digger/backend/models" "github.com/gin-gonic/gin" "github.com/google/uuid" - "io" - "log" - "net/http" ) func SetJobArtefact(c *gin.Context) { @@ -70,7 +71,6 @@ func SetJobArtefact(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{"message": "File uploaded successfully", "id": artefactRecord.ID}) - } func DownloadJobArtefact(c *gin.Context) { diff --git a/ee/backend/controllers/bitbucket.go b/ee/backend/controllers/bitbucket.go index 6c312f39d..4603644de 100644 --- a/ee/backend/controllers/bitbucket.go +++ b/ee/backend/controllers/bitbucket.go @@ -100,9 +100,9 @@ func (ee DiggerEEController) BitbucketWebhookHandler(c *gin.Context) { return } - var pullRequestCommentCreated = BitbucketCommentCreatedEvent{} - var repoPush = BitbucketPushEvent{} - var pullRequestCreated = BitbucketPullRequestCreatedEvent{} + pullRequestCommentCreated := BitbucketCommentCreatedEvent{} + repoPush := BitbucketPushEvent{} + pullRequestCreated := BitbucketPullRequestCreatedEvent{} bodyBytes, err := io.ReadAll(c.Request.Body) if err != nil { @@ -162,10 +162,10 @@ func handleIssueCommentEventBB(bitbucketProvider utils.BitbucketProvider, payloa commentBody := payload.Comment.Content.Raw branch := payload.PullRequest.Source.Branch.Name // TODO: figure why git fetch fails in bb pipeline - commitSha := "" //payload.PullRequest.Source.Commit.Hash + commitSha := "" // payload.PullRequest.Source.Commit.Hash defaultBranch := payload.PullRequest.Source.Branch.Name actor := payload.Actor.Nickname - //discussionId := payload.Comment.ID + // discussionId := payload.Comment.ID if !strings.HasPrefix(commentBody, "digger") { log.Printf("comment is not a Digger command, ignoring") diff --git a/ee/backend/controllers/github.go b/ee/backend/controllers/github.go index 1fbf7fd22..0362b6a1f 100644 --- a/ee/backend/controllers/github.go +++ b/ee/backend/controllers/github.go @@ -5,15 +5,16 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/diggerhq/digger/backend/models" - "github.com/diggerhq/digger/backend/utils" - "github.com/diggerhq/digger/ee/drift/middleware" - "github.com/gin-gonic/gin" "log" "math/rand" "net/http" "net/url" "os" + + "github.com/diggerhq/digger/backend/models" + "github.com/diggerhq/digger/backend/utils" + "github.com/diggerhq/digger/ee/drift/middleware" + "github.com/gin-gonic/gin" ) func GithubAppConnections(c *gin.Context) { @@ -148,7 +149,6 @@ func (d DiggerEEController) GithubAppConnectionsConfirm(c *gin.Context) { PemBase64 := base64.StdEncoding.EncodeToString([]byte(PEM)) encrypt := func(val string) (string, error) { - secret := os.Getenv("DIGGER_ENCRYPTION_SECRET") if secret == "" { log.Printf("ERROR: no encryption secret specified, please specify DIGGER_ENCRYPTION_SECRET as 32 bytes base64 string") diff --git a/ee/backend/controllers/gitlab.go b/ee/backend/controllers/gitlab.go index 8cd099a95..e15bbdfe7 100644 --- a/ee/backend/controllers/gitlab.go +++ b/ee/backend/controllers/gitlab.go @@ -39,7 +39,7 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) { c.Header("Content-Type", "application/json") log.Printf("GitlabWebhook") - //temp to get orgID TODO: fetch from db + // temp to get orgID TODO: fetch from db organisation, err := models.DB.GetOrganisation(models.DEFAULT_ORG_NAME) if err != nil { c.String(http.StatusInternalServerError, "Failed to get default organisation") @@ -103,7 +103,7 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) { } func GetGitlabRepoUrl(event interface{}) string { - var repoUrl = "" + repoUrl := "" switch event := event.(type) { case *gitlab.MergeCommentEvent: repoUrl = event.Project.GitHTTPURL @@ -127,10 +127,10 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab branch := payload.ObjectAttributes.SourceBranch commitSha := payload.ObjectAttributes.LastCommit.ID action := payload.ObjectAttributes.Action - //defaultBranch := payload.Repository.DefaultBranch - //actor := payload.User.Username - //discussionId := "" - //action := payload.ObjectAttributes.Action + // defaultBranch := payload.Repository.DefaultBranch + // actor := payload.User.Username + // discussionId := "" + // action := payload.ObjectAttributes.Action // temp hack: we initialize glService to publish an initial comment and then use that as a discussionId onwards ( glService, glerr := utils.GetGitlabService(gitlabProvider, projectId, repoName, repoFullName, prNumber, "") @@ -308,10 +308,10 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab GitlabCIMergeRequestIID: payload.ObjectAttributes.IID, GitlabciprojectId: payload.Project.ID, GitlabciprojectNamespace: payload.Project.Namespace, - //GitlabciprojectNamespaceId: 0, + // GitlabciprojectNamespaceId: 0, GitlabmergeRequestEventName: payload.EventType, - //GitlabCIPipelineID: , - //GitlabCIPipelineIID: "", + // GitlabCIPipelineID: , + // GitlabCIPipelineIID: "", GitlabCIProjectName: payload.Project.Name, GitlabDiscussionId: discussionId, }, @@ -530,10 +530,10 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla GitlabCIMergeRequestIID: payload.MergeRequest.IID, GitlabciprojectId: payload.ProjectID, GitlabciprojectNamespace: payload.Project.Namespace, - //GitlabciprojectNamespaceId: payload.Project.Namespace, + // GitlabciprojectNamespaceId: payload.Project.Namespace, GitlabmergeRequestEventName: payload.EventType, - //GitlabCIPipelineID: , - //GitlabCIPipelineIID: "", + // GitlabCIPipelineID: , + // GitlabCIPipelineIID: "", GitlabCIProjectName: payload.Project.Name, GitlabDiscussionId: discussionId, }, diff --git a/ee/backend/controllers/spec.go b/ee/backend/controllers/spec.go index 929ae5a06..aa5a76da2 100644 --- a/ee/backend/controllers/spec.go +++ b/ee/backend/controllers/spec.go @@ -3,6 +3,11 @@ package controllers import ( "encoding/json" "fmt" + "log" + "net/http" + "os" + "strings" + "github.com/dchest/uniuri" "github.com/diggerhq/digger/backend/models" "github.com/diggerhq/digger/libs/ci/generic" @@ -10,10 +15,6 @@ import ( "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "github.com/gin-gonic/gin" - "log" - "net/http" - "os" - "strings" ) func (d DiggerEEController) GetSpec(c *gin.Context) { @@ -32,9 +33,9 @@ func (d DiggerEEController) GetSpec(c *gin.Context) { actor := payload.Actor commitSha := "" - //defaultBranch := payload.DefaultBranch - //prBranch := payload.PrBranch - issueNumber := 000 + // defaultBranch := payload.DefaultBranch + // prBranch := payload.PrBranch + issueNumber := 0o00 config := digger_config.DiggerConfig{} err := json.Unmarshal([]byte(payload.DiggerConfig), &config) @@ -62,7 +63,7 @@ func (d DiggerEEController) GetSpec(c *gin.Context) { } job := jobs[0] - //temp to get orgID TODO: fetch from db + // temp to get orgID TODO: fetch from db org, err := models.DB.GetOrganisation(models.DEFAULT_ORG_NAME) if err != nil { c.String(http.StatusInternalServerError, "Failed to get default organisation") @@ -82,7 +83,7 @@ func (d DiggerEEController) GetSpec(c *gin.Context) { spec := spec.Spec{ SpecType: spec.SpecTypeManualJob, JobId: uniuri.New(), - //CommentId: "", + // CommentId: "", Job: jobSpec, Reporter: spec.ReporterSpec{ ReportingStrategy: "comments_per_run", @@ -116,5 +117,4 @@ func (d DiggerEEController) GetSpec(c *gin.Context) { log.Printf("specBytes: %v", spec) c.String(200, string(specBytes)) return - } diff --git a/ee/backend/main.go b/ee/backend/main.go index 64267c8cd..fd73064a2 100644 --- a/ee/backend/main.go +++ b/ee/backend/main.go @@ -4,6 +4,10 @@ import ( "crypto/fips140" "embed" "fmt" + "log" + "net/http" + "os" + "github.com/diggerhq/digger/backend/bootstrap" "github.com/diggerhq/digger/backend/config" ce_controllers "github.com/diggerhq/digger/backend/controllers" @@ -15,9 +19,6 @@ import ( "github.com/diggerhq/digger/ee/backend/providers/github" "github.com/diggerhq/digger/libs/license" "github.com/gin-gonic/gin" - "log" - "net/http" - "os" ) // based on https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications @@ -100,7 +101,6 @@ func main() { c.HTML(http.StatusOK, "healthy.tmpl", gin.H{}) return }) - } jobArtefactsGroup := r.Group("/job_artefacts") diff --git a/ee/backend/providers/github/providers.go b/ee/backend/providers/github/providers.go index 845c2506b..bb74b2568 100644 --- a/ee/backend/providers/github/providers.go +++ b/ee/backend/providers/github/providers.go @@ -4,17 +4,17 @@ import ( "context" "encoding/base64" "fmt" + "log" + net "net/http" + "os" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/diggerhq/digger/backend/models" "github.com/diggerhq/digger/backend/utils" "github.com/google/go-github/v61/github" - "log" - net "net/http" - "os" ) -type DiggerGithubEEClientProvider struct { -} +type DiggerGithubEEClientProvider struct{} func (gh DiggerGithubEEClientProvider) NewClient(netClient *net.Client) (*github.Client, error) { ghClient := github.NewClient(netClient) @@ -38,7 +38,7 @@ func getGithubEnterpriseUrls(githubHostname string) (string, string) { return githubEnterpriseBaseUrl, githubEnterpriseUploadUrl } -func (gh DiggerGithubEEClientProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubEEClientProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { githubAppPrivateKey := "" githubAppPrivateKeyB64 := os.Getenv("GITHUB_APP_PRIVATE_KEY_BASE64") diff --git a/ee/cli/cmd/digger/default.go b/ee/cli/cmd/digger/default.go index ee6060a62..405796ff5 100644 --- a/ee/cli/cmd/digger/default.go +++ b/ee/cli/cmd/digger/default.go @@ -4,6 +4,10 @@ import ( "crypto/fips140" "encoding/json" "fmt" + "log" + "os" + "runtime/debug" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/github" spec2 "github.com/diggerhq/digger/cli/pkg/spec" @@ -17,9 +21,6 @@ import ( comment_summary "github.com/diggerhq/digger/libs/comment_utils/summary" lib_spec "github.com/diggerhq/digger/libs/spec" "github.com/spf13/cobra" - "log" - "os" - "runtime/debug" ) var defaultCmd = &cobra.Command{ @@ -62,7 +63,7 @@ var defaultCmd = &cobra.Command{ usage.ReportErrorAndExit(spec.VCS.Actor, "Successfully ran spec", 0) } - var logLeader = "Unknown CI" + logLeader := "Unknown CI" ci := digger.DetectCI() switch ci { diff --git a/ee/cli/cmd/digger/main.go b/ee/cli/cmd/digger/main.go index 0f5dfd7f9..d8cafa20d 100644 --- a/ee/cli/cmd/digger/main.go +++ b/ee/cli/cmd/digger/main.go @@ -2,10 +2,11 @@ package main import ( "fmt" - "github.com/diggerhq/digger/cli/pkg/usage" - "github.com/diggerhq/digger/libs/license" "log" "os" + + "github.com/diggerhq/digger/cli/pkg/usage" + "github.com/diggerhq/digger/libs/license" ) /* @@ -34,7 +35,6 @@ func main() { if err := rootCmd.Execute(); err != nil { usage.ReportErrorAndExit("", fmt.Sprintf("Error occurred during command exec: %v", err), 8) } - } func init() { diff --git a/ee/cli/cmd/digger/main_test.go b/ee/cli/cmd/digger/main_test.go index 2e8cd98f2..9269904bc 100644 --- a/ee/cli/cmd/digger/main_test.go +++ b/ee/cli/cmd/digger/main_test.go @@ -1,26 +1,26 @@ package main import ( + "log" + "testing" + "github.com/diggerhq/digger/libs/backendapi" "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/ci/generic" "github.com/diggerhq/digger/libs/locking" "github.com/diggerhq/digger/libs/policy" "github.com/diggerhq/digger/libs/storage" - "log" "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/github/models" ghmodels "github.com/diggerhq/digger/cli/pkg/github/models" dggithub "github.com/diggerhq/digger/libs/ci/github" "github.com/diggerhq/digger/libs/comment_utils/reporting" - "github.com/diggerhq/digger/libs/comment_utils/summary" + comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary" configuration "github.com/diggerhq/digger/libs/digger_config" "github.com/google/go-github/v61/github" - "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -874,7 +874,6 @@ var githubInvalidContextJson = `{ ` func TestGitHubNewPullRequestContext(t *testing.T) { - actionContext, err := models.GetGitHubContext(githubContextNewPullRequestJson) context := actionContext.ToEventPackage() @@ -997,7 +996,7 @@ func TestGitHubNewPullRequestInMultiEnvProjectContext(t *testing.T) { // PullRequestManager Mock prManager := ci.MockPullRequestManager{ChangedFiles: []string{"dev/test.tf"}} - //lock := locking.MockLock{} + // lock := locking.MockLock{} impactedProjects, requestedProject, prNumber, err := dggithub.ProcessGitHubEvent(ghEvent, &diggerConfig, &prManager) assert.NoError(t, err) event := context.Event.(github.PullRequestEvent) @@ -1029,7 +1028,7 @@ func TestGitHubTestPRCommandCaseInsensitivity(t *testing.T) { var impactedProjects []configuration.Project impactedProjects = make([]configuration.Project, 1) impactedProjects[0] = project - var requestedProject = project + requestedProject := project workflows := make(map[string]configuration.Workflow, 1) workflows["default"] = configuration.Workflow{} jobs, _, err := generic.ConvertIssueCommentEventToJobs(*repo.FullName, *user.Login, *issue.Number, "digger plan", impactedProjects, &requestedProject, workflows, "prbranch", "main") diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index 30ca27146..095e509e1 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -2,6 +2,11 @@ package main import ( "fmt" + "log" + "net/http" + "os" + "time" + "github.com/diggerhq/digger/cli/pkg/utils" "github.com/diggerhq/digger/libs/backendapi" "github.com/diggerhq/digger/libs/ci" @@ -11,10 +16,6 @@ import ( "github.com/diggerhq/digger/libs/locking" "github.com/diggerhq/digger/libs/policy" "github.com/spf13/cobra" - "log" - "net/http" - "os" - "time" ) type RunConfig struct { @@ -76,12 +77,13 @@ func (r *RunConfig) GetServices() (*ci.PullRequestService, *ci.OrgService, *repo return &prService, &orgService, &reporter, nil } -var BackendApi backendapi.Api -var ReportStrategy reporting.ReportStrategy -var lock locking.Lock +var ( + BackendApi backendapi.Api + ReportStrategy reporting.ReportStrategy + lock locking.Lock +) func PreRun(cmd *cobra.Command, args []string) { - hostName := os.Getenv("DIGGER_HOSTNAME") token := os.Getenv("DIGGER_TOKEN") BackendApi = NewBackendApi(hostName, token) @@ -113,7 +115,7 @@ func PreRun(cmd *cobra.Command, args []string) { log.Println("Lock provider has been created successfully") } -func NewBackendApi(hostName string, authToken string) backendapi.Api { +func NewBackendApi(hostName, authToken string) backendapi.Api { var backendApi backendapi.Api if os.Getenv("NO_BACKEND") == "true" { log.Println("WARNING: running in 'backendless' mode. Features that require backend will not be available.") @@ -128,7 +130,7 @@ func NewBackendApi(hostName string, authToken string) backendapi.Api { return backendApi } -func NewPolicyChecker(hostname string, organisationName string, authToken string) policy.Checker { +func NewPolicyChecker(hostname, organisationName, authToken string) policy.Checker { var policyChecker policy.Checker if os.Getenv("NO_BACKEND") == "true" { log.Println("WARNING: running in 'backendless' mode. Features that require backend will not be available.") @@ -140,7 +142,8 @@ func NewPolicyChecker(hostname string, organisationName string, authToken string DiggerOrganisation: organisationName, AuthToken: authToken, HttpClient: http.DefaultClient, - }} + }, + } } return policyChecker } diff --git a/ee/cli/cmd/digger/run_spec.go b/ee/cli/cmd/digger/run_spec.go index 8cc624305..d88f72bca 100644 --- a/ee/cli/cmd/digger/run_spec.go +++ b/ee/cli/cmd/digger/run_spec.go @@ -3,6 +3,8 @@ package main import ( "encoding/json" "fmt" + "strings" + spec2 "github.com/diggerhq/digger/cli/pkg/spec" "github.com/diggerhq/digger/cli/pkg/usage" "github.com/diggerhq/digger/ee/cli/pkg/policy" @@ -11,7 +13,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" - "strings" ) var viperRunSpec *viper.Viper diff --git a/ee/cli/pkg/comment_updater/provider.go b/ee/cli/pkg/comment_updater/provider.go index 84f9a91fa..83b295076 100644 --- a/ee/cli/pkg/comment_updater/provider.go +++ b/ee/cli/pkg/comment_updater/provider.go @@ -2,12 +2,12 @@ package comment_updater import ( "fmt" + comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary" "github.com/diggerhq/digger/libs/digger_config" ) -type CommentUpdaterProviderAdvanced struct { -} +type CommentUpdaterProviderAdvanced struct{} func (c CommentUpdaterProviderAdvanced) Get(renderMode string) (comment_updater.CommentUpdater, error) { if renderMode == digger_config.CommentRenderModeBasic { diff --git a/ee/cli/pkg/comment_updater/updater.go b/ee/cli/pkg/comment_updater/updater.go index 98d955863..c36a32d0c 100644 --- a/ee/cli/pkg/comment_updater/updater.go +++ b/ee/cli/pkg/comment_updater/updater.go @@ -3,14 +3,14 @@ package comment_updater import ( "encoding/json" "fmt" - "github.com/diggerhq/digger/libs/ci" - "github.com/diggerhq/digger/libs/scheduler" "log" "strings" + + "github.com/diggerhq/digger/libs/ci" + "github.com/diggerhq/digger/libs/scheduler" ) -type AdvancedCommentUpdater struct { -} +type AdvancedCommentUpdater struct{} func DriftSummaryString(projectName string, issuesMap *map[string]*ci.Issue) string { driftStatusForProject := (*issuesMap)[projectName] @@ -22,7 +22,6 @@ func DriftSummaryString(projectName string, issuesMap *map[string]*ci.Issue) str } func (a AdvancedCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService ci.PullRequestService, prCommentId string) error { - issuesMap, err := getDriftStatusesFromPRIssues(jobs, prService) if err != nil { return fmt.Errorf("error while fetching drift status: %v", err) diff --git a/ee/cli/pkg/drift/github_issue.go b/ee/cli/pkg/drift/github_issue.go index ada018071..702c67e82 100644 --- a/ee/cli/pkg/drift/github_issue.go +++ b/ee/cli/pkg/drift/github_issue.go @@ -2,9 +2,10 @@ package drift import ( "fmt" + "log" + orchestrator "github.com/diggerhq/digger/libs/ci" "github.com/samber/lo" - "log" ) type GithubIssueNotification struct { @@ -12,7 +13,7 @@ type GithubIssueNotification struct { RelatedPrNumber *int64 } -func (ghi GithubIssueNotification) Send(projectName string, plan string) error { +func (ghi GithubIssueNotification) Send(projectName, plan string) error { log.Printf("Info: Sending drift notification regarding project: %v", projectName) title := fmt.Sprintf("Drift detected in project: %v", projectName) message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan) diff --git a/ee/cli/pkg/drift/provider.go b/ee/cli/pkg/drift/provider.go index f5f3a173f..a31edd873 100644 --- a/ee/cli/pkg/drift/provider.go +++ b/ee/cli/pkg/drift/provider.go @@ -2,10 +2,11 @@ package drift import ( "fmt" + "os" + core_drift "github.com/diggerhq/digger/cli/pkg/core/drift" "github.com/diggerhq/digger/cli/pkg/drift" "github.com/diggerhq/digger/libs/ci" - "os" ) type DriftNotificationProviderAdvanced struct{} diff --git a/ee/cli/pkg/github/providers.go b/ee/cli/pkg/github/providers.go index 6560b2b3b..70a9d657a 100644 --- a/ee/cli/pkg/github/providers.go +++ b/ee/cli/pkg/github/providers.go @@ -2,15 +2,16 @@ package github import ( "fmt" - dg_github "github.com/diggerhq/digger/libs/ci/github" - "github.com/google/go-github/v61/github" "log" "os" + + dg_github "github.com/diggerhq/digger/libs/ci/github" + "github.com/google/go-github/v61/github" ) type GithubServiceProviderAdvanced struct{} -func (_ GithubServiceProviderAdvanced) NewService(ghToken string, repoName string, owner string) (dg_github.GithubService, error) { +func (_ GithubServiceProviderAdvanced) NewService(ghToken, repoName, owner string) (dg_github.GithubService, error) { client := github.NewClient(nil) if ghToken != "" { client = client.WithAuthToken(ghToken) diff --git a/ee/cli/pkg/gitlab/gitlab.go b/ee/cli/pkg/gitlab/gitlab.go index 501984052..5c0cd5d47 100644 --- a/ee/cli/pkg/gitlab/gitlab.go +++ b/ee/cli/pkg/gitlab/gitlab.go @@ -2,6 +2,9 @@ package gitlab import ( "fmt" + "log" + "os" + "github.com/diggerhq/digger/cli/pkg/digger" "github.com/diggerhq/digger/cli/pkg/drift" "github.com/diggerhq/digger/cli/pkg/usage" @@ -13,8 +16,6 @@ import ( core_locking "github.com/diggerhq/digger/libs/locking" core_policy "github.com/diggerhq/digger/libs/policy" "github.com/diggerhq/digger/libs/scheduler" - "log" - "os" ) func GitLabCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCheckerProvider, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, githubServiceProvider dg_github.GithubServiceProvider, commentUpdaterProvider comment_updater.CommentUpdaterProvider, driftNotificationProvider drift.DriftNotificationProvider) { @@ -42,7 +43,7 @@ func GitLabCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh log.Printf("Digger digger_config read successfully\n") // default policy checker for backwards compatibility, will be overridden in orchestrator flow - var policyChecker = core_policy.NoOpPolicyChecker{} + policyChecker := core_policy.NoOpPolicyChecker{} currentDir, err := os.Getwd() if err != nil { @@ -66,7 +67,6 @@ func GitLabCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh runningMode := os.Getenv("INPUT_DIGGER_MODE") if runningMode == "drift-detection" { - for _, projectConfig := range diggerConfig.Projects { if !projectConfig.DriftDetection { continue @@ -125,5 +125,4 @@ func GitLabCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh println("Commands executed successfully") usage.ReportErrorAndExit(repoOwner, "Digger finished successfully", 0) - } diff --git a/ee/cli/pkg/policy/policy.go b/ee/cli/pkg/policy/policy.go index 3919fcdef..fc26bf377 100644 --- a/ee/cli/pkg/policy/policy.go +++ b/ee/cli/pkg/policy/policy.go @@ -1,13 +1,14 @@ package policy import ( - "github.com/diggerhq/digger/libs/git_utils" - "github.com/samber/lo" "os" "path" "path/filepath" "slices" "strings" + + "github.com/diggerhq/digger/libs/git_utils" + "github.com/samber/lo" ) const DefaultAccessPolicy = ` @@ -40,7 +41,7 @@ func getContents(filePath string) (string, error) { // /dev/vpc/subnets/access.rego // /dev/vpc/access.rego // /dev/access.rego -func GetPrefixesForPath(path string, fileName string) []string { +func GetPrefixesForPath(path, fileName string) []string { var prefixes []string parts := strings.Split(filepath.Clean(path), string(filepath.Separator)) for i := range parts { @@ -60,7 +61,7 @@ func GetPrefixesForPath(path string, fileName string) []string { return prefixes } -func (p DiggerRepoPolicyProvider) getPolicyFileContents(repo string, projectName string, projectDir string, fileName string) (string, error) { +func (p DiggerRepoPolicyProvider) getPolicyFileContents(repo, projectName, projectDir, fileName string) (string, error) { var contents string err := git_utils.CloneGitRepoAndDoAction(p.ManagementRepoUrl, "main", "", p.GitToken, "", func(basePath string) error { // we start with the project directory path prefixes as the highest priority @@ -96,7 +97,7 @@ func (p DiggerRepoPolicyProvider) getPolicyFileContents(repo string, projectName } // GetPolicy fetches policy for particular project, if not found then it will fallback to org level policy -func (p DiggerRepoPolicyProvider) GetAccessPolicy(organisation string, repo string, projectName string, projectDir string) (string, error) { +func (p DiggerRepoPolicyProvider) GetAccessPolicy(organisation, repo, projectName, projectDir string) (string, error) { policy, err := p.getPolicyFileContents(repo, projectName, projectDir, "access.rego") if err != nil { return policy, err @@ -107,7 +108,7 @@ func (p DiggerRepoPolicyProvider) GetAccessPolicy(organisation string, repo stri return policy, err } -func (p DiggerRepoPolicyProvider) GetPlanPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (p DiggerRepoPolicyProvider) GetPlanPolicy(organisation, repository, projectname, projectDir string) (string, error) { policy, err := p.getPolicyFileContents(repository, projectname, projectDir, "plan.rego") if err != nil { return policy, err @@ -117,7 +118,6 @@ func (p DiggerRepoPolicyProvider) GetPlanPolicy(organisation string, repository func (p DiggerRepoPolicyProvider) GetDriftPolicy() (string, error) { return "", nil - } func (p DiggerRepoPolicyProvider) GetOrganisation() string { diff --git a/ee/cli/pkg/policy/policy_test.go b/ee/cli/pkg/policy/policy_test.go index a46fe22e0..5ac482f31 100644 --- a/ee/cli/pkg/policy/policy_test.go +++ b/ee/cli/pkg/policy/policy_test.go @@ -1,10 +1,11 @@ package policy import ( - "github.com/stretchr/testify/assert" "log" "os" "testing" + + "github.com/stretchr/testify/assert" ) func init() { diff --git a/ee/cli/pkg/policy/providers.go b/ee/cli/pkg/policy/providers.go index 8226a3fd8..42bd80ff1 100644 --- a/ee/cli/pkg/policy/providers.go +++ b/ee/cli/pkg/policy/providers.go @@ -2,20 +2,21 @@ package policy import ( "fmt" - "github.com/diggerhq/digger/libs/policy" - lib_spec "github.com/diggerhq/digger/libs/spec" "log" "os" + + "github.com/diggerhq/digger/libs/policy" + lib_spec "github.com/diggerhq/digger/libs/spec" ) type AdvancedPolicyProvider struct{} -func (p AdvancedPolicyProvider) GetPolicyProvider(policySpec lib_spec.PolicySpec, diggerHost string, diggerOrg string, token string, vcsType string) (policy.Checker, error) { +func (p AdvancedPolicyProvider) GetPolicyProvider(policySpec lib_spec.PolicySpec, diggerHost, diggerOrg, token, vcsType string) (policy.Checker, error) { managementRepo := os.Getenv("DIGGER_MANAGEMENT_REPO") if managementRepo != "" { log.Printf("info: using management repo policy provider") - var token = "" - var tokenName = "" + token := "" + tokenName := "" switch vcsType { case "github": token = os.Getenv("GITHUB_TOKEN") @@ -45,7 +46,7 @@ func (p AdvancedPolicyProvider) GetPolicyProvider(policySpec lib_spec.PolicySpec type PolicyCheckerProviderAdvanced struct{} -func (p PolicyCheckerProviderAdvanced) Get(hostname string, organisationName string, authToken string) (policy.Checker, error) { +func (p PolicyCheckerProviderAdvanced) Get(hostname, organisationName, authToken string) (policy.Checker, error) { managementRepo := os.Getenv("DIGGER_MANAGEMENT_REPO") if managementRepo != "" { token := os.Getenv("GITHUB_TOKEN") diff --git a/ee/cli/pkg/vcs/providers.go b/ee/cli/pkg/vcs/providers.go index d74700e15..839a9edc9 100644 --- a/ee/cli/pkg/vcs/providers.go +++ b/ee/cli/pkg/vcs/providers.go @@ -2,11 +2,12 @@ package vcs import ( "fmt" + "os" + github2 "github.com/diggerhq/digger/ee/cli/pkg/github" "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/ci/gitlab" "github.com/diggerhq/digger/libs/spec" - "os" ) type VCSProviderAdvanced struct{} diff --git a/ee/drift/controllers/ci_jobs.go b/ee/drift/controllers/ci_jobs.go index 83af7373d..9d6135f63 100644 --- a/ee/drift/controllers/ci_jobs.go +++ b/ee/drift/controllers/ci_jobs.go @@ -1,14 +1,15 @@ package controllers import ( - "github.com/diggerhq/digger/backend/models" - "github.com/diggerhq/digger/ee/drift/middleware" - orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "log" "log/slog" "net/http" "time" + "github.com/diggerhq/digger/backend/models" + "github.com/diggerhq/digger/ee/drift/middleware" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" + "github.com/diggerhq/digger/libs/iac_utils" "github.com/gin-gonic/gin" ) @@ -35,7 +36,6 @@ func (mc MainController) SetJobStatusForProject(c *gin.Context) { var request SetJobStatusRequest err := c.BindJSON(&request) - if err != nil { log.Printf("Error binding JSON: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"}) @@ -116,7 +116,7 @@ func (mc MainController) SetJobStatusForProject(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) } -func ProjectDriftStateMachineApply(project models.Project, tfplan string, resourcesCreated uint, resourcesUpdated uint, resourcesDeleted uint) error { +func ProjectDriftStateMachineApply(project models.Project, tfplan string, resourcesCreated, resourcesUpdated, resourcesDeleted uint) error { isEmptyPlan := resourcesCreated == 0 && resourcesUpdated == 0 && resourcesDeleted == 0 wasEmptyPlan := project.DriftToCreate == 0 && project.DriftToUpdate == 0 && project.DriftToDelete == 0 if isEmptyPlan { diff --git a/ee/drift/controllers/drift.go b/ee/drift/controllers/drift.go index 6f79fe798..9079455d4 100644 --- a/ee/drift/controllers/drift.go +++ b/ee/drift/controllers/drift.go @@ -4,13 +4,14 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/diggerhq/digger/backend/models" "log" "net/http" "net/url" "os" "time" + "github.com/diggerhq/digger/backend/models" + "github.com/diggerhq/digger/backend/ci_backends" services2 "github.com/diggerhq/digger/ee/drift/services" "github.com/diggerhq/digger/ee/drift/utils" @@ -186,7 +187,6 @@ func (mc MainController) TriggerDriftRunForProject(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Trigger workflow error")}) return } - } func (mc MainController) ProcessAllDrift(c *gin.Context) { @@ -335,7 +335,6 @@ func (mc MainController) ProcessDriftForOrg(c *gin.Context) { log.Printf("got unexpected drift status for project: %v - status: %v", projectId, statusCode) } } - } c.String(200, "success") } diff --git a/ee/drift/controllers/notifications.go b/ee/drift/controllers/notifications.go index 0a63fec04..735aa8155 100644 --- a/ee/drift/controllers/notifications.go +++ b/ee/drift/controllers/notifications.go @@ -4,15 +4,16 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/diggerhq/digger/backend/models" - utils2 "github.com/diggerhq/digger/next/utils" - "github.com/gin-gonic/gin" - "github.com/slack-go/slack" "log" "net/http" "net/url" "os" "time" + + "github.com/diggerhq/digger/backend/models" + utils2 "github.com/diggerhq/digger/next/utils" + "github.com/gin-gonic/gin" + "github.com/slack-go/slack" ) func sendTestSlackWebhook(webhookURL string) error { diff --git a/ee/drift/main.go b/ee/drift/main.go index be98d4903..8d7e0fac6 100644 --- a/ee/drift/main.go +++ b/ee/drift/main.go @@ -2,13 +2,14 @@ package main import ( "fmt" - "github.com/diggerhq/digger/backend/models" - utils2 "github.com/diggerhq/digger/backend/utils" "log" "log/slog" "net/http" "os" + "github.com/diggerhq/digger/backend/models" + utils2 "github.com/diggerhq/digger/backend/utils" + "github.com/diggerhq/digger/backend/ci_backends" "github.com/diggerhq/digger/ee/drift/controllers" "github.com/diggerhq/digger/ee/drift/middleware" @@ -42,7 +43,6 @@ func init() { var Version = "dev" func main() { - sentryDsn := os.Getenv("SENTRY_DSN") if sentryDsn != "" { if err := sentry.Init(sentry.ClientOptions{ @@ -84,8 +84,8 @@ func main() { "commit_sha": Version, }) }) - //authorized := r.Group("/") - //authorized.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(dbmodels.CliJobAccessType, dbmodels.AccessPolicyType, models.AdminPolicyType)) + // authorized := r.Group("/") + // authorized.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(dbmodels.CliJobAccessType, dbmodels.AccessPolicyType, models.AdminPolicyType)) r.POST("/repos/:repo/projects/:projectName/jobs/:jobId/set-status", middleware.JobTokenAuth(), controller.SetJobStatusForProject) @@ -102,5 +102,4 @@ func main() { port = "3000" } r.Run(fmt.Sprintf(":%v", port)) - } diff --git a/ee/drift/middleware/job_token.go b/ee/drift/middleware/job_token.go index d0ea650be..0d8b34335 100644 --- a/ee/drift/middleware/job_token.go +++ b/ee/drift/middleware/job_token.go @@ -2,12 +2,13 @@ package middleware import ( "fmt" - "github.com/diggerhq/digger/backend/models" - "github.com/gin-gonic/gin" "log" "net/http" "strings" "time" + + "github.com/diggerhq/digger/backend/models" + "github.com/gin-gonic/gin" ) func CheckJobToken(c *gin.Context, token string) (*models.JobToken, error) { diff --git a/ee/drift/middleware/middleware.go b/ee/drift/middleware/middleware.go index 3f3fb079b..1f467e23b 100644 --- a/ee/drift/middleware/middleware.go +++ b/ee/drift/middleware/middleware.go @@ -1,4 +1,6 @@ package middleware -const ORGANISATION_ID_KEY = "organisation_ID" -const ACCESS_LEVEL_KEY = "access_level" +const ( + ORGANISATION_ID_KEY = "organisation_ID" + ACCESS_LEVEL_KEY = "access_level" +) diff --git a/ee/drift/middleware/webhooks.go b/ee/drift/middleware/webhooks.go index 60ff1afac..c46694b09 100644 --- a/ee/drift/middleware/webhooks.go +++ b/ee/drift/middleware/webhooks.go @@ -1,10 +1,11 @@ package middleware import ( - "github.com/gin-gonic/gin" "net/http" "os" "strings" + + "github.com/gin-gonic/gin" ) func WebhookAuth() gin.HandlerFunc { diff --git a/ee/drift/services/spec.go b/ee/drift/services/spec.go index ec63e8056..2c6a4ce57 100644 --- a/ee/drift/services/spec.go +++ b/ee/drift/services/spec.go @@ -2,10 +2,11 @@ package services import ( "fmt" + "os" + utils2 "github.com/diggerhq/digger/backend/utils" "github.com/diggerhq/digger/ee/drift/utils" "github.com/diggerhq/digger/libs/spec" - "os" ) func GetRunNameFromJob(spec spec.Spec) (*string, error) { @@ -13,14 +14,14 @@ func GetRunNameFromJob(spec spec.Spec) (*string, error) { diggerCommand := fmt.Sprintf("digger %v", jobSpec.JobType) jobIdShort := spec.JobId[:8] projectName := jobSpec.ProjectName - //requestedBy := jobSpec.RequestedBy - //prNumber := *jobSpec.PullRequestNumber + // requestedBy := jobSpec.RequestedBy + // prNumber := *jobSpec.PullRequestNumber runName := fmt.Sprintf("[%v] %v %v (drift)", jobIdShort, diggerCommand, projectName) return &runName, nil } -func GetVCSToken(vcsType string, repoFullName string, repoOwner string, repoName string, installationId int64, gh utils2.GithubClientProvider) (*string, error) { +func GetVCSToken(vcsType, repoFullName, repoOwner, repoName string, installationId int64, gh utils2.GithubClientProvider) (*string, error) { var token string switch vcsType { case "github": diff --git a/ee/drift/tasks/github.go b/ee/drift/tasks/github.go index 90a0011ea..fc43a300a 100644 --- a/ee/drift/tasks/github.go +++ b/ee/drift/tasks/github.go @@ -2,17 +2,18 @@ package tasks import ( "fmt" + "log" + "strconv" + "strings" + "github.com/diggerhq/digger/backend/models/dbmodels" "github.com/diggerhq/digger/ee/drift/utils" dg_configuration "github.com/diggerhq/digger/libs/digger_config" utils3 "github.com/diggerhq/digger/libs/git_utils" utils2 "github.com/diggerhq/digger/next/utils" - "log" - "strconv" - "strings" ) -func LoadProjectsFromGithubRepo(gh utils2.GithubClientProvider, installationId string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) error { +func LoadProjectsFromGithubRepo(gh utils2.GithubClientProvider, installationId, repoFullName, repoOwner, repoName, cloneUrl, branch string) error { link, err := dbmodels.DB.GetGithubAppInstallationLink(installationId) if err != nil { log.Printf("Error getting GetGithubAppInstallationLink: %v", err) diff --git a/ee/drift/utils/github.go b/ee/drift/utils/github.go index 993545aa9..5e8b51f77 100644 --- a/ee/drift/utils/github.go +++ b/ee/drift/utils/github.go @@ -4,6 +4,12 @@ import ( "context" "encoding/base64" "fmt" + "log" + "log/slog" + net "net/http" + "os" + "path" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/diggerhq/digger/backend/models" github2 "github.com/diggerhq/digger/libs/ci/github" @@ -12,11 +18,6 @@ import ( "github.com/diggerhq/digger/next/utils" "github.com/dominikbraun/graph" "github.com/google/go-github/v61/github" - "log" - "log/slog" - net "net/http" - "os" - "path" ) func GetGithubClient(gh utils.GithubClientProvider, installationId int64, repoFullName string) (*github.Client, *string, error) { @@ -33,7 +34,7 @@ func GetGithubClient(gh utils.GithubClientProvider, installationId int64, repoFu return ghClient, token, err } -func GetGithubService(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) { +func GetGithubService(gh utils.GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName string) (*github2.GithubService, *string, error) { slog.Debug("getting github client", "installationId", installationId, "repoFullName", repoFullName) ghClient, token, err := GetGithubClient(gh, installationId, repoFullName) if err != nil { @@ -50,15 +51,14 @@ func GetGithubService(gh utils.GithubClientProvider, installationId int64, repoF return &ghService, token, nil } -type DiggerGithubRealClientProvider struct { -} +type DiggerGithubRealClientProvider struct{} func (gh DiggerGithubRealClientProvider) NewClient(netClient *net.Client) (*github.Client, error) { ghClient := github.NewClient(netClient) return ghClient, nil } -func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubRealClientProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { githubAppPrivateKey := "" githubAppPrivateKeyB64 := os.Getenv("GITHUB_APP_PRIVATE_KEY_BASE64") if githubAppPrivateKeyB64 != "" { @@ -94,7 +94,7 @@ func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId i return ghClient, &token, nil } -func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) (string, *github2.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { +func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName, cloneUrl, branch string) (string, *github2.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { ghService, token, err := GetGithubService(gh, installationId, repoFullName, repoOwner, repoName) if err != nil { log.Printf("Error getting github service: %v", err) diff --git a/libs/backendapi/backend.go b/libs/backendapi/backend.go index 3bf9bfbd3..d0b251080 100644 --- a/libs/backendapi/backend.go +++ b/libs/backendapi/backend.go @@ -1,15 +1,16 @@ package backendapi import ( + "time" + "github.com/diggerhq/digger/libs/iac_utils" "github.com/diggerhq/digger/libs/scheduler" - "time" ) type Api interface { - ReportProject(repo string, projectName string, configuration string) error - ReportProjectRun(repo string, projectName string, startedAt time.Time, endedAt time.Time, status string, command string, output string) error - ReportProjectJobStatus(repo string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, PrCommentId string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) + ReportProject(repo, projectName, configuration string) error + ReportProjectRun(repo, projectName string, startedAt, endedAt time.Time, status, command, output string) error + ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) UploadJobArtefact(zipLocation string) (*int, *string, error) DownloadJobArtefact(downloadTo string) (*string, error) } diff --git a/libs/backendapi/diggerapi.go b/libs/backendapi/diggerapi.go index 7fe252ae8..ec2c77a1f 100644 --- a/libs/backendapi/diggerapi.go +++ b/libs/backendapi/diggerapi.go @@ -20,18 +20,17 @@ import ( "github.com/diggerhq/digger/libs/scheduler" ) -type NoopApi struct { -} +type NoopApi struct{} -func (n NoopApi) ReportProject(namespace string, projectName string, configurationYaml string) error { +func (n NoopApi) ReportProject(namespace, projectName, configurationYaml string) error { return nil } -func (n NoopApi) ReportProjectRun(namespace string, projectName string, startedAt time.Time, endedAt time.Time, status string, command string, output string) error { +func (n NoopApi) ReportProjectRun(namespace, projectName string, startedAt, endedAt time.Time, status, command, output string) error { return nil } -func (n NoopApi) ReportProjectJobStatus(repo string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, PrCommentId string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (n NoopApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { return nil, nil } @@ -49,7 +48,7 @@ type DiggerApi struct { HttpClient *http.Client } -func (d DiggerApi) ReportProject(namespace string, projectName string, configurationYaml string) error { +func (d DiggerApi) ReportProject(namespace, projectName, configurationYaml string) error { u, err := url.Parse(d.DiggerHost) if err != nil { slog.Error("not able to parse digger cloud url", "error", err) @@ -69,7 +68,6 @@ func (d DiggerApi) ReportProject(namespace string, projectName string, configura } req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) - if err != nil { return fmt.Errorf("error while creating request: %v", err) } @@ -78,7 +76,6 @@ func (d DiggerApi) ReportProject(namespace string, projectName string, configura req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.AuthToken)) resp, err := d.HttpClient.Do(req) - if err != nil { return fmt.Errorf("error while sending request: %v", err) } @@ -90,7 +87,7 @@ func (d DiggerApi) ReportProject(namespace string, projectName string, configura return nil } -func (d DiggerApi) ReportProjectRun(namespace string, projectName string, startedAt time.Time, endedAt time.Time, status string, command string, output string) error { +func (d DiggerApi) ReportProjectRun(namespace, projectName string, startedAt, endedAt time.Time, status, command, output string) error { u, err := url.Parse(d.DiggerHost) if err != nil { slog.Error("not able to parse digger cloud url", "error", err) @@ -114,7 +111,6 @@ func (d DiggerApi) ReportProjectRun(namespace string, projectName string, starte } req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) - if err != nil { return fmt.Errorf("error while creating request: %v", err) } @@ -123,7 +119,6 @@ func (d DiggerApi) ReportProjectRun(namespace string, projectName string, starte req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.AuthToken)) resp, err := d.HttpClient.Do(req) - if err != nil { return fmt.Errorf("error while sending request: %v", err) } @@ -135,7 +130,7 @@ func (d DiggerApi) ReportProjectRun(namespace string, projectName string, starte return nil } -func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, PrCommentId string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (d DiggerApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { repoNameForBackendReporting := strings.ReplaceAll(repo, "/", "-") u, err := url.Parse(d.DiggerHost) if err != nil { @@ -144,7 +139,7 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId } var planSummaryJson interface{} - var planFootprint = &iac_utils.IacPlanFootprint{} + planFootprint := &iac_utils.IacPlanFootprint{} if summary == nil { slog.Warn("warning: nil passed to plan result, sending empty") planSummaryJson = nil @@ -178,7 +173,6 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId } req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) - if err != nil { return nil, fmt.Errorf("error while creating request: %v", err) } @@ -187,7 +181,6 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.AuthToken)) resp, err := d.HttpClient.Do(req) - if err != nil { return nil, fmt.Errorf("error while sending request: %v", err) } @@ -335,7 +328,7 @@ func (d DiggerApi) DownloadJobArtefact(downloadTo string) (*string, error) { return &fileName, nil } -func NewBackendApi(hostName string, authToken string) Api { +func NewBackendApi(hostName, authToken string) Api { var backendApi Api if os.Getenv("NO_BACKEND") == "true" { slog.Warn("running in 'backendless' mode - features that require backend will not be available") diff --git a/libs/backendapi/mocks.go b/libs/backendapi/mocks.go index e9b358ad8..b6da8fd9d 100644 --- a/libs/backendapi/mocks.go +++ b/libs/backendapi/mocks.go @@ -1,23 +1,23 @@ package backendapi import ( + "time" + "github.com/diggerhq/digger/libs/iac_utils" "github.com/diggerhq/digger/libs/scheduler" - "time" ) -type MockBackendApi struct { -} +type MockBackendApi struct{} -func (t MockBackendApi) ReportProject(namespace string, projectName string, configuration string) error { +func (t MockBackendApi) ReportProject(namespace, projectName, configuration string) error { return nil } -func (t MockBackendApi) ReportProjectRun(repo string, projectName string, startedAt time.Time, endedAt time.Time, status string, command string, output string) error { +func (t MockBackendApi) ReportProjectRun(repo, projectName string, startedAt, endedAt time.Time, status, command, output string) error { return nil } -func (t MockBackendApi) ReportProjectJobStatus(repo string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, PrCommentId string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (t MockBackendApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { return nil, nil } diff --git a/libs/ci/azure/azure.go b/libs/ci/azure/azure.go index dd1b72cef..0beee288d 100644 --- a/libs/ci/azure/azure.go +++ b/libs/ci/azure/azure.go @@ -5,11 +5,12 @@ import ( "encoding/json" "errors" "fmt" - "github.com/diggerhq/digger/libs/ci" - "github.com/diggerhq/digger/libs/scheduler" "strconv" "strings" + "github.com/diggerhq/digger/libs/ci" + "github.com/diggerhq/digger/libs/scheduler" + digger_config2 "github.com/diggerhq/digger/libs/digger_config" "github.com/microsoft/azure-devops-go-api/azuredevops" "github.com/microsoft/azure-devops-go-api/azuredevops/git" @@ -117,9 +118,8 @@ func (a *Azure) UnmarshalJSON(data []byte) error { return nil } -func NewAzureReposService(patToken string, baseUrl string, projectName string, repositoryId string) (*AzureReposService, error) { +func NewAzureReposService(patToken, baseUrl, projectName, repositoryId string) (*AzureReposService, error) { client, err := git.NewClient(context.Background(), azuredevops.NewPatConnection(baseUrl, patToken)) - if err != nil { return nil, err } @@ -136,12 +136,11 @@ type AzureReposService struct { RepositoryId string } -func (a *AzureReposService) GetUserTeams(organisation string, user string) ([]string, error) { +func (a *AzureReposService) GetUserTeams(organisation, user string) ([]string, error) { return make([]string, 0), nil } func (a *AzureReposService) GetChangedFiles(prNumber int) ([]string, error) { - pullRequest, err := a.Client.GetPullRequestById(context.Background(), git.GetPullRequestByIdArgs{ Project: &a.ProjectName, PullRequestId: &prNumber, @@ -192,15 +191,15 @@ func (svc *AzureReposService) ListIssues() ([]*ci.Issue, error) { return nil, fmt.Errorf("implement me") } -func (svc *AzureReposService) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (svc *AzureReposService) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (svc *AzureReposService) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc *AzureReposService) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (a *AzureReposService) SetStatus(prNumber int, status string, statusContext string) error { +func (a *AzureReposService) SetStatus(prNumber int, status, statusContext string) error { var gitStatusState git.GitStatusState if status == "success" { gitStatusState = git.GitStatusStateValues.Succeeded @@ -259,7 +258,7 @@ func (a *AzureReposService) GetCombinedPullRequestStatus(prNumber int) (string, } } - var allSuccess = true + allSuccess := true for _, status := range latestUniqueRequestStatuses { if status.State != nil || *status.State != git.GitStatusStateValues.Succeeded { allSuccess = false @@ -326,7 +325,7 @@ func (a *AzureReposService) IsMerged(prNumber int) (bool, error) { return *pullRequest.Status == git.PullRequestStatusValues.Completed, nil } -func (a *AzureReposService) EditComment(prNumber int, id string, comment string) error { +func (a *AzureReposService) EditComment(prNumber int, id, comment string) error { threadId, err := strconv.Atoi(id) if err != nil { return err @@ -351,18 +350,18 @@ func (a *AzureReposService) DeleteComment(id string) error { return nil } -func (a *AzureReposService) CreateCommentReaction(id string, reaction string) error { +func (a *AzureReposService) CreateCommentReaction(id, reaction string) error { // TODO implement me return nil } func (a *AzureReposService) GetBranchName(prNumber int) (string, string, error) { - //TODO implement me + // TODO implement me return "", "", nil } -func (svc *AzureReposService) SetOutput(prNumber int, key string, value string) error { - //TODO implement me +func (svc *AzureReposService) SetOutput(prNumber int, key, value string) error { + // TODO implement me return nil } @@ -383,7 +382,6 @@ func (a *AzureReposService) GetComments(prNumber int) ([]ci.Comment, error) { }) } return result, nil - } func (svc *AzureReposService) GetApprovals(prNumber int) ([]string, error) { @@ -400,7 +398,6 @@ func ProcessAzureReposEvent(azureEvent interface{}, diggerConfig *digger_config2 case AzurePrEvent: prNumber = azureEvent.(AzurePrEvent).Resource.PullRequestId changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { return nil, nil, 0, fmt.Errorf("could not get changed files: %v", err) } @@ -409,7 +406,6 @@ func ProcessAzureReposEvent(azureEvent interface{}, diggerConfig *digger_config2 case AzureCommentEvent: prNumber = azureEvent.(AzureCommentEvent).Resource.PullRequest.PullRequestId changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { return nil, nil, 0, fmt.Errorf("could not get changed files: %v", err) } diff --git a/libs/ci/azure/azure_test.go b/libs/ci/azure/azure_test.go index b3ad4264a..b90e162c0 100644 --- a/libs/ci/azure/azure_test.go +++ b/libs/ci/azure/azure_test.go @@ -1,12 +1,12 @@ package azure import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetAzureReposContext(t *testing.T) { - context := "{\n \"id\": \"a5425068-eade-4753-a703-2a7670b94fca\",\n \"eventType\": \"ms.vss-code.git-pullrequest-comment-event\",\n \"publisherId\": \"tfs\",\n \"message\": {\n \"text\": \"Mohamed Habib has commented on a pull request\",\n \"html\": \"Mohamed Habib has commented on a pull request\",\n \"markdown\": \"Mohamed Habib has [commented](https://dev.azure.com/moehabib9/digger-test/_git/digger-test/pullrequest/1?discussionId=1) on a pull request\"\n },\n \"detailedMessage\": {\n \"text\": \"Mohamed Habib has commented on a pull request\\r\\ndigger plan\\r\\n\",\n \"html\": \"Mohamed Habib has commented on a pull request

digger plan

\",\n \"markdown\": \"Mohamed Habib has [commented](https://dev.azure.com/moehabib9/digger-test/_git/digger-test/pullrequest/1?discussionId=1) on a pull request\\r\\ndigger plan\\r\\n\"\n },\n \"resource\": {\n \"comment\": {\n \"id\": 1,\n \"parentCommentId\": 0,\n \"author\": {\n \"displayName\": \"Mohamed Habib\",\n \"url\": \"https://spsprodweu5.vssps.visualstudio.com/A8e479c0b-ee1b-43c7-9ed8-98f2a7ec6d01/_apis/Identities/d08e23e0-8b42-6ab5-a2b4-5f1dd4d1ed68\",\n \"_links\": {\n \"avatar\": {\n \"href\": \"https://dev.azure.com/moehabib9/_apis/GraphProfile/MemberAvatars/msa.ZDA4ZTIzZTAtOGI0Mi03YWI1LWEyYjQtNWYxZGQ0ZDFlZDY4\"\n }\n },\n \"id\": \"d08e23e0-8b42-6ab5-a2b4-5f1dd4d1ed68\",\n \"uniqueName\": \"moe.habib9@gmail.com\",\n \"imageUrl\": \"https://dev.azure.com/moehabib9/_apis/GraphProfile/MemberAvatars/msa.ZDA4ZTIzZTAtOGI0Mi03YWI1LWEyYjQtNWYxZGQ0ZDFlZDY4\",\n \"descriptor\": \"msa.ZDA4ZTIzZTAtOGI0Mi03YWI1LWEyYjQtNWYxZGQ0ZDFlZDY4\"\n },\n \"content\": \"digger plan\",\n \"publishedDate\": \"2023-05-31T14:43:11.517Z\",\n \"lastUpdatedDate\": \"2023-05-31T14:43:11.517Z\",\n \"lastContentUpdatedDate\": \"2023-05-31T14:43:11.517Z\",\n \"commentType\": \"text\",\n \"usersLiked\": [],\n \"_links\": {\n \"self\": {\n \"href\": \"https://dev.azure.com/moehabib9/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/pullRequests/1/threads/1/comments/1\"\n },\n \"repository\": {\n \"href\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331\"\n },\n \"threads\": {\n \"href\": \"https://dev.azure.com/moehabib9/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/pullRequests/1/threads/1\"\n },\n \"pullRequests\": {\n \"href\": \"https://dev.azure.com/moehabib9/_apis/git/pullRequests/1\"\n }\n }\n },\n \"pullRequest\": {\n \"repository\": {\n \"id\": \"7eca30de-5afd-48e0-8c1f-e557c437d331\",\n \"name\": \"digger-test\",\n \"url\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331\",\n \"project\": {\n \"id\": \"a2def61f-157f-4382-b12f-184921223688\",\n \"name\": \"digger-test\",\n \"url\": \"https://dev.azure.com/moehabib9/_apis/projects/a2def61f-157f-4382-b12f-184921223688\",\n \"state\": \"wellFormed\",\n \"revision\": 37,\n \"visibility\": \"private\",\n \"lastUpdateTime\": \"2023-05-31T14:04:47.047Z\"\n },\n \"size\": 44640,\n \"remoteUrl\": \"https://moehabib9@dev.azure.com/moehabib9/digger-test/_git/digger-test\",\n \"sshUrl\": \"git@ssh.dev.azure.com:v3/moehabib9/digger-test/digger-test\",\n \"webUrl\": \"https://dev.azure.com/moehabib9/digger-test/_git/digger-test\",\n \"isDisabled\": false,\n \"isInMaintenance\": false\n },\n \"pullRequestId\": 1,\n \"codeReviewId\": 1,\n \"status\": \"active\",\n \"createdBy\": {\n \"displayName\": \"Mohamed Habib\",\n \"url\": \"https://spsprodweu5.vssps.visualstudio.com/A8e479c0b-ee1b-43c7-9ed8-98f2a7ec6d01/_apis/Identities/d08e23e0-8b42-6ab5-a2b4-5f1dd4d1ed68\",\n \"_links\": {\n \"avatar\": {\n \"href\": \"https://dev.azure.com/moehabib9/_apis/GraphProfile/MemberAvatars/msa.ZDA4ZTIzZTAtOGI0Mi03YWI1LWEyYjQtNWYxZGQ0ZDFlZDY4\"\n }\n },\n \"id\": \"d08e23e0-8b42-6ab5-a2b4-5f1dd4d1ed68\",\n \"uniqueName\": \"moe.habib9@gmail.com\",\n \"imageUrl\": \"https://dev.azure.com/moehabib9/_api/_common/identityImage?id=d08e23e0-8b42-6ab5-a2b4-5f1dd4d1ed68\",\n \"descriptor\": \"msa.ZDA4ZTIzZTAtOGI0Mi03YWI1LWEyYjQtNWYxZGQ0ZDFlZDY4\"\n },\n \"creationDate\": \"2023-05-31T14:41:30.7525172Z\",\n \"title\": \"Updated main.tf\",\n \"description\": \"Updated main.tf\",\n \"sourceRefName\": \"refs/heads/test-1\",\n \"targetRefName\": \"refs/heads/main\",\n \"mergeStatus\": \"succeeded\",\n \"isDraft\": false,\n \"mergeId\": \"58795d35-ab4e-445b-9c73-859a3eea1bbe\",\n \"lastMergeSourceCommit\": {\n \"commitId\": \"881bdbdda340529887c8ad46c06b0342715c4653\",\n \"url\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/commits/881bdbdda340529887c8ad46c06b0342715c4653\"\n },\n \"lastMergeTargetCommit\": {\n \"commitId\": \"c254effb63be65b0f924b473385c45f4dc4c4e96\",\n \"url\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/commits/c254effb63be65b0f924b473385c45f4dc4c4e96\"\n },\n \"lastMergeCommit\": {\n \"commitId\": \"dccb8e2c3232118d6023d862472afb4bf146dc39\",\n \"author\": {\n \"name\": \"Mohamed Habib\",\n \"email\": \"moe.habib9@gmail.com\",\n \"date\": \"2023-05-31T14:41:30Z\"\n },\n \"committer\": {\n \"name\": \"Mohamed Habib\",\n \"email\": \"moe.habib9@gmail.com\",\n \"date\": \"2023-05-31T14:41:30Z\"\n },\n \"comment\": \"Merge pull request 1 from test-1 into main\",\n \"url\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/commits/dccb8e2c3232118d6023d862472afb4bf146dc39\"\n },\n \"reviewers\": [],\n \"url\": \"https://dev.azure.com/moehabib9/a2def61f-157f-4382-b12f-184921223688/_apis/git/repositories/7eca30de-5afd-48e0-8c1f-e557c437d331/pullRequests/1\",\n \"supportsIterations\": true,\n \"artifactId\": \"vstfs:///Git/PullRequestId/a2def61f-157f-4382-b12f-184921223688%2f7eca30de-5afd-48e0-8c1f-e557c437d331%2f1\"\n }\n },\n \"resourceVersion\": \"2.0\",\n \"resourceContainers\": {\n \"collection\": {\n \"id\": \"f658bb7d-1dad-4fd9-b188-2fe9e914b810\",\n \"baseUrl\": \"https://dev.azure.com/moehabib9/\"\n },\n \"account\": {\n \"id\": \"8e479c0b-ee1b-43c7-9ed8-98f2a7ec6d01\",\n \"baseUrl\": \"https://dev.azure.com/moehabib9/\"\n },\n \"project\": {\n \"id\": \"a2def61f-157f-4382-b12f-184921223688\",\n \"baseUrl\": \"https://dev.azure.com/moehabib9/\"\n }\n },\n \"createdDate\": \"2023-05-31T14:43:18.079Z\"\n}" az, _ := GetAzureReposContext(context) diff --git a/libs/ci/bitbucket/bitbucket.go b/libs/ci/bitbucket/bitbucket.go index 024f17c2d..2f9864869 100644 --- a/libs/ci/bitbucket/bitbucket.go +++ b/libs/ci/bitbucket/bitbucket.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/diggerhq/digger/libs/ci" - configuration "github.com/diggerhq/digger/libs/digger_config" "io/ioutil" "net/http" "strconv" "time" + + "github.com/diggerhq/digger/libs/ci" + configuration "github.com/diggerhq/digger/libs/digger_config" ) // Define the base URL for the Bitbucket API. @@ -148,15 +149,15 @@ func (svc BitbucketAPI) ListIssues() ([]*ci.Issue, error) { return nil, fmt.Errorf("implement me") } -func (svc BitbucketAPI) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (svc BitbucketAPI) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (svc BitbucketAPI) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc BitbucketAPI) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (b BitbucketAPI) EditComment(prNumber int, id string, comment string) error { +func (b BitbucketAPI) EditComment(prNumber int, id, comment string) error { url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments/%s", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber, id) commentBody := map[string]interface{}{ @@ -187,7 +188,7 @@ func (b BitbucketAPI) DeleteComment(id string) error { return nil } -func (b BitbucketAPI) CreateCommentReaction(id string, reaction string) error { +func (b BitbucketAPI) CreateCommentReaction(id, reaction string) error { // TODO implement me return nil } @@ -207,7 +208,6 @@ type Comment struct { } func (b BitbucketAPI) GetComments(prNumber int) ([]ci.Comment, error) { - url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber) resp, err := b.sendRequest("GET", url, nil) @@ -236,7 +236,6 @@ func (b BitbucketAPI) GetComments(prNumber int) ([]ci.Comment, error) { } return comments, nil - } func (svc BitbucketAPI) GetApprovals(prNumber int) ([]string, error) { @@ -254,7 +253,7 @@ type PullRequest struct { } } -func (b BitbucketAPI) SetStatus(prNumber int, status string, statusContext string) error { +func (b BitbucketAPI) SetStatus(prNumber int, status, statusContext string) error { prUrl := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber) resp, err := b.sendRequest("GET", prUrl, nil) @@ -322,7 +321,6 @@ func (b BitbucketAPI) GetCombinedPullRequestStatus(prNumber int) (string, error) url := fmt.Sprintf("%s/repositories/%s/%s/commit/%d/statuses", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber) resp, err := b.sendRequest("GET", url, nil) - if err != nil { return "", err } @@ -336,7 +334,6 @@ func (b BitbucketAPI) GetCombinedPullRequestStatus(prNumber int) (string, error) var statuses CommitStatuses err = json.NewDecoder(resp.Body).Decode(&statuses) - if err != nil { return "", err } @@ -370,7 +367,7 @@ func (b BitbucketAPI) GetCombinedPullRequestStatus(prNumber int) (string, error) } } - var allSuccess = true + allSuccess := true for _, status := range latestStatusByKey { if status.State != "SUCCESSFUL" { allSuccess = false @@ -382,7 +379,6 @@ func (b BitbucketAPI) GetCombinedPullRequestStatus(prNumber int) (string, error) } return "pending", nil - } func (b BitbucketAPI) MergePullRequest(prNumber int, mergeStrategy string) error { @@ -427,7 +423,6 @@ func (b BitbucketAPI) IsMergeable(prNumber int) (bool, error) { } func (b BitbucketAPI) IsMerged(prNumber int) (bool, error) { - url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber) resp, err := b.sendRequest("GET", url, nil) @@ -506,14 +501,14 @@ func (b BitbucketAPI) GetBranchName(prNumber int) (string, string, error) { return pullRequest.Source.Branch.Name, "", nil } -func (svc BitbucketAPI) SetOutput(prNumber int, key string, value string) error { - //TODO implement me +func (svc BitbucketAPI) SetOutput(prNumber int, key, value string) error { + // TODO implement me return nil } // Implement the OrgService interface. -func (b BitbucketAPI) GetUserTeams(organisation string, user string) ([]string, error) { +func (b BitbucketAPI) GetUserTeams(organisation, user string) ([]string, error) { return nil, fmt.Errorf("not implemented") } @@ -641,12 +636,10 @@ func (b BitbucketAPI) TriggerPipeline(branch string, variables []interface{}) (s } return "", nil - } func FindImpactedProjectsInBitbucket(diggerConfig *configuration.DiggerConfig, prNumber int, prService ci.PullRequestService) ([]configuration.Project, error) { changedFiles, err := prService.GetChangedFiles(prNumber) - if err != nil { fmt.Printf("Error getting changed files: %v", err) return nil, err diff --git a/libs/ci/ci.go b/libs/ci/ci.go index 5a86c46b9..0fc24d0d8 100644 --- a/libs/ci/ci.go +++ b/libs/ci/ci.go @@ -6,15 +6,15 @@ type PullRequestService interface { GetChangedFiles(prNumber int) ([]string, error) PublishComment(prNumber int, comment string) (*Comment, error) ListIssues() ([]*Issue, error) - PublishIssue(title string, body string, labels *[]string) (int64, error) - UpdateIssue(ID int64, title string, body string) (int64, error) - EditComment(prNumber int, id string, comment string) error + PublishIssue(title, body string, labels *[]string) (int64, error) + UpdateIssue(ID int64, title, body string) (int64, error) + EditComment(prNumber int, id, comment string) error DeleteComment(id string) error - CreateCommentReaction(id string, reaction string) error + CreateCommentReaction(id, reaction string) error GetComments(prNumber int) ([]Comment, error) GetApprovals(prNumber int) ([]string, error) // SetStatus set status of specified pull/merge request, status could be: "pending", "failure", "success" - SetStatus(prNumber int, status string, statusContext string) error + SetStatus(prNumber int, status, statusContext string) error GetCombinedPullRequestStatus(prNumber int) (string, error) MergePullRequest(prNumber int, mergeStrategy string) error // IsMergeable is still open and ready to be merged @@ -24,11 +24,11 @@ type PullRequestService interface { // IsClosed closed without merging IsClosed(prNumber int) (bool, error) GetBranchName(prNumber int) (string, string, error) - SetOutput(prNumber int, key string, value string) error + SetOutput(prNumber int, key, value string) error } type OrgService interface { - GetUserTeams(organisation string, user string) ([]string, error) + GetUserTeams(organisation, user string) ([]string, error) } type Issue struct { diff --git a/libs/ci/generic/events.go b/libs/ci/generic/events.go index a1d585d9e..24ea244c8 100644 --- a/libs/ci/generic/events.go +++ b/libs/ci/generic/events.go @@ -2,14 +2,15 @@ package generic import ( "fmt" + "strings" + "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/digger_config" "github.com/diggerhq/digger/libs/scheduler" "github.com/dominikbraun/graph" - "strings" ) -func GetRunEnvVars(defaultBranch string, prBranch string, projectName string, projectDir string) map[string]string { +func GetRunEnvVars(defaultBranch, prBranch, projectName, projectDir string) map[string]string { return map[string]string{ "DEFAULT_BRANCH": defaultBranch, "PR_BRANCH": prBranch, @@ -21,7 +22,6 @@ func GetRunEnvVars(defaultBranch string, prBranch string, projectName string, pr func ProcessIssueCommentEvent(prNumber int, commentBody string, diggerConfig *digger_config.DiggerConfig, dependencyGraph graph.Graph[string, digger_config.Project], ciService ci.PullRequestService) ([]digger_config.Project, map[string]digger_config.ProjectToSourceMapping, *digger_config.Project, int, error) { var impactedProjects []digger_config.Project changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { return nil, nil, nil, 0, fmt.Errorf("could not get changed files") } @@ -97,7 +97,7 @@ func FindAllProjectsDependantOnImpactedProjects(impactedProjects []digger_config return impactedProjectsWithDependantProjects, nil } -func ConvertIssueCommentEventToJobs(repoFullName string, requestedBy string, prNumber int, commentBody string, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, workflows map[string]digger_config.Workflow, prBranchName string, defaultBranch string) ([]scheduler.Job, bool, error) { +func ConvertIssueCommentEventToJobs(repoFullName, requestedBy string, prNumber int, commentBody string, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, workflows map[string]digger_config.Workflow, prBranchName, defaultBranch string) ([]scheduler.Job, bool, error) { jobs := make([]scheduler.Job, 0) prBranch := prBranchName @@ -135,10 +135,9 @@ func ConvertIssueCommentEventToJobs(repoFullName string, requestedBy string, prN } return jobs, coversAllImpactedProjects, nil - } -func CreateJobsForProjects(projects []digger_config.Project, command string, event string, repoFullName string, requestedBy string, workflows map[string]digger_config.Workflow, issueNumber *int, commitSha *string, defaultBranch string, prBranch string, performEnvVarsInterpolations bool) ([]scheduler.Job, error) { +func CreateJobsForProjects(projects []digger_config.Project, command, event, repoFullName, requestedBy string, workflows map[string]digger_config.Workflow, issueNumber *int, commitSha *string, defaultBranch, prBranch string, performEnvVarsInterpolations bool) ([]scheduler.Job, error) { jobs := make([]scheduler.Job, 0) for _, project := range projects { diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index d7213e196..1e48647ac 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -19,12 +19,12 @@ import ( ) type GithubServiceProvider interface { - NewService(ghToken string, repoName string, owner string) (GithubService, error) + NewService(ghToken, repoName, owner string) (GithubService, error) } type GithubServiceProviderBasic struct{} -func (_ GithubServiceProviderBasic) NewService(ghToken string, repoName string, owner string) (GithubService, error) { +func (_ GithubServiceProviderBasic) NewService(ghToken, repoName, owner string) (GithubService, error) { client := github.NewClient(nil) if ghToken != "" { client = client.WithAuthToken(ghToken) @@ -43,7 +43,7 @@ type GithubService struct { Owner string } -func (svc GithubService) GetUserTeams(organisation string, user string) ([]string, error) { +func (svc GithubService) GetUserTeams(organisation, user string) ([]string, error) { teamsResponse, _, err := svc.Client.Teams.ListTeams(context.Background(), organisation, nil) if err != nil { return nil, fmt.Errorf("failed to list github teams: %v", err) @@ -86,7 +86,7 @@ func (svc GithubService) GetChangedFiles(prNumber int) ([]string, error) { return fileNames, nil } -func (svc GithubService) GetChangedFilesForCommit(owner string, repo string, commitID string) ([]string, error) { +func (svc GithubService) GetChangedFilesForCommit(owner, repo, commitID string) ([]string, error) { var fileNames []string opts := github.ListOptions{PerPage: 100} @@ -139,7 +139,7 @@ func (svc GithubService) ListIssues() ([]*ci.Issue, error) { return allIssues, nil } -func (svc GithubService) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (svc GithubService) PublishIssue(title, body string, labels *[]string) (int64, error) { githubissue, _, err := svc.Client.Issues.Create(context.Background(), svc.Owner, svc.RepoName, &github.IssueRequest{Title: &title, Body: &body, Labels: labels}) if err != nil { return 0, fmt.Errorf("could not publish issue: %v", err) @@ -147,7 +147,7 @@ func (svc GithubService) PublishIssue(title string, body string, labels *[]strin return *githubissue.ID, err } -func (svc GithubService) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc GithubService) UpdateIssue(ID int64, title, body string) (int64, error) { githubissue, _, err := svc.Client.Issues.Edit(context.Background(), svc.Owner, svc.RepoName, int(ID), &github.IssueRequest{Title: &title, Body: &body}) if err != nil { return 0, fmt.Errorf("could not edit issue: %v", err) @@ -191,7 +191,7 @@ func (svc GithubService) GetApprovals(prNumber int) ([]string, error) { return approvals, err } -func (svc GithubService) EditComment(prNumber int, id string, comment string) error { +func (svc GithubService) EditComment(prNumber int, id, comment string) error { commentId, err := strconv.ParseInt(id, 10, 64) if err != nil { return fmt.Errorf("could not convert id %v to i64: %v", id, err) @@ -211,16 +211,18 @@ func (svc GithubService) DeleteComment(id string) error { type GithubCommentReaction string -const GithubCommentPlusOneReaction GithubCommentReaction = "+1" -const GithubCommentMinusOneReaction GithubCommentReaction = "-1" -const GithubCommentLaughReaction GithubCommentReaction = "laugh" -const GithubCommentConfusedReaction GithubCommentReaction = "confused" -const GithubCommentHeartReaction GithubCommentReaction = "heart" -const GithubCommentHoorayReaction GithubCommentReaction = "hooray" -const GithubCommentRocketReaction GithubCommentReaction = "rocket" -const GithubCommentEyesReaction GithubCommentReaction = "eyes" +const ( + GithubCommentPlusOneReaction GithubCommentReaction = "+1" + GithubCommentMinusOneReaction GithubCommentReaction = "-1" + GithubCommentLaughReaction GithubCommentReaction = "laugh" + GithubCommentConfusedReaction GithubCommentReaction = "confused" + GithubCommentHeartReaction GithubCommentReaction = "heart" + GithubCommentHoorayReaction GithubCommentReaction = "hooray" + GithubCommentRocketReaction GithubCommentReaction = "rocket" + GithubCommentEyesReaction GithubCommentReaction = "eyes" +) -func (svc GithubService) CreateCommentReaction(id string, reaction string) error { +func (svc GithubService) CreateCommentReaction(id, reaction string) error { commentId, err := strconv.ParseInt(id, 10, 64) if err != nil { return fmt.Errorf("could not convert id %v to i64: %v", id, err) @@ -243,7 +245,7 @@ func (svc GithubService) IsPullRequest(PrNumber int) (bool, error) { return issue.IsPullRequest(), nil } -func (svc GithubService) SetStatus(prNumber int, status string, statusContext string) error { +func (svc GithubService) SetStatus(prNumber int, status, statusContext string) error { // we have to check if prNumber is an issue or not isPullRequest, err := svc.IsPullRequest(prNumber) if err != nil { @@ -392,12 +394,12 @@ func (svc GithubService) IsClosed(prNumber int) (bool, error) { return pr.GetState() == "closed", nil } -func (svc GithubService) SetOutput(prNumber int, key string, value string) error { +func (svc GithubService) SetOutput(prNumber int, key, value string) error { gout := os.Getenv("GITHUB_ENV") if gout == "" { return fmt.Errorf("GITHUB_ENV not set, could not set the output in digger step") } - f, err := os.OpenFile(gout, os.O_APPEND|os.O_WRONLY, 0644) + f, err := os.OpenFile(gout, os.O_APPEND|os.O_WRONLY, 0o644) if err != nil { return fmt.Errorf("could not open file for writing during digger step") } @@ -631,7 +633,6 @@ func ProcessGitHubEvent(ghEvent interface{}, diggerConfig *digger_config.DiggerC "action", *event.Action) changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { slog.Error("could not get changed files", "error", err, "prNumber", prNumber) return nil, nil, 0, fmt.Errorf("could not get changed files") @@ -649,7 +650,6 @@ func ProcessGitHubEvent(ghEvent interface{}, diggerConfig *digger_config.DiggerC "comment", *event.Comment.Body) changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { slog.Error("could not get changed files", "error", err, "prNumber", prNumber) return nil, nil, 0, fmt.Errorf("could not get changed files") @@ -702,7 +702,6 @@ func ProcessGitHubPullRequestEvent(payload *github.PullRequestEvent, diggerConfi "action", *payload.Action) changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { slog.Error("could not get changed files", "error", err, "prNumber", prNumber) return nil, nil, prNumber, fmt.Errorf("could not get changed files") diff --git a/libs/ci/github/github_test.go b/libs/ci/github/github_test.go index 238e7441a..08908b632 100644 --- a/libs/ci/github/github_test.go +++ b/libs/ci/github/github_test.go @@ -1,15 +1,15 @@ package github import ( - "github.com/diggerhq/digger/libs/ci/generic" "testing" + "github.com/diggerhq/digger/libs/ci/generic" + "github.com/diggerhq/digger/libs/digger_config" "github.com/stretchr/testify/assert" ) func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) { - projects := []digger_config.Project{ { Name: "a", @@ -57,7 +57,6 @@ func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) { } dependencyGraph, err := digger_config.CreateProjectDependencyGraph(projects) - if err != nil { t.Errorf("Error creating dependency graph: %v", err) } diff --git a/libs/ci/github/mocks.go b/libs/ci/github/mocks.go index 9d25ba119..bd0b5f7fc 100644 --- a/libs/ci/github/mocks.go +++ b/libs/ci/github/mocks.go @@ -2,15 +2,16 @@ package github import ( "fmt" - "github.com/diggerhq/digger/libs/ci" "strconv" + + "github.com/diggerhq/digger/libs/ci" ) type MockCiService struct { CommentsPerPr map[int][]*ci.Comment } -func (t MockCiService) GetUserTeams(organisation string, user string) ([]string, error) { +func (t MockCiService) GetUserTeams(organisation, user string) ([]string, error) { return nil, nil } @@ -21,8 +22,8 @@ func (t MockCiService) GetApprovals(prNumber int) ([]string, error) { func (t MockCiService) GetChangedFiles(prNumber int) ([]string, error) { return nil, nil } -func (t MockCiService) PublishComment(prNumber int, comment string) (*ci.Comment, error) { +func (t MockCiService) PublishComment(prNumber int, comment string) (*ci.Comment, error) { latestId := 0 for _, comments := range t.CommentsPerPr { @@ -43,15 +44,15 @@ func (t MockCiService) ListIssues() ([]*ci.Issue, error) { return nil, fmt.Errorf("implement me") } -func (t MockCiService) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (t MockCiService) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc MockCiService) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error { +func (t MockCiService) SetStatus(prNumber int, status, statusContext string) error { return nil } @@ -87,7 +88,7 @@ func (t MockCiService) GetComments(prNumber int) ([]ci.Comment, error) { return comments, nil } -func (t MockCiService) EditComment(prNumber int, id string, comment string) error { +func (t MockCiService) EditComment(prNumber int, id, comment string) error { for _, comments := range t.CommentsPerPr { for _, c := range comments { if c.Id == id { @@ -103,7 +104,7 @@ func (t MockCiService) DeleteComment(id string) error { return nil } -func (t MockCiService) CreateCommentReaction(id string, reaction string) error { +func (t MockCiService) CreateCommentReaction(id, reaction string) error { // TODO implement me return nil } @@ -112,7 +113,7 @@ func (t MockCiService) GetBranchName(prNumber int) (string, string, error) { return "", "", nil } -func (svc MockCiService) SetOutput(prNumber int, key string, value string) error { - //TODO implement me +func (svc MockCiService) SetOutput(prNumber int, key, value string) error { + // TODO implement me return nil } diff --git a/libs/ci/gitlab/gitlab.go b/libs/ci/gitlab/gitlab.go index 53f418f92..c78e8435b 100644 --- a/libs/ci/gitlab/gitlab.go +++ b/libs/ci/gitlab/gitlab.go @@ -114,7 +114,6 @@ func ProcessGitLabEvent(gitlabContext *GitLabContext, diggerConfig *digger_confi "mergeRequestId", *mergeRequestId) changedFiles, err := service.GetChangedFiles(*mergeRequestId) - if err != nil { slog.Error("could not get changed files", "error", err, "mergeRequestId", *mergeRequestId) return nil, nil, fmt.Errorf("could not get changed files") @@ -163,7 +162,6 @@ func (gitlabService GitLabService) GetChangedFiles(mergeRequestId int) ([]string "mergeRequestId", mergeRequestId) mergeRequestChanges, _, err := gitlabService.Client.MergeRequests.ListMergeRequestDiffs(*gitlabService.Context.ProjectId, mergeRequestId, opt) - if err != nil { slog.Error("error getting GitLab merge request diffs", "error", err, "mergeRequestId", mergeRequestId) return nil, fmt.Errorf("error getting gitlab's merge request: %v", err) @@ -179,7 +177,7 @@ func (gitlabService GitLabService) GetChangedFiles(mergeRequestId int) ([]string return fileNames, nil } -func (gitlabService GitLabService) GetUserTeams(organisation string, user string) ([]string, error) { +func (gitlabService GitLabService) GetUserTeams(organisation, user string) ([]string, error) { return make([]string, 0), nil } @@ -218,19 +216,19 @@ func (svc GitLabService) ListIssues() ([]*ci.Issue, error) { return nil, fmt.Errorf("implement me") } -func (svc GitLabService) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (svc GitLabService) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (svc GitLabService) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc GitLabService) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } // SetStatus GitLab implementation is using https://docs.gitlab.com/15.11/ee/api/status_checks.html (external status checks) // https://docs.gitlab.com/ee/user/project/merge_requests/status_checks.html#add-a-status-check-service // only supported by 'Ultimate' plan -func (gitlabService GitLabService) SetStatus(mergeRequestID int, status string, statusContext string) error { - //TODO implement me +func (gitlabService GitLabService) SetStatus(mergeRequestID int, status, statusContext string) error { + // TODO implement me slog.Debug("setting status (not implemented)", "mergeRequestID", mergeRequestID, "status", status, @@ -239,7 +237,7 @@ func (gitlabService GitLabService) SetStatus(mergeRequestID int, status string, } func (gitlabService GitLabService) GetCombinedPullRequestStatus(mergeRequestID int) (string, error) { - //TODO implement me + // TODO implement me return "success", nil } @@ -303,7 +301,7 @@ func (gitlabService GitLabService) IsMerged(mergeRequestIID int) (bool, error) { return false, nil } -func (gitlabService GitLabService) EditComment(prNumber int, id string, comment string) error { +func (gitlabService GitLabService) EditComment(prNumber int, id, comment string) error { discussionId := gitlabService.Context.DiscussionID projectId := *gitlabService.Context.ProjectId mergeRequestIID := *gitlabService.Context.MergeRequestIId @@ -333,13 +331,13 @@ func (gitlabService GitLabService) DeleteComment(id string) error { return nil } -func (gitlabService GitLabService) CreateCommentReaction(id string, reaction string) error { +func (gitlabService GitLabService) CreateCommentReaction(id, reaction string) error { // TODO implement me return nil } func (gitlabService GitLabService) GetComments(prNumber int) ([]ci.Comment, error) { - //TODO implement me + // TODO implement me return nil, nil } @@ -350,7 +348,7 @@ func (gitlabService GitLabService) GetApprovals(prNumber int) ([]string, error) } func (gitlabService GitLabService) GetBranchName(prNumber int) (string, string, error) { - //TODO implement me + // TODO implement me projectId := *gitlabService.Context.ProjectId slog.Debug("getting branch name", "prNumber", prNumber, "projectId", projectId) @@ -378,8 +376,8 @@ func (gitlabService GitLabService) CheckBranchExists(branchName string) (bool, e return true, nil } -func (svc GitLabService) SetOutput(prNumber int, key string, value string) error { - //TODO implement me +func (svc GitLabService) SetOutput(prNumber int, key, value string) error { + // TODO implement me return nil } diff --git a/libs/ci/gitlab/gitlab_test.go b/libs/ci/gitlab/gitlab_test.go index 90cb0d172..1f649666b 100644 --- a/libs/ci/gitlab/gitlab_test.go +++ b/libs/ci/gitlab/gitlab_test.go @@ -1,8 +1,9 @@ package gitlab import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestParseGitLabContext(t *testing.T) { diff --git a/libs/ci/gitlab/webhooks.go b/libs/ci/gitlab/webhooks.go index 778512101..dc3486bdc 100644 --- a/libs/ci/gitlab/webhooks.go +++ b/libs/ci/gitlab/webhooks.go @@ -2,6 +2,7 @@ package gitlab import ( "fmt" + "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/ci/generic" "github.com/diggerhq/digger/libs/digger_config" @@ -15,7 +16,6 @@ func ProcessGitlabPullRequestEvent(payload *gitlab.MergeEvent, diggerConfig *dig var prNumber int prNumber = payload.ObjectAttributes.IID changedFiles, err := ciService.GetChangedFiles(prNumber) - if err != nil { return nil, nil, prNumber, fmt.Errorf("could not get changed files") } diff --git a/libs/ci/mocks.go b/libs/ci/mocks.go index 1e01a3e19..49c3805e5 100644 --- a/libs/ci/mocks.go +++ b/libs/ci/mocks.go @@ -8,13 +8,14 @@ type MockPullRequestManager struct { Approvals []string } -func (t MockPullRequestManager) GetUserTeams(organisation string, user string) ([]string, error) { +func (t MockPullRequestManager) GetUserTeams(organisation, user string) ([]string, error) { return t.Teams, nil } func (t MockPullRequestManager) GetChangedFiles(prNumber int) ([]string, error) { return t.ChangedFiles, nil } + func (t MockPullRequestManager) PublishComment(prNumber int, comment string) (*Comment, error) { return nil, nil } @@ -23,15 +24,15 @@ func (t MockPullRequestManager) ListIssues() ([]*Issue, error) { return nil, nil } -func (t MockPullRequestManager) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (t MockPullRequestManager) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, nil } -func (t MockPullRequestManager) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (t MockPullRequestManager) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (t MockPullRequestManager) SetStatus(prNumber int, status string, statusContext string) error { +func (t MockPullRequestManager) SetStatus(prNumber int, status, statusContext string) error { return nil } @@ -67,7 +68,7 @@ func (t MockPullRequestManager) GetComments(prNumber int) ([]Comment, error) { return []Comment{}, nil } -func (t MockPullRequestManager) EditComment(prNumber int, id string, comment string) error { +func (t MockPullRequestManager) EditComment(prNumber int, id, comment string) error { return nil } @@ -75,7 +76,7 @@ func (t MockPullRequestManager) DeleteComment(id string) error { return nil } -func (t MockPullRequestManager) CreateCommentReaction(id string, reaction string) error { +func (t MockPullRequestManager) CreateCommentReaction(id, reaction string) error { return nil } @@ -83,6 +84,6 @@ func (t MockPullRequestManager) GetBranchName(prNumber int) (string, string, err return "", "", nil } -func (t MockPullRequestManager) SetOutput(prNumber int, key string, value string) error { +func (t MockPullRequestManager) SetOutput(prNumber int, key, value string) error { return nil } diff --git a/libs/comment_utils/reporting/core.go b/libs/comment_utils/reporting/core.go index adfdbdce1..3ff91ef36 100644 --- a/libs/comment_utils/reporting/core.go +++ b/libs/comment_utils/reporting/core.go @@ -1,7 +1,7 @@ package reporting type Reporter interface { - Report(report string, reportFormatting func(report string) string) (commentId string, commentUrl string, error error) + Report(report string, reportFormatting func(report string) string) (commentId, commentUrl string, error error) Flush() (string, string, error) Suppress() error SupportsMarkdown() bool diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index df43b79f9..35d22cdb6 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -105,7 +105,7 @@ func (reporter StdOutReporter) Suppress() error { } type ReportStrategy interface { - Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error) + Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId, commentUrl string, error error) } type CommentPerRunStrategy struct { @@ -174,7 +174,6 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, } err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) - if err != nil { slog.Error("error editing comment", "error", err, "commentId", commentIdForThisRun, "prNumber", PrNumber) return "", "", fmt.Errorf("error editing comment: %v", err) diff --git a/libs/comment_utils/reporting/reporting_test.go b/libs/comment_utils/reporting/reporting_test.go index 5a20899e1..efb15a056 100644 --- a/libs/comment_utils/reporting/reporting_test.go +++ b/libs/comment_utils/reporting/reporting_test.go @@ -2,15 +2,16 @@ package reporting import ( "fmt" - "github.com/diggerhq/digger/libs/ci" "strconv" + + "github.com/diggerhq/digger/libs/ci" ) type MockCiService struct { CommentsPerPr map[int][]*ci.Comment } -func (t MockCiService) GetUserTeams(organisation string, user string) ([]string, error) { +func (t MockCiService) GetUserTeams(organisation, user string) ([]string, error) { return nil, nil } @@ -21,8 +22,8 @@ func (t MockCiService) GetApprovals(prNumber int) ([]string, error) { func (t MockCiService) GetChangedFiles(prNumber int) ([]string, error) { return nil, nil } -func (t MockCiService) PublishComment(prNumber int, comment string) (*ci.Comment, error) { +func (t MockCiService) PublishComment(prNumber int, comment string) (*ci.Comment, error) { latestId := 0 for _, comments := range t.CommentsPerPr { @@ -43,15 +44,15 @@ func (t MockCiService) ListIssues() ([]*ci.Issue, error) { return nil, fmt.Errorf("implement me") } -func (t MockCiService) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (t MockCiService) PublishIssue(title, body string, labels *[]string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (svc MockCiService) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error { +func (t MockCiService) SetStatus(prNumber int, status, statusContext string) error { return nil } @@ -87,7 +88,7 @@ func (t MockCiService) GetComments(prNumber int) ([]ci.Comment, error) { return comments, nil } -func (t MockCiService) EditComment(prNumber int, id string, comment string) error { +func (t MockCiService) EditComment(prNumber int, id, comment string) error { for _, comments := range t.CommentsPerPr { for _, c := range comments { if c.Id == id { @@ -103,7 +104,7 @@ func (svc MockCiService) DeleteComment(id string) error { return nil } -func (svc MockCiService) CreateCommentReaction(id string, reaction string) error { +func (svc MockCiService) CreateCommentReaction(id, reaction string) error { // TODO implement me return nil } @@ -112,6 +113,6 @@ func (svc MockCiService) GetBranchName(prNumber int) (string, string, error) { return "", "", nil } -func (svc MockCiService) SetOutput(prNumber int, key string, value string) error { +func (svc MockCiService) SetOutput(prNumber int, key, value string) error { return nil } diff --git a/libs/comment_utils/summary/provider.go b/libs/comment_utils/summary/provider.go index 1d4b8d452..d9fe2e9b5 100644 --- a/libs/comment_utils/summary/provider.go +++ b/libs/comment_utils/summary/provider.go @@ -2,6 +2,7 @@ package comment_updater import ( "fmt" + "github.com/diggerhq/digger/libs/digger_config" ) diff --git a/libs/comment_utils/summary/updater.go b/libs/comment_utils/summary/updater.go index e2cc88b50..b871c1f28 100644 --- a/libs/comment_utils/summary/updater.go +++ b/libs/comment_utils/summary/updater.go @@ -14,8 +14,7 @@ type CommentUpdater interface { UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService ci.PullRequestService, prCommentId string) error } -type BasicCommentUpdater struct { -} +type BasicCommentUpdater struct{} func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService ci.PullRequestService, prCommentId string) error { jobSpecs, err := scheduler.GetJobSpecs(jobs) @@ -41,13 +40,13 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum for i, job := range jobs { jobSpec := jobSpecs[i] prCommentUrl := job.PRCommentUrl - + // Safe handling of WorkflowRunUrl pointer workflowUrl := "#" if job.WorkflowRunUrl != nil { workflowUrl = *job.WorkflowRunUrl } - + message = message + fmt.Sprintf("|%v **%v** |%v | %v | %v | %v | %v|\n", job.Status.ToEmoji(), jobSpec.ProjectName, @@ -89,8 +88,7 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum return nil } -type NoopCommentUpdater struct { -} +type NoopCommentUpdater struct{} func (b NoopCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService ci.PullRequestService, prCommentId string) error { slog.Debug("noop comment updater called, no action taken", diff --git a/libs/crypto/decrypt.go b/libs/crypto/decrypt.go index 195e38cab..206b986a7 100644 --- a/libs/crypto/decrypt.go +++ b/libs/crypto/decrypt.go @@ -10,7 +10,7 @@ import ( "fmt" ) -func DecryptValueUsingPrivateKey(encryptedDataBase64 string, privateKeyPEM string) (string, error) { +func DecryptValueUsingPrivateKey(encryptedDataBase64, privateKeyPEM string) (string, error) { // Decode the Base64-encoded encrypted data encryptedData, err := base64.StdEncoding.DecodeString(encryptedDataBase64) if err != nil { diff --git a/libs/digger_config/config.go b/libs/digger_config/config.go index ff3715f83..e59c7fc70 100644 --- a/libs/digger_config/config.go +++ b/libs/digger_config/config.go @@ -1,13 +1,17 @@ package digger_config -const CommentRenderModeBasic = "basic" -const CommentRenderModeGroupByModule = "group_by_module" +const ( + CommentRenderModeBasic = "basic" + CommentRenderModeGroupByModule = "group_by_module" +) type AutomergeStrategy string -const AutomergeStrategySquash AutomergeStrategy = "squash" -const AutomergeStrategyMerge AutomergeStrategy = "merge" -const AutomergeStrategyRebase AutomergeStrategy = "rebase" +const ( + AutomergeStrategySquash AutomergeStrategy = "squash" + AutomergeStrategyMerge AutomergeStrategy = "merge" + AutomergeStrategyRebase AutomergeStrategy = "rebase" +) type DiggerConfig struct { ApplyAfterMerge bool diff --git a/libs/digger_config/converters.go b/libs/digger_config/converters.go index eedb4a1db..4c9900ac0 100644 --- a/libs/digger_config/converters.go +++ b/libs/digger_config/converters.go @@ -183,7 +183,6 @@ func copyReporterConfig(r *ReportingConfigYaml) ReporterConfig { return ReporterConfig{ AiSummary: r.AiSummary, } - } func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml) (*DiggerConfig, graph.Graph[string, Project], error) { @@ -363,7 +362,6 @@ func CreateProjectDependencyGraph(projects []Project) (graph.Graph[string, Proje return nil, fmt.Errorf("project '%s' does not exist", dependency) } err := g.AddVertex(dependencyProject) - if err != nil { return nil, err } diff --git a/libs/digger_config/digger_config.go b/libs/digger_config/digger_config.go index 4f6d31a5f..6f7467ad1 100644 --- a/libs/digger_config/digger_config.go +++ b/libs/digger_config/digger_config.go @@ -22,14 +22,11 @@ type DirWalker interface { GetDirs(workingDir string, config DiggerConfigYaml) ([]string, error) } -type FileSystemTopLevelTerraformDirWalker struct { -} +type FileSystemTopLevelTerraformDirWalker struct{} -type FileSystemTerragruntDirWalker struct { -} +type FileSystemTerragruntDirWalker struct{} -type FileSystemModuleDirWalker struct { -} +type FileSystemModuleDirWalker struct{} func ReadDiggerYmlFileContents(dir string) (string, error) { var diggerYmlBytes []byte @@ -77,7 +74,7 @@ func CheckOrCreateDiggerFile(dir string) error { return nil } -func GetFilesWithExtension(workingDir string, ext string) ([]string, error) { +func GetFilesWithExtension(workingDir, ext string) ([]string, error) { var files []string listOfFiles, err := os.ReadDir(workingDir) if err != nil { @@ -243,7 +240,7 @@ func LoadDiggerConfig(workingDir string, generateProjects bool, changedFiles []s return config, configYaml, projectDependencyGraph, nil } -func LoadDiggerConfigFromString(yamlString string, terraformDir string) (*DiggerConfig, *DiggerConfigYaml, graph.Graph[string, Project], error) { +func LoadDiggerConfigFromString(yamlString, terraformDir string) (*DiggerConfig, *DiggerConfigYaml, graph.Graph[string, Project], error) { slog.Info("loading digger configuration from string", "terraformDir", terraformDir) config := &DiggerConfig{} @@ -344,11 +341,10 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string, return err } } else if config.GenerateProjectsConfig != nil { - var dirWalker = &FileSystemTopLevelTerraformDirWalker{} + dirWalker := &FileSystemTopLevelTerraformDirWalker{} slog.Info("finding terraform directories for project generation", "terraformDir", terraformDir) dirs, err := dirWalker.GetDirs(terraformDir, config) - if err != nil { slog.Error("error walking through directories", "error", err, "terraformDir", terraformDir) return fmt.Errorf("error while walking through directories: %v", err) @@ -699,7 +695,7 @@ func ValidateDiggerConfig(config *DiggerConfig) error { return nil } -func hydrateDiggerConfigYamlWithTerragrunt(configYaml *DiggerConfigYaml, parsingConfig TerragruntParsingConfig, workingDir string, blockName string) error { +func hydrateDiggerConfigYamlWithTerragrunt(configYaml *DiggerConfigYaml, parsingConfig TerragruntParsingConfig, workingDir, blockName string) error { slog.Info("hydrating config with terragrunt projects", "workingDir", workingDir, "filterPath", parsingConfig.FilterPath) @@ -792,7 +788,6 @@ func hydrateDiggerConfigYamlWithTerragrunt(configYaml *DiggerConfigYaml, parsing // normalize paths projectDir := path.Join(pathPrefix, atlantisProject.Dir) atlantisProject.Autoplan.WhenModified, err = GetPatternsRelativeToRepo(projectDir, atlantisProject.Autoplan.WhenModified) - if err != nil { slog.Error("could not normalize patterns", "error", err, diff --git a/libs/digger_config/digger_config_test.go b/libs/digger_config/digger_config_test.go index b9efe32a7..24428ba79 100644 --- a/libs/digger_config/digger_config_test.go +++ b/libs/digger_config/digger_config_test.go @@ -67,7 +67,6 @@ func TestDiggerConfigWhenCustomFileName(t *testing.T) { assert.Equal(t, configPath, path.Join(tempDir, "digger-custom.yml")) os.Unsetenv("DIGGER_FILENAME") - } func TestDiggerConfigWhenOnlyYamlExists(t *testing.T) { @@ -578,7 +577,6 @@ workflows: assert.NotNil(t, workflow) assert.NotNil(t, workflow.Plan) assert.NotNil(t, workflow.Apply) - } func TestDiggerConfigMissingProjectsWorkflow(t *testing.T) { @@ -602,7 +600,6 @@ workflows: _, _, _, err := LoadDiggerConfig(tempDir, true, nil) assert.Equal(t, "failed to find workflow digger_config 'my_custom_workflow' for project 'my-first-app'", err.Error()) - } func TestDiggerConfigWithEmptyInitBlock(t *testing.T) { @@ -664,7 +661,7 @@ func TestDiggerConfigDependencyGraph(t *testing.T) { assert.NoError(t, err, "expected error to be nil") - orderedProjects, _ := graph.StableTopologicalSort(g, func(s string, s2 string) bool { + orderedProjects, _ := graph.StableTopologicalSort(g, func(s, s2 string) bool { return s < s2 }) @@ -744,7 +741,7 @@ func TestDiggerConfigDependencyGraph2(t *testing.T) { assert.NoError(t, err, "expected error to be nil") - orderedProjects, _ := graph.StableTopologicalSort(g, func(s string, s2 string) bool { + orderedProjects, _ := graph.StableTopologicalSort(g, func(s, s2 string) bool { return s > s2 }) @@ -887,7 +884,7 @@ func deleteTempDir(name string) { } } -func createFile(filepath string, content string) func() { +func createFile(filepath, content string) func() { f, err := os.Create(filepath) if err != nil { panic(err) @@ -906,7 +903,7 @@ func createFile(filepath string, content string) func() { } } -func createAndCloseFile(filepath string, content string) error { +func createAndCloseFile(filepath, content string) error { f, err := os.Create(filepath) if err != nil { return err @@ -1308,7 +1305,7 @@ projects: func TestGetModifiedProjectsReturnsCorrectSourceMappingWithDotFile(t *testing.T) { changedFiles := []string{"prod/main.tf", "dev/test/main.tf"} projects := []Project{ - Project{ + { Name: "dev", Dir: ".", }, @@ -1316,7 +1313,7 @@ func TestGetModifiedProjectsReturnsCorrectSourceMappingWithDotFile(t *testing.T) c := DiggerConfig{ Projects: projects, } - //expectedImpactingLocations := map[string]ProjectToSourceMapping{} + // expectedImpactingLocations := map[string]ProjectToSourceMapping{} impactedProjects, _ := c.GetModifiedProjects(changedFiles) assert.Equal(t, 0, len(impactedProjects)) @@ -1325,12 +1322,12 @@ func TestGetModifiedProjectsReturnsCorrectSourceMappingWithDotFile(t *testing.T) func TestGetModifiedProjectsReturnsCorrectSourceMapping(t *testing.T) { changedFiles := []string{"modules/bucket/main.tf", "dev/main.tf"} projects := []Project{ - Project{ + { Name: "dev", Dir: "dev", IncludePatterns: []string{"modules/**"}, }, - Project{ + { Name: "prod", Dir: "prod", IncludePatterns: []string{"modules/**"}, @@ -1351,7 +1348,6 @@ func TestGetModifiedProjectsReturnsCorrectSourceMapping(t *testing.T) { assert.Equal(t, 1, len(projectSourceMapping["prod"].ImpactingLocations)) assert.Equal(t, expectedImpactingLocations["dev"].ImpactingLocations, projectSourceMapping["dev"].ImpactingLocations) assert.Equal(t, expectedImpactingLocations["prod"].ImpactingLocations, projectSourceMapping["prod"].ImpactingLocations) - } func TestCognitoTokenSetFromMinConfig(t *testing.T) { diff --git a/libs/digger_config/terragrunt/atlantis/generate.go b/libs/digger_config/terragrunt/atlantis/generate.go index e55804333..a5302e3ba 100644 --- a/libs/digger_config/terragrunt/atlantis/generate.go +++ b/libs/digger_config/terragrunt/atlantis/generate.go @@ -3,8 +3,12 @@ package atlantis import ( "context" "log/slog" + "os" + "path/filepath" "regexp" "sort" + "strings" + "sync" "github.com/gruntwork-io/terragrunt/cli/commands/terraform" "github.com/gruntwork-io/terragrunt/config" @@ -14,11 +18,6 @@ import ( "github.com/hashicorp/go-getter" "golang.org/x/sync/singleflight" - - "os" - "path/filepath" - "strings" - "sync" ) // Parse env vars into a map @@ -36,7 +35,7 @@ func getEnvs() map[string]string { // Terragrunt imports can be relative or absolute // This makes relative paths absolute -func makePathAbsolute(gitRoot string, path string, parentPath string) string { +func makePathAbsolute(gitRoot, path, parentPath string) string { if strings.HasPrefix(path, filepath.ToSlash(gitRoot)) { return path } @@ -118,7 +117,7 @@ func sliceUnion(a, b []string) []string { } // Parses the terragrunt digger_config at `path` to find all modules it depends on -func getDependencies(ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies bool, path string, terragruntOptions *options.TerragruntOptions) ([]string, error) { +func getDependencies(ignoreParentTerragrunt, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies bool, path string, terragruntOptions *options.TerragruntOptions) ([]string, error) { res, err, _ := requestGroup.Do(path, func() (interface{}, error) { // Check if this path has already been computed cachedResult, ok := getDependenciesCache.get(path) @@ -312,7 +311,7 @@ func getDependencies(ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, g } } -func createBaseProject(dir string, workflow string, terraformVersion string, applyRequirements *[]string, autoPlan bool, dependencies []string, createProjectName bool, createWorkspace bool) *AtlantisProject { +func createBaseProject(dir, workflow, terraformVersion string, applyRequirements *[]string, autoPlan bool, dependencies []string, createProjectName, createWorkspace bool) *AtlantisProject { project := &AtlantisProject{ Dir: filepath.ToSlash(dir), Workflow: workflow, @@ -338,7 +337,7 @@ func createBaseProject(dir string, workflow string, terraformVersion string, app } // Creates an AtlantisProject for a directory -func createProject(ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies bool, defaultWorkflow string, defaultApplyRequirements []string, autoPlan bool, defaultTerraformVersion string, createProjectName bool, createWorkspace bool, sourcePath string, triggerProjectsFromDirOnly bool) (*AtlantisProject, []string, error) { +func createProject(ignoreParentTerragrunt, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies bool, defaultWorkflow string, defaultApplyRequirements []string, autoPlan bool, defaultTerraformVersion string, createProjectName, createWorkspace bool, sourcePath string, triggerProjectsFromDirOnly bool) (*AtlantisProject, []string, error) { options, err := options.NewTerragruntOptionsWithConfigPath(sourcePath) var potentialProjectDependencies []string @@ -477,7 +476,7 @@ func projectNameFromDir(projectDir string) string { return projectName } -func createHclProject(defaultWorkflow string, defaultApplyRequirements []string, autoplan bool, useProjectMarkers bool, defaultTerraformVersion string, ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies bool, createProjectName bool, createWorkspace bool, sourcePaths []string, workingDir string, projectHcl string) (*AtlantisProject, error) { +func createHclProject(defaultWorkflow string, defaultApplyRequirements []string, autoplan, useProjectMarkers bool, defaultTerraformVersion string, ignoreParentTerragrunt, ignoreDependencyBlocks bool, gitRoot string, cascadeDependencies, createProjectName, createWorkspace bool, sourcePaths []string, workingDir, projectHcl string) (*AtlantisProject, error) { var projectHclDependencies []string var childDependencies []string workflow := defaultWorkflow @@ -688,7 +687,6 @@ func getAllTerragruntProjectHclFiles(projectHclFiles []string, gitRoot string) m return nil }) - if err != nil { slog.Error("failed to walk directory tree", "error", err, "gitRoot", gitRoot) panic(err) @@ -706,7 +704,7 @@ func getAllTerragruntProjectHclFiles(projectHclFiles []string, gitRoot string) m return uniqueHclFileAbsPaths } -func Parse(gitRoot string, projectHclFiles []string, createHclProjectExternalChilds bool, autoMerge bool, parallel bool, filterPath string, createHclProjectChilds bool, ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, cascadeDependencies bool, defaultWorkflow string, defaultApplyRequirements []string, autoPlan bool, defaultTerraformVersion string, createProjectName bool, createWorkspace bool, preserveProjects bool, useProjectMarkers bool, executionOrderGroups bool, triggerProjectsFromDirOnly bool) (*AtlantisConfig, map[string][]string, error) { +func Parse(gitRoot string, projectHclFiles []string, createHclProjectExternalChilds, autoMerge, parallel bool, filterPath string, createHclProjectChilds, ignoreParentTerragrunt, ignoreDependencyBlocks, cascadeDependencies bool, defaultWorkflow string, defaultApplyRequirements []string, autoPlan bool, defaultTerraformVersion string, createProjectName, createWorkspace, preserveProjects, useProjectMarkers, executionOrderGroups, triggerProjectsFromDirOnly bool) (*AtlantisConfig, map[string][]string, error) { // Ensure the gitRoot has a trailing slash and is an absolute path absoluteGitRoot, err := filepath.Abs(gitRoot) if err != nil { diff --git a/libs/digger_config/terragrunt/atlantis/parse_hcl.go b/libs/digger_config/terragrunt/atlantis/parse_hcl.go index b7aaa8035..f950cee5b 100644 --- a/libs/digger_config/terragrunt/atlantis/parse_hcl.go +++ b/libs/digger_config/terragrunt/atlantis/parse_hcl.go @@ -1,6 +1,8 @@ package atlantis import ( + "path/filepath" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" "github.com/gruntwork-io/terragrunt/options" @@ -9,7 +11,6 @@ import ( "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclparse" "github.com/hashicorp/hcl/v2/hclwrite" - "path/filepath" ) const bareIncludeKey = "" diff --git a/libs/digger_config/terragrunt/atlantis/parse_locals.go b/libs/digger_config/terragrunt/atlantis/parse_locals.go index d22fef2cb..804c13f5e 100644 --- a/libs/digger_config/terragrunt/atlantis/parse_locals.go +++ b/libs/digger_config/terragrunt/atlantis/parse_locals.go @@ -5,6 +5,8 @@ package atlantis // parses the `locals` blocks and evaluates their contents. import ( + "path/filepath" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" "github.com/gruntwork-io/terragrunt/options" @@ -12,8 +14,6 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclparse" "github.com/zclconf/go-cty/cty" - - "path/filepath" ) // ResolvedLocals are the parsed result of local values this module cares about @@ -41,7 +41,7 @@ type ResolvedLocals struct { } // parseHcl uses the HCL2 parser to parse the given string into an HCL file body. -func parseHcl(parser *hclparse.Parser, hcl string, filename string) (file *hcl.File, err error) { +func parseHcl(parser *hclparse.Parser, hcl, filename string) (file *hcl.File, err error) { // The HCL2 parser and especially cty conversions will panic in many types of errors, so we have to recover from // those panics here and convert them to normal errors defer func() { @@ -68,7 +68,7 @@ func parseHcl(parser *hclparse.Parser, hcl string, filename string) (file *hcl.F } // Merges in values from a child into a parent set of `local` values -func mergeResolvedLocals(parent ResolvedLocals, child ResolvedLocals) ResolvedLocals { +func mergeResolvedLocals(parent, child ResolvedLocals) ResolvedLocals { if child.AtlantisWorkflow != "" { parent.AtlantisWorkflow = child.AtlantisWorkflow } diff --git a/libs/digger_config/terragrunt/atlantis/parse_tf.go b/libs/digger_config/terragrunt/atlantis/parse_tf.go index 9d5bf310d..51695f5d1 100644 --- a/libs/digger_config/terragrunt/atlantis/parse_tf.go +++ b/libs/digger_config/terragrunt/atlantis/parse_tf.go @@ -2,9 +2,10 @@ package atlantis import ( "errors" + "strings" + "github.com/gruntwork-io/terragrunt/util" "github.com/hashicorp/terraform-config-inspect/tfconfig" - "strings" ) var localModuleSourcePrefixes = []string{ @@ -21,7 +22,7 @@ func parseTerraformLocalModuleSource(path string) ([]string, error) { return nil, errors.New(diags.Error()) } - var sourceMap = map[string]bool{} + sourceMap := map[string]bool{} for _, mc := range module.ModuleCalls { if isLocalTerraformModuleSource(mc.Source) { modulePath := util.JoinPath(path, mc.Source) @@ -44,7 +45,7 @@ func parseTerraformLocalModuleSource(path string) ([]string, error) { } } - var sources = []string{} + sources := []string{} for source := range sourceMap { sources = append(sources, source) } diff --git a/libs/digger_config/utils.go b/libs/digger_config/utils.go index 3ca28b841..4194ef51a 100644 --- a/libs/digger_config/utils.go +++ b/libs/digger_config/utils.go @@ -25,7 +25,7 @@ func NormalizeFileName(fileName string) string { return res } -func MatchIncludeExcludePatternsToFile(fileToMatch string, includePatterns []string, excludePatterns []string) bool { +func MatchIncludeExcludePatternsToFile(fileToMatch string, includePatterns, excludePatterns []string) bool { fileToMatch = NormalizeFileName(fileToMatch) for i := range includePatterns { includePatterns[i] = NormalizeFileName(includePatterns[i]) diff --git a/libs/digger_config/utils_test.go b/libs/digger_config/utils_test.go index 7c5d857d6..4fe2e85ac 100644 --- a/libs/digger_config/utils_test.go +++ b/libs/digger_config/utils_test.go @@ -1,8 +1,9 @@ package digger_config import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestMatchIncludeExcludePatternsToFile(t *testing.T) { @@ -25,7 +26,6 @@ func TestMatchIncludeExcludePatternsToFile(t *testing.T) { var ep []string result = MatchIncludeExcludePatternsToFile("/projects/dev/test1", ip, ep) assert.Equal(t, false, result) - } func TestGetPatternsRelativeToRepo(t *testing.T) { @@ -43,5 +43,4 @@ func TestGetPatternsRelativeToRepo(t *testing.T) { includePatterns = []string{"*.hcl"} res, _ = GetPatternsRelativeToRepo(projectDir, includePatterns) assert.Equal(t, "myProject/terraform/environments/devel/*.hcl", res[0]) - } diff --git a/libs/digger_config/yaml.go b/libs/digger_config/yaml.go index 155d19756..31fdaf204 100644 --- a/libs/digger_config/yaml.go +++ b/libs/digger_config/yaml.go @@ -167,7 +167,7 @@ type TerragruntParsingConfig struct { PreserveProjects bool `yaml:"preserveProjects"` CascadeDependencies *bool `yaml:"cascadeDependencies,omitempty"` DefaultApplyRequirements []string `yaml:"defaultApplyRequirements"` - //NumExecutors int64 `yaml:"numExecutors"` + // NumExecutors int64 `yaml:"numExecutors"` ProjectHclFiles []string `yaml:"projectHclFiles"` CreateHclProjectChilds bool `yaml:"createHclProjectChilds"` CreateHclProjectExternalChilds *bool `yaml:"createHclProjectExternalChilds,omitempty"` @@ -253,7 +253,6 @@ func validateWorkflowConfigurationYaml(config *WorkflowConfigurationYaml) error } func (s *StepYaml) UnmarshalYAML(value *yaml.Node) error { - if value.Kind == yaml.ScalarNode { return value.Decode(&s.Action) } diff --git a/libs/execution/execution.go b/libs/execution/execution.go index 4583a2153..06c7a522a 100644 --- a/libs/execution/execution.go +++ b/libs/execution/execution.go @@ -108,8 +108,10 @@ type DiggerExecutor struct { type DiggerOperationType string -var DiggerOparationTypePlan DiggerOperationType = "plan" -var DiggerOparationTypeApply DiggerOperationType = "apply" +var ( + DiggerOparationTypePlan DiggerOperationType = "plan" + DiggerOparationTypeApply DiggerOperationType = "apply" +) type DiggerExecutorResult struct { OperationType DiggerOperationType @@ -161,7 +163,6 @@ func (d ProjectPathProvider) StoredPlanFilePath() string { } else { return strings.ReplaceAll(d.ProjectNamespace, "/", "-") + "-" + d.ProjectName + ".tfplan" } - } func (d ProjectPathProvider) LocalPlanFilePath() string { @@ -399,7 +400,7 @@ func reportApplyError(r reporting.Reporter, err error) { } } -func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOutput string) { +func reportTerraformApplyOutput(r reporting.Reporter, projectId, applyOutput string) { var formatter func(string) string if r.SupportsMarkdown() { formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply output", false) @@ -456,7 +457,6 @@ func reportAdditionalOutput(r reporting.Reporter, projectId string) { } func (d DiggerExecutor) Destroy() (bool, error) { - destroySteps := []configuration.Step{ { Action: "init", @@ -483,7 +483,7 @@ func (d DiggerExecutor) Destroy() (bool, error) { return true, nil } -func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string, stderr string, regexStr *string) string { +func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout, stderr string, regexStr *string) string { var errorStr string // removes output of terraform -version command that terraform-exec executes on every run @@ -534,11 +534,11 @@ func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string, return stdout[startPos:endPos] } -func cleanupTerraformApply(nonEmptyPlan bool, planError error, stdout string, stderr string) string { +func cleanupTerraformApply(nonEmptyPlan bool, planError error, stdout, stderr string) string { return cleanupTerraformOutput(nonEmptyPlan, planError, stdout, stderr, nil) } -func cleanupTerraformPlan(nonEmptyPlan bool, planError error, stdout string, stderr string) string { +func cleanupTerraformPlan(nonEmptyPlan bool, planError error, stdout, stderr string) string { regex := `───────────.+` return cleanupTerraformOutput(nonEmptyPlan, planError, stdout, stderr, ®ex) } diff --git a/libs/execution/execution_test.go b/libs/execution/execution_test.go index 096f5c0ed..2936bbdf3 100644 --- a/libs/execution/execution_test.go +++ b/libs/execution/execution_test.go @@ -1,9 +1,10 @@ package execution import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestCorrectCleanUpWithoutRegexDoesNotProduceException(t *testing.T) { diff --git a/libs/execution/io_writer_filtered_test.go b/libs/execution/io_writer_filtered_test.go index 2ecc87d7f..75a7aa4d8 100644 --- a/libs/execution/io_writer_filtered_test.go +++ b/libs/execution/io_writer_filtered_test.go @@ -2,9 +2,10 @@ package execution import ( "bytes" - "github.com/stretchr/testify/assert" "regexp" "testing" + + "github.com/stretchr/testify/assert" ) func TestNewFilteringWriter(t *testing.T) { @@ -24,5 +25,4 @@ func TestNewFilteringWriter(t *testing.T) { writer.Write([]byte("output")) assert.Equal(t, "output", buf.String()) - } diff --git a/libs/execution/runners.go b/libs/execution/runners.go index 3e3fc272a..43860a50a 100644 --- a/libs/execution/runners.go +++ b/libs/execution/runners.go @@ -10,13 +10,12 @@ import ( ) type CommandRun interface { - Run(workingDir string, shell string, commands []string, envs map[string]string) (string, string, error) + Run(workingDir, shell string, commands []string, envs map[string]string) (string, string, error) } -type CommandRunner struct { -} +type CommandRunner struct{} -func (c CommandRunner) Run(workingDir string, shell string, commands []string, envs map[string]string) (string, string, error) { +func (c CommandRunner) Run(workingDir, shell string, commands []string, envs map[string]string) (string, string, error) { var args []string if shell == "" { shell = "bash" @@ -52,7 +51,6 @@ func (c CommandRunner) Run(workingDir string, shell string, commands []string, e cmd.Stdout = mwout cmd.Stderr = mwerr err = cmd.Run() - if err != nil { return stdout.String(), stderr.String(), fmt.Errorf("error: %v", err) } diff --git a/libs/execution/terragrunt.go b/libs/execution/terragrunt.go index 582c5b4b5..d17ee3eaa 100644 --- a/libs/execution/terragrunt.go +++ b/libs/execution/terragrunt.go @@ -72,7 +72,7 @@ func (terragrunt Terragrunt) Show(params []string, envs map[string]string, planA return stdout, stderr, err } -func (terragrunt Terragrunt) runTerragruntCommand(command string, printOutputToStdout bool, envs map[string]string, filterRegex *string, arg ...string) (stdOut string, stdErr string, exitCode int, err error) { +func (terragrunt Terragrunt) runTerragruntCommand(command string, printOutputToStdout bool, envs map[string]string, filterRegex *string, arg ...string) (stdOut, stdErr string, exitCode int, err error) { args := []string{command} args = append(args, arg...) @@ -130,7 +130,6 @@ func (terragrunt Terragrunt) runTerragruntCommand(command string, printOutputToS cmd.Stderr = mwerr err = cmd.Run() - if err != nil { slog.Debug("Command execution details", "exitCode", cmd.ProcessState.ExitCode(), diff --git a/libs/execution/test_utils.go b/libs/execution/test_utils.go index 310b4a56b..21b9f14a4 100644 --- a/libs/execution/test_utils.go +++ b/libs/execution/test_utils.go @@ -21,7 +21,6 @@ func CreateInvalidTerraformTestFile(dir string) { defer func(f *os.File) { err := f.Close() if err != nil { - } }(f) @@ -40,7 +39,6 @@ func CreateValidTerraformTestFile(dir string) { defer func(f *os.File) { err := f.Close() if err != nil { - } }(f) @@ -59,7 +57,6 @@ func CreateMultiEnvDiggerYmlFile(dir string) { defer func(f *os.File) { err := f.Close() if err != nil { - } }(f) @@ -90,7 +87,6 @@ func CreateSingleEnvDiggerYmlFile(dir string) { defer func(f *os.File) { err := f.Close() if err != nil { - } }(f) @@ -126,7 +122,7 @@ workflows: } } -func CreateCustomDiggerYmlFile(dir string, diggerYaml string) { +func CreateCustomDiggerYmlFile(dir, diggerYaml string) { f, err := os.Create(dir + "/digger.yml") if err != nil { panic(err) @@ -135,7 +131,6 @@ func CreateCustomDiggerYmlFile(dir string, diggerYaml string) { defer func(f *os.File) { err := f.Close() if err != nil { - } }(f) diff --git a/libs/git_utils/clone_utils.go b/libs/git_utils/clone_utils.go index 5bd8b3cc8..6e0c36167 100644 --- a/libs/git_utils/clone_utils.go +++ b/libs/git_utils/clone_utils.go @@ -23,7 +23,7 @@ func createTempDir() (string, error) { type action func(string) error -func CloneGitRepoAndDoAction(repoUrl string, branch string, commitHash string, token string, tokenUsername string, action action) error { +func CloneGitRepoAndDoAction(repoUrl, branch, commitHash, token, tokenUsername string, action action) error { dir, err := createTempDir() if err != nil { slog.Error("Failed to create temporary directory", "error", err) @@ -107,7 +107,7 @@ func NewGitShell(workDir string, auth *GitAuth) *GitShell { } } -func NewGitShellWithTokenAuth(workDir string, token string, tokenUsername string) *GitShell { +func NewGitShellWithTokenAuth(workDir, token, tokenUsername string) *GitShell { auth := GitAuth{ Username: "x-access-token", Password: "", diff --git a/libs/iac_utils/iac_utils.go b/libs/iac_utils/iac_utils.go index b530379b4..473e3a99c 100644 --- a/libs/iac_utils/iac_utils.go +++ b/libs/iac_utils/iac_utils.go @@ -1,9 +1,10 @@ package iac_utils import ( + "sort" + "github.com/diggerhq/digger/libs/scheduler" "github.com/samber/lo" - "sort" ) type IacSummary struct { @@ -44,7 +45,7 @@ func (footprint IacPlanFootprint) hash() string { copy(addresses, footprint.Addresses) sort.Strings(addresses) // concatenate all the addresses after sorting to form the hash - return lo.Reduce(addresses, func(a string, b string, i int) string { + return lo.Reduce(addresses, func(a, b string, i int) string { return a + b }, "") } @@ -53,7 +54,7 @@ type IacUtils interface { GetSummaryFromPlanJson(planJson string) (bool, *IacSummary, error) GetSummaryFromApplyOutput(applyOutput string) (IacSummary, error) GetPlanFootprint(planJson string) (*IacPlanFootprint, error) - PerformPlanSimilarityCheck(footprint1 IacPlanFootprint, footprint2 IacPlanFootprint) (bool, error) + PerformPlanSimilarityCheck(footprint1, footprint2 IacPlanFootprint) (bool, error) SimilarityCheck(footprints []IacPlanFootprint) (bool, error) GetSummarizePlan(planJson string) (string, error) } diff --git a/libs/iac_utils/pulumi.go b/libs/iac_utils/pulumi.go index 3af64e070..4815b9439 100644 --- a/libs/iac_utils/pulumi.go +++ b/libs/iac_utils/pulumi.go @@ -4,10 +4,11 @@ import ( "bufio" "encoding/json" "fmt" - tfjson "github.com/hashicorp/terraform-json" - "github.com/samber/lo" "regexp" "strings" + + tfjson "github.com/hashicorp/terraform-json" + "github.com/samber/lo" ) // PulumiPreview represents the root structure of Pulumi preview JSON @@ -101,7 +102,7 @@ func (tu PulumiUtils) GetPlanFootprint(planJson string) (*IacPlanFootprint, erro return &footprint, nil } -func (tu PulumiUtils) PerformPlanSimilarityCheck(footprint1 IacPlanFootprint, footprint2 IacPlanFootprint) (bool, error) { +func (tu PulumiUtils) PerformPlanSimilarityCheck(footprint1, footprint2 IacPlanFootprint) (bool, error) { return footprint1.hash() == footprint2.hash(), nil } @@ -116,7 +117,6 @@ func (tu PulumiUtils) SimilarityCheck(footprints []IacPlanFootprint) (bool, erro return footprint == footprintHashes[0] }) return allSimilar, nil - } func (tu PulumiUtils) GetSummarizePlan(planJson string) (string, error) { diff --git a/libs/iac_utils/terraform.go b/libs/iac_utils/terraform.go index 97385df70..44728dbe2 100644 --- a/libs/iac_utils/terraform.go +++ b/libs/iac_utils/terraform.go @@ -5,16 +5,16 @@ import ( "bytes" "encoding/json" "fmt" + "regexp" + "strings" + "github.com/dineshba/tf-summarize/terraformstate" "github.com/dineshba/tf-summarize/writer" tfjson "github.com/hashicorp/terraform-json" "github.com/samber/lo" - "regexp" - "strings" ) -type TerraformUtils struct { -} +type TerraformUtils struct{} func parseTerraformPlanOutput(terraformJson string) (*tfjson.Plan, error) { var plan tfjson.Plan @@ -102,7 +102,7 @@ func (tu TerraformUtils) GetPlanFootprint(planJson string) (*IacPlanFootprint, e return &footprint, nil } -func (tu TerraformUtils) PerformPlanSimilarityCheck(footprint1 IacPlanFootprint, footprint2 IacPlanFootprint) (bool, error) { +func (tu TerraformUtils) PerformPlanSimilarityCheck(footprint1, footprint2 IacPlanFootprint) (bool, error) { return footprint1.hash() == footprint2.hash(), nil } @@ -117,7 +117,6 @@ func (tu TerraformUtils) SimilarityCheck(footprints []IacPlanFootprint) (bool, e return footprint == footprintHashes[0] }) return allSimilar, nil - } func (tu TerraformUtils) GetSummarizePlan(planJson string) (string, error) { diff --git a/libs/iac_utils/terraform_test.go b/libs/iac_utils/terraform_test.go index 19a6fe37d..e5de7137a 100644 --- a/libs/iac_utils/terraform_test.go +++ b/libs/iac_utils/terraform_test.go @@ -25,7 +25,6 @@ func TestGetPlanSummaryOnlyOutputsChanged(t *testing.T) { isEmpty, _, err := TerraformUtils{}.GetSummaryFromPlanJson(onlyOutputsChangedJson) assert.Nil(t, err) assert.False(t, isEmpty) - } func TestPlanOutputInvalidJsonFailsGracefully(t *testing.T) { @@ -57,7 +56,6 @@ func TestPlanFootprintSimilarity(t *testing.T) { footPrints = []IacPlanFootprint{*footprint1, *footprint2} isSimilar, _ = TerraformUtils{}.SimilarityCheck(footPrints) assert.False(t, isSimilar) - } func TestGetTfSummarizePlan(t *testing.T) { diff --git a/libs/license/license.go b/libs/license/license.go index b71c2df33..2dc70c4d9 100644 --- a/libs/license/license.go +++ b/libs/license/license.go @@ -10,8 +10,7 @@ import ( "os" ) -type LicenseKeyChecker struct { -} +type LicenseKeyChecker struct{} func (l LicenseKeyChecker) Check() error { licenseKey := os.Getenv("DIGGER_LICENSE_KEY") diff --git a/libs/license/license_test.go b/libs/license/license_test.go index 41196ab3a..ebfc8de70 100644 --- a/libs/license/license_test.go +++ b/libs/license/license_test.go @@ -4,8 +4,9 @@ package license import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestLicenseKeyChecker(t *testing.T) { diff --git a/libs/locking/aws/dynamo_locking_test.go b/libs/locking/aws/dynamo_locking_test.go index 02c97910b..ec8c1dc80 100644 --- a/libs/locking/aws/dynamo_locking_test.go +++ b/libs/locking/aws/dynamo_locking_test.go @@ -72,6 +72,7 @@ func TestDynamoDbLock_Lock(t *testing.T) { t.Fatalf("Expected true, got %v", locked) } } + func TestDynamoDbLock_GetLock(t *testing.T) { // Create a mock DynamoDB client client := mockDynamoDbClient{table: make(map[string]map[string]types.AttributeValue)} diff --git a/libs/locking/aws/envprovider/envprovider.go b/libs/locking/aws/envprovider/envprovider.go index 0ea0b7aee..b066ea8c0 100644 --- a/libs/locking/aws/envprovider/envprovider.go +++ b/libs/locking/aws/envprovider/envprovider.go @@ -97,7 +97,7 @@ func (e *EnvProvider) Retrieve(ctx context.Context) (aws.Credentials, error) { slog.Debug("Retrieving AWS credentials from environment") e.retrieved = false - //assign id from env vars + // assign id from env vars idEnvVars := []string{"DIGGER_AWS_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID", "AWS_ACCESS_KEY"} id, err := assignEnv(idEnvVars) if err != nil { @@ -106,7 +106,7 @@ func (e *EnvProvider) Retrieve(ctx context.Context) (aws.Credentials, error) { return aws.Credentials{}, ErrAccessKeyIDNotFound } - //assign secret from env vars + // assign secret from env vars secretEnvVars := []string{"DIGGER_AWS_SECRET_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY", "AWS_SECRET_KEY"} secret, err := assignEnv(secretEnvVars) if err != nil { diff --git a/libs/locking/azure/storage_account.go b/libs/locking/azure/storage_account.go index d04fcec19..739e71291 100644 --- a/libs/locking/azure/storage_account.go +++ b/libs/locking/azure/storage_account.go @@ -15,9 +15,7 @@ const ( TABLE_NAME = "DIGGERLOCK" ) -var ( - SERVICE_URL_FORMAT = "https://%s.table.core.windows.net" -) +var SERVICE_URL_FORMAT = "https://%s.table.core.windows.net" type StorageAccount struct { tableClient *aztables.Client diff --git a/libs/locking/gcp/gcp_lock_test.go b/libs/locking/gcp/gcp_lock_test.go index 164e09dd7..84e466360 100644 --- a/libs/locking/gcp/gcp_lock_test.go +++ b/libs/locking/gcp/gcp_lock_test.go @@ -132,7 +132,7 @@ func TestGoogleStorageLock_UnlockLockedWithDifferentId(t *testing.T) { bucketName := "digger-lock-test" fileName := "digger-lock-" + randomString(16) transactionId := 100 - //anotherTransactionId := 200 + // anotherTransactionId := 200 slog.Info("Testing unlocking with different transaction ID", "fileName", fileName, diff --git a/libs/locking/locking.go b/libs/locking/locking.go index 7de0e9338..53bbe7faa 100644 --- a/libs/locking/locking.go +++ b/libs/locking/locking.go @@ -33,8 +33,7 @@ type PullRequestLock struct { PrNumber int } -type NoOpLock struct { -} +type NoOpLock struct{} func (noOpLock NoOpLock) Lock(transactionId int, resource string) (bool, error) { return true, nil @@ -128,7 +127,6 @@ func (projectLock *PullRequestLock) verifyNoHangingLocks() (bool, error) { // TODO: Also include CI type (github, gitlab etc. into this lockID in order to avoid collision across VCS) lockId := projectLock.LockId() transactionId, err := projectLock.InternalLock.GetLock(lockId) - if err != nil { return false, err } diff --git a/libs/locking/locking_test.go b/libs/locking/locking_test.go index f0316cf8e..71fc0bf57 100644 --- a/libs/locking/locking_test.go +++ b/libs/locking/locking_test.go @@ -1,9 +1,10 @@ package locking import ( + "testing" + "github.com/diggerhq/digger/libs/comment_utils/reporting" "github.com/diggerhq/digger/libs/orchestrator" - "testing" "github.com/stretchr/testify/assert" ) diff --git a/libs/locking/utils.go b/libs/locking/utils.go index e01c17347..6736a32ed 100644 --- a/libs/locking/utils.go +++ b/libs/locking/utils.go @@ -2,6 +2,7 @@ package locking import ( "fmt" + "github.com/diggerhq/digger/libs/scheduler" ) diff --git a/libs/orchestrator/mock.go b/libs/orchestrator/mock.go index fddaaec24..9a26e36a2 100644 --- a/libs/orchestrator/mock.go +++ b/libs/orchestrator/mock.go @@ -2,6 +2,7 @@ package orchestrator import ( "fmt" + "github.com/diggerhq/digger/libs/ci" ) @@ -9,7 +10,7 @@ type MockGithubPullrequestManager struct { commands []string } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) GetUserTeams(organisation string, user string) ([]string, error) { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) GetUserTeams(organisation, user string) ([]string, error) { return []string{}, nil } @@ -28,16 +29,16 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) ListIssues() ( return nil, nil } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) PublishIssue(title string, body string, labels *[]string) (int64, error) { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) PublishIssue(title, body string, labels *[]string) (int64, error) { mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "PublishIssue") return 0, nil } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) UpdateIssue(ID int64, title string, body string) (int64, error) { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) UpdateIssue(ID int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) SetStatus(prNumber int, status string, statusContext string) error { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) SetStatus(prNumber int, status, statusContext string) error { mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "SetStatus") return nil } @@ -82,7 +83,7 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) GetApprovals(p return []string{}, nil } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) EditComment(prNumber int, id string, comment string) error { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) EditComment(prNumber int, id, comment string) error { mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "EditComment") return nil } @@ -91,7 +92,7 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) DeleteComment( return nil } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) CreateCommentReaction(id string, reaction string) error { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) CreateCommentReaction(id, reaction string) error { mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "CreateCommentReaction") return nil } @@ -101,7 +102,7 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) GetBranchName( return "", "", nil } -func (mockGithubPullrequestManager MockGithubPullrequestManager) SetOutput(prNumber int, key string, value string) error { +func (mockGithubPullrequestManager MockGithubPullrequestManager) SetOutput(prNumber int, key, value string) error { mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "SetOutput") return nil } diff --git a/libs/policy/core.go b/libs/policy/core.go index dd7b071ac..21ddf20ca 100644 --- a/libs/policy/core.go +++ b/libs/policy/core.go @@ -5,21 +5,21 @@ import ( ) type Provider interface { - GetAccessPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) - GetPlanPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) + GetAccessPolicy(organisation, repository, projectname, projectDir string) (string, error) + GetPlanPolicy(organisation, repository, projectname, projectDir string) (string, error) GetDriftPolicy() (string, error) GetOrganisation() string // TODO: remove this method from here since out of place } type Checker interface { // TODO refactor arguments - use AccessPolicyContext - CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation string, SCMrepository string, projectName string, projectDir string, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) - CheckPlanPolicy(SCMrepository string, SCMOrganisation string, projectname string, projectDir string, planOutput string) (bool, []string, error) - CheckDriftPolicy(SCMOrganisation string, SCMrepository string, projectname string) (bool, error) + CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) + CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) + CheckDriftPolicy(SCMOrganisation, SCMrepository, projectname string) (bool, error) } type PolicyCheckerProvider interface { - Get(hostname string, organisationName string, authToken string) (Checker, error) + Get(hostname, organisationName, authToken string) (Checker, error) } type AccessPolicyContext struct { diff --git a/libs/policy/mocks.go b/libs/policy/mocks.go index 138e814e6..b98689309 100644 --- a/libs/policy/mocks.go +++ b/libs/policy/mocks.go @@ -2,17 +2,16 @@ package policy import "github.com/diggerhq/digger/libs/ci" -type MockPolicyChecker struct { -} +type MockPolicyChecker struct{} -func (t MockPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation string, SCMrepository string, projectName string, projectDir string, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (t MockPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { return false, nil } -func (t MockPolicyChecker) CheckPlanPolicy(SCMrepository string, SCMOrganisation string, projectname string, projectDir string, planOutput string) (bool, []string, error) { +func (t MockPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { return false, nil, nil } -func (t MockPolicyChecker) CheckDriftPolicy(SCMOrganisation string, SCMrepository string, projectname string) (bool, error) { +func (t MockPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectname string) (bool, error) { return true, nil } diff --git a/libs/policy/policy.go b/libs/policy/policy.go index 1c3b0af2d..555020bee 100644 --- a/libs/policy/policy.go +++ b/libs/policy/policy.go @@ -28,18 +28,17 @@ type DiggerHttpPolicyProvider struct { HttpClient *http.Client } -type NoOpPolicyChecker struct { -} +type NoOpPolicyChecker struct{} -func (p NoOpPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation string, SCMrepository string, projectName string, projectDir string, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (p NoOpPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { return true, nil } -func (p NoOpPolicyChecker) CheckPlanPolicy(SCMrepository string, SCMOrganisation string, projectname string, projectDir string, planOutput string) (bool, []string, error) { +func (p NoOpPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { return true, nil, nil } -func (p NoOpPolicyChecker) CheckDriftPolicy(SCMOrganisation string, SCMrepository string, projectname string) (bool, error) { +func (p NoOpPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectname string) (bool, error) { return true, nil } @@ -133,7 +132,7 @@ func getDriftPolicyForOrganisation(p *DiggerHttpPolicyProvider) (string, *http.R return string(body), resp, nil } -func getAccessPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, projectName string) (string, *http.Response, error) { +func getAccessPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace, projectName string) (string, *http.Response, error) { // fetch RBAC policies for project from Digger API u, err := url.Parse(p.DiggerHost) if err != nil { @@ -148,7 +147,6 @@ func getAccessPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, "url", u.String()) req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { return "", nil, err } @@ -167,7 +165,7 @@ func getAccessPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, return string(body), resp, nil } -func getPlanPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, projectName string) (string, *http.Response, error) { +func getPlanPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace, projectName string) (string, *http.Response, error) { u, err := url.Parse(p.DiggerHost) if err != nil { slog.Error("Failed to parse digger cloud URL", "url", p.DiggerHost, "error", err) @@ -181,7 +179,6 @@ func getPlanPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, pr "url", u.String()) req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { return "", nil, err } @@ -201,7 +198,7 @@ func getPlanPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace string, pr } // GetPolicy fetches policy for particular project, if not found then it will fallback to org level policy -func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation string, repo string, projectName string, projectDir string) (string, error) { +func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation, repo, projectName, projectDir string) (string, error) { namespace := fmt.Sprintf("%v-%v", organisation, repo) slog.Debug("Getting access policy", @@ -256,7 +253,7 @@ func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation string, repo stri } } -func (p DiggerHttpPolicyProvider) GetPlanPolicy(organisation string, repo string, projectName string, projectDir string) (string, error) { +func (p DiggerHttpPolicyProvider) GetPlanPolicy(organisation, repo, projectName, projectDir string) (string, error) { namespace := fmt.Sprintf("%v-%v", organisation, repo) slog.Debug("Getting plan policy", @@ -344,7 +341,7 @@ type DiggerPolicyChecker struct { } // TODO refactor to use AccessPolicyContext - too many arguments -func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation string, SCMrepository string, projectName string, projectDir string, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { slog.Debug("Checking access policy", "organisation", SCMOrganisation, "repository", SCMrepository, @@ -353,7 +350,6 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic "requestedBy", requestedBy) policy, err := p.PolicyProvider.GetAccessPolicy(SCMOrganisation, SCMrepository, projectName, projectDir) - if err != nil { slog.Error("Error fetching policy", "error", err) return false, err @@ -370,7 +366,7 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic } // list of pull request approvals (if applicable) - var approvals = make([]string, 0) + approvals := make([]string, 0) if prService != nil && prNumber != nil { approvals, err = (*prService).GetApprovals(*prNumber) if err != nil { @@ -404,7 +400,6 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic rego.Query("data.digger.allow"), rego.Module("digger", policy), ).PrepareForEval(ctx) - if err != nil { slog.Error("Failed to prepare policy evaluation", "error", err) return false, err @@ -440,7 +435,7 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic return true, nil } -func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository string, SCMOrganisation string, projectname string, projectDir string, planOutput string) (bool, []string, error) { +func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { slog.Debug("Checking plan policy", "organisation", SCMOrganisation, "repository", SCMrepository, @@ -475,7 +470,6 @@ func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository string, SCMOrganisati rego.Query("data.digger.deny"), rego.Module("digger", policy), ).PrepareForEval(ctx) - if err != nil { slog.Error("Failed to prepare plan policy evaluation", "error", err) return false, nil, err @@ -521,14 +515,14 @@ func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository string, SCMOrganisati return true, []string{}, nil } -func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation string, SCMrepository string, projectName string) (bool, error) { +func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectName string) (bool, error) { slog.Debug("Checking drift policy", "organisation", SCMOrganisation, "repository", SCMrepository, "project", projectName) // TODO: Get rid of organisation if its not needed - //organisation := p.PolicyProvider.GetOrganisation() + // organisation := p.PolicyProvider.GetOrganisation() policy, err := p.PolicyProvider.GetDriftPolicy() if err != nil { slog.Error("Error fetching drift policy", "error", err) @@ -554,7 +548,6 @@ func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation string, SCMreposit rego.Query("data.digger.enable"), rego.Module("digger", policy), ).PrepareForEval(ctx) - if err != nil { slog.Error("Failed to prepare drift policy evaluation", "error", err) return false, err @@ -588,7 +581,7 @@ func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation string, SCMreposit return true, nil } -func NewPolicyChecker(hostname string, organisationName string, authToken string) Checker { +func NewPolicyChecker(hostname, organisationName, authToken string) Checker { var policyChecker Checker if os.Getenv("NO_BACKEND") == "true" { slog.Warn("Running in 'backendless' mode. Features that require backend will not be available.") @@ -604,7 +597,8 @@ func NewPolicyChecker(hostname string, organisationName string, authToken string DiggerOrganisation: organisationName, AuthToken: authToken, HttpClient: http.DefaultClient, - }} + }, + } } return policyChecker } diff --git a/libs/policy/policy_test.go b/libs/policy/policy_test.go index 4b4d134c3..76b3325f7 100644 --- a/libs/policy/policy_test.go +++ b/libs/policy/policy_test.go @@ -1,14 +1,14 @@ package policy import ( - "github.com/diggerhq/digger/libs/ci" "testing" + + "github.com/diggerhq/digger/libs/ci" ) -type OpaExamplePolicyProvider struct { -} +type OpaExamplePolicyProvider struct{} -func (s *OpaExamplePolicyProvider) GetAccessPolicy(_ string, _ string) (string, error) { +func (s *OpaExamplePolicyProvider) GetAccessPolicy(_, _ string) (string, error) { return "package digger\n" + "\n" + "# user-role assignments\n" + @@ -45,14 +45,13 @@ func (s *OpaExamplePolicyProvider) GetOrganisation() string { return "ORGANISATIONDIGGER" } -type DiggerDefaultPolicyProvider struct { -} +type DiggerDefaultPolicyProvider struct{} -func (s *DiggerDefaultPolicyProvider) GetAccessPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerDefaultPolicyProvider) GetAccessPolicy(organisation, repository, projectname, projectDir string) (string, error) { return DefaultAccessPolicy, nil } -func (s *DiggerDefaultPolicyProvider) GetPlanPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerDefaultPolicyProvider) GetPlanPolicy(organisation, repository, projectname, projectDir string) (string, error) { return "package digger\n", nil } @@ -64,10 +63,9 @@ func (s *DiggerDefaultPolicyProvider) GetOrganisation() string { return "ORGANISATIONDIGGER" } -type DiggerExamplePolicyProvider struct { -} +type DiggerExamplePolicyProvider struct{} -func (s *DiggerExamplePolicyProvider) GetAccessPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerExamplePolicyProvider) GetAccessPolicy(organisation, repository, projectname, projectDir string) (string, error) { return "package digger\n" + "\n" + "user_permissions := {\n" + @@ -83,7 +81,7 @@ func (s *DiggerExamplePolicyProvider) GetAccessPolicy(organisation string, repos "", nil } -func (s *DiggerExamplePolicyProvider) GetPlanPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerExamplePolicyProvider) GetPlanPolicy(organisation, repository, projectname, projectDir string) (string, error) { return "package digger\n", nil } @@ -95,10 +93,9 @@ func (s *DiggerExamplePolicyProvider) GetOrganisation() string { return "ORGANISATIONDIGGER" } -type DiggerExamplePolicyProvider2 struct { -} +type DiggerExamplePolicyProvider2 struct{} -func (s *DiggerExamplePolicyProvider2) GetAccessPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerExamplePolicyProvider2) GetAccessPolicy(organisation, repository, projectname, projectDir string) (string, error) { return "package digger\n" + "\n" + "user_permissions := {\n" + @@ -117,7 +114,7 @@ func (s *DiggerExamplePolicyProvider2) GetAccessPolicy(organisation string, repo "", nil } -func (s *DiggerExamplePolicyProvider2) GetPlanPolicy(organisation string, repository string, projectname string, projectDir string) (string, error) { +func (s *DiggerExamplePolicyProvider2) GetPlanPolicy(organisation, repository, projectname, projectDir string) (string, error) { return "package digger\n\ndeny[sprintf(message, [resource.address])] {\n message := \"Cannot create EC2 instances!\"\n resource := input.terraform.resource_changes[_]\n resource.change.actions[_] == \"create\"\n resource[type] == \"aws_instance\"\n}\n", nil } @@ -217,7 +214,7 @@ func TestDiggerAccessPolicyChecker_Check(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var p = &DiggerPolicyChecker{ + p := &DiggerPolicyChecker{ PolicyProvider: tt.fields.PolicyProvider, } ciService := ci.MockPullRequestManager{Teams: []string{"engineering"}} @@ -270,7 +267,7 @@ func TestDiggerPlanPolicyChecker_Check(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var p = &DiggerPolicyChecker{ + p := &DiggerPolicyChecker{ PolicyProvider: tt.fields.PolicyProvider, } got, _, err := p.CheckPlanPolicy("", "", "", "", tt.planJsonOutput) diff --git a/libs/policy/providers.go b/libs/policy/providers.go index 306875dce..cc34ac829 100644 --- a/libs/policy/providers.go +++ b/libs/policy/providers.go @@ -8,7 +8,7 @@ import ( type PolicyCheckerProviderBasic struct{} -func (p PolicyCheckerProviderBasic) Get(hostname string, organisationName string, authToken string) (Checker, error) { +func (p PolicyCheckerProviderBasic) Get(hostname, organisationName, authToken string) (Checker, error) { var policyChecker Checker if os.Getenv("NO_BACKEND") == "true" { slog.Warn("Running in 'backendless' mode. No policies will be supported.") @@ -24,7 +24,8 @@ func (p PolicyCheckerProviderBasic) Get(hostname string, organisationName string DiggerOrganisation: organisationName, AuthToken: authToken, HttpClient: http.DefaultClient, - }} + }, + } } return policyChecker, nil } diff --git a/libs/scheduler/aws.go b/libs/scheduler/aws.go index 2ea99bc37..bf068ab0e 100644 --- a/libs/scheduler/aws.go +++ b/libs/scheduler/aws.go @@ -45,10 +45,12 @@ const ( /** * This is a singleton patterns for external data client and allows mocking for testing */ -var defaultCognitoClient CognitoClient -var defaultStsClient StsClient -var defaultTokenFetcher GithubTokenFetcher -var defaultLoadConfig = config.LoadDefaultConfig +var ( + defaultCognitoClient CognitoClient + defaultStsClient StsClient + defaultTokenFetcher GithubTokenFetcher + defaultLoadConfig = config.LoadDefaultConfig +) func getCognitoClient(cfg aws.Config) CognitoClient { if defaultCognitoClient == nil { @@ -348,7 +350,7 @@ func (fetcher *GithubAwsTokenFetcher) GetIdentityToken() ([]byte, error) { return []byte(parsed.Value), nil } -func GetProviderFromRole(role string, region string) *stscreds.WebIdentityRoleProvider { +func GetProviderFromRole(role, region string) *stscreds.WebIdentityRoleProvider { if role == "" { return nil } diff --git a/libs/scheduler/aws_test.go b/libs/scheduler/aws_test.go index acc5c0a35..4da7f285a 100644 --- a/libs/scheduler/aws_test.go +++ b/libs/scheduler/aws_test.go @@ -23,6 +23,7 @@ type AwsRoleProviderMock struct { // Mock client for testing cognito token retrieval type MockCognitoClient struct{} + func (m *MockCognitoClient) GetId(ctx context.Context, params *cognitoidentity.GetIdInput, optFns ...func(*cognitoidentity.Options)) (*cognitoidentity.GetIdOutput, error) { if params.IdentityPoolId == nil || *params.IdentityPoolId == "" { return nil, errors.New("IdentityPoolId is required") @@ -32,6 +33,7 @@ func (m *MockCognitoClient) GetId(ctx context.Context, params *cognitoidentity.G IdentityId: aws.String("mock-identity-id"), }, nil } + func (m *MockCognitoClient) GetCredentialsForIdentity(ctx context.Context, params *cognitoidentity.GetCredentialsForIdentityInput, optFns ...func(*cognitoidentity.Options)) (*cognitoidentity.GetCredentialsForIdentityOutput, error) { if params.IdentityId == nil || *params.IdentityId == "" { return nil, errors.New("IdentityId is required") @@ -46,20 +48,20 @@ func (m *MockCognitoClient) GetCredentialsForIdentity(ctx context.Context, param }, nil } -type MockTokenFetcher struct {} -func (m *MockTokenFetcher) SetAudience(audience string) {} +type MockTokenFetcher struct{} + +func (m *MockTokenFetcher) SetAudience(audience string) {} func (m *MockTokenFetcher) GetIdentityToken() ([]byte, error) { return []byte("mock-identity-token"), nil } - func setupCognitoMocks() (*MockCognitoClient, *MockTokenFetcher) { client := MockCognitoClient{} tokenFetcher := MockTokenFetcher{} defaultCognitoClient = &client defaultTokenFetcher = &tokenFetcher - defaultLoadConfig = func(ctx context.Context, optFns ...func(*config.LoadOptions) (error)) (cfg aws.Config, err error) { - return aws.Config{}, nil // Mock the aws.Config struct + defaultLoadConfig = func(ctx context.Context, optFns ...func(*config.LoadOptions) error) (cfg aws.Config, err error) { + return aws.Config{}, nil // Mock the aws.Config struct } return &client, &tokenFetcher } @@ -136,36 +138,35 @@ func TestPopulationForNoAwsRoleToAssumeDoesNotSetValueOfKeys(t *testing.T) { } func TestGetCognitoTokenGetsCreds(t *testing.T) { - - setupCognitoMocks() + setupCognitoMocks() mockProject := digger_config.Project{ AwsCognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ CognitoPoolId: "mock-identity-pool-id", - AwsAccountId: "mock-aws-account-id", - AwsRegion: "mock-aws-region", + AwsAccountId: "mock-aws-account-id", + AwsRegion: "mock-aws-region", }, } credentials, err := GetCognitoToken(*mockProject.AwsCognitoOidcConfig, "token.actions.githubusercontent.com") assert.Nil(t, err) assert.Equal(t, credentials, map[string]string{ - "AWS_ACCESS_KEY_ID": "mock-access-key-id", - "AWS_SECRET_ACCESS_KEY": "mock-secret-access-key", - "AWS_SESSION_TOKEN": "mock-session-token", - }); + "AWS_ACCESS_KEY_ID": "mock-access-key-id", + "AWS_SECRET_ACCESS_KEY": "mock-secret-access-key", + "AWS_SESSION_TOKEN": "mock-session-token", + }) t.Cleanup(func() { defaultCognitoClient = nil defaultTokenFetcher = nil - }) + }) } func TestGetCognitoTokenReturnsErrorForMissingPoolId(t *testing.T) { setupCognitoMocks() mockProject := digger_config.Project{ - AwsCognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ + AwsCognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ AwsAccountId: "mock-aws-account-id", - AwsRegion: "mock-aws-region", + AwsRegion: "mock-aws-region", }, } @@ -179,7 +180,7 @@ func TestGetCognitoTokenReturnsErrorForMissingAccountId(t *testing.T) { mockProject := digger_config.Project{ AwsCognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ CognitoPoolId: "mock-identity-pool-id", - AwsRegion: "mock-aws-region", + AwsRegion: "mock-aws-region", }, } @@ -193,7 +194,7 @@ func TestGetCognitoTokenReturnsErrorForMissingRegion(t *testing.T) { mockProject := digger_config.Project{ AwsCognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ CognitoPoolId: "mock-identity-pool-id", - AwsAccountId: "mock-aws-account-id", + AwsAccountId: "mock-aws-account-id", }, } @@ -203,46 +204,42 @@ func TestGetCognitoTokenReturnsErrorForMissingRegion(t *testing.T) { } func TestParseRegionFromPoolId(t *testing.T) { - region := parseRegionFromPoolId("us-east-1:00000000-0000-0000-0000-000000000000") // this is a fake guid generated locally. + region := parseRegionFromPoolId("us-east-1:00000000-0000-0000-0000-000000000000") // this is a fake guid generated locally. assert.Equal(t, region, "us-east-1") } func TestGetAuthStrategyDefault(t *testing.T) { - job := Job{ ProjectName: "test-project", - ProjectDir: "/tmp/test-project", + ProjectDir: "/tmp/test-project", } - + strategy := job.GetAuthStrategy() assert.Equal(t, strategy, BackendConfig) } func TestGetAuthStrategyCognito(t *testing.T) { - job := Job{ ProjectName: "test-project", - ProjectDir: "/tmp/test-project", + ProjectDir: "/tmp/test-project", CognitoOidcConfig: &digger_config.AwsCognitoOidcConfig{ CognitoPoolId: "mock-identity-pool-id", - AwsAccountId: "mock-aws-account-id", - AwsRegion: "mock-aws-region", + AwsAccountId: "mock-aws-account-id", + AwsRegion: "mock-aws-region", }, } - + strategy := job.GetAuthStrategy() assert.Equal(t, strategy, Cognito) } func TestGetAuthStrategyTerragrunt(t *testing.T) { - job := Job{ ProjectName: "test-project", - ProjectDir: "/tmp/test-project", - Terragrunt: true, + ProjectDir: "/tmp/test-project", + Terragrunt: true, } - + strategy := job.GetAuthStrategy() assert.Equal(t, strategy, Terragrunt) } - diff --git a/libs/scheduler/convert.go b/libs/scheduler/convert.go index c6d280101..f94bd09de 100644 --- a/libs/scheduler/convert.go +++ b/libs/scheduler/convert.go @@ -7,7 +7,7 @@ import ( "github.com/diggerhq/digger/libs/digger_config" ) -func ConvertProjectsToJobs(actor string, repoNamespace string, command string, prNumber int, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, workflows map[string]digger_config.Workflow) ([]Job, bool, error) { +func ConvertProjectsToJobs(actor, repoNamespace, command string, prNumber int, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, workflows map[string]digger_config.Workflow) ([]Job, bool, error) { jobs := make([]Job, 0) slog.Info("Converting projects to jobs", diff --git a/libs/scheduler/jobs.go b/libs/scheduler/jobs.go index 16609478d..eb1b926f9 100644 --- a/libs/scheduler/jobs.go +++ b/libs/scheduler/jobs.go @@ -9,8 +9,10 @@ import ( type IacType string -var IacTypeTerraform IacType = "terraform" -var IacTypePulumi IacType = "pulumi" +var ( + IacTypeTerraform IacType = "terraform" + IacTypePulumi IacType = "pulumi" +) type Job struct { ProjectName string @@ -57,7 +59,6 @@ func ToConfigStep(configState configuration.Step) Step { ExtraArgs: configState.ExtraArgs, Shell: configState.Shell, } - } func ToConfigStage(configStage *configuration.Stage) *Stage { diff --git a/libs/scheduler/json_models.go b/libs/scheduler/json_models.go index d9330f41a..5d4933128 100644 --- a/libs/scheduler/json_models.go +++ b/libs/scheduler/json_models.go @@ -60,7 +60,7 @@ func (j *JobJson) IsApply() bool { return slices.Contains(j.Commands, "digger apply") } -func JobToJson(job Job, jobType DiggerCommand, organisationName string, branch string, commitSha string, jobToken string, backendHostname string, project digger_config.Project) JobJson { +func JobToJson(job Job, jobType DiggerCommand, organisationName, branch, commitSha, jobToken, backendHostname string, project digger_config.Project) JobJson { stateRole, commandRole, region := "", "", "" if project.AwsRoleToAssume != nil { diff --git a/libs/scheduler/json_models_test.go b/libs/scheduler/json_models_test.go index 3d6ab6ec4..c5421f1a1 100644 --- a/libs/scheduler/json_models_test.go +++ b/libs/scheduler/json_models_test.go @@ -2,10 +2,11 @@ package scheduler import ( "fmt" - "github.com/stretchr/testify/assert" "reflect" "slices" "testing" + + "github.com/stretchr/testify/assert" ) func TestAllFieldsInJobJsonAreAlsoInJob(t *testing.T) { @@ -23,7 +24,7 @@ func TestAllFieldsInJobJsonAreAlsoInJob(t *testing.T) { jobFields = append(jobFields, jobVal.Type().Field(j).Name) } - fieldsToIgnore := []string{"Commit", "Branch", "JobType", "AwsRoleRegion", "StateRoleName", "CommandRoleName", "StateRoleArn", "CommandRoleArn","BackendHostname", "BackendOrganisationName", "BackendJobToken"} + fieldsToIgnore := []string{"Commit", "Branch", "JobType", "AwsRoleRegion", "StateRoleName", "CommandRoleName", "StateRoleArn", "CommandRoleArn", "BackendHostname", "BackendOrganisationName", "BackendJobToken"} for i := 0; i < nFieldsSpec; i++ { field := specVal.Type().Field(i).Name if slices.Contains(fieldsToIgnore, field) { @@ -49,7 +50,7 @@ func TestAllFieldsInJobAreAlsoInJobJson(t *testing.T) { } fmt.Printf("%v ::\n", specFields) - fieldsToIgnore := []string{"ProjectWorkflow", "StateEnvProvider", "CommandEnvProvider","StateRoleArn", "CommandRoleArn", "CognitoOidcConfig", "SkipMergeCheck"} + fieldsToIgnore := []string{"ProjectWorkflow", "StateEnvProvider", "CommandEnvProvider", "StateRoleArn", "CommandRoleArn", "CognitoOidcConfig", "SkipMergeCheck"} for i := 0; i < nFieldsJob; i++ { field := jobVal.Type().Field(i).Name if slices.Contains(fieldsToIgnore, field) { diff --git a/libs/scheduler/utils.go b/libs/scheduler/utils.go index 983fb56a5..f22fe0573 100644 --- a/libs/scheduler/utils.go +++ b/libs/scheduler/utils.go @@ -17,11 +17,13 @@ func ParseProjectName(comment string) string { type DiggerCommand string -const DiggerCommandNoop DiggerCommand = "noop" -const DiggerCommandPlan DiggerCommand = "plan" -const DiggerCommandApply DiggerCommand = "apply" -const DiggerCommandLock DiggerCommand = "lock" -const DiggerCommandUnlock DiggerCommand = "unlock" +const ( + DiggerCommandNoop DiggerCommand = "noop" + DiggerCommandPlan DiggerCommand = "plan" + DiggerCommandApply DiggerCommand = "apply" + DiggerCommandLock DiggerCommand = "lock" + DiggerCommandUnlock DiggerCommand = "unlock" +) func GetCommandFromComment(comment string) (*DiggerCommand, error) { supportedCommands := map[string]DiggerCommand{ diff --git a/libs/spec/payloads.go b/libs/spec/payloads.go index 076febca1..0683e20e7 100644 --- a/libs/spec/payloads.go +++ b/libs/spec/payloads.go @@ -4,8 +4,8 @@ type GetSpecPayload struct { Command string `json:"command"` RepoFullName string `json:"repo_full_name"` Actor string `json:"actor"` - //DefaultBranch string `json:"default_branch"` - //PrBranch string `json:"pr_branch"` + // DefaultBranch string `json:"default_branch"` + // PrBranch string `json:"pr_branch"` DiggerConfig string `json:"digger_config"` Project string `json:"project"` } diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 360d65287..c6816a2fa 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -315,12 +315,12 @@ func (v VCSProviderBasic) GetOrgService(vcsSpec VcsSpec) (ci.OrgService, error) } type SpecPolicyProvider interface { - GetPolicyProvider(policySpec PolicySpec, diggerHost string, diggerOrg string, token string, vcsType string) (policy2.Checker, error) + GetPolicyProvider(policySpec PolicySpec, diggerHost, diggerOrg, token, vcsType string) (policy2.Checker, error) } type BasicPolicyProvider struct{} -func (p BasicPolicyProvider) GetPolicyProvider(policySpec PolicySpec, diggerHost string, diggerOrg string, token string, vcsType string) (policy2.Checker, error) { +func (p BasicPolicyProvider) GetPolicyProvider(policySpec PolicySpec, diggerHost, diggerOrg, token, vcsType string) (policy2.Checker, error) { slog.Debug("Getting policy provider", "policyType", policySpec.PolicyType, "diggerHost", diggerHost, @@ -345,7 +345,7 @@ func (p BasicPolicyProvider) GetPolicyProvider(policySpec PolicySpec, diggerHost type PlanStorageProvider struct{} -func (p PlanStorageProvider) GetPlanStorage(repoOwner string, repositoryName string, prNumber int) (storage2.PlanStorage, error) { +func (p PlanStorageProvider) GetPlanStorage(repoOwner, repositoryName string, prNumber int) (storage2.PlanStorage, error) { slog.Debug("Getting plan storage", "repoOwner", repoOwner, "repositoryName", repositoryName, diff --git a/libs/spec/variables_provider.go b/libs/spec/variables_provider.go index 8742d0024..dce44d8b3 100644 --- a/libs/spec/variables_provider.go +++ b/libs/spec/variables_provider.go @@ -2,9 +2,10 @@ package spec import ( "fmt" + "os" + digger_crypto "github.com/diggerhq/digger/libs/crypto" "github.com/samber/lo" - "os" ) type VariablesProvider struct{} diff --git a/libs/spec/variables_provider_test.go b/libs/spec/variables_provider_test.go index be9d91de6..06fca4530 100644 --- a/libs/spec/variables_provider_test.go +++ b/libs/spec/variables_provider_test.go @@ -6,9 +6,10 @@ import ( "crypto/x509" "encoding/base64" "encoding/pem" - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/assert" ) // generateTestKeyPair generates a test RSA key pair @@ -45,7 +46,6 @@ func TestDecryptProvider(t *testing.T) { }{ { variables: []VariableSpec{{ - Name: "XYZ", Value: "simple text", IsSecret: false, @@ -55,9 +55,8 @@ func TestDecryptProvider(t *testing.T) { }, { variables: []VariableSpec{{ - Name: "XYZ", - //Value: "simple text", + // Value: "simple text", IsSecret: true, }}, plaintext: "secret text", diff --git a/libs/storage/aws_plan_storage.go b/libs/storage/aws_plan_storage.go index 5b654df8d..153c6d7d4 100644 --- a/libs/storage/aws_plan_storage.go +++ b/libs/storage/aws_plan_storage.go @@ -40,7 +40,7 @@ type PlanStorageAWS struct { KMSEncryptionId string } -func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType string, KMSEncryptionId string) (*PlanStorageAWS, error) { +func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType, KMSEncryptionId string) (*PlanStorageAWS, error) { if bucketName == "" { slog.Error("AWS S3 bucket name not provided") return nil, fmt.Errorf("AWS_S3_BUCKET is not defined") diff --git a/libs/storage/aws_plan_storage_test.go b/libs/storage/aws_plan_storage_test.go index 758aa4220..4967182e0 100644 --- a/libs/storage/aws_plan_storage_test.go +++ b/libs/storage/aws_plan_storage_test.go @@ -48,6 +48,7 @@ func (m *emulateS3Client) HeadObject(ctx context.Context, params *s3.HeadObjectI } return nil, &types.NotFound{} } + func (m *emulateS3Client) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) { buf, err := io.ReadAll(params.Body) if err != nil { @@ -66,6 +67,7 @@ func (m *emulateS3Client) GetObject(ctx context.Context, params *s3.GetObjectInp } return nil, &types.NotFound{} } + func (m *emulateS3Client) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) { delete(m.objects, *params.Key) return &s3.DeleteObjectOutput{}, nil @@ -92,7 +94,6 @@ func TestPlanStorageAWS_PlanExists(t *testing.T) { } func TestPlanStorageAWS_E2E(t *testing.T) { - client := &emulateS3Client{ objects: make(map[string][]byte), } diff --git a/libs/storage/azure_plan_storage.go b/libs/storage/azure_plan_storage.go index f0ca4e7fd..309bd6a55 100644 --- a/libs/storage/azure_plan_storage.go +++ b/libs/storage/azure_plan_storage.go @@ -7,9 +7,9 @@ import ( "os" "path/filepath" + "github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror" - "github.com/Azure/azure-sdk-for-go/sdk/azcore" ) type PlanStorageAzure struct { @@ -18,7 +18,7 @@ type PlanStorageAzure struct { Context context.Context } -func (psa *PlanStorageAzure) PlanExists(artifactName string, storedPlanFilePath string) (bool, error) { +func (psa *PlanStorageAzure) PlanExists(artifactName, storedPlanFilePath string) (bool, error) { slog.Debug("Checking if plan exists in Azure Blob Storage", "container", psa.ContainerName, "path", storedPlanFilePath, @@ -52,7 +52,7 @@ func (psa *PlanStorageAzure) PlanExists(artifactName string, storedPlanFilePath return true, nil } -func (psa *PlanStorageAzure) StorePlanFile(fileContents []byte, artifactName string, fileName string) error { +func (psa *PlanStorageAzure) StorePlanFile(fileContents []byte, artifactName, fileName string) error { slog.Debug("Storing plan file in Azure Blob Storage", "container", psa.ContainerName, "path", fileName, @@ -66,7 +66,6 @@ func (psa *PlanStorageAzure) StorePlanFile(fileContents []byte, artifactName str fileContents, &azblob.UploadBufferOptions{}, ) - if err != nil { slog.Error("Failed to write file to Azure Blob Storage", "container", psa.ContainerName, @@ -81,7 +80,7 @@ func (psa *PlanStorageAzure) StorePlanFile(fileContents []byte, artifactName str return nil } -func (psa *PlanStorageAzure) RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) { +func (psa *PlanStorageAzure) RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) { slog.Debug("Retrieving plan from Azure Blob Storage", "container", psa.ContainerName, "path", storedPlanFilePath, @@ -127,7 +126,7 @@ func (psa *PlanStorageAzure) RetrievePlan(localPlanFilePath string, artifactName return &fileName, nil } -func (psa *PlanStorageAzure) DeleteStoredPlan(artifactName string, storedPlanFilePath string) error { +func (psa *PlanStorageAzure) DeleteStoredPlan(artifactName, storedPlanFilePath string) error { slog.Debug("Deleting stored plan from Azure Blob Storage", "container", psa.ContainerName, "path", storedPlanFilePath, @@ -139,7 +138,6 @@ func (psa *PlanStorageAzure) DeleteStoredPlan(artifactName string, storedPlanFil storedPlanFilePath, &azblob.DeleteBlobOptions{}, ) - if err != nil { slog.Error("Unable to delete file from Azure Blob Storage", "container", psa.ContainerName, diff --git a/libs/storage/gcp_plan_storage.go b/libs/storage/gcp_plan_storage.go index 4e355a40e..04ac4f9c1 100644 --- a/libs/storage/gcp_plan_storage.go +++ b/libs/storage/gcp_plan_storage.go @@ -17,7 +17,7 @@ type PlanStorageGcp struct { Context context.Context } -func (psg *PlanStorageGcp) PlanExists(artifactName string, storedPlanFilePath string) (bool, error) { +func (psg *PlanStorageGcp) PlanExists(artifactName, storedPlanFilePath string) (bool, error) { slog.Debug("Checking if plan exists in GCP storage", "bucket", psg.Bucket.BucketName(), "path", storedPlanFilePath, @@ -47,7 +47,7 @@ func (psg *PlanStorageGcp) PlanExists(artifactName string, storedPlanFilePath st return true, nil } -func (psg *PlanStorageGcp) StorePlanFile(fileContents []byte, artifactName string, fileName string) error { +func (psg *PlanStorageGcp) StorePlanFile(fileContents []byte, artifactName, fileName string) error { slog.Debug("Storing plan file in GCP storage", "bucket", psg.Bucket.BucketName(), "path", fileName, @@ -73,7 +73,7 @@ func (psg *PlanStorageGcp) StorePlanFile(fileContents []byte, artifactName strin return nil } -func (psg *PlanStorageGcp) RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) { +func (psg *PlanStorageGcp) RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) { slog.Debug("Retrieving plan from GCP storage", "bucket", psg.Bucket.BucketName(), "path", storedPlanFilePath, @@ -122,7 +122,7 @@ func (psg *PlanStorageGcp) RetrievePlan(localPlanFilePath string, artifactName s return &fileName, nil } -func (psg *PlanStorageGcp) DeleteStoredPlan(artifactName string, storedPlanFilePath string) error { +func (psg *PlanStorageGcp) DeleteStoredPlan(artifactName, storedPlanFilePath string) error { slog.Debug("Deleting stored plan from GCP storage", "bucket", psg.Bucket.BucketName(), "path", storedPlanFilePath, @@ -130,7 +130,6 @@ func (psg *PlanStorageGcp) DeleteStoredPlan(artifactName string, storedPlanFileP obj := psg.Bucket.Object(storedPlanFilePath) err := obj.Delete(psg.Context) - if err != nil { slog.Error("Unable to delete file from GCP bucket", "bucket", psg.Bucket.BucketName(), diff --git a/libs/storage/io.go b/libs/storage/io.go index 4a6c581cc..a627fae7d 100644 --- a/libs/storage/io.go +++ b/libs/storage/io.go @@ -9,13 +9,12 @@ import ( ) type Zip interface { - GetFileFromZip(zipFile string, filename string) (string, error) + GetFileFromZip(zipFile, filename string) (string, error) } -type Zipper struct { -} +type Zipper struct{} -func (z *Zipper) GetFileFromZip(zipFile string, filename string) (string, error) { +func (z *Zipper) GetFileFromZip(zipFile, filename string) (string, error) { reader, err := zip.OpenReader(zipFile) if err != nil { return "", err diff --git a/libs/storage/io_test.go b/libs/storage/io_test.go index e34384a82..ddadd85f1 100644 --- a/libs/storage/io_test.go +++ b/libs/storage/io_test.go @@ -3,11 +3,12 @@ package storage import ( "archive/zip" "bytes" - configuration "github.com/diggerhq/digger/libs/digger_config" - "github.com/stretchr/testify/assert" "io/ioutil" "os" "testing" + + configuration "github.com/diggerhq/digger/libs/digger_config" + "github.com/stretchr/testify/assert" ) func TestGetFileFromZip(t *testing.T) { @@ -28,7 +29,7 @@ func TestGetFileFromZip(t *testing.T) { } zipFileName := "test.zip" - err = ioutil.WriteFile(zipFileName, buf.Bytes(), 0644) + err = ioutil.WriteFile(zipFileName, buf.Bytes(), 0o644) if err != nil { t.Fatal(err) } @@ -62,7 +63,7 @@ func TestGetFileFromZipNoFileExists(t *testing.T) { } zipFileName := "test.zip" - err = ioutil.WriteFile(zipFileName, buf.Bytes(), 0644) + err = ioutil.WriteFile(zipFileName, buf.Bytes(), 0o644) if err != nil { t.Fatal(err) } @@ -83,7 +84,7 @@ func TestNormalizeFileName(t *testing.T) { type NormTest struct { input, expectedOutput string // input and expected normalized output } - var normTests = []NormTest{ + normTests := []NormTest{ {"my/directory", "/my/directory"}, {"./my/directory", "/my/directory"}, } @@ -100,7 +101,7 @@ func TestMatchIncludeExcludePatternsToFile(t *testing.T) { excludePatterns []string expectedResult bool } - var normTests = []MatchTest{ + normTests := []MatchTest{ {"dev/main.tf", []string{"dev/**"}, []string{}, true}, {"dev/main.tf", []string{"./dev/**"}, []string{}, true}, {"dev/main.tf", []string{"dev/**"}, []string{"dev/"}, true}, diff --git a/libs/storage/mocks.go b/libs/storage/mocks.go index c07673d66..911c798da 100644 --- a/libs/storage/mocks.go +++ b/libs/storage/mocks.go @@ -1,20 +1,19 @@ package storage -type MockPlanStorage struct { -} +type MockPlanStorage struct{} -func (t MockPlanStorage) StorePlanFile(fileContents []byte, artifactName string, fileName string) error { +func (t MockPlanStorage) StorePlanFile(fileContents []byte, artifactName, fileName string) error { return nil } -func (t MockPlanStorage) RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) { +func (t MockPlanStorage) RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) { return nil, nil } -func (t MockPlanStorage) DeleteStoredPlan(artifactName string, storedPlanFilePath string) error { +func (t MockPlanStorage) DeleteStoredPlan(artifactName, storedPlanFilePath string) error { return nil } -func (t MockPlanStorage) PlanExists(artifactName string, storedPlanFilePath string) (bool, error) { +func (t MockPlanStorage) PlanExists(artifactName, storedPlanFilePath string) (bool, error) { return false, nil } diff --git a/libs/storage/plan_storage.go b/libs/storage/plan_storage.go index 646b72aeb..12c940fe8 100644 --- a/libs/storage/plan_storage.go +++ b/libs/storage/plan_storage.go @@ -13,10 +13,10 @@ import ( "os/exec" "strings" + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/diggerhq/digger/libs/locking/gcp" "github.com/google/go-github/v61/github" - "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" - "github.com/Azure/azure-sdk-for-go/sdk/azidentity" ) type GithubPlanStorage struct { @@ -27,7 +27,7 @@ type GithubPlanStorage struct { ZipManager Zipper } -func (gps *GithubPlanStorage) StorePlanFile(fileContents []byte, artifactName string, storedPlanFilePath string) error { +func (gps *GithubPlanStorage) StorePlanFile(fileContents []byte, artifactName, storedPlanFilePath string) error { slog.Debug("Storing plan file in GitHub artifacts", "owner", gps.Owner, "repo", gps.RepoName, @@ -147,7 +147,7 @@ func doRequest(method, url string, headers map[string]string, body []byte) (*htt return resp, nil } -func (gps *GithubPlanStorage) RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) { +func (gps *GithubPlanStorage) RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) { slog.Debug("Retrieving plan from GitHub artifacts", "owner", gps.Owner, "repo", gps.RepoName, @@ -155,7 +155,6 @@ func (gps *GithubPlanStorage) RetrievePlan(localPlanFilePath string, artifactNam "localPath", localPlanFilePath) plansFilename, err := gps.DownloadLatestPlans(artifactName) - if err != nil { slog.Error("Failed to download plan from GitHub artifacts", "error", err, @@ -172,7 +171,6 @@ func (gps *GithubPlanStorage) RetrievePlan(localPlanFilePath string, artifactNam } plansFilename, err = gps.ZipManager.GetFileFromZip(plansFilename, localPlanFilePath) - if err != nil { slog.Error("Failed to extract plan from zip", "error", err, @@ -189,7 +187,7 @@ func (gps *GithubPlanStorage) RetrievePlan(localPlanFilePath string, artifactNam return &plansFilename, nil } -func (gps *GithubPlanStorage) PlanExists(artifactName string, storedPlanFilePath string) (bool, error) { +func (gps *GithubPlanStorage) PlanExists(artifactName, storedPlanFilePath string) (bool, error) { slog.Debug("Checking if plan exists in GitHub artifacts", "owner", gps.Owner, "repo", gps.RepoName, @@ -199,7 +197,6 @@ func (gps *GithubPlanStorage) PlanExists(artifactName string, storedPlanFilePath artifacts, _, err := gps.Client.Actions.ListArtifacts(context.Background(), gps.Owner, gps.RepoName, &github.ListOptions{ PerPage: 100, }) - if err != nil { slog.Error("Failed to list GitHub artifacts", "error", err, @@ -228,7 +225,7 @@ func (gps *GithubPlanStorage) PlanExists(artifactName string, storedPlanFilePath return true, nil } -func (gps *GithubPlanStorage) DeleteStoredPlan(artifactName string, storedPlanFilePath string) error { +func (gps *GithubPlanStorage) DeleteStoredPlan(artifactName, storedPlanFilePath string) error { // GitHub artifacts can't be deleted via API, they expire automatically slog.Debug("GitHub artifacts cannot be deleted, they expire automatically", "artifactName", artifactName, @@ -245,7 +242,6 @@ func (gps *GithubPlanStorage) DownloadLatestPlans(storedPlanFilePath string) (st artifacts, _, err := gps.Client.Actions.ListArtifacts(context.Background(), gps.Owner, gps.RepoName, &github.ListOptions{ PerPage: 100, }) - if err != nil { slog.Error("Failed to list GitHub artifacts", "error", err, @@ -263,7 +259,6 @@ func (gps *GithubPlanStorage) DownloadLatestPlans(storedPlanFilePath string) (st } downloadUrl, _, err := gps.Client.Actions.DownloadArtifact(context.Background(), gps.Owner, gps.RepoName, *latestPlans.ID, 0) - if err != nil { slog.Error("Failed to get download URL for artifact", "error", err, @@ -276,7 +271,6 @@ func (gps *GithubPlanStorage) DownloadLatestPlans(storedPlanFilePath string) (st "url", downloadUrl.String(), "outputFile", filename) err = downloadArtifactIntoFile(downloadUrl, filename) - if err != nil { slog.Error("Failed to download artifact to file", "error", err, @@ -335,7 +329,7 @@ func getLatestArtifactWithName(artifacts []*github.Artifact, name string) *githu } // TODO: refactor this function to make it fully parametarised and no reliance on env variables -func NewPlanStorage(ghToken string, ghRepoOwner string, ghRepositoryName string, prNumber *int) (PlanStorage, error) { +func NewPlanStorage(ghToken, ghRepoOwner, ghRepositoryName string, prNumber *int) (PlanStorage, error) { var planStorage PlanStorage uploadDestination := strings.ToLower(os.Getenv("PLAN_UPLOAD_DESTINATION")) @@ -392,7 +386,7 @@ func NewPlanStorage(ghToken string, ghRepoOwner string, ghRepositoryName string, } case uploadDestination == "gitlab": slog.Warn("GitLab plan storage not yet implemented") - //TODO implement me + // TODO implement me case uploadDestination == "azure": containerName := strings.ToLower(os.Getenv("PLAN_UPLOAD_AZURE_STORAGE_CONTAINER_NAME")) if containerName == "" { diff --git a/libs/storage/storage.go b/libs/storage/storage.go index 46462712d..646362a26 100644 --- a/libs/storage/storage.go +++ b/libs/storage/storage.go @@ -1,8 +1,8 @@ package storage type PlanStorage interface { - StorePlanFile(fileContents []byte, artifactName string, storedPlanFilePath string) error - RetrievePlan(localPlanFilePath string, artifactName string, storedPlanFilePath string) (*string, error) - DeleteStoredPlan(artifactName string, storedPlanFilePath string) error - PlanExists(artifactName string, storedPlanFilePath string) (bool, error) + StorePlanFile(fileContents []byte, artifactName, storedPlanFilePath string) error + RetrievePlan(localPlanFilePath, artifactName, storedPlanFilePath string) (*string, error) + DeleteStoredPlan(artifactName, storedPlanFilePath string) error + PlanExists(artifactName, storedPlanFilePath string) (bool, error) } diff --git a/next/ci_backends/ci_backends.go b/next/ci_backends/ci_backends.go index 4548dae52..ae3060189 100644 --- a/next/ci_backends/ci_backends.go +++ b/next/ci_backends/ci_backends.go @@ -6,7 +6,7 @@ import ( ) type CiBackend interface { - TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error + TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error GetWorkflowUrl(spec spec.Spec) (string, error) } diff --git a/next/ci_backends/github_actions.go b/next/ci_backends/github_actions.go index d171d2f50..e8c81ee4c 100644 --- a/next/ci_backends/github_actions.go +++ b/next/ci_backends/github_actions.go @@ -3,17 +3,18 @@ package ci_backends import ( "context" "encoding/json" + "log" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "github.com/google/go-github/v61/github" - "log" ) type GithubActionCi struct { Client *github.Client } -func (g GithubActionCi) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error { +func (g GithubActionCi) TriggerWorkflow(spec spec.Spec, runName, vcsToken string) error { log.Printf("TriggerGithubWorkflow: repoOwner: %v, repoName: %v, commentId: %v", spec.VCS.RepoOwner, spec.VCS.RepoName, spec.CommentId) client := g.Client specBytes, err := json.Marshal(spec) diff --git a/next/ci_backends/provider.go b/next/ci_backends/provider.go index 9fd5b2ae2..d74dd35b3 100644 --- a/next/ci_backends/provider.go +++ b/next/ci_backends/provider.go @@ -2,8 +2,9 @@ package ci_backends import ( "fmt" - "github.com/diggerhq/digger/next/utils" "log" + + "github.com/diggerhq/digger/next/utils" ) type CiBackendProvider interface { diff --git a/next/controllers/drift.go b/next/controllers/drift.go index ee896ab13..7cee207fe 100644 --- a/next/controllers/drift.go +++ b/next/controllers/drift.go @@ -4,17 +4,18 @@ import ( "bytes" "encoding/json" "fmt" + "log" + "net/http" + "net/url" + "os" + "time" + "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/ci_backends" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/services" "github.com/diggerhq/digger/next/utils" "github.com/gin-gonic/gin" - "log" - "net/http" - "net/url" - "os" - "time" ) type TriggerDriftRequest struct { @@ -25,7 +26,6 @@ func (d DiggerController) TriggerDriftDetectionForProject(c *gin.Context) { var request TriggerDriftRequest err := c.BindJSON(&request) - if err != nil { log.Printf("Error binding JSON: %v", err) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payload received"}) @@ -95,7 +95,6 @@ func (d DiggerController) TriggerDriftDetectionForProject(c *gin.Context) { "project_id": projectId, }) return - } func (d DiggerController) TriggerCronForMatchingProjects(c *gin.Context) { diff --git a/next/controllers/github.go b/next/controllers/github.go index ecbd39e92..a51f31a6b 100644 --- a/next/controllers/github.go +++ b/next/controllers/github.go @@ -6,6 +6,16 @@ import ( "encoding/json" "errors" "fmt" + "log" + "math/rand" + "net/http" + "net/url" + "os" + "path" + "reflect" + "strconv" + "strings" + "github.com/diggerhq/digger/backend/middleware" "github.com/diggerhq/digger/backend/segment" backend_utils "github.com/diggerhq/digger/backend/utils" @@ -25,15 +35,6 @@ import ( "github.com/samber/lo" "golang.org/x/oauth2" "gorm.io/gorm" - "log" - "math/rand" - "net/http" - "net/url" - "os" - "path" - "reflect" - "strconv" - "strings" ) type DiggerController struct { @@ -105,7 +106,6 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) { } func GithubAppSetup(c *gin.Context) { - type githubWebhook struct { URL string `json:"url"` Active bool `json:"active"` @@ -243,10 +243,9 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) { "WebhookSecret": cfg.GetWebhookSecret(), "URL": cfg.GetHTMLURL(), }) - } -func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisation string, ghRepoName string, ghRepoUrl string, installationId int64) (*model.Repo, *model.Organization, error) { +func createOrGetDiggerRepoForGithubRepo(ghRepoFullName, ghRepoOrganisation, ghRepoName, ghRepoUrl string, installationId int64) (*model.Repo, *model.Organization, error) { link, err := dbmodels.DB.GetGithubInstallationLinkForInstallationId(installationId) if err != nil { log.Printf("Error fetching installation link: %v", err) @@ -322,7 +321,7 @@ func handlePullRequestEvent(gh next_utils.GithubClientProvider, payload *github. repoName := *payload.Repo.Name repoOwner := *payload.Repo.Owner.Login repoFullName := *payload.Repo.FullName - //cloneURL := *payload.Repo.CloneURL + // cloneURL := *payload.Repo.CloneURL prNumber := *payload.PullRequest.Number isDraft := payload.PullRequest.GetDraft() commitSha := payload.PullRequest.Head.GetSHA() @@ -491,14 +490,13 @@ func handlePullRequestEvent(gh next_utils.GithubClientProvider, payload *github. return nil } -func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, repoOwner string, repoName string, batchId string, prNumber int, prService ci.PullRequestService, gh next_utils.GithubClientProvider) error { +func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName, repoOwner, repoName, batchId string, prNumber int, prService ci.PullRequestService, gh next_utils.GithubClientProvider) error { _, err := dbmodels.DB.GetDiggerBatch(batchId) if err != nil { log.Printf("failed to get digger batch, %v\n", err) return fmt.Errorf("failed to get digger batch, %v\n", err) } diggerJobs, err := dbmodels.DB.GetPendingParentDiggerJobs(batchId) - if err != nil { log.Printf("failed to get pending digger jobs, %v\n", err) return fmt.Errorf("failed to get pending digger jobs, %v\n", err) @@ -523,7 +521,7 @@ func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, rep return nil } -func getDiggerConfigForBranch(gh next_utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { +func getDiggerConfigForBranch(gh next_utils.GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName, cloneUrl, branch string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], error) { ghService, token, err := next_utils.GetGithubService(gh, installationId, repoFullName, repoOwner, repoName) if err != nil { log.Printf("Error getting github service: %v", err) @@ -559,7 +557,7 @@ func getDiggerConfigForBranch(gh next_utils.GithubClientProvider, installationId } // TODO: Refactor this func to receive ghService as input -func getDiggerConfigForPR(gh next_utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], *string, *string, error) { +func getDiggerConfigForPR(gh next_utils.GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName, cloneUrl string, prNumber int) (string, *dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], *string, *string, error) { ghService, _, err := next_utils.GetGithubService(gh, installationId, repoFullName, repoOwner, repoName) if err != nil { log.Printf("Error getting github service: %v", err) @@ -583,7 +581,7 @@ func getDiggerConfigForPR(gh next_utils.GithubClientProvider, installationId int return diggerYmlStr, ghService, config, dependencyGraph, &prBranch, &prCommitSha, nil } -func GetRepoByInstllationId(installationId int64, repoOwner string, repoName string) (*model.Repo, error) { +func GetRepoByInstllationId(installationId int64, repoOwner, repoName string) (*model.Repo, error) { link, err := dbmodels.DB.GetGithubAppInstallationLink(installationId) if err != nil { log.Printf("Error getting GetGithubAppInstallationLink: %v", err) @@ -613,7 +611,7 @@ func getBatchType(jobs []orchestrator_scheduler.Job) orchestrator_scheduler.Digg func (d DiggerController) GithubAppCallbackPage(c *gin.Context) { installationId := c.Request.URL.Query()["installation_id"][0] - //setupAction := c.Request.URL.Query()["setup_action"][0] + // setupAction := c.Request.URL.Query()["setup_action"][0] code := c.Request.URL.Query()["code"][0] clientId := os.Getenv("GITHUB_APP_CLIENT_ID") clientSecret := os.Getenv("GITHUB_APP_CLIENT_SECRET") @@ -756,7 +754,7 @@ func (d DiggerController) GithubReposPage(c *gin.Context) { // why this validation is needed: https://roadie.io/blog/avoid-leaking-github-org-data/ // validation based on https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app , step 3 -func validateGithubCallback(githubClientProvider next_utils.GithubClientProvider, clientId string, clientSecret string, code string, installationId int64) (bool, *github.Installation, error) { +func validateGithubCallback(githubClientProvider next_utils.GithubClientProvider, clientId, clientSecret, code string, installationId int64) (bool, *github.Installation, error) { ctx := context.Background() type OAuthAccessResponse struct { AccessToken string `json:"access_token"` diff --git a/next/controllers/github_after_merge.go b/next/controllers/github_after_merge.go index 41f82a527..5d52b4859 100644 --- a/next/controllers/github_after_merge.go +++ b/next/controllers/github_after_merge.go @@ -3,6 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "log" + "os" + "strings" + "github.com/diggerhq/digger/libs/ci/generic" dg_github "github.com/diggerhq/digger/libs/ci/github" dg_configuration "github.com/diggerhq/digger/libs/digger_config" @@ -11,9 +15,6 @@ import ( "github.com/diggerhq/digger/next/services" nextutils "github.com/diggerhq/digger/next/utils" "github.com/google/go-github/v61/github" - "log" - "os" - "strings" ) func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload *github.PushEvent) error { @@ -59,7 +60,7 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * p := dbmodels.DB.Query.Project projects, err := dbmodels.DB.Query.Project.Where(p.RepoID.Eq(repo.ID)).Find() - var dgprojects = []dg_configuration.Project{} + dgprojects := []dg_configuration.Project{} for _, proj := range projects { projectBranch := proj.Branch if targetBranch == projectBranch { @@ -127,7 +128,7 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * impactedProjectsJobMap[j.ProjectName] = j } - for i, _ := range planJobs { + for i := range planJobs { planJob := planJobs[i] applyJob := applyJobs[i] projectName := planJob.ProjectName diff --git a/next/controllers/projects.go b/next/controllers/projects.go index aac9ea857..864e2bc94 100644 --- a/next/controllers/projects.go +++ b/next/controllers/projects.go @@ -3,6 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "log" + "net/http" + "time" + "github.com/diggerhq/digger/backend/models" "github.com/diggerhq/digger/libs/comment_utils/reporting" "github.com/diggerhq/digger/libs/digger_config" @@ -14,9 +18,6 @@ import ( "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/utils" "github.com/gin-gonic/gin" - "log" - "net/http" - "time" ) type SetJobStatusRequest struct { @@ -31,7 +32,7 @@ type SetJobStatusRequest struct { func (d DiggerController) SetJobStatusForProject(c *gin.Context) { jobId := c.Param("jobId") - //orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY) + // orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY) //if !exists { // c.String(http.StatusForbidden, "Not allowed to access this resource") @@ -41,7 +42,6 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { var request SetJobStatusRequest err := c.BindJSON(&request) - if err != nil { log.Printf("Error binding JSON: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"}) @@ -156,7 +156,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { // get batch ID // check if all jobs have succeeded at this point // if so, perform merge of PR (if configured to do so) - //batch := job.Batch + // batch := job.Batch err = dbmodels.DB.UpdateBatchStatus(batch) if err != nil { log.Printf("Error updating batch status: %v", err) diff --git a/next/controllers/runs.go b/next/controllers/runs.go index 1d2c0a7af..79ffa949f 100644 --- a/next/controllers/runs.go +++ b/next/controllers/runs.go @@ -2,14 +2,15 @@ package controllers import ( "fmt" + "log" + "net/http" + "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/services" next_utils "github.com/diggerhq/digger/next/utils" "github.com/gin-gonic/gin" - "log" - "net/http" ) func processSingleQueueItem(gh next_utils.GithubClientProvider, queueItem model.DiggerRunQueueItem) error { @@ -68,7 +69,6 @@ func (d DiggerController) TriggerRunForProjectAssumingUser(c *gin.Context) { var request TriggerRunAssumingUserRequest err := c.BindJSON(&request) - if err != nil { log.Printf("Error binding JSON: %v", err) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payload received"}) @@ -85,7 +85,7 @@ func (d DiggerController) TriggerRunForProjectAssumingUser(c *gin.Context) { return } - //branch := project.Branch + // branch := project.Branch projectName := project.Name r := dbmodels.DB.Query.Repo @@ -98,9 +98,9 @@ func (d DiggerController) TriggerRunForProjectAssumingUser(c *gin.Context) { orgId := repo.OrganizationID repoId := repo.ID - //repoFullName := repo.RepoFullName - //repoOwner := repo.RepoOrganisation - //repoName := repo.RepoName + // repoFullName := repo.RepoFullName + // repoOwner := repo.RepoOrganisation + // repoName := repo.RepoName appInstallation, err := dbmodels.DB.GetGithubAppInstallationByOrgAndRepo(orgId, repo.RepoFullName, dbmodels.GithubAppInstallActive) if err != nil { diff --git a/next/controllers/static.go b/next/controllers/static.go index 4d1e7aa38..3c6bcef6e 100644 --- a/next/controllers/static.go +++ b/next/controllers/static.go @@ -1,8 +1,9 @@ package controllers import ( - "github.com/gin-gonic/gin" "net/http" + + "github.com/gin-gonic/gin" ) func Home(c *gin.Context) { diff --git a/next/dbmodels/scheduler.go b/next/dbmodels/scheduler.go index 0668388a9..48c9dddca 100644 --- a/next/dbmodels/scheduler.go +++ b/next/dbmodels/scheduler.go @@ -3,24 +3,29 @@ package dbmodels import ( "encoding/json" "fmt" + "log" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/model" - "log" ) type DiggerVCSType string -const DiggerVCSGithub DiggerVCSType = "github" -const DiggerVCSGitlab DiggerVCSType = "gitlab" +const ( + DiggerVCSGithub DiggerVCSType = "github" + DiggerVCSGitlab DiggerVCSType = "gitlab" +) type DiggerJobLinkStatus int8 type BatchEventType string -const DiggerBatchMergeEvent = "merge_event" -const DiggerBatchPullRequestEvent = "pull_request_event" -const DiggerBatchDriftEvent = "drift_event" -const DiggerBatchManualTriggerEvent = "manual_trigger" +const ( + DiggerBatchMergeEvent = "merge_event" + DiggerBatchPullRequestEvent = "pull_request_event" + DiggerBatchDriftEvent = "drift_event" + DiggerBatchManualTriggerEvent = "manual_trigger" +) const ( DiggerJobLinkCreated DiggerJobLinkStatus = 1 diff --git a/next/dbmodels/setup.go b/next/dbmodels/setup.go index 5ef335b7d..4a9366a97 100644 --- a/next/dbmodels/setup.go +++ b/next/dbmodels/setup.go @@ -1,13 +1,14 @@ package dbmodels import ( + "log/slog" + "os" + "github.com/diggerhq/digger/next/models_generated" slogGorm "github.com/orandin/slog-gorm" "gorm.io/driver/postgres" _ "gorm.io/driver/postgres" "gorm.io/gorm" - "log/slog" - "os" ) type Database struct { @@ -30,7 +31,6 @@ func ConnectDatabase() { database, err := gorm.Open(postgres.Open(os.Getenv("DIGGER_DATABASE_URL")), &gorm.Config{ Logger: gormLogger, }) - if err != nil { panic("Failed to connect to database!") } @@ -40,5 +40,4 @@ func ConnectDatabase() { Query: query, GormDB: database, } - } diff --git a/next/dbmodels/storage.go b/next/dbmodels/storage.go index 3bfae9203..06969b398 100644 --- a/next/dbmodels/storage.go +++ b/next/dbmodels/storage.go @@ -4,6 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "log" + "net/http" + "time" + "github.com/dchest/uniuri" configuration "github.com/diggerhq/digger/libs/digger_config" "github.com/diggerhq/digger/libs/scheduler" @@ -12,9 +16,6 @@ import ( "github.com/google/uuid" "github.com/samber/lo" "gorm.io/gorm" - "log" - "net/http" - "time" ) func (db *Database) GetProjectsFromContext(c *gin.Context, orgIdKey string) ([]model.Project, bool) { @@ -33,7 +34,6 @@ func (db *Database) GetProjectsFromContext(c *gin.Context, orgIdKey string) ([]m Joins("INNER JOIN repos ON projects.repo_id = repos.id"). Joins("INNER JOIN organizations ON projects.organization_id = organizations.id"). Where("projects.organization_id = ?", loggedInOrganisationId).Find(&projects).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, false @@ -58,7 +58,6 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]mode err := db.GormDB. Joins("INNER JOIN organizations ON repos.organization_id = organizations.id"). Where("repos.organization_id = ?", loggedInOrganisationId).Find(&repos).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, false @@ -171,7 +170,6 @@ func (db *Database) GetProjectByProjectId(c *gin.Context, projectId uint, orgIdK Joins("INNER JOIN organizations ON projects.organization_id = organizations.id"). Where("projects.organization_id = ?", loggedInOrganisationId). Where("projects.id = ?", projectId).First(&project).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, false @@ -187,7 +185,6 @@ func (db *Database) GetProject(projectId string) (*model.Project, error) { err := db.GormDB. Where("id = ?", projectId). First(&project).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, err @@ -208,7 +205,6 @@ func (db *Database) GetProjectByName(orgId any, repo *model.Repo, name string) ( Where("projects.organization_id = ?", orgId). Where("repos.id = ?", repo.ID). Where("projects.name = ?", name).First(&project).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, err @@ -227,7 +223,6 @@ func (db *Database) GetProjectByRepo(orgId any, repo *model.Repo) ([]model.Proje Joins("INNER JOIN organizations ON projects.organization_id = organizations.id"). Where("projects.organization_id = ?", orgId). Where("repos.id = ?", repo.ID).Find(&projects).Error - if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil @@ -286,7 +281,6 @@ func (db *Database) GetDefaultRepo(c *gin.Context, orgIdKey string) (*model.Repo err := db.GormDB. Joins("INNER JOIN organizations ON repos.organization_id = organizations.id"). Where("organizations.id = ?", loggedInOrganisationId).First(&repo).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, false @@ -303,7 +297,6 @@ func (db *Database) GetRepo(orgIdKey any, repoName string) (*model.Repo, error) err := db.GormDB. Joins("INNER JOIN organizations ON repos.organization_id = organizations.id"). Where("organizations.id = ? AND repos.name=?", orgIdKey, repoName).First(&repo).Error - if err != nil { log.Printf("Failed to find digger repo for orgId: %v, and repoName: %v, error: %v\n", orgIdKey, repoName, err) return nil, err @@ -317,7 +310,6 @@ func (db *Database) GetRepoByFullName(orgIdKey any, repofullName string) (*model err := db.GormDB. Joins("INNER JOIN organizations ON repos.organization_id = organizations.id"). Where("organizations.id = ? AND repos.repo_full_name=?", orgIdKey, repofullName).First(&repo).Error - if err != nil { log.Printf("Failed to find digger repo for orgId: %v, and repoName: %v, error: %v\n", orgIdKey, repofullName, err) return nil, err @@ -337,13 +329,12 @@ func (db *Database) GetRepoById(repoId int64) (*model.Repo, error) { } // GetRepoByOrgAndId returns digger repo by organisationId and repo name (diggerhq-digger) -func (db *Database) GetRepoByOrgAndId(orgIdKey any, repoId any) (*model.Repo, error) { +func (db *Database) GetRepoByOrgAndId(orgIdKey, repoId any) (*model.Repo, error) { var repo model.Repo err := db.GormDB. Joins("INNER JOIN organizations ON repos.organization_id = organizations.id"). Where("organizations.id = ? AND repos.ID=?", orgIdKey, repoId).First(&repo).Error - if err != nil { log.Printf("Failed to find digger repo for orgId: %v, and repoId: %v, error: %v\n", orgIdKey, repoId, err) return nil, err @@ -352,8 +343,7 @@ func (db *Database) GetRepoByOrgAndId(orgIdKey any, repoId any) (*model.Repo, er } // GithubRepoAdded handles github drift that github repo has been added to the app installation -func (db *Database) GithubRepoAdded(installationId int64, appId int64, login string, accountId int64, repoFullName string) (*model.GithubAppInstallation, error) { - +func (db *Database) GithubRepoAdded(installationId, appId int64, login string, accountId int64, repoFullName string) (*model.GithubAppInstallation, error) { // check if item exist already item := &model.GithubAppInstallation{} result := db.GormDB.Where("github_installation_id = ? AND repo=? AND github_app_id=?", installationId, repoFullName, appId).First(item) @@ -381,7 +371,7 @@ func (db *Database) GithubRepoAdded(installationId int64, appId int64, login str return item, nil } -func (db *Database) GithubRepoRemoved(installationId int64, appId int64, repoFullName string, orgId string) (*model.GithubAppInstallation, error) { +func (db *Database) GithubRepoRemoved(installationId, appId int64, repoFullName, orgId string) (*model.GithubAppInstallation, error) { item := &model.GithubAppInstallation{} err := db.GormDB.Where("github_installation_id = ? AND status=? AND github_app_id=? AND repo=?", installationId, GithubAppInstallActive, appId, repoFullName).First(item).Error if err != nil { @@ -518,7 +508,7 @@ func (db *Database) CreateGithubInstallationLink(org *model.Organization, instal var list []model.GithubAppInstallationLink // if there are other installation for this org, we need to make them inactive - //orgstbname := db.Query.Organization.TableName() + // orgstbname := db.Query.Organization.TableName() result = db.GormDB.Where("github_installation_id <> ? AND organization_id = ? AND status=?", installationId, org.ID, GithubAppInstallationLinkActive).Find(&list) if result.Error != nil { if !errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -650,7 +640,7 @@ func (db *Database) GetDiggerBatch(batchId string) (*model.DiggerBatch, error) { return batch, nil } -func (db *Database) CreateDiggerBatch(orgid string, vcsType DiggerVCSType, githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, diggerConfig string, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, batchEventType BatchEventType) (*model.DiggerBatch, error) { +func (db *Database) CreateDiggerBatch(orgid string, vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, PRNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, batchEventType BatchEventType) (*model.DiggerBatch, error) { uid := uuid.New() batch := &model.DiggerBatch{ ID: uid.String(), @@ -713,7 +703,6 @@ func (db *Database) UpdateBatchStatus(batch *model.DiggerBatch) error { } return nil - } func (db *Database) CreateDiggerJob(batchId string, serializedJob []byte, workflowFile string) (*model.DiggerJob, error) { @@ -730,8 +719,10 @@ func (db *Database) CreateDiggerJob(batchId string, serializedJob []byte, workfl } workflowUrl := "#" - job := &model.DiggerJob{DiggerJobID: jobId, Status: int16(scheduler.DiggerJobCreated), - BatchID: batchIdStr, JobSpec: serializedJob, DiggerJobSummaryID: summary.ID, WorkflowRunURL: workflowUrl, WorkflowFile: workflowFile} + job := &model.DiggerJob{ + DiggerJobID: jobId, Status: int16(scheduler.DiggerJobCreated), + BatchID: batchIdStr, JobSpec: serializedJob, DiggerJobSummaryID: summary.ID, WorkflowRunURL: workflowUrl, WorkflowFile: workflowFile, + } result = db.GormDB.Save(job) if result.Error != nil { return nil, result.Error @@ -746,7 +737,6 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ err := db.GormDB. Where("project_name = ? AND repo_id= ?", projectName, repoId).Order("created_at desc").Find(&runs).Error - if err != nil { log.Printf("Unknown error occurred while fetching database, %v\n", err) return nil, err @@ -756,7 +746,7 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ return runs, nil } -func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId int64, projectId string, ProjectName string, RunType RunType, planStageId string, applyStageId string, triggeredByUserId *string) (*model.DiggerRun, error) { +func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId, DiggerConfig string, GithubInstallationId, RepoId int64, projectId, ProjectName string, RunType RunType, planStageId, applyStageId string, triggeredByUserId *string) (*model.DiggerRun, error) { dr := &model.DiggerRun{ ID: uuid.NewString(), Triggertype: Triggertype, @@ -826,7 +816,7 @@ func (db *Database) GetDiggerRunStage(id string) (*model.DiggerRunStage, error) return drs, nil } -func (db *Database) CreateDiggerRunQueueItem(diggeRrunId string, projectId string) (*model.DiggerRunQueueItem, error) { +func (db *Database) CreateDiggerRunQueueItem(diggeRrunId, projectId string) (*model.DiggerRunQueueItem, error) { drq := &model.DiggerRunQueueItem{ DiggerRunID: diggeRrunId, ProjectID: projectId, @@ -930,7 +920,7 @@ WHERE return runqueuesWithData, nil } -func (db *Database) UpdateDiggerJobSummary(diggerJobSummaryId string, resourcesCreated uint, resourcesUpdated uint, resourcesDeleted uint) (*model.DiggerJob, error) { +func (db *Database) UpdateDiggerJobSummary(diggerJobSummaryId string, resourcesCreated, resourcesUpdated, resourcesDeleted uint) (*model.DiggerJob, error) { diggerJob, err := db.GetDiggerJob(diggerJobSummaryId) if err != nil { return nil, fmt.Errorf("Could not get digger job") @@ -1052,7 +1042,7 @@ func (db *Database) GetDiggerJobParentLinksByParentId(parentId *string) ([]model return jobParentLinks, nil } -func (db *Database) CreateDiggerJobParentLink(parentJobId string, jobId string) error { +func (db *Database) CreateDiggerJobParentLink(parentJobId, jobId string) error { jobParentLink := model.DiggerJobParentLink{ParentDiggerJobID: parentJobId, DiggerJobID: jobId} result := db.GormDB.Create(&jobParentLink) if result.Error != nil { @@ -1097,7 +1087,7 @@ func (db *Database) GetOrganisation(tenantId any) (*model.Organization, error) { // return org, nil //} -func (db *Database) CreateProject(name string, org *model.Organization, repo *model.Repo, isGenerated bool, isInMainBranch bool) (*model.Project, error) { +func (db *Database) CreateProject(name string, org *model.Organization, repo *model.Repo, isGenerated, isInMainBranch bool) (*model.Project, error) { project := &model.Project{ Name: name, OrganizationID: org.ID, @@ -1124,7 +1114,7 @@ func (db *Database) UpdateProject(project *model.Project) error { return nil } -func (db *Database) CreateRepo(name string, repoFullName string, repoOrganisation string, repoName string, repoUrl string, org *model.Organization, diggerConfig string) (*model.Repo, error) { +func (db *Database) CreateRepo(name, repoFullName, repoOrganisation, repoName, repoUrl string, org *model.Organization, diggerConfig string) (*model.Repo, error) { var repo model.Repo // check if repo exist already, do nothing in this case result := db.GormDB.Where("name = ? AND organization_id=?", name, org.ID).Find(&repo) @@ -1169,7 +1159,6 @@ func (db *Database) CreateRepo(name string, repoFullName string, repoOrganisatio //} func (db *Database) CreateDiggerJobToken(organisationId string) (*model.DiggerJobToken, error) { - // create a digger job token // prefixing token to make easier to retire this type of tokens later token := "cli:" + uuid.New().String() @@ -1225,7 +1214,7 @@ func (db *Database) GetJobToken(tenantId any) (*model.DiggerJobToken, error) { return token, nil } -func (db *Database) CreateGithubAppInstallation(installationId int64, githubAppId int64, login string, accountId int, repoFullName string) (*model.GithubAppInstallation, error) { +func (db *Database) CreateGithubAppInstallation(installationId, githubAppId int64, login string, accountId int, repoFullName string) (*model.GithubAppInstallation, error) { installation := &model.GithubAppInstallation{ GithubInstallationID: installationId, GithubAppID: githubAppId, @@ -1295,7 +1284,6 @@ func (db *Database) UpdateRepoDiggerConfig(orgId string, config configuration.Di } return nil }) - if err != nil { return fmt.Errorf("error while updating projects from config: %v", err) } diff --git a/next/main.go b/next/main.go index fd22bd5d9..9aa320945 100644 --- a/next/main.go +++ b/next/main.go @@ -3,6 +3,12 @@ package main import ( "embed" "fmt" + "io/fs" + "log" + "log/slog" + "net/http" + "os" + "github.com/diggerhq/digger/backend/config" "github.com/diggerhq/digger/next/ci_backends" controllers "github.com/diggerhq/digger/next/controllers" @@ -13,11 +19,6 @@ import ( sentrygin "github.com/getsentry/sentry-go/gin" "github.com/gin-gonic/gin" sloggin "github.com/samber/slog-gin" - "io/fs" - "log" - "log/slog" - "net/http" - "os" ) //go:embed templates @@ -32,7 +33,6 @@ func init() { var Version = "dev" func main() { - sentryDsn := os.Getenv("SENTRY_DSN") if sentryDsn != "" { if err := sentry.Init(sentry.ClientOptions{ @@ -96,11 +96,10 @@ func main() { r.POST("/_internal/trigger_drift", middleware.WebhookAuth(), diggerController.TriggerDriftDetectionForProject) r.POST("/_internal/runs", middleware.WebhookAuth(), diggerController.TriggerRunForProjectAssumingUser) - //authorized := r.Group("/") - //authorized.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(dbmodels.CliJobAccessType, dbmodels.AccessPolicyType, models.AdminPolicyType)) + // authorized := r.Group("/") + // authorized.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(dbmodels.CliJobAccessType, dbmodels.AccessPolicyType, models.AdminPolicyType)) r.POST("/repos/:repo/projects/:projectName/jobs/:jobId/set-status", middleware.JobTokenAuth(), diggerController.SetJobStatusForProject) port := config.GetPort() r.Run(fmt.Sprintf(":%d", port)) - } diff --git a/next/middleware/job_token_auth.go b/next/middleware/job_token_auth.go index 44ff982ab..dedc8fbfa 100644 --- a/next/middleware/job_token_auth.go +++ b/next/middleware/job_token_auth.go @@ -2,13 +2,14 @@ package middleware import ( "fmt" - "github.com/diggerhq/digger/next/dbmodels" - "github.com/diggerhq/digger/next/model" - "github.com/gin-gonic/gin" "log" "net/http" "strings" "time" + + "github.com/diggerhq/digger/next/dbmodels" + "github.com/diggerhq/digger/next/model" + "github.com/gin-gonic/gin" ) func CheckJobToken(c *gin.Context, token string) (*model.DiggerJobToken, error) { diff --git a/next/middleware/middleware.go b/next/middleware/middleware.go index 3f3fb079b..1f467e23b 100644 --- a/next/middleware/middleware.go +++ b/next/middleware/middleware.go @@ -1,4 +1,6 @@ package middleware -const ORGANISATION_ID_KEY = "organisation_ID" -const ACCESS_LEVEL_KEY = "access_level" +const ( + ORGANISATION_ID_KEY = "organisation_ID" + ACCESS_LEVEL_KEY = "access_level" +) diff --git a/next/middleware/supabase_cookie_auth.go b/next/middleware/supabase_cookie_auth.go index b1ee542e9..17dc20550 100644 --- a/next/middleware/supabase_cookie_auth.go +++ b/next/middleware/supabase_cookie_auth.go @@ -3,13 +3,14 @@ package middleware import ( "encoding/json" "fmt" + "log" + "net/http" + "os" + "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/supa" "github.com/gin-gonic/gin" - "log" - "net/http" - "os" ) func SupabaseCookieAuth() gin.HandlerFunc { diff --git a/next/middleware/webhooks.go b/next/middleware/webhooks.go index 60ff1afac..c46694b09 100644 --- a/next/middleware/webhooks.go +++ b/next/middleware/webhooks.go @@ -1,10 +1,11 @@ package middleware import ( - "github.com/gin-gonic/gin" "net/http" "os" "strings" + + "github.com/gin-gonic/gin" ) func WebhookAuth() gin.HandlerFunc { diff --git a/next/services/config.go b/next/services/config.go index 29d79af04..60b9ffe89 100644 --- a/next/services/config.go +++ b/next/services/config.go @@ -2,14 +2,15 @@ package services import ( "fmt" + "log" + dg_configuration "github.com/diggerhq/digger/libs/digger_config" utils3 "github.com/diggerhq/digger/libs/git_utils" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/utils" - "log" ) -func GetWorkflowsForRepoAndBranch(gh utils.GithubClientProvider, repoId int64, branch string, commitHash string) (map[string]dg_configuration.Workflow, error) { +func GetWorkflowsForRepoAndBranch(gh utils.GithubClientProvider, repoId int64, branch, commitHash string) (map[string]dg_configuration.Workflow, error) { r := dbmodels.DB.Query.Repo repo, err := dbmodels.DB.Query.Repo.Where(r.ID.Eq(repoId)).First() if err != nil { @@ -53,7 +54,6 @@ func GetWorkflowsForRepoAndBranch(gh utils.GithubClientProvider, repoId int64, b } return nil }) - if err != nil { log.Printf("could not load digger config :%v", err) return nil, fmt.Errorf("could not load digger config :%v", err) diff --git a/next/services/drift.go b/next/services/drift.go index 41515b711..d2653d845 100644 --- a/next/services/drift.go +++ b/next/services/drift.go @@ -3,11 +3,12 @@ package services import ( "encoding/json" "fmt" + "log" + "strings" + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/model" - "log" - "strings" ) func SaveUpdatedDriftStatus(batch model.DiggerBatch, job model.DiggerJob, terraformOutput string) error { diff --git a/next/services/runs.go b/next/services/runs.go index 3d8739eb1..a899ceaca 100644 --- a/next/services/runs.go +++ b/next/services/runs.go @@ -2,6 +2,9 @@ package services import ( "fmt" + "log" + "time" + "github.com/diggerhq/digger/libs/ci" "github.com/diggerhq/digger/libs/ci/github" orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" @@ -9,8 +12,6 @@ import ( "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/model" nextutils "github.com/diggerhq/digger/next/utils" - "log" - "time" ) func RunQueuesStateMachine(queueItem *model.DiggerRunQueueItem, service ci.PullRequestService, gh nextutils.GithubClientProvider) error { diff --git a/next/services/scheduler.go b/next/services/scheduler.go index c97fe4e6c..0dfc5d34c 100644 --- a/next/services/scheduler.go +++ b/next/services/scheduler.go @@ -3,6 +3,10 @@ package services import ( "encoding/json" "fmt" + "log" + "os" + "strconv" + "github.com/diggerhq/digger/libs/ci/generic" dg_configuration "github.com/diggerhq/digger/libs/digger_config" orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" @@ -11,12 +15,9 @@ import ( "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/utils" "github.com/dominikbraun/graph" - "log" - "os" - "strconv" ) -func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId string, job *model.DiggerJob, gh utils.GithubClientProvider) error { +func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname, repoOwner, repoName, batchId string, job *model.DiggerJob, gh utils.GithubClientProvider) error { maxConcurrencyForBatch, err := strconv.Atoi(os.Getenv("MAX_DIGGER_CONCURRENCY_PER_BATCH")) if err != nil { log.Printf("WARN: could not get max concurrency for batch, setting it to 0: %v", err) @@ -57,7 +58,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner return nil } -func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId string, job *model.DiggerJob) error { +func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, repoFullname, repoOwner, repoName, batchId string, job *model.DiggerJob) error { log.Printf("TriggerJob jobId: %v", job.DiggerJobID) if job.JobSpec == nil { @@ -113,7 +114,7 @@ func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, return nil } -func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projectId string, command string, event dbmodels.BatchEventType, batchType orchestrator_scheduler.DiggerCommand) (*string, *string, error) { +func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projectId, command string, event dbmodels.BatchEventType, batchType orchestrator_scheduler.DiggerCommand) (*string, *string, error) { p := dbmodels.DB.Query.Project project, err := dbmodels.DB.Query.Project.Where(p.ID.Eq(projectId)).First() if err != nil { @@ -143,7 +144,7 @@ func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projec installationId := appInstallation.GithubInstallationID log.Printf("installation id is: %v", installationId) - var dgprojects = []dg_configuration.Project{dbmodels.ToDiggerProject(project)} + dgprojects := []dg_configuration.Project{dbmodels.ToDiggerProject(project)} projectsGraph, err := dg_configuration.CreateProjectDependencyGraph(dgprojects) workflows, err := GetWorkflowsForRepoAndBranch(gh, repo.ID, branch, "") if err != nil { @@ -200,10 +201,9 @@ func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projec } return batchId, &commitSha, nil - } -func ConvertJobsToDiggerJobs(jobType orchestrator_scheduler.DiggerCommand, vcsType dbmodels.DiggerVCSType, organisationId string, jobsMap map[string]orchestrator_scheduler.Job, projectMap map[string]dg_configuration.Project, projectsGraph graph.Graph[string, dg_configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner string, repoName string, repoFullName string, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, batchEventType dbmodels.BatchEventType) (*string, []*model.DiggerJob, error) { +func ConvertJobsToDiggerJobs(jobType orchestrator_scheduler.DiggerCommand, vcsType dbmodels.DiggerVCSType, organisationId string, jobsMap map[string]orchestrator_scheduler.Job, projectMap map[string]dg_configuration.Project, projectsGraph graph.Graph[string, dg_configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner, repoName, repoFullName, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, batchEventType dbmodels.BatchEventType) (*string, []*model.DiggerJob, error) { result := make([]*model.DiggerJob, 0) organisation, err := dbmodels.DB.GetOrganisationById(organisationId) if err != nil { @@ -236,7 +236,7 @@ func ConvertJobsToDiggerJobs(jobType orchestrator_scheduler.DiggerCommand, vcsTy if err != nil { return nil, nil, fmt.Errorf("failed to create batch: %v", err) } - for pname, _ := range marshalledJobsMap { + for pname := range marshalledJobsMap { _, err := dbmodels.DB.CreateDiggerJob(batch.ID, marshalledJobsMap[pname], projectMap[pname].WorkflowFile) if err != nil { return nil, nil, fmt.Errorf("failed to create job: %v %v", pname, err) diff --git a/next/services/spec.go b/next/services/spec.go index 645f3e71d..41d01dc54 100644 --- a/next/services/spec.go +++ b/next/services/spec.go @@ -3,16 +3,17 @@ package services import ( "encoding/json" "fmt" + "log" + "os" + "strconv" + "strings" + "github.com/diggerhq/digger/libs/digger_config" "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/libs/spec" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/utils" - "log" - "os" - "strconv" - "strings" ) func GetVCSTokenFromJob(job model.DiggerJob, gh utils.GithubClientProvider) (*string, error) { diff --git a/next/supa/supa.go b/next/supa/supa.go index c5941b76a..432887f21 100644 --- a/next/supa/supa.go +++ b/next/supa/supa.go @@ -2,8 +2,9 @@ package supa import ( "fmt" - "github.com/supabase-community/supabase-go" "os" + + "github.com/supabase-community/supabase-go" ) var client *supabase.Client = nil diff --git a/next/utils/crontab.go b/next/utils/crontab.go index 2ad614317..1e2d488a9 100644 --- a/next/utils/crontab.go +++ b/next/utils/crontab.go @@ -2,8 +2,9 @@ package utils import ( "fmt" - "github.com/robfig/cron/v3" "time" + + "github.com/robfig/cron/v3" ) func MatchesCrontab(cronString string, timestamp time.Time) (bool, error) { diff --git a/next/utils/crontab_test.go b/next/utils/crontab_test.go index c6e4864cd..47b500916 100644 --- a/next/utils/crontab_test.go +++ b/next/utils/crontab_test.go @@ -2,9 +2,10 @@ package utils import ( "fmt" - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestCrontTabMatching(t *testing.T) { @@ -27,5 +28,4 @@ func TestCrontTabMatching(t *testing.T) { return } assert.False(t, matches) - } diff --git a/next/utils/github.go b/next/utils/github.go index cbf50acf1..b0e0f545d 100644 --- a/next/utils/github.go +++ b/next/utils/github.go @@ -4,17 +4,18 @@ import ( "context" "encoding/base64" "fmt" + "log" + net "net/http" + "os" + "strings" + "time" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/diggerhq/digger/libs/ci" github2 "github.com/diggerhq/digger/libs/ci/github" "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/dbmodels" "github.com/google/go-github/v61/github" - "log" - net "net/http" - "os" - "strings" - "time" ) func createTempDir() string { @@ -34,8 +35,7 @@ func GetGithubHostname() string { } // just a wrapper around github client to be able to use mocks -type DiggerGithubRealClientProvider struct { -} +type DiggerGithubRealClientProvider struct{} type DiggerGithubClientMockProvider struct { MockedHTTPClient *net.Client @@ -43,7 +43,7 @@ type DiggerGithubClientMockProvider struct { type GithubClientProvider interface { NewClient(netClient *net.Client) (*github.Client, error) - Get(githubAppId int64, installationId int64) (*github.Client, *string, error) + Get(githubAppId, installationId int64) (*github.Client, *string, error) FetchCredentials(githubAppId string) (string, string, string, string, error) } @@ -52,7 +52,7 @@ func (gh DiggerGithubRealClientProvider) NewClient(netClient *net.Client) (*gith return ghClient, nil } -func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubRealClientProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { githubAppPrivateKey := "" githubAppPrivateKeyB64 := os.Getenv("GITHUB_APP_PRIVATE_KEY_BASE64") if githubAppPrivateKeyB64 != "" { @@ -91,12 +91,13 @@ func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId i func (gh DiggerGithubRealClientProvider) FetchCredentials(githubAppId string) (string, string, string, string, error) { return "", "", "", "", nil } + func (gh DiggerGithubClientMockProvider) NewClient(netClient *net.Client) (*github.Client, error) { ghClient := github.NewClient(gh.MockedHTTPClient) return ghClient, nil } -func (gh DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { +func (gh DiggerGithubClientMockProvider) Get(githubAppId, installationId int64) (*github.Client, *string, error) { ghClient, _ := gh.NewClient(gh.MockedHTTPClient) token := "token" return ghClient, &token, nil @@ -116,7 +117,8 @@ func GetGithubClient(gh GithubClientProvider, installationId int64, repoFullName ghClient, token, err := gh.Get(installation.GithubAppID, installation.GithubInstallationID) return ghClient, token, err } -func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) { + +func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName, repoOwner, repoName string) (*github2.GithubService, *string, error) { ghClient, token, err := GetGithubClient(gh, installationId, repoFullName) if err != nil { log.Printf("Error creating github app client: %v", err) @@ -177,7 +179,7 @@ func SetPRStatusForJobs(prService ci.PullRequestService, prNumber int, jobs []sc return nil } -func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (int64, string, error) { +func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner, repoName, diggerJobID string) (int64, string, error) { timeFilter := time.Now().Add(-5 * time.Minute) runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), repoOwner, repoName, &github.ListWorkflowRunsOptions{ Created: ">=" + timeFilter.Format(time.RFC3339), @@ -199,7 +201,6 @@ func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, return *workflowRun.ID, fmt.Sprintf("https://github.com/%v/%v/actions/runs/%v", repoOwner, repoName, *workflowRun.ID), nil } } - } } return 0, "#", fmt.Errorf("workflow not found") From eaa833c786a61da027c8417a2cb0a4c1f40e16d5 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 22:35:22 +0530 Subject: [PATCH 02/33] format code merged after sync --- libs/digger_config/terragrunt/atlantis/base_blocks.go | 5 +++-- libs/digger_config/terragrunt/atlantis/generate.go | 2 +- .../terragrunt/atlantis/generate_test.go | 11 +++++------ libs/digger_config/terragrunt/atlantis/parse_hcl.go | 3 +-- .../digger_config/terragrunt/atlantis/parse_locals.go | 8 +++----- .../terragrunt/atlantis/partial_parse.go | 7 ++++--- next/controllers/projects.go | 1 + 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libs/digger_config/terragrunt/atlantis/base_blocks.go b/libs/digger_config/terragrunt/atlantis/base_blocks.go index 9cac060b2..55b2fe157 100644 --- a/libs/digger_config/terragrunt/atlantis/base_blocks.go +++ b/libs/digger_config/terragrunt/atlantis/base_blocks.go @@ -2,6 +2,9 @@ package atlantis import ( "fmt" + "log/slog" + "strings" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" "github.com/gruntwork-io/terragrunt/options" @@ -11,8 +14,6 @@ import ( "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/gocty" - "log/slog" - "strings" ) const ( diff --git a/libs/digger_config/terragrunt/atlantis/generate.go b/libs/digger_config/terragrunt/atlantis/generate.go index 8db73886c..30869cc28 100644 --- a/libs/digger_config/terragrunt/atlantis/generate.go +++ b/libs/digger_config/terragrunt/atlantis/generate.go @@ -166,7 +166,7 @@ func getDependencies(ignoreParentTerragrunt, ignoreDependencyBlocks bool, gitRoo getDependenciesCache.set(path, getDependenciesOutput{nil, err}) return nil, err } - //locals := ResolvedLocals{} + // locals := ResolvedLocals{} // Get deps from locals if locals.ExtraAtlantisDependencies != nil { diff --git a/libs/digger_config/terragrunt/atlantis/generate_test.go b/libs/digger_config/terragrunt/atlantis/generate_test.go index b2dc2012e..be4bb92a7 100644 --- a/libs/digger_config/terragrunt/atlantis/generate_test.go +++ b/libs/digger_config/terragrunt/atlantis/generate_test.go @@ -1,12 +1,13 @@ package atlantis import ( - "github.com/stretchr/testify/assert" - "golang.org/x/sync/singleflight" - "gopkg.in/yaml.v3" "log/slog" "os" "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/sync/singleflight" + "gopkg.in/yaml.v3" ) func init() { @@ -20,14 +21,13 @@ func init() { } func resetForRun() error { - // reset caches getDependenciesCache = newGetDependenciesCache() requestGroup = singleflight.Group{} return nil } -func runTest(t *testing.T, goldenFile string, testPath string, createProjectName bool, workflowName string, withWorkspace bool, parallel bool, ignoreParentTerragrunt bool, ignoreDependencyBlocks bool, cascadeDependencies bool) { +func runTest(t *testing.T, goldenFile, testPath string, createProjectName bool, workflowName string, withWorkspace, parallel, ignoreParentTerragrunt, ignoreDependencyBlocks, cascadeDependencies bool) { resetForRun() atlantisConfig, _, err := Parse( testPath, @@ -51,7 +51,6 @@ func runTest(t *testing.T, goldenFile string, testPath string, createProjectName false, false, ) - if err != nil { slog.Error("failed to parse terragrunt configuration", "error", err) t.Fatal(err) diff --git a/libs/digger_config/terragrunt/atlantis/parse_hcl.go b/libs/digger_config/terragrunt/atlantis/parse_hcl.go index 4b00b96bf..6a4428879 100644 --- a/libs/digger_config/terragrunt/atlantis/parse_hcl.go +++ b/libs/digger_config/terragrunt/atlantis/parse_hcl.go @@ -1,9 +1,8 @@ package atlantis import ( - "path/filepath" - "log/slog" + "path/filepath" "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" diff --git a/libs/digger_config/terragrunt/atlantis/parse_locals.go b/libs/digger_config/terragrunt/atlantis/parse_locals.go index e964bbba2..425024224 100644 --- a/libs/digger_config/terragrunt/atlantis/parse_locals.go +++ b/libs/digger_config/terragrunt/atlantis/parse_locals.go @@ -5,9 +5,8 @@ package atlantis // parses the `locals` blocks and evaluates their contents. import ( - "path/filepath" - "log/slog" + "path/filepath" "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" @@ -43,8 +42,7 @@ type ResolvedLocals struct { } // parseHcl uses the HCL2 parser to parse the given string into an HCL file body. -func parseHcl(parser *hclparse.Parser, hcl string, filename string) (file *hcl.File, err error) { - +func parseHcl(parser *hclparse.Parser, hcl, filename string) (file *hcl.File, err error) { // The HCL2 parser and especially cty conversions will panic in many types of errors, so we have to recover from // those panics here and convert them to normal errors defer func() { @@ -108,7 +106,7 @@ func parseLocals(path string, terragruntOptions *options.TerragruntOptions, incl return ResolvedLocals{}, err } - //Parse the HCL string into an AST body + // Parse the HCL string into an AST body parser := hclparse.NewParser() file, err := parseHcl(parser, configString, path) if err != nil { diff --git a/libs/digger_config/terragrunt/atlantis/partial_parse.go b/libs/digger_config/terragrunt/atlantis/partial_parse.go index f88134e2e..49fe7ebb2 100644 --- a/libs/digger_config/terragrunt/atlantis/partial_parse.go +++ b/libs/digger_config/terragrunt/atlantis/partial_parse.go @@ -3,6 +3,8 @@ package atlantis import ( "encoding/json" "fmt" + "path/filepath" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/config" "github.com/gruntwork-io/terragrunt/options" @@ -12,7 +14,6 @@ import ( "github.com/hashicorp/hcl/v2/hclparse" "github.com/zclconf/go-cty/cty" ctyjson "github.com/zclconf/go-cty/cty/json" - "path/filepath" ) // terragruntDependencies is a struct that can be used to only decode the dependencies block. @@ -418,8 +419,8 @@ func TerragruntConfigFromPartialConfigString( decodeList []config.PartialDecodeSectionType, ) (*config.TerragruntConfig, error) { if terragruntOptions.UsePartialParseConfigCache { - var cacheKey = fmt.Sprintf("%#v-%#v-%#v-%#v", filename, configString, includeFromChild, decodeList) - var config, found = terragruntConfigCache.Get(cacheKey) + cacheKey := fmt.Sprintf("%#v-%#v-%#v-%#v", filename, configString, includeFromChild, decodeList) + config, found := terragruntConfigCache.Get(cacheKey) if !found { terragruntOptions.Logger.Debugf("Cache miss for '%s' (partial parsing), decodeList: '%v'.", filename, decodeList) diff --git a/next/controllers/projects.go b/next/controllers/projects.go index 864e2bc94..632be6a9c 100644 --- a/next/controllers/projects.go +++ b/next/controllers/projects.go @@ -14,6 +14,7 @@ import ( orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/services" + //"github.com/diggerhq/digger/next/middleware" "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/utils" From e0e41597f1a872678c8c6c232cd93eb385687448 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:06:46 +0530 Subject: [PATCH 03/33] add staticcheck linter in .golangci.yml --- .golangci.yml | 6 ++++++ go.work | 8 +++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index aedeaa770..1e698c37e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,3 +19,9 @@ formatters: # Choose whether to use the extra rules. # Default: false extra-rules: true + +# See: https://golangci-lint.run/usage/linters/ +linters: + default: none + enable: + - staticcheck diff --git a/go.work b/go.work index 4122be1a9..801479b65 100644 --- a/go.work +++ b/go.work @@ -6,14 +6,12 @@ use ( ./cli ./cli_e2e ./dgctl - ./next - ./next/dbgen - + ./ee/backend ./ee/cli ./ee/drift ./libs - - + ./next + ./next/dbgen ) From cb49375a8438960d9217962604b9aacf2c493459 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:07:25 +0530 Subject: [PATCH 04/33] staticcheck: lint files with autofix --- backend/bootstrap/main.go | 9 +++++---- backend/controllers/github.go | 14 +++++++------- backend/controllers/projects.go | 10 +++++----- backend/controllers/runs.go | 2 +- backend/models/storage.go | 18 +++++++----------- backend/utils/pr_comment.go | 4 ++-- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/backend/bootstrap/main.go b/backend/bootstrap/main.go index 31af388f7..4df18fed4 100644 --- a/backend/bootstrap/main.go +++ b/backend/bootstrap/main.go @@ -261,13 +261,14 @@ func initLogging() { logLevel := os.Getenv("DIGGER_LOG_LEVEL") var level slog.Leveler - if logLevel == "DEBUG" { + switch logLevel { + case "DEBUG": level = slog.LevelDebug - } else if logLevel == "WARN" { + case "WARN": level = slog.LevelWarn - } else if logLevel == "ERROR" { + case "ERROR": level = slog.LevelError - } else { + default: level = slog.LevelInfo } diff --git a/backend/controllers/github.go b/backend/controllers/github.go index 08e4d9132..cb2e125a2 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -1746,7 +1746,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "issueNumber", issueNumber, "comment", *payload.Comment.Body, ) - commentReporterManager.UpdateComment(fmt.Sprintf(":x: generate requires argument -p ")) + commentReporterManager.UpdateComment(":x: generate requires argument -p ") return fmt.Errorf("generate requires argument -p ") } @@ -1771,7 +1771,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "issueNumber", issueNumber, ) - commentReporterManager.UpdateComment(fmt.Sprintf(":white_check_mark: Successfully loaded project")) + commentReporterManager.UpdateComment(":white_check_mark: Successfully loaded project") generationEndpoint := os.Getenv("DIGGER_GENERATION_ENDPOINT") if generationEndpoint == "" { @@ -1779,7 +1779,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "issueNumber", issueNumber, "projectName", projectName, ) - commentReporterManager.UpdateComment(fmt.Sprintf(":x: server does not have generation endpoint configured, please verify")) + commentReporterManager.UpdateComment(":x: server does not have generation endpoint configured, please verify") return fmt.Errorf("server does not have generation endpoint configured, please verify") } apiToken := os.Getenv("DIGGER_GENERATION_API_TOKEN") @@ -1874,7 +1874,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "codeLength", len(appCode), ) - commentReporterManager.UpdateComment(fmt.Sprintf(":white_check_mark: Successfully loaded code from commit")) + commentReporterManager.UpdateComment(":white_check_mark: Successfully loaded code from commit") slog.Info("Generating Terraform code", "projectName", projectName, @@ -1883,7 +1883,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "codeLength", len(appCode), ) - commentReporterManager.UpdateComment(fmt.Sprintf("Generating terraform...")) + commentReporterManager.UpdateComment("Generating terraform...") terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, apiToken) if err != nil { slog.Error("Failed to generate Terraform code", @@ -1901,7 +1901,7 @@ func GenerateTerraformFromCode(payload *github.IssueCommentEvent, commentReporte "codeLength", len(terraformCode), ) - commentReporterManager.UpdateComment(fmt.Sprintf(":white_check_mark: Generated terraform")) + commentReporterManager.UpdateComment(":white_check_mark: Generated terraform") // Committing the generated Terraform code to the repository slog.Info("Preparing to commit generated Terraform code", @@ -2102,7 +2102,7 @@ func CreateDiggerWorkflowWithPullRequest(org *models.Organisation, client *githu ) ctx := context.Background() - if strings.Index(githubRepo, "/") == -1 { + if !strings.Contains(githubRepo, "/") { slog.Error("GitHub repo is in wrong format", "githubRepo", githubRepo, ) diff --git a/backend/controllers/projects.go b/backend/controllers/projects.go index 02765df86..9b92fc4bf 100644 --- a/backend/controllers/projects.go +++ b/backend/controllers/projects.go @@ -1323,7 +1323,7 @@ func CreateTerraformOutputsSummary(gh utils.GithubClientProvider, batch *models. return fmt.Errorf("error converting Digger YAML to config: %v", err) } - if batch.Status == orchestrator_scheduler.BatchJobSucceeded && config.Reporting.AiSummary == true { + if batch.Status == orchestrator_scheduler.BatchJobSucceeded && config.Reporting.AiSummary { slog.Info("Batch succeeded and AI summary enabled, generating summary", "batchId", batch.ID, "prNumber", batch.PrNumber, @@ -1524,8 +1524,8 @@ func AutomergePRforBatchIfEnabled(gh utils.GithubClientProvider, batch *models.D if batch.Status == orchestrator_scheduler.BatchJobSucceeded && batch.BatchType == orchestrator_scheduler.DiggerCommandApply && - batch.CoverAllImpactedProjects == true && - automerge == true { + batch.CoverAllImpactedProjects && + automerge { slog.Info("Conditions met for auto-merge, proceeding", "batchId", batch.ID, @@ -1616,8 +1616,8 @@ func DeleteOlderPRCommentsIfEnabled(gh utils.GithubClientProvider, batch *models if (batch.Status == orchestrator_scheduler.BatchJobSucceeded || batch.Status == orchestrator_scheduler.BatchJobFailed) && batch.BatchType == orchestrator_scheduler.DiggerCommandPlan && - batch.CoverAllImpactedProjects == true && - deleteOlderComments == true { + batch.CoverAllImpactedProjects && + deleteOlderComments { slog.Info("Conditions met for deleting prior comments, proceeding", "batchId", batch.ID, diff --git a/backend/controllers/runs.go b/backend/controllers/runs.go index 922378284..c78847ff4 100644 --- a/backend/controllers/runs.go +++ b/backend/controllers/runs.go @@ -223,7 +223,7 @@ func ApproveRun(c *gin.Context) { return } - if run.IsApproved == false { + if !run.IsApproved { run.ApprovalAuthor = "a_user" run.IsApproved = true run.ApprovalDate = time.Now() diff --git a/backend/models/storage.go b/backend/models/storage.go index fd2956a3a..f40a59886 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -526,7 +526,7 @@ func (db *Database) GetGithubAppInstallationByIdAndRepo(installationId int64, re } // If not found, the values will be default values, which means ID will be 0 - if installation.Model.ID == 0 { + if installation.ID == 0 { return nil, fmt.Errorf("GithubAppInstallation with id=%v doesn't exist", installationId) } return &installation, nil @@ -554,7 +554,7 @@ func (db *Database) GetGithubAppInstallationLink(installationId int64) (*GithubA } // If not found, the values will be default values, which means ID will be 0 - if link.Model.ID == 0 { + if link.ID == 0 { return nil, nil } return &link, nil @@ -838,7 +838,7 @@ func (db *Database) UpdateBatchStatus(batch *DiggerBatch) error { allJobsSucceeded = false } } - if allJobsSucceeded == true { + if allJobsSucceeded { batch.Status = scheduler.BatchJobSucceeded slog.Info("all jobs succeeded, marking batch as succeeded", "batchId", batchId, @@ -1116,8 +1116,7 @@ func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated, return nil, fmt.Errorf("Could not get digger job") } - var jobSummary *DiggerJobSummary - jobSummary = &diggerJob.DiggerJobSummary + var jobSummary *DiggerJobSummary = &diggerJob.DiggerJobSummary jobSummary.ResourcesCreated = resourcesCreated jobSummary.ResourcesUpdated = resourcesUpdated jobSummary.ResourcesDeleted = resourcesDeleted @@ -1158,8 +1157,7 @@ func (db *Database) UpdateDiggerJob(job *DiggerJob) error { func (db *Database) GetDiggerJobsForBatch(batchId uuid.UUID) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId) + var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId) result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs) if result.Error != nil { @@ -1217,8 +1215,7 @@ func (db *Database) GetJobsByRepoName(orgId uint, repoFullName string) ([]querie func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status []scheduler.DiggerJobStatus) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) + var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs) if result.Error != nil { @@ -1241,8 +1238,7 @@ func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status [] func (db *Database) GetDiggerJobsWithStatus(status scheduler.DiggerJobStatus) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("status = ?", status) + var where *gorm.DB = db.GormDB.Where("status = ?", status) result := where.Preload("Batch").Find(&jobs) if result.Error != nil { diff --git a/backend/utils/pr_comment.go b/backend/utils/pr_comment.go index 568d2624c..600929d83 100644 --- a/backend/utils/pr_comment.go +++ b/backend/utils/pr_comment.go @@ -150,8 +150,8 @@ func ReportInitialJobsStatus(cr *CommentReporter, jobs []scheduler.Job) error { if len(jobs) == 0 { message = message + ":construction_worker: No projects impacted" } else { - message = message + fmt.Sprintf("| Project | Status |\n") - message = message + fmt.Sprintf("|---------|--------|\n") + message = message + "| Project | Status |\n" + message = message + "|---------|--------|\n" for _, job := range jobs { message = message + fmt.Sprintf(""+ "|:clock11: **%v**|pending...|\n", job.ProjectName) From 085e6cb7224afa5bc2b43433ec8afaa1e0690928 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:12:37 +0530 Subject: [PATCH 05/33] staticcheck backend/ : lint files --- backend/controllers/github_api.go | 1 - backend/middleware/basic.go | 1 - backend/middleware/webhook.go | 1 - backend/models/setup.go | 1 - backend/models/storage.go | 2 +- 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/backend/controllers/github_api.go b/backend/controllers/github_api.go index 8bc29ec44..17659d3a9 100644 --- a/backend/controllers/github_api.go +++ b/backend/controllers/github_api.go @@ -82,5 +82,4 @@ func LinkGithubInstallationToOrgApi(c *gin.Context) { slog.Info("Successfully created Github installation link", "installationId", installationId, "orgId", org.ID) // Return status 200 c.JSON(http.StatusOK, gin.H{"status": "Successfully created Github installation link"}) - return } diff --git a/backend/middleware/basic.go b/backend/middleware/basic.go index 225126bd6..c5d8fc53f 100644 --- a/backend/middleware/basic.go +++ b/backend/middleware/basic.go @@ -84,6 +84,5 @@ func HttpBasicApiAuth() gin.HandlerFunc { c.Abort() return } - return } } diff --git a/backend/middleware/webhook.go b/backend/middleware/webhook.go index bdedae092..5d735bee0 100644 --- a/backend/middleware/webhook.go +++ b/backend/middleware/webhook.go @@ -30,6 +30,5 @@ func InternalApiAuth() gin.HandlerFunc { } c.Next() - return } } diff --git a/backend/models/setup.go b/backend/models/setup.go index 53b385071..40751782a 100644 --- a/backend/models/setup.go +++ b/backend/models/setup.go @@ -7,7 +7,6 @@ import ( sloggorm "github.com/imdatngo/slog-gorm/v2" "gorm.io/driver/postgres" - _ "gorm.io/driver/postgres" "gorm.io/gorm" ) diff --git a/backend/models/storage.go b/backend/models/storage.go index f40a59886..e48261b4f 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -848,7 +848,7 @@ func (db *Database) UpdateBatchStatus(batch *DiggerBatch) error { } func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte, workflowFile string) (*DiggerJob, error) { - if serializedJob == nil || len(serializedJob) == 0 { + if len(serializedJob) == 0 { return nil, fmt.Errorf("serializedJob can't be empty") } jobId := uniuri.New() From f97fd0957c4631bd80ccb23dac270351b73a8744 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:14:26 +0530 Subject: [PATCH 06/33] staticcheck background/projects-refresh-service/ : lint files --- .../projects-refresh-service/projects_refesh_main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/background/projects-refresh-service/projects_refesh_main.go b/background/projects-refresh-service/projects_refesh_main.go index e8fdb0c5d..65386a27c 100644 --- a/background/projects-refresh-service/projects_refesh_main.go +++ b/background/projects-refresh-service/projects_refesh_main.go @@ -14,13 +14,14 @@ func init() { logLevel := os.Getenv("DIGGER_LOG_LEVEL") var level slog.Leveler - if logLevel == "DEBUG" { + switch logLevel { + case "DEBUG": level = slog.LevelDebug - } else if logLevel == "WARN" { + case "WARN": level = slog.LevelWarn - } else if logLevel == "ERROR" { + case "ERROR": level = slog.LevelError - } else { + default: level = slog.LevelInfo } From c2e266223dac002e332ced109cbb8e1f6d05f9da Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:16:07 +0530 Subject: [PATCH 07/33] staticcheck cli/ : lint files with autofix --- cli/cmd/digger/default.go | 11 +++++------ cli/cmd/digger/main_test.go | 3 +-- cli/pkg/digger/digger.go | 6 +++--- cli/pkg/digger/digger_test.go | 24 ++++++------------------ cli/pkg/github/github.go | 4 ++-- cli/pkg/integration/integration_test.go | 2 +- cli/pkg/spec/manual.go | 2 +- 7 files changed, 19 insertions(+), 33 deletions(-) diff --git a/cli/cmd/digger/default.go b/cli/cmd/digger/default.go index d16f08f8f..7a8dcedc5 100644 --- a/cli/cmd/digger/default.go +++ b/cli/cmd/digger/default.go @@ -22,11 +22,12 @@ import ( func initLogger() { logLevel := os.Getenv("DIGGER_LOG_LEVEL") var level slog.Leveler - if logLevel == "DEBUG" { + switch logLevel { + case "DEBUG": level = slog.LevelDebug - } else if logLevel == "WARN" { + case "WARN": level = slog.LevelWarn - } else { + default: level = slog.LevelInfo } logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ @@ -48,9 +49,7 @@ var defaultCmd = &cobra.Command{ usage.ReportErrorAndExit("", fmt.Sprintf("could not load spec json: %v", err), 1) } - var spec_err error - - spec_err = spec2.RunSpec( + var spec_err error = spec2.RunSpec( spec, lib_spec.VCSProviderBasic{}, lib_spec.JobSpecProvider{}, diff --git a/cli/cmd/digger/main_test.go b/cli/cmd/digger/main_test.go index 88be2296c..e827fa4aa 100644 --- a/cli/cmd/digger/main_test.go +++ b/cli/cmd/digger/main_test.go @@ -1016,8 +1016,7 @@ func TestGitHubTestPRCommandCaseInsensitivity(t *testing.T) { ghEvent.Comment.Body = &comment project := configuration.Project{Name: "test project", Workflow: "default"} - var impactedProjects []configuration.Project - impactedProjects = make([]configuration.Project, 1) + var impactedProjects []configuration.Project = make([]configuration.Project, 1) impactedProjects[0] = project requestedProject := project workflows := make(map[string]configuration.Workflow, 1) diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index 90368fc64..4881e1acc 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -125,7 +125,7 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic } } - if allAppliesSuccess == true && reportFinalStatusToBackend == true { + if allAppliesSuccess && reportFinalStatusToBackend { currentJob := jobs[0] jobPrCommentId, jobPrCommentUrl, err := reporter.Flush() @@ -322,7 +322,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org if err != nil { slog.Error("Failed to report plan.", "error", err) } - msg := fmt.Sprintf("Plan is not allowed") + msg := "Plan is not allowed" slog.Error(msg) return nil, msg, fmt.Errorf("%s", msg) } else { @@ -667,7 +667,7 @@ func RunJob( return fmt.Errorf("%s", msg) } if !planIsAllowed { - msg := fmt.Sprintf("Plan is not allowed") + msg := "Plan is not allowed" slog.Error(msg) err = backendApi.ReportProjectRun(repo, job.ProjectName, runStartedAt, time.Now(), "FAILED", command, msg) if err != nil { diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index 17764af99..6d2f18a9b 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -393,24 +393,12 @@ func TestCorrectCommandExecutionWhenPlanning(t *testing.T) { func allCommandsInOrderWithParams(terraformExecutor *MockTerraformExecutor, commandRunner *MockCommandRunner, prManager *MockPRManager, lock *MockProjectLock, planStorage *MockPlanStorage, planPathProvider *MockPlanPathProvider) []string { var commands []RunInfo - for _, command := range terraformExecutor.Commands { - commands = append(commands, command) - } - for _, command := range commandRunner.Commands { - commands = append(commands, command) - } - for _, command := range prManager.Commands { - commands = append(commands, command) - } - for _, command := range lock.Commands { - commands = append(commands, command) - } - for _, command := range planStorage.Commands { - commands = append(commands, command) - } - for _, command := range planPathProvider.Commands { - commands = append(commands, command) - } + commands = append(commands, terraformExecutor.Commands...) + commands = append(commands, commandRunner.Commands...) + commands = append(commands, prManager.Commands...) + commands = append(commands, lock.Commands...) + commands = append(commands, planStorage.Commands...) + commands = append(commands, planPathProvider.Commands...) sort.Slice(commands, func(i, j int) bool { return commands[i].Timestamp.Before(commands[j].Timestamp) diff --git a/cli/pkg/github/github.go b/cli/pkg/github/github.go index 96965ca18..3d5b67272 100644 --- a/cli/pkg/github/github.go +++ b/cli/pkg/github/github.go @@ -114,7 +114,7 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh } slog.Info("Digger digger_config read successfully") - if diggerConfig.PrLocks == false { + if !diggerConfig.PrLocks { slog.Info("Using noop lock as configured in digger.yml") lock = core_locking.NoOpLock{} } @@ -351,7 +351,7 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh } func logCommands(projectCommands []scheduler.Job) { - logMessage := fmt.Sprintf("Following commands are going to be executed:\n") + logMessage := "Following commands are going to be executed:\n" for _, pc := range projectCommands { logMessage += fmt.Sprintf("project: %s: commands: ", pc.ProjectName) for _, c := range pc.Commands { diff --git a/cli/pkg/integration/integration_test.go b/cli/pkg/integration/integration_test.go index 405249adf..1a529673a 100644 --- a/cli/pkg/integration/integration_test.go +++ b/cli/pkg/integration/integration_test.go @@ -64,7 +64,7 @@ func getProjectLockForTests() (error, *locking.PullRequestLock) { func randomString(length int) string { const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) + seededRand := rand.New(rand.NewSource(time.Now().UnixNano())) b := make([]byte, length) for i := range b { diff --git a/cli/pkg/spec/manual.go b/cli/pkg/spec/manual.go index ec92cdb33..ad3edd27b 100644 --- a/cli/pkg/spec/manual.go +++ b/cli/pkg/spec/manual.go @@ -137,7 +137,7 @@ func RunSpecManualCommand( slog.Info("<========= DIGGER RUNNING IN MANUAL MODE =========>") allAppliesSuccess, _, err := digger.RunJobs(jobs, prService, orgService, lock, reporter, planStorage, policyChecker, commentUpdater, noopBackendApi, spec.JobId, false, false, commentId, currentDir) slog.Info("<========= DIGGER COMPLETED =========>") - if err != nil || allAppliesSuccess == false { + if err != nil || !allAppliesSuccess { usage.ReportErrorAndExit(spec.VCS.RepoOwner, "Terraform execution failed", 1) } From c0450d72139d57c601f5e3f83e1648e553a92381 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:19:11 +0530 Subject: [PATCH 08/33] staticcheck cli/ : remove duplicate imports --- ee/cli/cmd/digger/main_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ee/cli/cmd/digger/main_test.go b/ee/cli/cmd/digger/main_test.go index 9269904bc..bae1b81a7 100644 --- a/ee/cli/cmd/digger/main_test.go +++ b/ee/cli/cmd/digger/main_test.go @@ -12,7 +12,6 @@ import ( "github.com/diggerhq/digger/libs/storage" "github.com/diggerhq/digger/cli/pkg/digger" - "github.com/diggerhq/digger/cli/pkg/github/models" ghmodels "github.com/diggerhq/digger/cli/pkg/github/models" dggithub "github.com/diggerhq/digger/libs/ci/github" "github.com/diggerhq/digger/libs/comment_utils/reporting" @@ -874,7 +873,7 @@ var githubInvalidContextJson = `{ ` func TestGitHubNewPullRequestContext(t *testing.T) { - actionContext, err := models.GetGitHubContext(githubContextNewPullRequestJson) + actionContext, err := ghmodels.GetGitHubContext(githubContextNewPullRequestJson) context := actionContext.ToEventPackage() assert.NoError(t, err) @@ -952,7 +951,7 @@ func TestInvalidGitHubContext(t *testing.T) { } func TestGitHubNewPullRequestInMultiEnvProjectContext(t *testing.T) { - actionContext, err := models.GetGitHubContext(githubContextNewPullRequestJson) + actionContext, err := ghmodels.GetGitHubContext(githubContextNewPullRequestJson) context := actionContext.ToEventPackage() assert.NoError(t, err) ghEvent := context.Event From 40456a74e093006bb8ebf06a19e149b7e001327c Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:24:09 +0530 Subject: [PATCH 09/33] staticcheck ee/backend/ : lint files --- ee/backend/controllers/github.go | 2 +- ee/backend/controllers/spec.go | 1 - ee/backend/controllers/web.go | 9 +++++---- ee/backend/main.go | 1 - 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ee/backend/controllers/github.go b/ee/backend/controllers/github.go index 0362b6a1f..b2a4bf132 100644 --- a/ee/backend/controllers/github.go +++ b/ee/backend/controllers/github.go @@ -195,7 +195,7 @@ func (d DiggerEEController) GithubAppConnectionsConfirm(c *gin.Context) { _, err = models.DB.CreateVCSConnection(cfg.GetName(), models.DiggerVCSGithub, cfg.GetID(), cfg.GetClientID(), clientSecretEnc, webhookSecretEnc, PEMEnc, PEM64Enc, *cfg.Owner.Login, cfg.GetHTMLURL(), "", "", "", "", orgId) if err != nil { log.Printf("failed to create github app connection record: %v", err) - c.String(500, fmt.Sprintf("Failed to create github app record on callback")) + c.String(500, "Failed to create github app record on callback") return } diff --git a/ee/backend/controllers/spec.go b/ee/backend/controllers/spec.go index aa5a76da2..55211b305 100644 --- a/ee/backend/controllers/spec.go +++ b/ee/backend/controllers/spec.go @@ -116,5 +116,4 @@ func (d DiggerEEController) GetSpec(c *gin.Context) { log.Printf("specBytes: %v", spec) c.String(200, string(specBytes)) - return } diff --git a/ee/backend/controllers/web.go b/ee/backend/controllers/web.go index bda53f630..d4f440341 100644 --- a/ee/backend/controllers/web.go +++ b/ee/backend/controllers/web.go @@ -97,7 +97,8 @@ func (web *WebController) PoliciesPage(c *gin.Context) { func (web *WebController) AddPolicyPage(c *gin.Context) { orgId := c.GetHeader(middleware.ORGANISATION_ID_KEY) - if c.Request.Method == "GET" { + switch c.Request.Method { + case "GET": message := "" projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY) @@ -115,7 +116,7 @@ func (web *WebController) AddPolicyPage(c *gin.Context) { c.HTML(http.StatusOK, "policy_add.tmpl", gin.H{ "Message": message, "Projects": projects, "PolicyTypes": policyTypes, }) - } else if c.Request.Method == "POST" { + case "POST": policyText := c.PostForm("policytext") if policyText == "" { message := "Policy can't be empty" @@ -209,8 +210,8 @@ func (web *WebController) RunDetailsPage(c *gin.Context) { stateSyncOutput := "" terraformPlanOutput := "" runOutput := string(ansihtml.ConvertToHTMLWithClasses([]byte(run.Output), "terraform-output-", true)) - runOutput = strings.Replace(runOutput, " ", "  ", -1) - runOutput = strings.Replace(runOutput, "\n", "
\n", -1) + runOutput = strings.ReplaceAll(runOutput, " ", "  ") + runOutput = strings.ReplaceAll(runOutput, "\n", "
\n") planIndex := strings.Index(runOutput, "Terraform used the selected providers to generate the following execution") if planIndex != -1 { diff --git a/ee/backend/main.go b/ee/backend/main.go index fd73064a2..3d9e7dd1a 100644 --- a/ee/backend/main.go +++ b/ee/backend/main.go @@ -99,7 +99,6 @@ func main() { } else { r.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "healthy.tmpl", gin.H{}) - return }) } From d14b69d0bd35ecdafc878e6cf50b27c0db5ca9f9 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:25:37 +0530 Subject: [PATCH 10/33] staticcheck ee/cli/ : lint files --- ee/cli/cmd/digger/main_test.go | 3 +-- ee/cli/pkg/comment_updater/provider.go | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ee/cli/cmd/digger/main_test.go b/ee/cli/cmd/digger/main_test.go index bae1b81a7..36cc731d5 100644 --- a/ee/cli/cmd/digger/main_test.go +++ b/ee/cli/cmd/digger/main_test.go @@ -1024,8 +1024,7 @@ func TestGitHubTestPRCommandCaseInsensitivity(t *testing.T) { ghEvent.Comment.Body = &comment project := configuration.Project{Name: "test project", Workflow: "default"} - var impactedProjects []configuration.Project - impactedProjects = make([]configuration.Project, 1) + var impactedProjects []configuration.Project = make([]configuration.Project, 1) impactedProjects[0] = project requestedProject := project workflows := make(map[string]configuration.Workflow, 1) diff --git a/ee/cli/pkg/comment_updater/provider.go b/ee/cli/pkg/comment_updater/provider.go index 83b295076..e4ba7ac89 100644 --- a/ee/cli/pkg/comment_updater/provider.go +++ b/ee/cli/pkg/comment_updater/provider.go @@ -10,12 +10,13 @@ import ( type CommentUpdaterProviderAdvanced struct{} func (c CommentUpdaterProviderAdvanced) Get(renderMode string) (comment_updater.CommentUpdater, error) { - if renderMode == digger_config.CommentRenderModeBasic { + switch renderMode { + case digger_config.CommentRenderModeBasic: return comment_updater.BasicCommentUpdater{}, nil - } else if renderMode == digger_config.CommentRenderModeGroupByModule { + case digger_config.CommentRenderModeGroupByModule: commentUpdater := comment_updater.BasicCommentUpdater{} return commentUpdater, nil - } else { + default: return nil, fmt.Errorf("Unknown comment render mode found: %v", renderMode) } } From 75dae1535d7043b50e95c32a98414aa31d3669d2 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:29:56 +0530 Subject: [PATCH 11/33] staticcheck libs/ : lint files with autofix --- libs/ci/azure/azure.go | 17 +++++----- libs/ci/bitbucket/bitbucket.go | 7 ++-- libs/ci/github/github.go | 7 ++-- libs/ci/gitlab/webhooks.go | 5 ++- libs/comment_utils/summary/provider.go | 9 +++--- libs/comment_utils/summary/updater.go | 2 +- libs/digger_config/converters.go | 19 ++--------- libs/digger_config/digger_config.go | 8 ++--- .../terragrunt/atlantis/generate_test.go | 3 +- libs/execution/io_writer_filtered_test.go | 2 +- libs/locking/gcp/gcp_lock_test.go | 2 +- libs/locking/locking.go | 7 ++-- libs/policy/policy.go | 32 ++++++++++--------- libs/scheduler/json_models.go | 14 ++------ libs/spec/providers.go | 6 ++-- libs/storage/aws_plan_storage.go | 7 ++-- libs/storage/plan_storage.go | 14 ++++---- 17 files changed, 70 insertions(+), 91 deletions(-) diff --git a/libs/ci/azure/azure.go b/libs/ci/azure/azure.go index 0beee288d..d66f2103b 100644 --- a/libs/ci/azure/azure.go +++ b/libs/ci/azure/azure.go @@ -201,13 +201,14 @@ func (svc *AzureReposService) UpdateIssue(ID int64, title, body string) (int64, func (a *AzureReposService) SetStatus(prNumber int, status, statusContext string) error { var gitStatusState git.GitStatusState - if status == "success" { + switch status { + case "success": gitStatusState = git.GitStatusStateValues.Succeeded - } else if status == "failure" { + case "failure": gitStatusState = git.GitStatusStateValues.Failed - } else if status == "pending" { + case "pending": gitStatusState = git.GitStatusStateValues.Pending - } else { + default: gitStatusState = git.GitStatusStateValues.NotSet } @@ -394,9 +395,9 @@ func ProcessAzureReposEvent(azureEvent interface{}, diggerConfig *digger_config2 var impactedProjects []digger_config2.Project var prNumber int - switch azureEvent.(type) { + switch azureEvent := azureEvent.(type) { case AzurePrEvent: - prNumber = azureEvent.(AzurePrEvent).Resource.PullRequestId + prNumber = azureEvent.Resource.PullRequestId changedFiles, err := ciService.GetChangedFiles(prNumber) if err != nil { return nil, nil, 0, fmt.Errorf("could not get changed files: %v", err) @@ -404,14 +405,14 @@ func ProcessAzureReposEvent(azureEvent interface{}, diggerConfig *digger_config2 impactedProjects, _ = diggerConfig.GetModifiedProjects(changedFiles) case AzureCommentEvent: - prNumber = azureEvent.(AzureCommentEvent).Resource.PullRequest.PullRequestId + prNumber = azureEvent.Resource.PullRequest.PullRequestId changedFiles, err := ciService.GetChangedFiles(prNumber) if err != nil { return nil, nil, 0, fmt.Errorf("could not get changed files: %v", err) } impactedProjects, _ = diggerConfig.GetModifiedProjects(changedFiles) - requestedProject := ci.ParseProjectName(azureEvent.(AzureCommentEvent).Resource.Comment.Content) + requestedProject := ci.ParseProjectName(azureEvent.Resource.Comment.Content) if requestedProject == "" { return impactedProjects, nil, prNumber, nil diff --git a/libs/ci/bitbucket/bitbucket.go b/libs/ci/bitbucket/bitbucket.go index 2f9864869..fdd2cc70c 100644 --- a/libs/ci/bitbucket/bitbucket.go +++ b/libs/ci/bitbucket/bitbucket.go @@ -266,11 +266,12 @@ func (b BitbucketAPI) SetStatus(prNumber int, status, statusContext string) erro url := fmt.Sprintf("%s/repositories/%s/%s/commit/%s/statuses/build", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prResponse.Source.Commit.Hash) - if status == "failure" { + switch status { + case "failure": status = "FAILED" - } else if status == "success" { + case "success": status = "SUCCESSFUL" - } else if status == "pending" { + case "pending": status = "INPROGRESS" } diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index 1e48647ac..61a1f3a2b 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -403,7 +403,7 @@ func (svc GithubService) SetOutput(prNumber int, key, value string) error { if err != nil { return fmt.Errorf("could not open file for writing during digger step") } - _, err = f.WriteString(fmt.Sprintf("%v=%v", key, value)) + _, err = fmt.Fprintf(f, "%v=%v", key, value) if err != nil { return fmt.Errorf("could not write digger file step") } @@ -580,7 +580,7 @@ func ConvertGithubPullRequestEventToJobs(payload *github.PullRequestEvent, impac }) } else if *payload.Action == "converted_to_draft" { var commands []string - if config.AllowDraftPRs == false && len(workflow.Configuration.OnPullRequestConvertedToDraft) == 0 { + if !config.AllowDraftPRs && len(workflow.Configuration.OnPullRequestConvertedToDraft) == 0 { commands = []string{"digger unlock"} } else { commands = workflow.Configuration.OnPullRequestConvertedToDraft @@ -694,8 +694,7 @@ func ProcessGitHubEvent(ghEvent interface{}, diggerConfig *digger_config.DiggerC func ProcessGitHubPullRequestEvent(payload *github.PullRequestEvent, diggerConfig *digger_config.DiggerConfig, dependencyGraph graph.Graph[string, digger_config.Project], ciService ci.PullRequestService) ([]digger_config.Project, map[string]digger_config.ProjectToSourceMapping, int, error) { var impactedProjects []digger_config.Project - var prNumber int - prNumber = *payload.PullRequest.Number + var prNumber int = *payload.PullRequest.Number slog.Info("processing GitHub pull request event", "prNumber", prNumber, diff --git a/libs/ci/gitlab/webhooks.go b/libs/ci/gitlab/webhooks.go index dc3486bdc..4339fa3cd 100644 --- a/libs/ci/gitlab/webhooks.go +++ b/libs/ci/gitlab/webhooks.go @@ -13,8 +13,7 @@ import ( func ProcessGitlabPullRequestEvent(payload *gitlab.MergeEvent, diggerConfig *digger_config.DiggerConfig, dependencyGraph graph.Graph[string, digger_config.Project], ciService ci.PullRequestService) ([]digger_config.Project, map[string]digger_config.ProjectToSourceMapping, int, error) { var impactedProjects []digger_config.Project - var prNumber int - prNumber = payload.ObjectAttributes.IID + var prNumber int = payload.ObjectAttributes.IID changedFiles, err := ciService.GetChangedFiles(prNumber) if err != nil { return nil, nil, prNumber, fmt.Errorf("could not get changed files") @@ -129,7 +128,7 @@ func ConvertGithubPullRequestEventToJobs(payload *gitlab.MergeEvent, impactedPro // TODO: Figure how to detect gitlab's "PR converted to draft" event } else if payload.ObjectAttributes.Action == "converted_to_draft" { var commands []string - if config.AllowDraftPRs == false && len(workflow.Configuration.OnPullRequestConvertedToDraft) == 0 { + if !config.AllowDraftPRs && len(workflow.Configuration.OnPullRequestConvertedToDraft) == 0 { commands = []string{"digger unlock"} } else { commands = workflow.Configuration.OnPullRequestConvertedToDraft diff --git a/libs/comment_utils/summary/provider.go b/libs/comment_utils/summary/provider.go index d9fe2e9b5..64dfeb9de 100644 --- a/libs/comment_utils/summary/provider.go +++ b/libs/comment_utils/summary/provider.go @@ -13,14 +13,15 @@ type CommentUpdaterProvider interface { type CommentUpdaterProviderBasic struct{} func (c CommentUpdaterProviderBasic) Get(renderMode string) (CommentUpdater, error) { - if renderMode == digger_config.CommentRenderModeBasic { + switch renderMode { + case digger_config.CommentRenderModeBasic: return BasicCommentUpdater{}, nil - } else if renderMode == digger_config.CommentRenderModeGroupByModule { + case digger_config.CommentRenderModeGroupByModule: commentUpdater := BasicCommentUpdater{} return commentUpdater, nil - } else if renderMode == "noop" { + case "noop": return NoopCommentUpdater{}, nil - } else { + default: return nil, fmt.Errorf("Unknown comment render mode found: %v", renderMode) } } diff --git a/libs/comment_utils/summary/updater.go b/libs/comment_utils/summary/updater.go index b871c1f28..cb9fd2bcc 100644 --- a/libs/comment_utils/summary/updater.go +++ b/libs/comment_utils/summary/updater.go @@ -35,7 +35,7 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum message := "" message = message + fmt.Sprintf("| Project | Status | %v | + | ~ | - |\n", jobTypeTitle) - message = message + fmt.Sprintf("|---------|--------|------|---|---|---|\n") + message = message + "|---------|--------|------|---|---|---|\n" for i, job := range jobs { jobSpec := jobSpecs[i] diff --git a/libs/digger_config/converters.go b/libs/digger_config/converters.go index 4c9900ac0..23f262b94 100644 --- a/libs/digger_config/converters.go +++ b/libs/digger_config/converters.go @@ -102,19 +102,11 @@ func copyTerraformEnvConfig(terraformEnvConfig *TerraformEnvConfigYaml) *Terrafo result.Commands = make([]EnvVar, len(terraformEnvConfig.Commands)) for i, s := range terraformEnvConfig.State { - item := EnvVar{ - s.Name, - s.ValueFrom, - s.Value, - } + item := EnvVar(s) result.State[i] = item } for i, s := range terraformEnvConfig.Commands { - item := EnvVar{ - s.Name, - s.ValueFrom, - s.Value, - } + item := EnvVar(s) result.Commands[i] = item } @@ -128,12 +120,7 @@ func copyStage(stage *StageYaml) *Stage { result.Steps = make([]Step, len(stage.Steps)) for i, s := range stage.Steps { - item := Step{ - Action: s.Action, - Value: s.Value, - ExtraArgs: s.ExtraArgs, - Shell: s.Shell, - } + item := Step(s) result.Steps[i] = item } return &result diff --git a/libs/digger_config/digger_config.go b/libs/digger_config/digger_config.go index 6f7467ad1..82f852578 100644 --- a/libs/digger_config/digger_config.go +++ b/libs/digger_config/digger_config.go @@ -81,7 +81,7 @@ func GetFilesWithExtension(workingDir, ext string) ([]string, error) { slog.Error("error reading directory", "error", err, "dir", workingDir) - return nil, errors.New(fmt.Sprintf("error reading directory %s: %v", workingDir, err)) + return nil, fmt.Errorf("error reading directory %s: %v", workingDir, err) } for _, f := range listOfFiles { if !f.IsDir() { @@ -393,7 +393,7 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string, // if blocks of include/exclude patterns defined for _, b := range config.GenerateProjectsConfig.Blocks { - if b.Terragrunt == true { + if b.Terragrunt { if checkBlockInChangedFiles(*b.RootDir, changedFiles) { slog.Info("generating terragrunt projects for block", "blockName", b.BlockName, @@ -518,7 +518,7 @@ func LoadDiggerConfigYaml(workingDir string, generateProjects bool, changedFiles return configYaml, err } - if generateProjects == true { + if generateProjects { slog.Info("generating projects from config", "fileName", fileName) err = HandleYamlProjectGeneration(configYaml, workingDir, changedFiles) if err != nil { @@ -947,7 +947,7 @@ func isFileExists(path string) bool { } func retrieveConfigFile(workingDir string) (string, error) { - var fileName string = "digger" + fileName := "digger" customConfigFile := os.Getenv("DIGGER_FILENAME") != "" if customConfigFile { diff --git a/libs/digger_config/terragrunt/atlantis/generate_test.go b/libs/digger_config/terragrunt/atlantis/generate_test.go index be4bb92a7..18f567be5 100644 --- a/libs/digger_config/terragrunt/atlantis/generate_test.go +++ b/libs/digger_config/terragrunt/atlantis/generate_test.go @@ -11,8 +11,7 @@ import ( ) func init() { - var level slog.Leveler - level = slog.LevelDebug + var level slog.Leveler = slog.LevelDebug handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ Level: level, }) diff --git a/libs/execution/io_writer_filtered_test.go b/libs/execution/io_writer_filtered_test.go index 75a7aa4d8..6e1f19071 100644 --- a/libs/execution/io_writer_filtered_test.go +++ b/libs/execution/io_writer_filtered_test.go @@ -9,7 +9,7 @@ import ( ) func TestNewFilteringWriter(t *testing.T) { - var buf bytes.Buffer = bytes.Buffer{} + buf := bytes.Buffer{} pattern := regexp.MustCompile("sensitive") writer := NewFilteringWriter(nil, &buf, pattern) diff --git a/libs/locking/gcp/gcp_lock_test.go b/libs/locking/gcp/gcp_lock_test.go index 84e466360..e4d37c891 100644 --- a/libs/locking/gcp/gcp_lock_test.go +++ b/libs/locking/gcp/gcp_lock_test.go @@ -14,7 +14,7 @@ import ( func randomString(length int) string { const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) + seededRand := rand.New(rand.NewSource(time.Now().UnixNano())) b := make([]byte, length) for i := range b { diff --git a/libs/locking/locking.go b/libs/locking/locking.go index 53bbe7faa..fa0ed88e6 100644 --- a/libs/locking/locking.go +++ b/libs/locking/locking.go @@ -254,7 +254,8 @@ func GetLock() (Lock, error) { return &NoOpLock{}, nil } - if lockProvider == "" || lockProvider == "aws" { + switch lockProvider { + case "", "aws": slog.Info("Using AWS lock provider") // https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ @@ -292,7 +293,7 @@ func GetLock() (Lock, error) { dynamoDb := dynamodb.NewFromConfig(cfg) dynamoDbLock := aws.DynamoDbLock{DynamoDb: dynamoDb} return &dynamoDbLock, nil - } else if lockProvider == "gcp" { + case "gcp": slog.Info("Using GCP lock provider") ctx, client := gcp.GetGoogleStorageClient() defer func(client *storage.Client) { @@ -309,7 +310,7 @@ func GetLock() (Lock, error) { bucket := client.Bucket(bucketName) lock := gcp.GoogleStorageLock{Client: client, Bucket: bucket, Context: ctx} return &lock, nil - } else if lockProvider == "azure" { + case "azure": slog.Info("Using Azure lock provider") return azure.NewStorageAccountLock() } diff --git a/libs/policy/policy.go b/libs/policy/policy.go index 555020bee..a3f244a82 100644 --- a/libs/policy/policy.go +++ b/libs/policy/policy.go @@ -3,7 +3,6 @@ package policy import ( "context" "encoding/json" - "errors" "fmt" "io" "log/slog" @@ -233,23 +232,24 @@ func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation, repo, projectNam "error", err) return "", fmt.Errorf("error while fetching access policy for organisation: %v", err) } - if resp.StatusCode == 200 { + switch resp.StatusCode { + case 200: slog.Debug("Found organisation access policy", "organisation", organisation) return content, nil - } else if resp.StatusCode == 404 { + case 404: slog.Debug("Organisation access policy not found, using default", "organisation", organisation) return DefaultAccessPolicy, nil - } else { + default: slog.Error("Unexpected response for organisation policy", "statusCode", resp.StatusCode, "response", content) - return "", errors.New(fmt.Sprintf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode)) + return "", fmt.Errorf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode) } } else { slog.Error("Unexpected response for project policy", "statusCode", resp.StatusCode, "response", content) - return "", errors.New(fmt.Sprintf("unexpected response while fetching project policy: %v code %v", content, resp.StatusCode)) + return "", fmt.Errorf("unexpected response while fetching project policy: %v code %v", content, resp.StatusCode) } } @@ -288,23 +288,24 @@ func (p DiggerHttpPolicyProvider) GetPlanPolicy(organisation, repo, projectName, "error", err) return "", err } - if resp.StatusCode == 200 { + switch resp.StatusCode { + case 200: slog.Debug("Found organisation plan policy", "organisation", organisation) return content, nil - } else if resp.StatusCode == 404 { + case 404: slog.Debug("Organisation plan policy not found", "organisation", organisation) return "", nil - } else { + default: slog.Error("Unexpected response for organisation policy", "statusCode", resp.StatusCode, "response", content) - return "", errors.New(fmt.Sprintf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode)) + return "", fmt.Errorf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode) } } else { slog.Error("Unexpected response for project policy", "statusCode", resp.StatusCode, "response", content) - return "", errors.New(fmt.Sprintf("unexpected response while fetching project policy: %v code %v", content, resp.StatusCode)) + return "", fmt.Errorf("unexpected response while fetching project policy: %v code %v", content, resp.StatusCode) } } @@ -318,17 +319,18 @@ func (p DiggerHttpPolicyProvider) GetDriftPolicy() (string, error) { "error", err) return "", err } - if resp.StatusCode == 200 { + switch resp.StatusCode { + case 200: slog.Debug("Found drift policy", "organisation", p.DiggerOrganisation) return content, nil - } else if resp.StatusCode == 404 { + case 404: slog.Debug("Drift policy not found", "organisation", p.DiggerOrganisation) return "", nil - } else { + default: slog.Error("Unexpected response for drift policy", "statusCode", resp.StatusCode, "response", content) - return "", errors.New(fmt.Sprintf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode)) + return "", fmt.Errorf("unexpected response while fetching organisation policy: %v, code %v", content, resp.StatusCode) } } diff --git a/libs/scheduler/json_models.go b/libs/scheduler/json_models.go index 5d4933128..64e504dbf 100644 --- a/libs/scheduler/json_models.go +++ b/libs/scheduler/json_models.go @@ -134,12 +134,7 @@ func jsonToStage(stageJson StageJson) *Stage { } steps := make([]Step, len(stageJson.Steps)) for i, step := range stageJson.Steps { - steps[i] = Step{ - Action: step.Action, - Value: step.Value, - ExtraArgs: step.ExtraArgs, - Shell: step.Shell, - } + steps[i] = Step(step) } return &Stage{ Steps: steps, @@ -153,12 +148,7 @@ func stageToJson(stage *Stage) StageJson { } steps := make([]StepJson, len(stage.Steps)) for i, step := range stage.Steps { - steps[i] = StepJson{ - Action: step.Action, - Value: step.Value, - ExtraArgs: step.ExtraArgs, - Shell: step.Shell, - } + steps[i] = StepJson(step) } return StageJson{ Steps: steps, diff --git a/libs/spec/providers.go b/libs/spec/providers.go index c6816a2fa..5a8d255c2 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -153,10 +153,8 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c } } - isSupportMarkdown := true - if vcs == "bitbucket" { - isSupportMarkdown = false - } + isSupportMarkdown := !(vcs == "bitbucket") + switch reporterSpec.ReporterType { case "noop": slog.Debug("Using NoOp reporter") diff --git a/libs/storage/aws_plan_storage.go b/libs/storage/aws_plan_storage.go index 153c6d7d4..b1af03796 100644 --- a/libs/storage/aws_plan_storage.go +++ b/libs/storage/aws_plan_storage.go @@ -65,10 +65,11 @@ func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType if encryptionEnabled { planStorage.EncryptionEnabled = true - if encryptionType == "AES256" { + switch encryptionType { + case "AES256": slog.Debug("Using AES256 encryption for S3 storage") planStorage.EncryptionType = ServerSideEncryptionAes256 - } else if encryptionType == "KMS" { + case "KMS": if KMSEncryptionId == "" { slog.Error("KMS encryption requested but no KMS key specified") return nil, fmt.Errorf("KMS encryption requested but no KMS key specified") @@ -76,7 +77,7 @@ func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType slog.Debug("Using KMS encryption for S3 storage", "kmsKeyId", KMSEncryptionId) planStorage.EncryptionType = ServerSideEncryptionAwsKms planStorage.KMSEncryptionId = KMSEncryptionId - } else { + default: slog.Error("Unknown encryption type specified", "encryptionType", encryptionType) return nil, fmt.Errorf("unknown encryption type specified for aws plan bucket: %v", encryptionType) } diff --git a/libs/storage/plan_storage.go b/libs/storage/plan_storage.go index 12c940fe8..a6723b254 100644 --- a/libs/storage/plan_storage.go +++ b/libs/storage/plan_storage.go @@ -311,7 +311,7 @@ func getLatestArtifactWithName(artifacts []*github.Artifact, name string) *githu if *item.Name != name { continue } - if latest == nil || item.UpdatedAt.Time.After(latest.UpdatedAt.Time) { + if latest == nil || item.UpdatedAt.After(latest.UpdatedAt.Time) { latest = item } } @@ -338,8 +338,8 @@ func NewPlanStorage(ghToken, ghRepoOwner, ghRepositoryName string, prNumber *int "owner", ghRepoOwner, "repo", ghRepositoryName) - switch { - case uploadDestination == "github": + switch uploadDestination { + case "github": if ghToken == "" { slog.Error("GITHUB_TOKEN not specified for GitHub plan storage") return nil, fmt.Errorf("failed to get github service: GITHUB_TOKEN not specified") @@ -353,7 +353,7 @@ func NewPlanStorage(ghToken, ghRepoOwner, ghRepositoryName string, prNumber *int PullRequestNumber: *prNumber, ZipManager: zipManager, } - case uploadDestination == "gcp": + case "gcp": ctx, client := gcp.GetGoogleStorageClient() bucketName := strings.ToLower(os.Getenv("GOOGLE_STORAGE_PLAN_ARTEFACT_BUCKET")) if bucketName == "" { @@ -367,7 +367,7 @@ func NewPlanStorage(ghToken, ghRepoOwner, ghRepositoryName string, prNumber *int Bucket: bucket, Context: ctx, } - case uploadDestination == "aws": + case "aws": bucketName := strings.ToLower(os.Getenv("AWS_S3_BUCKET")) encryptionEnabled := os.Getenv("PLAN_UPLOAD_S3_ENCRYPTION_ENABLED") == "true" encryptionType := os.Getenv("PLAN_UPLOAD_S3_ENCRYPTION_TYPE") @@ -384,10 +384,10 @@ func NewPlanStorage(ghToken, ghRepoOwner, ghRepositoryName string, prNumber *int slog.Error("Failed to create AWS plan storage", "error", err) return nil, fmt.Errorf("error while creating AWS plan storage: %v", err) } - case uploadDestination == "gitlab": + case "gitlab": slog.Warn("GitLab plan storage not yet implemented") // TODO implement me - case uploadDestination == "azure": + case "azure": containerName := strings.ToLower(os.Getenv("PLAN_UPLOAD_AZURE_STORAGE_CONTAINER_NAME")) if containerName == "" { slog.Error("PLAN_UPLOAD_AZURE_STORAGE_CONTAINER_NAME not defined for Azure plan storage") From 1064fb0c1e3d8cbc1151b7f1dff50f021002b2d8 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:34:30 +0530 Subject: [PATCH 12/33] staticcheck libs/ : lint files remove deprecated ioutils --- libs/ci/bitbucket/bitbucket.go | 6 +++--- libs/ci/github/github.go | 2 +- libs/ci/gitlab/webhooks.go | 2 +- libs/digger_config/digger_config.go | 2 +- libs/execution/runners.go | 3 +-- libs/spec/providers.go | 2 +- libs/storage/io.go | 6 +++--- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/libs/ci/bitbucket/bitbucket.go b/libs/ci/bitbucket/bitbucket.go index fdd2cc70c..bd45f3e15 100644 --- a/libs/ci/bitbucket/bitbucket.go +++ b/libs/ci/bitbucket/bitbucket.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "time" @@ -124,7 +124,7 @@ func (b BitbucketAPI) PublishComment(prNumber int, comment string) (*ci.Comment, return nil, fmt.Errorf("failed to publish comment. Status code: %d", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response:", err) return nil, fmt.Errorf("error reading response: %v", err) @@ -624,7 +624,7 @@ func (b BitbucketAPI) TriggerPipeline(branch string, variables []interface{}) (s return "", fmt.Errorf("failed to trigger pipeline: %d", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response:", err) return "", fmt.Errorf("error reading response: %v", err) diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index 61a1f3a2b..1cdfc5cd2 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -694,7 +694,7 @@ func ProcessGitHubEvent(ghEvent interface{}, diggerConfig *digger_config.DiggerC func ProcessGitHubPullRequestEvent(payload *github.PullRequestEvent, diggerConfig *digger_config.DiggerConfig, dependencyGraph graph.Graph[string, digger_config.Project], ciService ci.PullRequestService) ([]digger_config.Project, map[string]digger_config.ProjectToSourceMapping, int, error) { var impactedProjects []digger_config.Project - var prNumber int = *payload.PullRequest.Number + prNumber := *payload.PullRequest.Number slog.Info("processing GitHub pull request event", "prNumber", prNumber, diff --git a/libs/ci/gitlab/webhooks.go b/libs/ci/gitlab/webhooks.go index 4339fa3cd..4573f1fd8 100644 --- a/libs/ci/gitlab/webhooks.go +++ b/libs/ci/gitlab/webhooks.go @@ -13,7 +13,7 @@ import ( func ProcessGitlabPullRequestEvent(payload *gitlab.MergeEvent, diggerConfig *digger_config.DiggerConfig, dependencyGraph graph.Graph[string, digger_config.Project], ciService ci.PullRequestService) ([]digger_config.Project, map[string]digger_config.ProjectToSourceMapping, int, error) { var impactedProjects []digger_config.Project - var prNumber int = payload.ObjectAttributes.IID + prNumber := payload.ObjectAttributes.IID changedFiles, err := ciService.GetChangedFiles(prNumber) if err != nil { return nil, nil, prNumber, fmt.Errorf("could not get changed files") diff --git a/libs/digger_config/digger_config.go b/libs/digger_config/digger_config.go index 82f852578..e1a99711f 100644 --- a/libs/digger_config/digger_config.go +++ b/libs/digger_config/digger_config.go @@ -381,7 +381,7 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string, } } } - if config.GenerateProjectsConfig.Blocks != nil && len(config.GenerateProjectsConfig.Blocks) > 0 { + if len(config.GenerateProjectsConfig.Blocks) > 0 { slog.Info("processing project generation blocks", "blockCount", len(config.GenerateProjectsConfig.Blocks)) diff --git a/libs/execution/runners.go b/libs/execution/runners.go index 43860a50a..07797373e 100644 --- a/libs/execution/runners.go +++ b/libs/execution/runners.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "os/exec" ) @@ -22,7 +21,7 @@ func (c CommandRunner) Run(workingDir, shell string, commands []string, envs map args = []string{"-eo", "pipefail"} } - scriptFile, err := ioutil.TempFile("", "run-script") + scriptFile, err := os.CreateTemp("", "run-script") if err != nil { return "", "", fmt.Errorf("error creating script file: %v", err) } diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 5a8d255c2..b760b2578 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -153,7 +153,7 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c } } - isSupportMarkdown := !(vcs == "bitbucket") + isSupportMarkdown := vcs != "bitbucket" switch reporterSpec.ReporterType { case "noop": diff --git a/libs/storage/io.go b/libs/storage/io.go index a627fae7d..923b2c8f2 100644 --- a/libs/storage/io.go +++ b/libs/storage/io.go @@ -4,7 +4,7 @@ import ( "archive/zip" "bytes" "io" - "io/ioutil" + "os" "strings" ) @@ -41,14 +41,14 @@ func (z *Zipper) GetFileFromZip(zipFile, filename string) (string, error) { // Create a temporary file within our temp-images directory that follows // a particular naming pattern - tempFile, err := ioutil.TempFile("", "digger-*.tfplan") + tempFile, err := os.CreateTemp("", "digger-*.tfplan") if err != nil { return "", err } defer tempFile.Close() // Read all of the contents of our archive into a byte array. - contents, err := ioutil.ReadAll(buf) + contents, err := io.ReadAll(buf) if err != nil { return "", err } From 7142c0f6126e7829be596bc1467c9cd8a4058732 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:36:07 +0530 Subject: [PATCH 13/33] staticcheck next/ : lint files with autofix --- next/controllers/github.go | 6 +++--- next/controllers/github_after_merge.go | 2 +- next/dbmodels/storage.go | 13 +++++-------- next/services/scheduler.go | 2 +- next/services/spec.go | 8 ++++---- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/next/controllers/github.go b/next/controllers/github.go index a51f31a6b..c0d3e7cd0 100644 --- a/next/controllers/github.go +++ b/next/controllers/github.go @@ -353,7 +353,7 @@ func handlePullRequestEvent(gh next_utils.GithubClientProvider, payload *github. p := dbmodels.DB.Query.Project projects, err := dbmodels.DB.Query.Project.Where(p.RepoID.Eq(repo.ID)).Find() - var dgprojects []dg_configuration.Project = []dg_configuration.Project{} + dgprojects := []dg_configuration.Project{} for _, proj := range projects { projectBranch := proj.Branch if targetBranch == projectBranch { @@ -367,7 +367,7 @@ func handlePullRequestEvent(gh next_utils.GithubClientProvider, payload *github. log.Printf("error getting workflows from config: %v", err) return fmt.Errorf("error getting workflows from config") } - var config *dg_configuration.DiggerConfig = &dg_configuration.DiggerConfig{ + config := &dg_configuration.DiggerConfig{ ApplyAfterMerge: true, AllowDraftPRs: false, CommentRenderMode: "", @@ -602,7 +602,7 @@ func getBatchType(jobs []orchestrator_scheduler.Job) orchestrator_scheduler.Digg allJobsContainApply := lo.EveryBy(jobs, func(job orchestrator_scheduler.Job) bool { return lo.Contains(job.Commands, "digger apply") }) - if allJobsContainApply == true { + if allJobsContainApply { return orchestrator_scheduler.BatchTypeApply } else { return orchestrator_scheduler.BatchTypePlan diff --git a/next/controllers/github_after_merge.go b/next/controllers/github_after_merge.go index 5d52b4859..bc1de7cac 100644 --- a/next/controllers/github_after_merge.go +++ b/next/controllers/github_after_merge.go @@ -73,7 +73,7 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * log.Printf("error getting workflows from config: %v", err) return fmt.Errorf("error getting workflows from config") } - var config *dg_configuration.DiggerConfig = &dg_configuration.DiggerConfig{ + config := &dg_configuration.DiggerConfig{ ApplyAfterMerge: true, AllowDraftPRs: false, CommentRenderMode: "", diff --git a/next/dbmodels/storage.go b/next/dbmodels/storage.go index 06969b398..a855dcd10 100644 --- a/next/dbmodels/storage.go +++ b/next/dbmodels/storage.go @@ -697,7 +697,7 @@ func (db *Database) UpdateBatchStatus(batch *model.DiggerBatch) error { allJobsSucceeded = false } } - if allJobsSucceeded == true { + if allJobsSucceeded { batch.Status = int16(scheduler.BatchJobSucceeded) db.GormDB.Save(batch) } @@ -956,8 +956,7 @@ func (db *Database) UpdateDiggerJob(job *model.DiggerJob) error { func (db *Database) GetDiggerJobsForBatch(batchId string) ([]model.DiggerJob, error) { jobs := make([]model.DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId) + var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId) result := where.Find(&jobs) if result.Error != nil { @@ -971,8 +970,7 @@ func (db *Database) GetDiggerJobsForBatch(batchId string) ([]model.DiggerJob, er func (db *Database) GetDiggerJobsForBatchWithStatus(batchId string, status []scheduler.DiggerJobStatus) ([]model.DiggerJob, error) { jobs := make([]model.DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) + var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) result := where.Find(&jobs) if result.Error != nil { @@ -986,8 +984,7 @@ func (db *Database) GetDiggerJobsForBatchWithStatus(batchId string, status []sch func (db *Database) GetDiggerJobsWithStatus(status scheduler.DiggerJobStatus) ([]model.DiggerJob, error) { jobs := make([]model.DiggerJob, 0) - var where *gorm.DB - where = db.GormDB.Where("status = ?", status) + var where *gorm.DB = db.GormDB.Where("status = ?", status) result := where.Find(&jobs) if result.Error != nil { @@ -1275,7 +1272,7 @@ func (db *Database) UpdateRepoDiggerConfig(orgId string, config configuration.Di return fmt.Errorf("could not create project: %v", err) } } else { - if isMainBranch == true { + if isMainBranch { p.IsInMainBranch = isMainBranch } p.IsGenerated = dc.Generated diff --git a/next/services/scheduler.go b/next/services/scheduler.go index 0dfc5d34c..ac2fb79bb 100644 --- a/next/services/scheduler.go +++ b/next/services/scheduler.go @@ -152,7 +152,7 @@ func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projec return nil, nil, fmt.Errorf("error retrieving digger.yml workflows: %v", err) } - var config *dg_configuration.DiggerConfig = &dg_configuration.DiggerConfig{ + config := &dg_configuration.DiggerConfig{ ApplyAfterMerge: true, AllowDraftPRs: false, CommentRenderMode: "", diff --git a/next/services/spec.go b/next/services/spec.go index 41d01dc54..29808557b 100644 --- a/next/services/spec.go +++ b/next/services/spec.go @@ -68,7 +68,7 @@ func RefreshVariableSpecForJob(job *model.DiggerJob) error { return fmt.Errorf("could not get orb: %v", err) } - repoName := strings.Replace(batch.RepoFullName, "/", "-", -1) + repoName := strings.ReplaceAll(batch.RepoFullName, "/", "-") repo, err := dbmodels.DB.GetRepo(batch.OrganizationID, repoName) if err != nil { log.Printf("could not get repo: %v", err) @@ -152,11 +152,11 @@ func GetRunNameFromJob(job model.DiggerJob) (*string, error) { switch batch.EventType { case dbmodels.DiggerBatchMergeEvent: - runName += fmt.Sprintf(" (merge)") + runName += " (merge)" case dbmodels.DiggerBatchDriftEvent: - runName += fmt.Sprintf(" (drift)") + runName += " (drift)" case dbmodels.DiggerBatchManualTriggerEvent: - runName += fmt.Sprintf(" (user)") + runName += " (user)" case dbmodels.DiggerBatchPullRequestEvent: runName += fmt.Sprintf(" PR: %v", prNumber) } From 3ebc86c60efe10169e337a39634583a078464a22 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Wed, 23 Jul 2025 23:37:42 +0530 Subject: [PATCH 14/33] staticcheck next/ : lint files remove duplicate imports --- next/controllers/drift.go | 1 - next/dbmodels/setup.go | 1 - next/dbmodels/storage.go | 2 +- next/middleware/job_token_auth.go | 1 - next/middleware/webhooks.go | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/next/controllers/drift.go b/next/controllers/drift.go index 7cee207fe..0f7f6c480 100644 --- a/next/controllers/drift.go +++ b/next/controllers/drift.go @@ -94,7 +94,6 @@ func (d DiggerController) TriggerDriftDetectionForProject(c *gin.Context) { "status": "successful", "project_id": projectId, }) - return } func (d DiggerController) TriggerCronForMatchingProjects(c *gin.Context) { diff --git a/next/dbmodels/setup.go b/next/dbmodels/setup.go index 4a9366a97..1e9286d4d 100644 --- a/next/dbmodels/setup.go +++ b/next/dbmodels/setup.go @@ -7,7 +7,6 @@ import ( "github.com/diggerhq/digger/next/models_generated" slogGorm "github.com/orandin/slog-gorm" "gorm.io/driver/postgres" - _ "gorm.io/driver/postgres" "gorm.io/gorm" ) diff --git a/next/dbmodels/storage.go b/next/dbmodels/storage.go index a855dcd10..eeefd9cbc 100644 --- a/next/dbmodels/storage.go +++ b/next/dbmodels/storage.go @@ -706,7 +706,7 @@ func (db *Database) UpdateBatchStatus(batch *model.DiggerBatch) error { } func (db *Database) CreateDiggerJob(batchId string, serializedJob []byte, workflowFile string) (*model.DiggerJob, error) { - if serializedJob == nil || len(serializedJob) == 0 { + if len(serializedJob) == 0 { return nil, fmt.Errorf("serializedJob can't be empty") } jobId := uniuri.New() diff --git a/next/middleware/job_token_auth.go b/next/middleware/job_token_auth.go index dedc8fbfa..8b0c7fac3 100644 --- a/next/middleware/job_token_auth.go +++ b/next/middleware/job_token_auth.go @@ -66,6 +66,5 @@ func JobTokenAuth() gin.HandlerFunc { c.Abort() return } - return } } diff --git a/next/middleware/webhooks.go b/next/middleware/webhooks.go index c46694b09..d41b9695a 100644 --- a/next/middleware/webhooks.go +++ b/next/middleware/webhooks.go @@ -30,6 +30,5 @@ func WebhookAuth() gin.HandlerFunc { } c.Next() - return } } From 84ec2d6030e14361b2eafdaae733221884ca3d2a Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:33:01 +0530 Subject: [PATCH 15/33] govet: add linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 1e698c37e..56229b46d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,3 +25,4 @@ linters: default: none enable: - staticcheck + - govet From a15d209a3e5cd4ca9d5ec7fceb6770edb39eac6d Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:33:22 +0530 Subject: [PATCH 16/33] govet: lint backend/ --- backend/controllers/projects.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/controllers/projects.go b/backend/controllers/projects.go index 9b92fc4bf..43f291472 100644 --- a/backend/controllers/projects.go +++ b/backend/controllers/projects.go @@ -1204,7 +1204,7 @@ func UpdateCommentsForBatchGroup(gh utils.GithubClientProvider, batch *models.Di "commentId", detail.CommentId, ) - reporter := reporting.SourceGroupingReporter{serializedJobs, batch.PrNumber, ghService} + reporter := reporting.SourceGroupingReporter{Jobs: serializedJobs, PrNumber: batch.PrNumber, PrService: ghService} err := reporter.UpdateComment(sourceDetails, detail.SourceLocation, projectToTerraformOutput) if err != nil { slog.Warn("Error updating comment for source location", From 3e1d8ffea60444a6bb85949c8650fc963cbc9e23 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:37:25 +0530 Subject: [PATCH 17/33] =?UTF-8?q?=E2=9A=A0=EF=B8=8F=20possible=20fix:=20re?= =?UTF-8?q?turn=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ee/backend/controllers/bitbucket.go | 2 +- ee/backend/controllers/gitlab.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ee/backend/controllers/bitbucket.go b/ee/backend/controllers/bitbucket.go index 4603644de..c303e372b 100644 --- a/ee/backend/controllers/bitbucket.go +++ b/ee/backend/controllers/bitbucket.go @@ -277,7 +277,7 @@ func handleIssueCommentEventBB(bitbucketProvider utils.BitbucketProvider, payloa if err != nil { log.Printf("error setting status for PR: %v", err) utils.InitCommentReporter(bbService, issueNumber, fmt.Sprintf(":x: error setting status for PR: %v", err)) - fmt.Errorf("error setting status for PR: %v", err) + return fmt.Errorf("error setting status for PR: %v", err) } impactedProjectsMap := make(map[string]dg_configuration.Project) diff --git a/ee/backend/controllers/gitlab.go b/ee/backend/controllers/gitlab.go index e15bbdfe7..e72383bdf 100644 --- a/ee/backend/controllers/gitlab.go +++ b/ee/backend/controllers/gitlab.go @@ -270,7 +270,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab if err != nil { log.Printf("error setting status for PR: %v", err) utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: error setting status for PR: %v", err)) - fmt.Errorf("error setting status for PR: %v", err) + return fmt.Errorf("error setting status for PR: %v", err) } impactedProjectsMap := make(map[string]dg_configuration.Project) @@ -462,7 +462,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla if err != nil { log.Printf("error setting status for PR: %v", err) utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: error setting status for PR: %v", err)) - fmt.Errorf("error setting status for PR: %v", err) + return fmt.Errorf("error setting status for PR: %v", err) } impactedProjectsMap := make(map[string]dg_configuration.Project) From cb3538fda98a8b9bf656968be0b5989e782abf1e Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:38:59 +0530 Subject: [PATCH 18/33] govet: lint ee/drift/ --- ee/cli/pkg/drift/github_issue.go | 1 - ee/cli/pkg/drift/provider.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ee/cli/pkg/drift/github_issue.go b/ee/cli/pkg/drift/github_issue.go index 702c67e82..36c13dc38 100644 --- a/ee/cli/pkg/drift/github_issue.go +++ b/ee/cli/pkg/drift/github_issue.go @@ -40,5 +40,4 @@ func (ghi GithubIssueNotification) Send(projectName, plan string) error { } return err } - return nil } diff --git a/ee/cli/pkg/drift/provider.go b/ee/cli/pkg/drift/provider.go index a31edd873..24a622b17 100644 --- a/ee/cli/pkg/drift/provider.go +++ b/ee/cli/pkg/drift/provider.go @@ -16,7 +16,7 @@ func (d DriftNotificationProviderAdvanced) Get(prService ci.PullRequestService) DriftAsGithubIssues := os.Getenv("INPUT_DRIFT_GITHUB_ISSUES") var notification core_drift.Notification if slackNotificationUrl != "" { - notification = drift.SlackNotification{slackNotificationUrl} + notification = drift.SlackNotification{Url: slackNotificationUrl} } else if DriftAsGithubIssues != "" { notification = GithubIssueNotification{GithubService: &prService} } else { From ddd7e0ec31534476800f03a52a583bf13d766e91 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:40:55 +0530 Subject: [PATCH 19/33] govet: lint libs/ --- next/controllers/projects.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next/controllers/projects.go b/next/controllers/projects.go index 632be6a9c..6546d0c76 100644 --- a/next/controllers/projects.go +++ b/next/controllers/projects.go @@ -228,7 +228,7 @@ func UpdateCommentsForBatchGroup(gh utils.GithubClientProvider, batch *model.Dig } for _, detail := range sourceDetails { - reporter := reporting.SourceGroupingReporter{serializedJobs, int(batch.PrNumber), ghService} + reporter := reporting.SourceGroupingReporter{Jobs: serializedJobs, PrNumber: int(batch.PrNumber), PrService: ghService} reporter.UpdateComment(sourceDetails, detail.SourceLocation, projectToTerraformOutput) } return nil From 613f9eebff7f9b11a29065e5b4f6b7c0c9f94b41 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:41:11 +0530 Subject: [PATCH 20/33] =?UTF-8?q?=E2=9A=A0=EF=B8=8F=20possible=20fix:=20re?= =?UTF-8?q?turn=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next/controllers/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next/controllers/github.go b/next/controllers/github.go index c0d3e7cd0..0edb68f30 100644 --- a/next/controllers/github.go +++ b/next/controllers/github.go @@ -440,7 +440,7 @@ func handlePullRequestEvent(gh next_utils.GithubClientProvider, payload *github. if err != nil { log.Printf("error setting status for PR: %v", err) backend_utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: error setting status for PR: %v", err)) - fmt.Errorf("error setting status for PR: %v", err) + return fmt.Errorf("error setting status for PR: %v", err) } impactedProjectsMap := make(map[string]dg_configuration.Project) From 54d08c04f44edb97f112e3920519c862b8339b5b Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:43:08 +0530 Subject: [PATCH 21/33] gocritic: add linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 56229b46d..ba40b8844 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,3 +26,4 @@ linters: enable: - staticcheck - govet + - gocritic From f37d2c0ef9acaf0dea65f00cb767e895993a2508 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:50:10 +0530 Subject: [PATCH 22/33] gocritic: lint backend/ : [rename local variable like Org -> org] --- backend/controllers/github.go | 8 ++--- backend/models/orgs.go | 6 ++-- backend/models/storage.go | 60 +++++++++++++++++------------------ backend/tasks/runs_test.go | 4 +-- backend/utils/bitbucket.go | 6 ++-- backend/utils/graphs.go | 4 +-- backend/utils/pr_comment.go | 10 +++--- 7 files changed, 49 insertions(+), 49 deletions(-) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index cb2e125a2..fbc28cc2f 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -248,9 +248,9 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) { slog.Info("Exchanging code for GitHub app credentials", "code", code) // TODO: to make tls verification configurable for debug purposes - //var transport *http.Transport = nil - //_, exists := os.LookupEnv("DIGGER_GITHUB_SKIP_TLS") - //if exists { + // var transport *http.Transport = nil + // _, exists := os.LookupEnv("DIGGER_GITHUB_SKIP_TLS") + // if exists { // transport = &http.Transport{ // TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // } @@ -2725,7 +2725,7 @@ func validateGithubCallback(githubClientProvider utils.GithubClientProvider, cli &oauth2.Token{AccessToken: t.AccessToken}, ) tc := oauth2.NewClient(ctx, ts) - //tc := &http.Client{ + // tc := &http.Client{ // Transport: &oauth2.Transport{ // Base: httpClient.Transport, // Source: oauth2.ReuseTokenSource(nil, ts), diff --git a/backend/models/orgs.go b/backend/models/orgs.go index 94d0a5065..46cc6ad6a 100644 --- a/backend/models/orgs.go +++ b/backend/models/orgs.go @@ -118,9 +118,9 @@ type Project struct { } func (p *Project) MapToJsonStruct() interface{} { - //lastRun, _ := DB.GetLastDiggerRunForProject(p.Name) - //status := RunSucceeded - //if lastRun != nil { + // lastRun, _ := DB.GetLastDiggerRunForProject(p.Name) + // status := RunSucceeded + // if lastRun != nil { // status = lastRun.Status //} return struct { diff --git a/backend/models/storage.go b/backend/models/storage.go index e48261b4f..891f8b5cc 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -560,17 +560,17 @@ func (db *Database) GetGithubAppInstallationLink(installationId int64) (*GithubA return &link, nil } -func (db *Database) CreateVCSConnection(name string, vcsType DiggerVCSType, githubId int64, ClientID, ClientSecretEncrypted, WebhookSecretEncrypted, PrivateKeyEncrypted, PrivateKeyBase64Encrypted, Org, url, bitbucketAccessTokenEnc, bitbucketWebhookSecretEnc, gitlabWebhookSecret, gitlabAccessToken string, orgId uint) (*VCSConnection, error) { +func (db *Database) CreateVCSConnection(name string, vcsType DiggerVCSType, githubId int64, clientID, clientSecretEncrypted, webhookSecretEncrypted, privateKeyEncrypted, privateKeyBase64Encrypted, org, url, bitbucketAccessTokenEnc, bitbucketWebhookSecretEnc, gitlabWebhookSecret, gitlabAccessToken string, orgId uint) (*VCSConnection, error) { app := VCSConnection{ Name: name, VCSType: vcsType, GithubId: githubId, - ClientID: ClientID, - ClientSecretEncrypted: ClientSecretEncrypted, - WebhookSecretEncrypted: WebhookSecretEncrypted, - PrivateKeyEncrypted: PrivateKeyEncrypted, - PrivateKeyBase64Encrypted: PrivateKeyBase64Encrypted, - Org: Org, + ClientID: clientID, + ClientSecretEncrypted: clientSecretEncrypted, + WebhookSecretEncrypted: webhookSecretEncrypted, + PrivateKeyEncrypted: privateKeyEncrypted, + PrivateKeyBase64Encrypted: privateKeyBase64Encrypted, + Org: org, GithubAppUrl: url, BitbucketWebhookSecretEncrypted: bitbucketWebhookSecretEnc, BitbucketAccessTokenEncrypted: bitbucketAccessTokenEnc, @@ -773,17 +773,17 @@ func (db *Database) GetDiggerBatch(batchId *uuid.UUID) (*DiggerBatch, error) { return batch, nil } -func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, PRNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutputs, coverAllImpactedProjects bool, VCSConnectionId *uint) (*DiggerBatch, error) { +func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, prNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutputs, coverAllImpactedProjects bool, vcsConnectionId *uint) (*DiggerBatch, error) { uid := uuid.New() batch := &DiggerBatch{ ID: uid, VCS: vcsType, - VCSConnectionId: VCSConnectionId, + VCSConnectionId: vcsConnectionId, GithubInstallationId: githubInstallationId, RepoOwner: repoOwner, RepoName: repoName, RepoFullName: repoFullname, - PrNumber: PRNumber, + PrNumber: prNumber, CommentId: commentId, Status: scheduler.BatchJobCreated, BranchName: branchName, @@ -802,7 +802,7 @@ func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationI slog.Info("digger batch created successfully", "batchId", batch.ID, "repoFullName", repoFullname, - "prNumber", PRNumber) + "prNumber", prNumber) return batch, nil } @@ -898,17 +898,17 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ return runs, nil } -func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId, DiggerConfig string, GithubInstallationId int64, RepoId uint, ProjectName string, RunType RunType, planStageId, applyStageId *uint) (*DiggerRun, error) { +func (db *Database) CreateDiggerRun(triggertype string, prNumber int, status DiggerRunStatus, commitId, diggerConfig string, githubInstallationId int64, repoId uint, projectName string, runType RunType, planStageId, applyStageId *uint) (*DiggerRun, error) { dr := &DiggerRun{ - Triggertype: Triggertype, - PrNumber: &PrNumber, - Status: Status, - CommitId: CommitId, - DiggerConfig: DiggerConfig, - GithubInstallationId: GithubInstallationId, - RepoId: RepoId, - ProjectName: ProjectName, - RunType: RunType, + Triggertype: triggertype, + PrNumber: &prNumber, + Status: status, + CommitId: commitId, + DiggerConfig: diggerConfig, + GithubInstallationId: githubInstallationId, + RepoId: repoId, + ProjectName: projectName, + RunType: runType, PlanStageId: planStageId, ApplyStageId: applyStageId, IsApproved: false, @@ -918,15 +918,15 @@ func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status Dig slog.Error("failed to create digger run", "runId", dr.ID, "error", result.Error, - "projectName", ProjectName, - "repoId", RepoId) + "projectName", projectName, + "repoId", repoId) return nil, result.Error } slog.Info("digger run created successfully", "runId", dr.ID, - "projectName", ProjectName, - "prNumber", PrNumber, - "runType", RunType) + "projectName", projectName, + "prNumber", prNumber, + "runType", runType) return dr, nil } @@ -1116,7 +1116,7 @@ func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated, return nil, fmt.Errorf("Could not get digger job") } - var jobSummary *DiggerJobSummary = &diggerJob.DiggerJobSummary + jobSummary := &diggerJob.DiggerJobSummary jobSummary.ResourcesCreated = resourcesCreated jobSummary.ResourcesUpdated = resourcesUpdated jobSummary.ResourcesDeleted = resourcesDeleted @@ -1157,7 +1157,7 @@ func (db *Database) UpdateDiggerJob(job *DiggerJob) error { func (db *Database) GetDiggerJobsForBatch(batchId uuid.UUID) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId) + where := db.GormDB.Where("digger_jobs.batch_id = ?", batchId) result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs) if result.Error != nil { @@ -1215,7 +1215,7 @@ func (db *Database) GetJobsByRepoName(orgId uint, repoFullName string) ([]querie func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status []scheduler.DiggerJobStatus) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB = db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) + where := db.GormDB.Where("digger_jobs.batch_id = ?", batchId).Where("status IN ?", status) result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs) if result.Error != nil { @@ -1238,7 +1238,7 @@ func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status [] func (db *Database) GetDiggerJobsWithStatus(status scheduler.DiggerJobStatus) ([]DiggerJob, error) { jobs := make([]DiggerJob, 0) - var where *gorm.DB = db.GormDB.Where("status = ?", status) + where := db.GormDB.Where("status = ?", status) result := where.Preload("Batch").Find(&jobs) if result.Error != nil { diff --git a/backend/tasks/runs_test.go b/backend/tasks/runs_test.go index 3e710f4b0..d181e57c7 100644 --- a/backend/tasks/runs_test.go +++ b/backend/tasks/runs_test.go @@ -112,7 +112,7 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) { // BatchStatus: orchestrator_scheduler.BatchJobFailed, // InitialStatus: models.RunPlanning, // NextExpectedStatus: models.RunFailed, - //}, + // }, { BatchStatus: orchestrator_scheduler.BatchJobSucceeded, InitialStatus: models.RunPlanning, @@ -128,7 +128,7 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) { // BatchStatus: orchestrator_scheduler.BatchJobFailed, // InitialStatus: models.RunApplying, // NextExpectedStatus: models.RunFailed, - //}, + // }, { BatchStatus: orchestrator_scheduler.BatchJobSucceeded, InitialStatus: models.RunApplying, diff --git a/backend/utils/bitbucket.go b/backend/utils/bitbucket.go index b78de133d..98ada7d1f 100644 --- a/backend/utils/bitbucket.go +++ b/backend/utils/bitbucket.go @@ -37,11 +37,11 @@ func GetBitbucketService(bb BitbucketProvider, token, repoOwner, repoName string // token := os.Getenv("DIGGER_BITBUCKET_ACCESS_TOKEN") - //client, err := bb.NewClient(token) - //if err != nil { + // client, err := bb.NewClient(token) + // if err != nil { // return nil, fmt.Errorf("could not get bitbucket client: %v", err) //} - //context := orchestrator_bitbucket.BitbucketContext{ + // context := orchestrator_bitbucket.BitbucketContext{ // RepositoryName: repoName, // RepositoryFullName: repoFullName, // PullRequestID: &prNumber, diff --git a/backend/utils/graphs.go b/backend/utils/graphs.go index a2d0b866a..733da0580 100644 --- a/backend/utils/graphs.go +++ b/backend/utils/graphs.go @@ -15,7 +15,7 @@ import ( ) // ConvertJobsToDiggerJobs jobs is map with project name as a key and a Job as a value -func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner, repoName, repoFullName, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutput, coverAllImpactedProjects bool, VCSConnectionId *uint) (*uuid.UUID, map[string]*models.DiggerJob, error) { +func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.DiggerVCSType, organisationId uint, jobsMap map[string]scheduler.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner, repoName, repoFullName, commitSha string, commentId int64, diggerConfigStr string, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutput, coverAllImpactedProjects bool, vcsConnectionId *uint) (*uuid.UUID, map[string]*models.DiggerJob, error) { slog.Info("Converting jobs to Digger jobs", "jobType", jobType, "vcsType", vcsType, @@ -72,7 +72,7 @@ func ConvertJobsToDiggerJobs(jobType scheduler.DiggerCommand, vcsType models.Dig ) } - batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, gitlabProjectId, aiSummaryCommentId, reportTerraformOutput, coverAllImpactedProjects, VCSConnectionId) + batch, err := models.DB.CreateDiggerBatch(vcsType, githubInstallationId, repoOwner, repoName, repoFullName, prNumber, diggerConfigStr, branch, jobType, &commentId, gitlabProjectId, aiSummaryCommentId, reportTerraformOutput, coverAllImpactedProjects, vcsConnectionId) if err != nil { slog.Error("Failed to create batch", "error", err) return nil, nil, fmt.Errorf("failed to create batch: %v", err) diff --git a/backend/utils/pr_comment.go b/backend/utils/pr_comment.go index 600929d83..ac4f99291 100644 --- a/backend/utils/pr_comment.go +++ b/backend/utils/pr_comment.go @@ -148,12 +148,12 @@ func ReportInitialJobsStatus(cr *CommentReporter, jobs []scheduler.Job) error { message := "" if len(jobs) == 0 { - message = message + ":construction_worker: No projects impacted" + message += ":construction_worker: No projects impacted" } else { - message = message + "| Project | Status |\n" - message = message + "|---------|--------|\n" + message += "| Project | Status |\n" + message += "|---------|--------|\n" for _, job := range jobs { - message = message + fmt.Sprintf(""+ + message += fmt.Sprintf(""+ "|:clock11: **%v**|pending...|\n", job.ProjectName) } } @@ -204,7 +204,7 @@ func ReportNoProjectsImpacted(cr *CommentReporter, jobs []scheduler.Job) error { message := "" + ":construction_worker: The following projects are impacted\n\n" for _, job := range jobs { - message = message + fmt.Sprintf(""+ + message += fmt.Sprintf(""+ "\n"+ ":clock11: **%v**: pending...\n"+ "\n"+ From 5773953c336cfe6fb4c0057c47115b18ad006500 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:54:22 +0530 Subject: [PATCH 23/33] gocritic: lint cli/ : [rename `local` variables like - SCMRepository -> scmRepository] --- cli/pkg/digger/digger.go | 18 +++++++++--------- cli/pkg/digger/digger_test.go | 2 +- cli/pkg/spec/manual.go | 12 ++++++------ cli/pkg/spec/spec.go | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index 4881e1acc..ff7411ab0 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -190,10 +190,10 @@ func reportPolicyError(projectName, command, requestedBy string, reporter report return msg } -func run(command string, job orchestrator.Job, policyChecker policy.Checker, orgService ci.OrgService, SCMOrganisation, SCMrepository string, PRNumber *int, requestedBy string, reporter reporting.Reporter, lock locking2.Lock, prService ci.PullRequestService, projectNamespace, workingDir string, planStorage storage.PlanStorage, appliesPerProject map[string]bool) (*execution.DiggerExecutorResult, string, error) { +func run(command string, job orchestrator.Job, policyChecker policy.Checker, orgService ci.OrgService, scmOrganisation, scmRepository string, prNumber *int, requestedBy string, reporter reporting.Reporter, lock locking2.Lock, prService ci.PullRequestService, projectNamespace, workingDir string, planStorage storage.PlanStorage, appliesPerProject map[string]bool) (*execution.DiggerExecutorResult, string, error) { slog.Info("Running command for project", "command", command, "project name", job.ProjectName, "project workflow", job.ProjectWorkflow) - allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, &prService, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, requestedBy, []string{}) + allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, &prService, scmOrganisation, scmRepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, requestedBy, []string{}) if err != nil { return nil, "error checking policy", fmt.Errorf("error checking policy: %v", err) } @@ -241,7 +241,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org ProjectPath: projectPath, ProjectNamespace: projectNamespace, ProjectName: job.ProjectName, - PRNumber: PRNumber, + PRNumber: prNumber, } diggerExecutor := execution.LockingExecutorWrapper{ @@ -292,7 +292,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org } else if planPerformed { if isNonEmptyPlan { reportTerraformPlanOutput(reporter, projectLock.LockId(), plan) - planIsAllowed, messages, err := policyChecker.CheckPlanPolicy(SCMrepository, SCMOrganisation, job.ProjectName, job.ProjectDir, planJsonOutput) + planIsAllowed, messages, err := policyChecker.CheckPlanPolicy(scmRepository, scmOrganisation, job.ProjectName, job.ProjectDir, planJsonOutput) if err != nil { msg := fmt.Sprintf("Failed to validate plan. %v", err) slog.Error("Failed to validate plan.", "error", err) @@ -317,7 +317,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org for _, message := range messages { preformattedMessaged = append(preformattedMessaged, fmt.Sprintf(" %v", message)) } - planReportMessage = planReportMessage + strings.Join(preformattedMessaged, "
") + planReportMessage += strings.Join(preformattedMessaged, "
") _, _, err = reporter.Report(planReportMessage, planPolicyFormatter) if err != nil { slog.Error("Failed to report plan.", "error", err) @@ -393,7 +393,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org return nil, msg, fmt.Errorf("%s", msg) } - _, violations, err := policyChecker.CheckPlanPolicy(SCMrepository, SCMOrganisation, job.ProjectName, job.ProjectDir, terraformPlanJsonStr) + _, violations, err := policyChecker.CheckPlanPolicy(scmRepository, scmOrganisation, job.ProjectName, job.ProjectDir, terraformPlanJsonStr) if err != nil { msg := fmt.Sprintf("Failed to check plan policy. %v", err) slog.Error("Failed to check plan policy.", "error", err) @@ -405,7 +405,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org planPolicyViolations = []string{} } - allowedToApply, err := policyChecker.CheckAccessPolicy(orgService, &prService, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, requestedBy, planPolicyViolations) + allowedToApply, err := policyChecker.CheckAccessPolicy(orgService, &prService, scmOrganisation, scmRepository, job.ProjectName, job.ProjectDir, command, job.PullRequestNumber, requestedBy, planPolicyViolations) if err != nil { msg := fmt.Sprintf("Failed to run plan policy check before apply. %v", err) slog.Error("Failed to run plan policy check before apply", "error", err) @@ -726,12 +726,12 @@ func RunJob( return nil } -func runDriftDetection(policyChecker policy.Checker, SCMOrganisation, SCMrepository, projectName, requestedBy, eventName string, diggerExecutor execution.Executor, notification *core_drift.Notification) (string, error) { +func runDriftDetection(policyChecker policy.Checker, scmOrganisation, scmRepository, projectName, requestedBy, eventName string, diggerExecutor execution.Executor, notification *core_drift.Notification) (string, error) { err := usage.SendUsageRecord(requestedBy, eventName, "drift-detect") if err != nil { slog.Error("Failed to send usage report.", "error", err) } - policyEnabled, err := policyChecker.CheckDriftPolicy(SCMOrganisation, SCMrepository, projectName) + policyEnabled, err := policyChecker.CheckDriftPolicy(scmOrganisation, scmRepository, projectName) if err != nil { msg := fmt.Sprintf("failed to check drift policy. %v", err) slog.Error(msg) diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index 6d2f18a9b..a5a98d079 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -106,7 +106,7 @@ func (m *MockPRManager) PublishIssue(title, body string, labels *[]string) (int6 return 0, nil } -func (m *MockPRManager) UpdateIssue(ID int64, title, body string) (int64, error) { +func (m *MockPRManager) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/cli/pkg/spec/manual.go b/cli/pkg/spec/manual.go index ad3edd27b..070d7ea26 100644 --- a/cli/pkg/spec/manual.go +++ b/cli/pkg/spec/manual.go @@ -26,7 +26,7 @@ func RunSpecManualCommand( reporterProvider spec.ReporterProvider, backedProvider spec.BackendApiProvider, policyProvider spec.SpecPolicyProvider, - PlanStorageProvider spec.PlanStorageProvider, + planStorageProvider spec.PlanStorageProvider, commentUpdaterProvider comment_summary.CommentUpdaterProvider, ) error { job, err := jobProvider.GetJob(spec.Job) @@ -39,19 +39,19 @@ func RunSpecManualCommand( usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get job: %v", err), 1) } - //prService, err := vcsProvider.GetPrService(spec.VCS) + // prService, err := vcsProvider.GetPrService(spec.VCS) //if err != nil { // usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get prservice: %v", err), 1) //} var prService ci.PullRequestService = ci.MockPullRequestManager{} - //orgService, err := vcsProvider.GetOrgService(spec.VCS) + // orgService, err := vcsProvider.GetOrgService(spec.VCS) //if err != nil { // usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get orgservice: %v", err), 1) //} var orgService ci.OrgService = ci.MockPullRequestManager{} - //reporter, err := reporterProvider.GetReporter(spec.Reporter, prService, *spec.Job.PullRequestNumber) + // reporter, err := reporterProvider.GetReporter(spec.Reporter, prService, *spec.Job.PullRequestNumber) //if err != nil { // usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get reporter: %v", err), 1) //} @@ -106,7 +106,7 @@ func RunSpecManualCommand( usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get policy provider: %v", err), 1) } - //planStorage, err := PlanStorageProvider.GetPlanStorage(spec.VCS.RepoOwner, spec.VCS.RepoName, *spec.Job.PullRequestNumber) + // planStorage, err := PlanStorageProvider.GetPlanStorage(spec.VCS.RepoOwner, spec.VCS.RepoName, *spec.Job.PullRequestNumber) //if err != nil { // usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("could not get plan storage: %v", err), 8) //} @@ -114,7 +114,7 @@ func RunSpecManualCommand( jobs := []scheduler.Job{job} - //fullRepoName := fmt.Sprintf("%v-%v", spec.VCS.RepoOwner, spec.VCS.RepoName) + // fullRepoName := fmt.Sprintf("%v-%v", spec.VCS.RepoOwner, spec.VCS.RepoName) //_, err = backendApi.ReportProjectJobStatus(fullRepoName, spec.Job.ProjectName, spec.JobId, "started", time.Now(), nil, "", "") //if err != nil { // usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("Failed to report jobSpec status to backend. Exiting. %v", err), 4) diff --git a/cli/pkg/spec/spec.go b/cli/pkg/spec/spec.go index b31e4f756..61016448d 100644 --- a/cli/pkg/spec/spec.go +++ b/cli/pkg/spec/spec.go @@ -33,7 +33,7 @@ func RunSpec( reporterProvider spec.ReporterProvider, backedProvider spec.BackendApiProvider, policyProvider spec.SpecPolicyProvider, - PlanStorageProvider spec.PlanStorageProvider, + planStorageProvider spec.PlanStorageProvider, variablesProvider spec.VariablesProvider, commentUpdaterProvider comment_summary.CommentUpdaterProvider, ) error { @@ -118,7 +118,7 @@ func RunSpec( reportError(spec, backendApi, message, err) } - planStorage, err := PlanStorageProvider.GetPlanStorage(spec.VCS.RepoOwner, spec.VCS.RepoName, *spec.Job.PullRequestNumber) + planStorage, err := planStorageProvider.GetPlanStorage(spec.VCS.RepoOwner, spec.VCS.RepoName, *spec.Job.PullRequestNumber) if err != nil { message := fmt.Sprintf("could not get planStorage: %v", err) reportError(spec, backendApi, message, err) From 62dfd240fc93ce5362d404108b543270018d8ec3 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 00:57:34 +0530 Subject: [PATCH 24/33] gocritic : lint ee/* --- ee/backend/controllers/web.go | 2 +- ee/cli/pkg/comment_updater/updater.go | 6 +++--- ee/cli/pkg/gitlab/gitlab.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ee/backend/controllers/web.go b/ee/backend/controllers/web.go index d4f440341..7c5d09dfc 100644 --- a/ee/backend/controllers/web.go +++ b/ee/backend/controllers/web.go @@ -65,7 +65,7 @@ func (web *WebController) ReposPage(c *gin.Context) { maps.Copy(pageContext, gin.H{ "Repos": repos, - //"GithubApp": githubApp, + // "GithubApp": githubApp, }) c.HTML(http.StatusOK, "repos.tmpl", pageContext) } diff --git a/ee/cli/pkg/comment_updater/updater.go b/ee/cli/pkg/comment_updater/updater.go index c36a32d0c..316c992cd 100644 --- a/ee/cli/pkg/comment_updater/updater.go +++ b/ee/cli/pkg/comment_updater/updater.go @@ -43,9 +43,9 @@ func (a AdvancedCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, pr workflowUrl = *job.WorkflowRunUrl } - message = message + fmt.Sprintf("\n", job.ProjectName) - message = message + fmt.Sprintf("%v **%v** %v%v %v\n", job.Status.ToEmoji(), jobSpec.ProjectName, workflowUrl, job.Status.ToString(), job.ResourcesSummaryString(isPlan), DriftSummaryString(job.ProjectName, issuesMap)) - message = message + fmt.Sprintf("\n", job.ProjectName) + message += fmt.Sprintf("\n", job.ProjectName) + message += fmt.Sprintf("%v **%v** %v%v %v\n", job.Status.ToEmoji(), jobSpec.ProjectName, workflowUrl, job.Status.ToString(), job.ResourcesSummaryString(isPlan), DriftSummaryString(job.ProjectName, issuesMap)) + message += fmt.Sprintf("\n", job.ProjectName) } prService.EditComment(prNumber, prCommentId, message) diff --git a/ee/cli/pkg/gitlab/gitlab.go b/ee/cli/pkg/gitlab/gitlab.go index 5c0cd5d47..43836a7f6 100644 --- a/ee/cli/pkg/gitlab/gitlab.go +++ b/ee/cli/pkg/gitlab/gitlab.go @@ -50,14 +50,14 @@ func GitLabCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh usage.ReportErrorAndExit(repoName, fmt.Sprintf("Failed to get current dir. %s", err), 4) } - //gitLabContext, err := gitlab.ParseGitLabContext() + // gitLabContext, err := gitlab.ParseGitLabContext() //if err != nil { // fmt.Printf("failed to parse GitLab context. %s\n", err.Error()) // os.Exit(4) //} // it's ok to not have merge request info if it has been merged - //if (gitLabContext.MergeRequestIId == nil || len(gitLabContext.OpenMergeRequests) == 0) && gitLabContext.EventType != "merge_request_merge" { + // if (gitLabContext.MergeRequestIId == nil || len(gitLabContext.OpenMergeRequests) == 0) && gitLabContext.EventType != "merge_request_merge" { // fmt.Println("No merge request found.") // os.Exit(0) //} From 0eb9f9ab25e9036686e641cdb72d9d1c20ed214e Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:07:02 +0530 Subject: [PATCH 25/33] gocritic: lint libs/ : rename local function variables in camelcase --- libs/backendapi/diggerapi.go | 8 ++-- libs/backendapi/mocks.go | 2 +- libs/ci/azure/azure.go | 8 ++-- libs/ci/bitbucket/bitbucket.go | 2 +- libs/ci/generic/events.go | 2 +- libs/ci/github/github.go | 10 ++-- libs/ci/github/mocks.go | 2 +- libs/ci/gitlab/gitlab.go | 2 +- libs/ci/mocks.go | 2 +- libs/comment_utils/reporting/reporting.go | 32 ++++++------- .../reporting/source_grouping.go | 2 +- libs/comment_utils/summary/updater.go | 6 +-- libs/digger_config/yaml.go | 12 ++--- libs/orchestrator/mock.go | 2 +- libs/policy/mocks.go | 6 +-- libs/policy/policy.go | 48 +++++++++---------- libs/scheduler/aws_test.go | 2 +- libs/spec/payloads.go | 4 +- libs/storage/aws_plan_storage.go | 8 ++-- 19 files changed, 78 insertions(+), 82 deletions(-) diff --git a/libs/backendapi/diggerapi.go b/libs/backendapi/diggerapi.go index ec2c77a1f..62415f0c3 100644 --- a/libs/backendapi/diggerapi.go +++ b/libs/backendapi/diggerapi.go @@ -30,7 +30,7 @@ func (n NoopApi) ReportProjectRun(namespace, projectName string, startedAt, ende return nil } -func (n NoopApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (n NoopApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, prCommentUrl, prCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { return nil, nil } @@ -130,7 +130,7 @@ func (d DiggerApi) ReportProjectRun(namespace, projectName string, startedAt, en return nil } -func (d DiggerApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (d DiggerApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, prCommentUrl, prCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { repoNameForBackendReporting := strings.ReplaceAll(repo, "/", "-") u, err := url.Parse(d.DiggerHost) if err != nil { @@ -161,8 +161,8 @@ func (d DiggerApi) ReportProjectJobStatus(repo, projectName, jobId, status strin "timestamp": timestamp, "job_summary": planSummaryJson, "job_plan_footprint": planFootprint.ToJson(), - "pr_comment_url": PrCommentUrl, - "pr_comment_id": PrCommentId, + "pr_comment_url": prCommentUrl, + "pr_comment_id": prCommentId, "terraform_output": terraformOutput, } diff --git a/libs/backendapi/mocks.go b/libs/backendapi/mocks.go index b6da8fd9d..5aad0f6f6 100644 --- a/libs/backendapi/mocks.go +++ b/libs/backendapi/mocks.go @@ -17,7 +17,7 @@ func (t MockBackendApi) ReportProjectRun(repo, projectName string, startedAt, en return nil } -func (t MockBackendApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, PrCommentUrl, PrCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { +func (t MockBackendApi) ReportProjectJobStatus(repo, projectName, jobId, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson, prCommentUrl, prCommentId, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) { return nil, nil } diff --git a/libs/ci/azure/azure.go b/libs/ci/azure/azure.go index d66f2103b..2ef4905ec 100644 --- a/libs/ci/azure/azure.go +++ b/libs/ci/azure/azure.go @@ -246,10 +246,8 @@ func (a *AzureReposService) GetCombinedPullRequestStatus(prNumber int) (string, if res, ok := latestUniqueRequestStatuses[key]; !ok { latestUniqueRequestStatuses[key] = &status - } else { - if status.CreationDate.Time.After(res.CreationDate.Time) { - latestUniqueRequestStatuses[key] = &status - } + } else if status.CreationDate.Time.After(res.CreationDate.Time) { + latestUniqueRequestStatuses[key] = &status } } @@ -433,7 +431,7 @@ func ProcessAzureReposEvent(azureEvent interface{}, diggerConfig *digger_config2 func ConvertAzureEventToCommands(parseAzureContext Azure, impactedProjects []digger_config2.Project, requestedProject *digger_config2.Project, workflows map[string]digger_config2.Workflow) ([]scheduler.Job, bool, error) { jobs := make([]scheduler.Job, 0) - //&dependencyGraph, diggerProjectNamespace, parsedAzureContext.BaseUrl, parsedAzureContext.EventType, prNumber, + // &dependencyGraph, diggerProjectNamespace, parsedAzureContext.BaseUrl, parsedAzureContext.EventType, prNumber, switch parseAzureContext.EventType { case AzurePrCreated, AzurePrUpdated, AzurePrReopened: diff --git a/libs/ci/bitbucket/bitbucket.go b/libs/ci/bitbucket/bitbucket.go index bd45f3e15..619def93b 100644 --- a/libs/ci/bitbucket/bitbucket.go +++ b/libs/ci/bitbucket/bitbucket.go @@ -153,7 +153,7 @@ func (svc BitbucketAPI) PublishIssue(title, body string, labels *[]string) (int6 return 0, fmt.Errorf("implement me") } -func (svc BitbucketAPI) UpdateIssue(ID int64, title, body string) (int64, error) { +func (svc BitbucketAPI) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/libs/ci/generic/events.go b/libs/ci/generic/events.go index 24ea244c8..7bd24afca 100644 --- a/libs/ci/generic/events.go +++ b/libs/ci/generic/events.go @@ -183,7 +183,7 @@ func CreateJobsForProjects(projects []digger_config.Project, command, event, rep CommandEnvVars: commandEnvVars, StateEnvVars: stateEnvVars, PullRequestNumber: issueNumber, - EventName: event, //"issue_comment", + EventName: event, // "issue_comment", Namespace: repoFullName, RequestedBy: requestedBy, StateEnvProvider: StateEnvProvider, diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index 1cdfc5cd2..b00c17ca2 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -147,8 +147,8 @@ func (svc GithubService) PublishIssue(title, body string, labels *[]string) (int return *githubissue.ID, err } -func (svc GithubService) UpdateIssue(ID int64, title, body string) (int64, error) { - githubissue, _, err := svc.Client.Issues.Edit(context.Background(), svc.Owner, svc.RepoName, int(ID), &github.IssueRequest{Title: &title, Body: &body}) +func (svc GithubService) UpdateIssue(id int64, title, body string) (int64, error) { + githubissue, _, err := svc.Client.Issues.Edit(context.Background(), svc.Owner, svc.RepoName, int(id), &github.IssueRequest{Title: &title, Body: &body}) if err != nil { return 0, fmt.Errorf("could not edit issue: %v", err) } @@ -236,10 +236,10 @@ func (svc GithubService) CreateCommentReaction(id, reaction string) error { return nil } -func (svc GithubService) IsPullRequest(PrNumber int) (bool, error) { - issue, _, err := svc.Client.Issues.Get(context.Background(), svc.Owner, svc.RepoName, PrNumber) +func (svc GithubService) IsPullRequest(prNumber int) (bool, error) { + issue, _, err := svc.Client.Issues.Get(context.Background(), svc.Owner, svc.RepoName, prNumber) if err != nil { - slog.Error("error getting pull request (as issue)", "error", err, "prNumber", PrNumber) + slog.Error("error getting pull request (as issue)", "error", err, "prNumber", prNumber) return false, fmt.Errorf("error getting pull request (as issue): %v", err) } return issue.IsPullRequest(), nil diff --git a/libs/ci/github/mocks.go b/libs/ci/github/mocks.go index bd0b5f7fc..38be5f456 100644 --- a/libs/ci/github/mocks.go +++ b/libs/ci/github/mocks.go @@ -48,7 +48,7 @@ func (t MockCiService) PublishIssue(title, body string, labels *[]string) (int64 return 0, fmt.Errorf("implement me") } -func (svc MockCiService) UpdateIssue(ID int64, title, body string) (int64, error) { +func (svc MockCiService) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/libs/ci/gitlab/gitlab.go b/libs/ci/gitlab/gitlab.go index c78e8435b..f4fff5fd8 100644 --- a/libs/ci/gitlab/gitlab.go +++ b/libs/ci/gitlab/gitlab.go @@ -220,7 +220,7 @@ func (svc GitLabService) PublishIssue(title, body string, labels *[]string) (int return 0, fmt.Errorf("implement me") } -func (svc GitLabService) UpdateIssue(ID int64, title, body string) (int64, error) { +func (svc GitLabService) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/libs/ci/mocks.go b/libs/ci/mocks.go index 49c3805e5..5968f507e 100644 --- a/libs/ci/mocks.go +++ b/libs/ci/mocks.go @@ -28,7 +28,7 @@ func (t MockPullRequestManager) PublishIssue(title, body string, labels *[]strin return 0, nil } -func (t MockPullRequestManager) UpdateIssue(ID int64, title, body string) (int64, error) { +func (t MockPullRequestManager) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/libs/comment_utils/reporting/reporting.go b/libs/comment_utils/reporting/reporting.go index 35d22cdb6..cb0696c3e 100644 --- a/libs/comment_utils/reporting/reporting.go +++ b/libs/comment_utils/reporting/reporting.go @@ -113,10 +113,10 @@ type CommentPerRunStrategy struct { TimeOfRun time.Time } -func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - comments, err := ciService.GetComments(PrNumber) +func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, prNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { + comments, err := ciService.GetComments(prNumber) if err != nil { - slog.Error("error getting comments", "error", err, "prNumber", PrNumber) + slog.Error("error getting comments", "error", err, "prNumber", prNumber) return "", "", fmt.Errorf("error getting comments: %v", err) } @@ -126,11 +126,11 @@ func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, Pr } else { reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") } - commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) + commentId, commentUrl, err := upsertComment(ciService, prNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) return commentId, commentUrl, err } -func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) { +func upsertComment(ciService ci.PullRequestService, prNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) { report = reportFormatter(report) commentIdForThisRun := "" var commentBody string @@ -151,9 +151,9 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, } else { commentMessage = utils.AsCollapsibleComment(reportTitle, false)(report) } - comment, err := ciService.PublishComment(PrNumber, commentMessage) + comment, err := ciService.PublishComment(prNumber, commentMessage) if err != nil { - slog.Error("error publishing comment", "error", err, "prNumber", PrNumber) + slog.Error("error publishing comment", "error", err, "prNumber", prNumber) return "", "", fmt.Errorf("error publishing comment: %v", err) } return fmt.Sprintf("%v", comment.Id), comment.Url, nil @@ -173,9 +173,9 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody) } - err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) + err := ciService.EditComment(prNumber, commentIdForThisRun, completeComment) if err != nil { - slog.Error("error editing comment", "error", err, "commentId", commentIdForThisRun, "prNumber", PrNumber) + slog.Error("error editing comment", "error", err, "commentId", commentIdForThisRun, "prNumber", prNumber) return "", "", fmt.Errorf("error editing comment: %v", err) } return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil @@ -185,24 +185,24 @@ type LatestRunCommentStrategy struct { TimeOfRun time.Time } -func (strategy LatestRunCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - comments, err := ciService.GetComments(PrNumber) +func (strategy LatestRunCommentStrategy) Report(ciService ci.PullRequestService, prNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { + comments, err := ciService.GetComments(prNumber) if err != nil { - slog.Error("error getting comments", "error", err, "prNumber", PrNumber) + slog.Error("error getting comments", "error", err, "prNumber", prNumber) return "", "", fmt.Errorf("error getting comments: %v", err) } reportTitle := "Digger latest run report" - commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) + commentId, commentUrl, err := upsertComment(ciService, prNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) return commentId, commentUrl, err } type MultipleCommentsStrategy struct{} -func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { - comment, err := ciService.PublishComment(PrNumber, reportFormatter(report)) +func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, prNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) { + comment, err := ciService.PublishComment(prNumber, reportFormatter(report)) if err != nil { - slog.Error("error publishing comment", "error", err, "prNumber", PrNumber) + slog.Error("error publishing comment", "error", err, "prNumber", prNumber) return "", "", err } return comment.Id, comment.Url, nil diff --git a/libs/comment_utils/reporting/source_grouping.go b/libs/comment_utils/reporting/source_grouping.go index 4b1f691e9..37e87f579 100644 --- a/libs/comment_utils/reporting/source_grouping.go +++ b/libs/comment_utils/reporting/source_grouping.go @@ -78,7 +78,7 @@ func (r SourceGroupingReporter) UpdateComment(sourceDetails []SourceDetails, loc } message := "" - message = message + fmt.Sprintf("# Group: %v (similar: %v)\n", location, allSimilarInGroup) + message += fmt.Sprintf("# Group: %v (similar: %v)\n", location, allSimilarInGroup) slog.Info("generating comment for source location", "location", location, diff --git a/libs/comment_utils/summary/updater.go b/libs/comment_utils/summary/updater.go index cb9fd2bcc..0d7aa98f0 100644 --- a/libs/comment_utils/summary/updater.go +++ b/libs/comment_utils/summary/updater.go @@ -34,8 +34,8 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum "jobType", jobType) message := "" - message = message + fmt.Sprintf("| Project | Status | %v | + | ~ | - |\n", jobTypeTitle) - message = message + "|---------|--------|------|---|---|---|\n" + message += fmt.Sprintf("| Project | Status | %v | + | ~ | - |\n", jobTypeTitle) + message += "|---------|--------|------|---|---|---|\n" for i, job := range jobs { jobSpec := jobSpecs[i] @@ -47,7 +47,7 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum workflowUrl = *job.WorkflowRunUrl } - message = message + fmt.Sprintf("|%v **%v** |%v | %v | %v | %v | %v|\n", + message += fmt.Sprintf("|%v **%v** |%v | %v | %v | %v | %v|\n", job.Status.ToEmoji(), jobSpec.ProjectName, workflowUrl, diff --git a/libs/digger_config/yaml.go b/libs/digger_config/yaml.go index 31fdaf204..0e43f6b4b 100644 --- a/libs/digger_config/yaml.go +++ b/libs/digger_config/yaml.go @@ -304,14 +304,12 @@ func (s *StepYaml) extract(stepMap map[string]interface{}, action string) { extraArgs = append(extraArgs, v.(string)) } s.ExtraArgs = extraArgs - } else { - if stepMap[action] != nil { - if v, ok := stepMap[action].(map[string]interface{})["extra_args"]; ok { - for _, v := range v.([]interface{}) { - extraArgs = append(extraArgs, v.(string)) - } - s.ExtraArgs = extraArgs + } else if stepMap[action] != nil { + if v, ok := stepMap[action].(map[string]interface{})["extra_args"]; ok { + for _, v := range v.([]interface{}) { + extraArgs = append(extraArgs, v.(string)) } + s.ExtraArgs = extraArgs } } } diff --git a/libs/orchestrator/mock.go b/libs/orchestrator/mock.go index 9a26e36a2..e32d77a61 100644 --- a/libs/orchestrator/mock.go +++ b/libs/orchestrator/mock.go @@ -34,7 +34,7 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) PublishIssue(t return 0, nil } -func (mockGithubPullrequestManager *MockGithubPullrequestManager) UpdateIssue(ID int64, title, body string) (int64, error) { +func (mockGithubPullrequestManager *MockGithubPullrequestManager) UpdateIssue(id int64, title, body string) (int64, error) { return 0, fmt.Errorf("implement me") } diff --git a/libs/policy/mocks.go b/libs/policy/mocks.go index b98689309..388e1c735 100644 --- a/libs/policy/mocks.go +++ b/libs/policy/mocks.go @@ -4,14 +4,14 @@ import "github.com/diggerhq/digger/libs/ci" type MockPolicyChecker struct{} -func (t MockPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (t MockPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, scmOrganisation, scmrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { return false, nil } -func (t MockPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { +func (t MockPolicyChecker) CheckPlanPolicy(scmRepository, scmOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { return false, nil, nil } -func (t MockPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectname string) (bool, error) { +func (t MockPolicyChecker) CheckDriftPolicy(scmOrganisation, scmRepository, projectname string) (bool, error) { return true, nil } diff --git a/libs/policy/policy.go b/libs/policy/policy.go index a3f244a82..479fc93a7 100644 --- a/libs/policy/policy.go +++ b/libs/policy/policy.go @@ -29,15 +29,15 @@ type DiggerHttpPolicyProvider struct { type NoOpPolicyChecker struct{} -func (p NoOpPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (p NoOpPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, scmOrganisation, scmRepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { return true, nil } -func (p NoOpPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { +func (p NoOpPolicyChecker) CheckPlanPolicy(scmRepository, scmOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { return true, nil, nil } -func (p NoOpPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectname string) (bool, error) { +func (p NoOpPolicyChecker) CheckDriftPolicy(scmOrganisation, scmRepository, projectname string) (bool, error) { return true, nil } @@ -343,24 +343,24 @@ type DiggerPolicyChecker struct { } // TODO refactor to use AccessPolicyContext - too many arguments -func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, SCMOrganisation, SCMrepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { +func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prService *ci.PullRequestService, scmOrganisation, scmRepository, projectName, projectDir, command string, prNumber *int, requestedBy string, planPolicyViolations []string) (bool, error) { slog.Debug("Checking access policy", - "organisation", SCMOrganisation, - "repository", SCMrepository, + "organisation", scmOrganisation, + "repository", scmRepository, "project", projectName, "command", command, "requestedBy", requestedBy) - policy, err := p.PolicyProvider.GetAccessPolicy(SCMOrganisation, SCMrepository, projectName, projectDir) + policy, err := p.PolicyProvider.GetAccessPolicy(scmOrganisation, scmRepository, projectName, projectDir) if err != nil { slog.Error("Error fetching policy", "error", err) return false, err } - teams, err := ciService.GetUserTeams(SCMOrganisation, requestedBy) + teams, err := ciService.GetUserTeams(scmOrganisation, requestedBy) if err != nil { slog.Error("Error fetching user teams", - "organisation", SCMOrganisation, + "organisation", scmOrganisation, "user", requestedBy, "error", err) slog.Warn("Teams failed to be fetched, using empty list for access policy checks") @@ -380,7 +380,7 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic input := map[string]interface{}{ "user": requestedBy, - "organisation": SCMOrganisation, + "organisation": scmOrganisation, "teams": teams, "approvals": approvals, "planPolicyViolations": planPolicyViolations, @@ -437,13 +437,13 @@ func (p DiggerPolicyChecker) CheckAccessPolicy(ciService ci.OrgService, prServic return true, nil } -func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { +func (p DiggerPolicyChecker) CheckPlanPolicy(scmRepository, scmOrganisation, projectname, projectDir, planOutput string) (bool, []string, error) { slog.Debug("Checking plan policy", - "organisation", SCMOrganisation, - "repository", SCMrepository, + "organisation", scmOrganisation, + "repository", scmRepository, "project", projectname) - policy, err := p.PolicyProvider.GetPlanPolicy(SCMOrganisation, SCMrepository, projectname, projectDir) + policy, err := p.PolicyProvider.GetPlanPolicy(scmOrganisation, scmRepository, projectname, projectDir) if err != nil { slog.Error("Failed to get plan policy", "error", err) return false, nil, fmt.Errorf("failed get plan policy: %v", err) @@ -504,23 +504,23 @@ func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository, SCMOrganisation, pro if len(decisionsResult) > 0 { slog.Info("Plan policy check failed", "violations", len(decisionsResult), - "organisation", SCMOrganisation, - "repository", SCMrepository, + "organisation", scmOrganisation, + "repository", scmRepository, "project", projectname) return false, decisionsResult, nil } slog.Info("Plan policy check passed", - "organisation", SCMOrganisation, - "repository", SCMrepository, + "organisation", scmOrganisation, + "repository", scmRepository, "project", projectname) return true, []string{}, nil } -func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, projectName string) (bool, error) { +func (p DiggerPolicyChecker) CheckDriftPolicy(scmOrganisation, scmRepository, projectName string) (bool, error) { slog.Debug("Checking drift policy", - "organisation", SCMOrganisation, - "repository", SCMrepository, + "organisation", scmOrganisation, + "repository", scmRepository, "project", projectName) // TODO: Get rid of organisation if its not needed @@ -532,7 +532,7 @@ func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, pr } input := map[string]interface{}{ - "organisation": SCMOrganisation, + "organisation": scmOrganisation, "project": projectName, } @@ -571,14 +571,14 @@ func (p DiggerPolicyChecker) CheckDriftPolicy(SCMOrganisation, SCMrepository, pr } if !decision { slog.Info("Drift detection disabled by policy", - "organisation", SCMOrganisation, + "organisation", scmOrganisation, "project", projectName) return false, nil } } slog.Info("Drift detection enabled by policy", - "organisation", SCMOrganisation, + "organisation", scmOrganisation, "project", projectName) return true, nil } diff --git a/libs/scheduler/aws_test.go b/libs/scheduler/aws_test.go index 4da7f285a..44c14f5b7 100644 --- a/libs/scheduler/aws_test.go +++ b/libs/scheduler/aws_test.go @@ -89,7 +89,7 @@ func (a *AwsRoleProviderMock) RetrieveWithContext(context.Context) (aws.Credenti func (a *AwsRoleProviderMock) IsExpired() bool { return false } // TODO: uncomment this test after figuring out how to create a mock compatible with WebIdentityRoleProvider -//func TestPopulationForAwsRoleToAssumeSetsValueOfKeys(t *testing.T) { +// func TestPopulationForAwsRoleToAssumeSetsValueOfKeys(t *testing.T) { // stateEnvVars := make(map[string]string) // commandEnvVars := make(map[string]string) // diff --git a/libs/spec/payloads.go b/libs/spec/payloads.go index 0683e20e7..bac52c2f0 100644 --- a/libs/spec/payloads.go +++ b/libs/spec/payloads.go @@ -15,8 +15,8 @@ func (p GetSpecPayload) ToMapStruct() map[string]interface{} { "command": p.Command, "repo_full_name": p.RepoFullName, "actor": p.Actor, - //"default_branch": p.DefaultBranch, - //"pr_branch": p.PrBranch, + // "default_branch": p.DefaultBranch, + // "pr_branch": p.PrBranch, "digger_config": p.DiggerConfig, "project": p.Project, } diff --git a/libs/storage/aws_plan_storage.go b/libs/storage/aws_plan_storage.go index b1af03796..a3a35c6ea 100644 --- a/libs/storage/aws_plan_storage.go +++ b/libs/storage/aws_plan_storage.go @@ -40,7 +40,7 @@ type PlanStorageAWS struct { KMSEncryptionId string } -func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType, KMSEncryptionId string) (*PlanStorageAWS, error) { +func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType, kmsEncryptionId string) (*PlanStorageAWS, error) { if bucketName == "" { slog.Error("AWS S3 bucket name not provided") return nil, fmt.Errorf("AWS_S3_BUCKET is not defined") @@ -70,13 +70,13 @@ func NewAWSPlanStorage(bucketName string, encryptionEnabled bool, encryptionType slog.Debug("Using AES256 encryption for S3 storage") planStorage.EncryptionType = ServerSideEncryptionAes256 case "KMS": - if KMSEncryptionId == "" { + if kmsEncryptionId == "" { slog.Error("KMS encryption requested but no KMS key specified") return nil, fmt.Errorf("KMS encryption requested but no KMS key specified") } - slog.Debug("Using KMS encryption for S3 storage", "kmsKeyId", KMSEncryptionId) + slog.Debug("Using KMS encryption for S3 storage", "kmsKeyId", kmsEncryptionId) planStorage.EncryptionType = ServerSideEncryptionAwsKms - planStorage.KMSEncryptionId = KMSEncryptionId + planStorage.KMSEncryptionId = kmsEncryptionId default: slog.Error("Unknown encryption type specified", "encryptionType", encryptionType) return nil, fmt.Errorf("unknown encryption type specified for aws plan bucket: %v", encryptionType) From 994da92b70d44e308a5b935603b8908f3f030b32 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:08:13 +0530 Subject: [PATCH 26/33] gocritic: lint next/ : rename local function variables in camelcase --- next/controllers/github.go | 4 ++-- next/controllers/projects.go | 6 +++--- next/dbmodels/storage.go | 28 ++++++++++++++-------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/next/controllers/github.go b/next/controllers/github.go index 0edb68f30..593ed756b 100644 --- a/next/controllers/github.go +++ b/next/controllers/github.go @@ -206,7 +206,7 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) { } // TODO: to make tls verification configurable for debug purposes - //var transport *http.Transport = nil + // var transport *http.Transport = nil //_, exists := os.LookupEnv("DIGGER_GITHUB_SKIP_TLS") //if exists { // transport = &http.Transport{ @@ -787,7 +787,7 @@ func validateGithubCallback(githubClientProvider next_utils.GithubClientProvider &oauth2.Token{AccessToken: t.AccessToken}, ) tc := oauth2.NewClient(ctx, ts) - //tc := &http.Client{ + // tc := &http.Client{ // Transport: &oauth2.Transport{ // Base: httpClient.Transport, // Source: oauth2.ReuseTokenSource(nil, ts), diff --git a/next/controllers/projects.go b/next/controllers/projects.go index 6546d0c76..e964fadfe 100644 --- a/next/controllers/projects.go +++ b/next/controllers/projects.go @@ -15,7 +15,7 @@ import ( "github.com/diggerhq/digger/next/dbmodels" "github.com/diggerhq/digger/next/services" - //"github.com/diggerhq/digger/next/middleware" + // "github.com/diggerhq/digger/next/middleware" "github.com/diggerhq/digger/next/model" "github.com/diggerhq/digger/next/utils" "github.com/gin-gonic/gin" @@ -35,7 +35,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { // orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY) - //if !exists { + // if !exists { // c.String(http.StatusForbidden, "Not allowed to access this resource") // return //} @@ -165,7 +165,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { return } - //err = AutomergePRforBatchIfEnabled(d.GithubClientProvider, batch) + // err = AutomergePRforBatchIfEnabled(d.GithubClientProvider, batch) //if err != nil { // log.Printf("Error merging PR with automerge option: %v", err) // c.JSON(http.StatusInternalServerError, gin.H{"error": "Error merging PR with automerge option"}) diff --git a/next/dbmodels/storage.go b/next/dbmodels/storage.go index eeefd9cbc..912a2363d 100644 --- a/next/dbmodels/storage.go +++ b/next/dbmodels/storage.go @@ -67,7 +67,7 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]mode return repos, true } -//func (db *Database) GetPoliciesFromContext(c *gin.Context, orgIdKey string) ([]Policy, bool) { +// func (db *Database) GetPoliciesFromContext(c *gin.Context, orgIdKey string) ([]Policy, bool) { // loggedInOrganisationId, exists := c.Get(orgIdKey) // // log.Printf("getPoliciesFromContext, org id: %v\n", loggedInOrganisationId) @@ -94,7 +94,7 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]mode // return policies, true //} -//func (db *Database) GetProjectRunsForOrg(orgId int) ([]ProjectRun, error) { +// func (db *Database) GetProjectRunsForOrg(orgId int) ([]ProjectRun, error) { // var runs []ProjectRun // // err := db.GormDB.Preload("Project").Preload("Project.Organisation").Preload("Project.Repo"). @@ -112,7 +112,7 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]mode // return runs, nil //} -//func (db *Database) GetProjectRunsFromContext(c *gin.Context, orgIdKey string) ([]ProjectRun, bool) { +// func (db *Database) GetProjectRunsFromContext(c *gin.Context, orgIdKey string) ([]ProjectRun, bool) { // loggedInOrganisationId, exists := c.Get(orgIdKey) // // log.Printf("getProjectRunsFromContext, org id: %v\n", loggedInOrganisationId) @@ -130,7 +130,7 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]mode // //} -//func (db *Database) GetProjectByRunId(c *gin.Context, runId uint, orgIdKey string) (*ProjectRun, bool) { +// func (db *Database) GetProjectByRunId(c *gin.Context, runId uint, orgIdKey string) (*ProjectRun, bool) { // loggedInOrganisationId, exists := c.Get(orgIdKey) // if !exists { // c.String(http.StatusForbidden, "Not allowed to access this resource") @@ -243,7 +243,7 @@ func (db *Database) GetProjectVariables(projectId string) ([]model.EnvVar, error return variables, nil } -//func (db *Database) GetPolicyByPolicyId(c *gin.Context, policyId uint, orgIdKey string) (*Policy, bool) { +// func (db *Database) GetPolicyByPolicyId(c *gin.Context, policyId uint, orgIdKey string) (*Policy, bool) { // loggedInOrganisationId, exists := c.Get(orgIdKey) // if !exists { // c.String(http.StatusForbidden, "Not allowed to access this resource") @@ -563,7 +563,7 @@ func (db *Database) MakeGithubAppInstallationLinkInactive(link *model.GithubAppI return link, nil } -//func (db *Database) CreateDiggerJobLink(diggerJobId string, repoFullName string) (*GithubDiggerJobLink, error) { +// func (db *Database) CreateDiggerJobLink(diggerJobId string, repoFullName string) (*GithubDiggerJobLink, error) { // link := GithubDiggerJobLink{Status: DiggerJobLinkCreated, DiggerJobId: diggerJobId, RepoFullName: repoFullName} // result := db.GormDB.Save(&link) // if result.Error != nil { @@ -574,7 +574,7 @@ func (db *Database) MakeGithubAppInstallationLinkInactive(link *model.GithubAppI // return &link, nil //} -//func (db *Database) GetDiggerJobLink(diggerJobId string) (*GithubDiggerJobLink, error) { +// func (db *Database) GetDiggerJobLink(diggerJobId string) (*GithubDiggerJobLink, error) { // link := GithubDiggerJobLink{} // result := db.GormDB.Where("digger_job_id = ?", diggerJobId).Find(&link) // if result.Error != nil { @@ -587,7 +587,7 @@ func (db *Database) MakeGithubAppInstallationLinkInactive(link *model.GithubAppI // return &link, nil //} -//func (db *Database) UpdateDiggerJobLink(diggerJobId string, repoFullName string, githubJobId int64) (*GithubDiggerJobLink, error) { +// func (db *Database) UpdateDiggerJobLink(diggerJobId string, repoFullName string, githubJobId int64) (*GithubDiggerJobLink, error) { // jobLink := GithubDiggerJobLink{} // // check if there is already a link to another org, and throw an error in this case // result := db.GormDB.Where("digger_job_id = ? AND repo_full_name=? ", diggerJobId, repoFullName).Find(&jobLink) @@ -640,7 +640,7 @@ func (db *Database) GetDiggerBatch(batchId string) (*model.DiggerBatch, error) { return batch, nil } -func (db *Database) CreateDiggerBatch(orgid string, vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, PRNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, batchEventType BatchEventType) (*model.DiggerBatch, error) { +func (db *Database) CreateDiggerBatch(orgid string, vcsType DiggerVCSType, githubInstallationId int64, repoOwner, repoName, repoFullname string, prNumber int, diggerConfig, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, batchEventType BatchEventType) (*model.DiggerBatch, error) { uid := uuid.New() batch := &model.DiggerBatch{ ID: uid.String(), @@ -650,7 +650,7 @@ func (db *Database) CreateDiggerBatch(orgid string, vcsType DiggerVCSType, githu RepoOwner: repoOwner, RepoName: repoName, RepoFullName: repoFullname, - PrNumber: int64(PRNumber), + PrNumber: int64(prNumber), CommentID: *commentId, Status: int16(scheduler.BatchJobCreated), BranchName: branchName, @@ -746,10 +746,10 @@ func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([ return runs, nil } -func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId, DiggerConfig string, GithubInstallationId, RepoId int64, projectId, ProjectName string, RunType RunType, planStageId, applyStageId string, triggeredByUserId *string) (*model.DiggerRun, error) { +func (db *Database) CreateDiggerRun(triggertype string, PrNumber int, Status DiggerRunStatus, CommitId, DiggerConfig string, GithubInstallationId, RepoId int64, projectId, ProjectName string, RunType RunType, planStageId, applyStageId string, triggeredByUserId *string) (*model.DiggerRun, error) { dr := &model.DiggerRun{ ID: uuid.NewString(), - Triggertype: Triggertype, + Triggertype: triggertype, PrNumber: int64(PrNumber), Status: string(Status), CommitID: CommitId, @@ -1073,7 +1073,7 @@ func (db *Database) GetOrganisation(tenantId any) (*model.Organization, error) { return org, nil } -//func (db *Database) CreateOrganisation(name string, externalSource string, tenantId string) (*model.Organization, error) { +// func (db *Database) CreateOrganisation(name string, externalSource string, tenantId string) (*model.Organization, error) { // org := &model.Organization{Name: name, ExternalSource: externalSource, ExternalId: tenantId} // result := db.GormDB.Save(org) // if result.Error != nil { @@ -1142,7 +1142,7 @@ func (db *Database) CreateRepo(name, repoFullName, repoOrganisation, repoName, r return &repo, nil } -//func (db *Database) GetToken(tenantId any) (*Token, error) { +// func (db *Database) GetToken(tenantId any) (*Token, error) { // token := &Token{} // result := db.GormDB.Take(token, "value = ?", tenantId) // if result.Error != nil { From c5b4030b1728e6421528fdaa03356b6df755eea0 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:09:12 +0530 Subject: [PATCH 27/33] gocritic: lint next/dbgen --- next/dbgen/dbgen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next/dbgen/dbgen.go b/next/dbgen/dbgen.go index 97321d37a..22d3198a1 100644 --- a/next/dbgen/dbgen.go +++ b/next/dbgen/dbgen.go @@ -29,7 +29,7 @@ func main() { // Generate basic type-safe DAO API for struct `model.User` following conventions - //g.ApplyBasic( + // g.ApplyBasic( // // Generate struct `User` based on table `users` // g.GenerateModel("users"), // g.GenerateModel("organizations"), From 2c1bfc669d1b684a601976df6144775efc03fa7a Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:12:29 +0530 Subject: [PATCH 28/33] whitespace: add linter and lint whole code --- .golangci.yml | 1 + backend/controllers/connections.go | 1 - backend/controllers/github.go | 1 - backend/controllers/projects.go | 3 --- backend/models/storage_digger_jobs.go | 1 - cli/cmd/digger/root.go | 1 - cli/pkg/digger/digger.go | 5 ----- cli/pkg/github/github.go | 1 - cli/pkg/spec/manual.go | 1 - dgctl/cmd/exec.go | 1 - ee/backend/controllers/bitbucket.go | 1 - ee/backend/controllers/gitlab.go | 2 -- ee/cli/cmd/digger/root.go | 1 - ee/drift/controllers/ci_jobs.go | 1 - ee/drift/controllers/drift.go | 1 - libs/ci/gitlab/gitlab.go | 1 - libs/ci/gitlab/webhooks.go | 1 - libs/digger_config/converters.go | 1 - libs/execution/execution.go | 2 -- libs/spec/providers.go | 1 - next/controllers/drift.go | 1 - next/controllers/github.go | 1 - next/controllers/github_after_merge.go | 2 -- next/controllers/projects.go | 1 - next/services/drift.go | 1 - next/services/scheduler.go | 1 - next/utils/github.go | 1 - 27 files changed, 1 insertion(+), 35 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ba40b8844..426f312d0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,3 +27,4 @@ linters: - staticcheck - govet - gocritic + - whitespace diff --git a/backend/controllers/connections.go b/backend/controllers/connections.go index 04c40c522..5d2c92938 100644 --- a/backend/controllers/connections.go +++ b/backend/controllers/connections.go @@ -123,7 +123,6 @@ func CreateVCSConnectionApi(c *gin.Context) { slog.Error("Could not create VCS connection", "error", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create VCS connection"}) return - } slog.Info("Created VCS connection", "connectionId", connection.ID, "organisationId", org.ID) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index fbc28cc2f..5b036474b 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -1603,7 +1603,6 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu if config.CommentRenderMode == dg_configuration.CommentRenderModeGroupByModule && (*diggerCommand == orchestrator_scheduler.DiggerCommandPlan || *diggerCommand == orchestrator_scheduler.DiggerCommandApply) { - slog.Info("Using GroupByModule render mode for comments", "issueNumber", issueNumber) sourceDetails, err := comment_updater.PostInitialSourceComments(ghService, issueNumber, impactedProjectsSourceMapping) diff --git a/backend/controllers/projects.go b/backend/controllers/projects.go index 43f291472..e12e160a7 100644 --- a/backend/controllers/projects.go +++ b/backend/controllers/projects.go @@ -1526,7 +1526,6 @@ func AutomergePRforBatchIfEnabled(gh utils.GithubClientProvider, batch *models.D batch.BatchType == orchestrator_scheduler.DiggerCommandApply && batch.CoverAllImpactedProjects && automerge { - slog.Info("Conditions met for auto-merge, proceeding", "batchId", batch.ID, "prNumber", batch.PrNumber, @@ -1618,7 +1617,6 @@ func DeleteOlderPRCommentsIfEnabled(gh utils.GithubClientProvider, batch *models batch.BatchType == orchestrator_scheduler.DiggerCommandPlan && batch.CoverAllImpactedProjects && deleteOlderComments { - slog.Info("Conditions met for deleting prior comments, proceeding", "batchId", batch.ID, "prNumber", batch.PrNumber, @@ -1706,7 +1704,6 @@ func DeleteOlderPRCommentsIfEnabled(gh utils.GithubClientProvider, batch *models } return nil - } else { if batch.BatchType != orchestrator_scheduler.DiggerCommandPlan { slog.Debug("Skipping deletion of prior comments - not an plan command", diff --git a/backend/models/storage_digger_jobs.go b/backend/models/storage_digger_jobs.go index bfa7bcabe..b367a986e 100644 --- a/backend/models/storage_digger_jobs.go +++ b/backend/models/storage_digger_jobs.go @@ -56,7 +56,6 @@ func (db *Database) CreateCiJobFromSpec(spec spec.Spec, runName, projectName, ba if err != nil { log.Printf("failed to marshal backend spec: %v", err) return nil, err - } marshalledVcsSpec, err := json.Marshal(spec.VCS) diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index 143c06c33..2f5bb4add 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -71,7 +71,6 @@ func (r *RunConfig) GetServices() (*ci.PullRequestService, *ci.OrgService, *repo orgService = orchestrator_github.MockCiService{} default: return nil, nil, nil, fmt.Errorf("unknown reporter: %v", r.Reporter) - } return &prService, &orgService, &reporter, nil diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index ff7411ab0..626980364 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -166,7 +166,6 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic slog.Error("error updating aggregate status check", "error", err) return false, false, err } - } atLeastOneApply := len(appliesPerProject) > 0 @@ -266,7 +265,6 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org executor := diggerExecutor.Executor.(execution.DiggerExecutor) switch command { - case "digger plan": err := usage.SendUsageRecord(requestedBy, job.EventName, "plan") if err != nil { @@ -381,7 +379,6 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org return nil, comment, fmt.Errorf("%s", comment) } else { - // checking policies (plan, access) var planPolicyViolations []string @@ -572,7 +569,6 @@ func RunJob( slog.Info("Running commands for project", "commands", job.Commands, "project name", job.ProjectName) for _, command := range job.Commands { - allowedToPerformCommand, err := policyChecker.CheckAccessPolicy(orgService, nil, SCMOrganisation, SCMrepository, job.ProjectName, job.ProjectDir, command, nil, requestedBy, []string{}) if err != nil { return fmt.Errorf("error checking policy: %v", err) @@ -721,7 +717,6 @@ func RunJob( slog.Error("Error reporting Run.", "error", err) } } - } return nil } diff --git a/cli/pkg/github/github.go b/cli/pkg/github/github.go index 3d5b67272..699e5e679 100644 --- a/cli/pkg/github/github.go +++ b/cli/pkg/github/github.go @@ -248,7 +248,6 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh } } } else { - impactedProjects, requestedProject, prNumber, err := dg_github.ProcessGitHubEvent(ghEvent, diggerConfig, &githubPrService) if err != nil { if errors.Is(err, dg_github.UnhandledMergeGroupEventError) { diff --git a/cli/pkg/spec/manual.go b/cli/pkg/spec/manual.go index 070d7ea26..1e503f1c9 100644 --- a/cli/pkg/spec/manual.go +++ b/cli/pkg/spec/manual.go @@ -79,7 +79,6 @@ func RunSpecManualCommand( if err != nil { slog.Error("ExtractZip err", "error", err) usage.ReportErrorAndExit(spec.VCS.Actor, fmt.Sprintf("artefact zip extraction err: %v", err), 1) - } // remove the zipPath diff --git a/dgctl/cmd/exec.go b/dgctl/cmd/exec.go index 9540a0230..850a229ac 100644 --- a/dgctl/cmd/exec.go +++ b/dgctl/cmd/exec.go @@ -153,7 +153,6 @@ func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner, repoNa } } } - } return nil, nil, nil, fmt.Errorf("workflow not found") } diff --git a/ee/backend/controllers/bitbucket.go b/ee/backend/controllers/bitbucket.go index c303e372b..e64ad64ac 100644 --- a/ee/backend/controllers/bitbucket.go +++ b/ee/backend/controllers/bitbucket.go @@ -305,7 +305,6 @@ func handleIssueCommentEventBB(bitbucketProvider utils.BitbucketProvider, payloa if config.CommentRenderMode == dg_configuration.CommentRenderModeGroupByModule && (*diggerCommand == scheduler.DiggerCommandPlan || *diggerCommand == scheduler.DiggerCommandApply) { - sourceDetails, err := comment_updater.PostInitialSourceComments(bbService, issueNumber, impactedProjectsSourceMapping) if err != nil { log.Printf("PostInitialSourceComments error: %v", err) diff --git a/ee/backend/controllers/gitlab.go b/ee/backend/controllers/gitlab.go index e72383bdf..222ab9fdf 100644 --- a/ee/backend/controllers/gitlab.go +++ b/ee/backend/controllers/gitlab.go @@ -154,7 +154,6 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: Could not check if branch exists, error: %v", err)) log.Printf("Could not check if branch exists, error: %v", err) return fmt.Errorf("Could not check if branch exists: %v", err) - } if !branchExists { log.Printf("automatic branch deletion is configured, ignoring pr closed event") @@ -490,7 +489,6 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla if config.CommentRenderMode == dg_configuration.CommentRenderModeGroupByModule && (*diggerCommand == scheduler.DiggerCommandPlan || *diggerCommand == scheduler.DiggerCommandApply) { - sourceDetails, err := comment_updater.PostInitialSourceComments(glService, issueNumber, impactedProjectsSourceMapping) if err != nil { log.Printf("PostInitialSourceComments error: %v", err) diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index 095e509e1..f1864fa11 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -71,7 +71,6 @@ func (r *RunConfig) GetServices() (*ci.PullRequestService, *ci.OrgService, *repo orgService = orchestrator_github.MockCiService{} default: return nil, nil, nil, fmt.Errorf("unknown reporter: %v", r.Reporter) - } return &prService, &orgService, &reporter, nil diff --git a/ee/drift/controllers/ci_jobs.go b/ee/drift/controllers/ci_jobs.go index 9d6135f63..fbca29a20 100644 --- a/ee/drift/controllers/ci_jobs.go +++ b/ee/drift/controllers/ci_jobs.go @@ -81,7 +81,6 @@ func (mc MainController) SetJobStatusForProject(c *gin.Context) { log.Printf("Error retrieving project: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving project"}) return - } summary := job.DiggerJobSummary err = ProjectDriftStateMachineApply(*project, job.TerraformOutput, summary.ResourcesCreated, summary.ResourcesUpdated, summary.ResourcesDeleted) diff --git a/ee/drift/controllers/drift.go b/ee/drift/controllers/drift.go index 9079455d4..2f9d996b5 100644 --- a/ee/drift/controllers/drift.go +++ b/ee/drift/controllers/drift.go @@ -164,7 +164,6 @@ func (mc MainController) TriggerDriftRunForProject(c *gin.Context) { log.Printf("Error creating CI backend: %v %v", project.Name, err) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("error creating CI backend")}) return - } batch, err := models.DB.CreateDiggerBatch(models.DiggerVCSGithub, installationid, repoOwner, repoName, repoFullName, 0, "", branch, scheduler.DiggerCommandPlan, nil, 0, "", true, false, nil) diff --git a/libs/ci/gitlab/gitlab.go b/libs/ci/gitlab/gitlab.go index f4fff5fd8..e82577dda 100644 --- a/libs/ci/gitlab/gitlab.go +++ b/libs/ci/gitlab/gitlab.go @@ -484,7 +484,6 @@ func ConvertGitLabEventToCommands(event GitLabEvent, gitLabContext *GitLabContex var StateEnvProvider *stscreds.WebIdentityRoleProvider var CommandEnvProvider *stscreds.WebIdentityRoleProvider if project.AwsRoleToAssume != nil { - if project.AwsRoleToAssume.Command != "" { StateEnvProvider = scheduler.GetProviderFromRole(project.AwsRoleToAssume.State, project.AwsRoleToAssume.AwsRoleRegion) } else { diff --git a/libs/ci/gitlab/webhooks.go b/libs/ci/gitlab/webhooks.go index 4573f1fd8..4474f88ae 100644 --- a/libs/ci/gitlab/webhooks.go +++ b/libs/ci/gitlab/webhooks.go @@ -157,7 +157,6 @@ func ConvertGithubPullRequestEventToJobs(payload *gitlab.MergeEvent, impactedPro SkipMergeCheck: skipMerge, }) } - } return jobs, true, nil } diff --git a/libs/digger_config/converters.go b/libs/digger_config/converters.go index 23f262b94..86eff0bba 100644 --- a/libs/digger_config/converters.go +++ b/libs/digger_config/converters.go @@ -27,7 +27,6 @@ func copyProjects(projects []*ProjectYaml) []Project { var roleToAssume *AssumeRoleForProject = nil if p.AwsRoleToAssume != nil { - // set a default region to us-east-1 the same default AWS uses if p.AwsRoleToAssume.AwsRoleRegion == "" { p.AwsRoleToAssume.AwsRoleRegion = "us-east-1" diff --git a/libs/execution/execution.go b/libs/execution/execution.go index 06c7a522a..655f620ab 100644 --- a/libs/execution/execution.go +++ b/libs/execution/execution.go @@ -195,7 +195,6 @@ func (d DiggerExecutor) RetrievePlanJson() (string, error) { showArgs := make([]string, 0) terraformPlanOutput, _, _ := executor.TerraformExecutor.Show(showArgs, executor.CommandEnvVars, *storedPlanPath) return terraformPlanOutput, nil - } else { return "", fmt.Errorf("stored plan does not exist") } @@ -257,7 +256,6 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin } if d.PlanStorage != nil { - fileBytes, err := os.ReadFile(d.PlanPathProvider.LocalPlanFilePath()) if err != nil { fmt.Println("Error reading file:", err) diff --git a/libs/spec/providers.go b/libs/spec/providers.go index b760b2578..d86060972 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -118,7 +118,6 @@ func (l LockProvider) GetLock(lockSpec LockSpec) (locking.Lock, error) { case "azure": slog.Info("Using Azure lock provider") return azure.NewStorageAccountLock() - } } slog.Error("Could not determine lock provider", diff --git a/next/controllers/drift.go b/next/controllers/drift.go index 0f7f6c480..ad4f91a45 100644 --- a/next/controllers/drift.go +++ b/next/controllers/drift.go @@ -68,7 +68,6 @@ func (d DiggerController) TriggerDriftDetectionForProject(c *gin.Context) { log.Printf("Error creating github service: %v", err) c.JSON(http.StatusBadRequest, gin.H{"error": "error creating github service"}) return - } batchId, _, err := services.CreateJobAndBatchForProjectFromBranch(d.GithubClientProvider, projectId, "digger plan", dbmodels.DiggerBatchDriftEvent, scheduler.DiggerCommandPlan) diff --git a/next/controllers/github.go b/next/controllers/github.go index 593ed756b..10c6e8b8c 100644 --- a/next/controllers/github.go +++ b/next/controllers/github.go @@ -652,7 +652,6 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) { log.Printf("Error retrieving github client: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching organisation"}) return - } // we get repos accessible to this installation diff --git a/next/controllers/github_after_merge.go b/next/controllers/github_after_merge.go index bc1de7cac..1e95824ae 100644 --- a/next/controllers/github_after_merge.go +++ b/next/controllers/github_after_merge.go @@ -148,7 +148,6 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * if err != nil { log.Printf("Error creating jobspec: %v %v", projectName, err) return fmt.Errorf("error creating jobspec") - } applyJobToken, err := dbmodels.DB.CreateDiggerJobToken(orgId) @@ -210,7 +209,6 @@ func handlePushEventApplyAfterMerge(gh nextutils.GithubClientProvider, payload * } dbmodels.DB.CreateDiggerRunQueueItem(diggerRun.ID, project.ID) - } return nil diff --git a/next/controllers/projects.go b/next/controllers/projects.go index e964fadfe..171826596 100644 --- a/next/controllers/projects.go +++ b/next/controllers/projects.go @@ -176,7 +176,6 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) { if err != nil { log.Printf("Error getting batch details: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting batch details"}) - } c.JSON(http.StatusOK, res) diff --git a/next/services/drift.go b/next/services/drift.go index d2653d845..02ef4a29c 100644 --- a/next/services/drift.go +++ b/next/services/drift.go @@ -25,7 +25,6 @@ func SaveUpdatedDriftStatus(batch model.DiggerBatch, job model.DiggerJob, terraf if err != nil { log.Printf("error whilst retrieving job repo %v", err) return fmt.Errorf("error whilst retrieving job repo %v", err) - } project, err := dbmodels.DB.GetProjectByName(orgId, repo, jobSpec.ProjectName) diff --git a/next/services/scheduler.go b/next/services/scheduler.go index ac2fb79bb..08925d43c 100644 --- a/next/services/scheduler.go +++ b/next/services/scheduler.go @@ -150,7 +150,6 @@ func CreateJobAndBatchForProjectFromBranch(gh utils.GithubClientProvider, projec if err != nil { log.Printf("error retrieving digger.yml workflows: %v ", err) return nil, nil, fmt.Errorf("error retrieving digger.yml workflows: %v", err) - } config := &dg_configuration.DiggerConfig{ ApplyAfterMerge: true, diff --git a/next/utils/github.go b/next/utils/github.go index b0e0f545d..7274214d8 100644 --- a/next/utils/github.go +++ b/next/utils/github.go @@ -162,7 +162,6 @@ func SetPRStatusForJobs(prService ci.PullRequestService, prNumber int, jobs []sc log.Printf("error setting status: %v", err) return fmt.Errorf("error setting pr status: %v", err) } - } else { err := prService.SetStatus(prNumber, "success", "digger/plan") if err != nil { From f5cf5d898069fa703bbd71a8d864bedaa2fc0735 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:15:08 +0530 Subject: [PATCH 29/33] add linters: - mirror - unconvert - usestdlibvars - misspell --- .golangci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 426f312d0..273857361 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,3 +28,7 @@ linters: - govet - gocritic - whitespace + - mirror + - unconvert + - usestdlibvars + - misspell From 6c8e49d0a7259a1a2c7231bd4beaafdf65a9c848 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:22:57 +0530 Subject: [PATCH 30/33] 1. unconvert: remove redundant typeconversions like string(some_str_variable) 2. usestdlibvars: use http.Method instead of "GET" and similar changes --- backend/middleware/jwt.go | 2 +- backend/service_clients/projects_service.go | 4 +-- backend/services/auth.go | 4 +-- backend/services/spec.go | 4 +-- dgctl/cmd/exec.go | 2 +- ee/backend/controllers/web.go | 4 +-- libs/backendapi/diggerapi.go | 10 +++---- libs/ci/github/github.go | 3 ++- libs/ci/gitlab/gitlab.go | 3 ++- libs/comment_utils/summary/updater.go | 2 +- libs/crypto/decrypt_test.go | 3 ++- libs/license/license.go | 2 +- libs/policy/policy.go | 30 ++++++++++----------- libs/scheduler/aws.go | 2 +- libs/spec/variables_provider.go | 2 +- libs/storage/azure_plan_storage.go | 2 +- next/controllers/drift.go | 2 +- next/services/spec.go | 4 +-- 18 files changed, 44 insertions(+), 41 deletions(-) diff --git a/backend/middleware/jwt.go b/backend/middleware/jwt.go index dd6059ef0..a5b414713 100644 --- a/backend/middleware/jwt.go +++ b/backend/middleware/jwt.go @@ -312,7 +312,7 @@ func CORSMiddleware() gin.HandlerFunc { c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") - if c.Request.Method == "OPTIONS" { + if c.Request.Method == http.MethodOptions { c.AbortWithStatus(204) return } diff --git a/backend/service_clients/projects_service.go b/backend/service_clients/projects_service.go index f69f2f628..6a7a1d007 100644 --- a/backend/service_clients/projects_service.go +++ b/backend/service_clients/projects_service.go @@ -68,7 +68,7 @@ func TriggerProjectsRefreshService(cloneUrl, branch, githubToken, repoFullName, // Create HTTP request apiURL := fmt.Sprintf("https://api.machines.dev/v1/apps/%s/machines", os.Getenv("DIGGER_PROJECTS_SVC_APP_NAME")) - req2, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payload)) + req2, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewBuffer(payload)) if err != nil { slog.Error("Error creating request", "error", err) return nil, err @@ -87,7 +87,7 @@ func TriggerProjectsRefreshService(cloneUrl, branch, githubToken, repoFullName, } defer resp.Body.Close() - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { body, err2 := io.ReadAll(resp.Body) slog.Error("Error triggering projects refresh service", "statusCode", resp.StatusCode, "body", body, "readyErr", err2) return nil, fmt.Errorf("error triggering projects refresh service") diff --git a/backend/services/auth.go b/backend/services/auth.go index 63cc95261..d5c1d2ad7 100644 --- a/backend/services/auth.go +++ b/backend/services/auth.go @@ -53,7 +53,7 @@ func (a *Auth) getAuthToken() (string, error) { return "", err } - req, err := http.NewRequest("POST", authUrl, bytes.NewBuffer(jsonPayload)) + req, err := http.NewRequest(http.MethodPost, authUrl, bytes.NewBuffer(jsonPayload)) if err != nil { return "", err } @@ -81,7 +81,7 @@ func (a *Auth) getAuthToken() (string, error) { func (a *Auth) FetchTokenPermissions(tokenId string) ([]string, error) { accessTokenUrl := a.Host + "/identity/resources/vendor-only/tenants/access-tokens/v1/" + tokenId - req, err := http.NewRequest("GET", accessTokenUrl, nil) + req, err := http.NewRequest(http.MethodGet, accessTokenUrl, nil) if err != nil { return nil, fmt.Errorf("error while fetching token permissions: %v", err.Error()) } diff --git a/backend/services/spec.go b/backend/services/spec.go index 9d9cf17d9..cca4b87ec 100644 --- a/backend/services/spec.go +++ b/backend/services/spec.go @@ -91,7 +91,7 @@ func GetVCSTokenFromJob(job models.DiggerJob, gh utils.GithubClientProvider) (*s func GetRunNameFromJob(job models.DiggerJob) (*string, error) { var jobSpec scheduler.JobJson - err := json.Unmarshal([]byte(job.SerializedJobSpec), &jobSpec) + err := json.Unmarshal(job.SerializedJobSpec, &jobSpec) if err != nil { slog.Error("Could not unmarshal job spec", "jobId", job.DiggerJobID, "error", err) return nil, fmt.Errorf("could not marshal json string: %v", err) @@ -144,7 +144,7 @@ func getVariablesSpecFromEnvMap(envVars map[string]string) []spec.VariableSpec { func GetSpecFromJob(job models.DiggerJob) (*spec.Spec, error) { var jobSpec scheduler.JobJson - err := json.Unmarshal([]byte(job.SerializedJobSpec), &jobSpec) + err := json.Unmarshal(job.SerializedJobSpec, &jobSpec) if err != nil { slog.Error("Could not unmarshal job spec", "jobId", job.DiggerJobID, "error", err) return nil, fmt.Errorf("could not marshal json string: %v", err) diff --git a/dgctl/cmd/exec.go b/dgctl/cmd/exec.go index 850a229ac..1470ab0e6 100644 --- a/dgctl/cmd/exec.go +++ b/dgctl/cmd/exec.go @@ -104,7 +104,7 @@ func GetSpec(diggerUrl, authToken, command, actor, projectMarshalled, diggerConf log.Fatalf("Not able to marshal request: %v", err) } - req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) + req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("error while creating request: %v", err) } diff --git a/ee/backend/controllers/web.go b/ee/backend/controllers/web.go index 7c5d09dfc..d8474d54a 100644 --- a/ee/backend/controllers/web.go +++ b/ee/backend/controllers/web.go @@ -98,7 +98,7 @@ func (web *WebController) AddPolicyPage(c *gin.Context) { orgId := c.GetHeader(middleware.ORGANISATION_ID_KEY) switch c.Request.Method { - case "GET": + case http.MethodGet: message := "" projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY) @@ -116,7 +116,7 @@ func (web *WebController) AddPolicyPage(c *gin.Context) { c.HTML(http.StatusOK, "policy_add.tmpl", gin.H{ "Message": message, "Projects": projects, "PolicyTypes": policyTypes, }) - case "POST": + case http.MethodPost: policyText := c.PostForm("policytext") if policyText == "" { message := "Policy can't be empty" diff --git a/libs/backendapi/diggerapi.go b/libs/backendapi/diggerapi.go index 62415f0c3..c4ceecffb 100644 --- a/libs/backendapi/diggerapi.go +++ b/libs/backendapi/diggerapi.go @@ -67,7 +67,7 @@ func (d DiggerApi) ReportProject(namespace, projectName, configurationYaml strin return fmt.Errorf("not able to marshal request: %v", err) } - req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) + req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(jsonData)) if err != nil { return fmt.Errorf("error while creating request: %v", err) } @@ -110,7 +110,7 @@ func (d DiggerApi) ReportProjectRun(namespace, projectName string, startedAt, en return fmt.Errorf("not able to marshal request: %v", err) } - req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) + req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(jsonData)) if err != nil { return fmt.Errorf("error while creating request: %v", err) } @@ -172,7 +172,7 @@ func (d DiggerApi) ReportProjectJobStatus(repo, projectName, jobId, status strin return nil, fmt.Errorf("not able to marshal request: %v", err) } - req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonData)) + req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("error while creating request: %v", err) } @@ -241,7 +241,7 @@ func (d DiggerApi) UploadJobArtefact(zipLocation string) (*int, *string, error) multipartWriter.Close() // Create a new HTTP request - req, err := http.NewRequest("PUT", uploadUrl, &requestBody) + req, err := http.NewRequest(http.MethodPut, uploadUrl, &requestBody) if err != nil { slog.Error("error creating request", "error", err) return nil, nil, fmt.Errorf("error creating request: %v", err) @@ -293,7 +293,7 @@ func (d DiggerApi) DownloadJobArtefact(downloadTo string) (*string, error) { } // Create a new HTTP request - req, err := http.NewRequest("GET", downloadUrl, nil) + req, err := http.NewRequest(http.MethodGet, downloadUrl, nil) if err != nil { slog.Error("error creating request", "error", err) return nil, fmt.Errorf("error creating request: %v", err) diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index b00c17ca2..0238c28c9 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log/slog" + "net/http" "os" "strconv" "strings" @@ -438,7 +439,7 @@ func (svc GithubService) GetHeadCommitFromBranch(branch string) (string, string, func (svc GithubService) CheckBranchExists(branchName string) (bool, error) { _, resp, err := svc.Client.Repositories.GetBranch(context.Background(), svc.Owner, svc.RepoName, branchName, 3) if err != nil { - if resp != nil && resp.StatusCode == 404 { + if resp != nil && resp.StatusCode == http.StatusNotFound { return false, nil } return false, err diff --git a/libs/ci/gitlab/gitlab.go b/libs/ci/gitlab/gitlab.go index e82577dda..40551d35b 100644 --- a/libs/ci/gitlab/gitlab.go +++ b/libs/ci/gitlab/gitlab.go @@ -3,6 +3,7 @@ package gitlab import ( "fmt" "log/slog" + "net/http" "strconv" "strings" @@ -367,7 +368,7 @@ func (gitlabService GitLabService) CheckBranchExists(branchName string) (bool, e _, resp, err := gitlabService.Client.Branches.GetBranch(projectId, branchName) if err != nil { - if resp != nil && resp.StatusCode == 404 { + if resp != nil && resp.StatusCode == http.StatusNotFound { return false, nil } slog.Error("error checking if branch exists", "error", err, "branchName", branchName) diff --git a/libs/comment_utils/summary/updater.go b/libs/comment_utils/summary/updater.go index 0d7aa98f0..b4cb3f9ee 100644 --- a/libs/comment_utils/summary/updater.go +++ b/libs/comment_utils/summary/updater.go @@ -25,7 +25,7 @@ func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNum firstJobSpec := jobSpecs[0] jobType := firstJobSpec.JobType - jobTypeTitle := cases.Title(language.AmericanEnglish).String(string(jobType)) + jobTypeTitle := cases.Title(language.AmericanEnglish).String(jobType) slog.Info("updating comment with job results", "prNumber", prNumber, diff --git a/libs/crypto/decrypt_test.go b/libs/crypto/decrypt_test.go index d583d7949..0e7641c0f 100644 --- a/libs/crypto/decrypt_test.go +++ b/libs/crypto/decrypt_test.go @@ -7,6 +7,7 @@ import ( "crypto/x509" "encoding/base64" "encoding/pem" + "strings" "testing" ) @@ -125,7 +126,7 @@ func TestDecryptWithPrivateKeyInvalidInput(t *testing.T) { _, err := DecryptValueUsingPrivateKey(tc.encryptedData, tc.privateKeyPEM) if err == nil { t.Errorf("Expected an error, but got none") - } else if !bytes.Contains([]byte(err.Error()), []byte(tc.expectedErrMsg)) { + } else if !strings.Contains(err.Error(), tc.expectedErrMsg) { t.Errorf("Expected error message containing '%s', but got: %v", tc.expectedErrMsg, err) } }) diff --git a/libs/license/license.go b/libs/license/license.go index 2dc70c4d9..ad23b1683 100644 --- a/libs/license/license.go +++ b/libs/license/license.go @@ -30,7 +30,7 @@ func (l LicenseKeyChecker) Check() error { } // Create a new POST request - req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData)) if err != nil { slog.Error("Error creating request for license validation", "error", err) return fmt.Errorf("error creating request for license validation: %v", err) diff --git a/libs/policy/policy.go b/libs/policy/policy.go index 479fc93a7..7f4a54536 100644 --- a/libs/policy/policy.go +++ b/libs/policy/policy.go @@ -52,7 +52,7 @@ func getAccessPolicyForOrganisation(p *DiggerHttpPolicyProvider) (string, *http. slog.Debug("Fetching org access policy", "organisation", organisation, "url", u.String()) - req, err := http.NewRequest("GET", u.String(), nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return "", nil, err } @@ -82,7 +82,7 @@ func getPlanPolicyForOrganisation(p *DiggerHttpPolicyProvider) (string, *http.Re slog.Debug("Fetching org plan policy", "organisation", organisation, "url", u.String()) - req, err := http.NewRequest("GET", u.String(), nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return "", nil, err } @@ -112,7 +112,7 @@ func getDriftPolicyForOrganisation(p *DiggerHttpPolicyProvider) (string, *http.R slog.Debug("Fetching org drift policy", "organisation", organisation, "url", u.String()) - req, err := http.NewRequest("GET", u.String(), nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return "", nil, err } @@ -145,7 +145,7 @@ func getAccessPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace, project "projectName", projectName, "url", u.String()) - req, err := http.NewRequest("GET", u.String(), nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return "", nil, err } @@ -177,7 +177,7 @@ func getPlanPolicyForNamespace(p *DiggerHttpPolicyProvider, namespace, projectNa "projectName", projectName, "url", u.String()) - req, err := http.NewRequest("GET", u.String(), nil) + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return "", nil, err } @@ -215,13 +215,13 @@ func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation, repo, projectNam } // project policy found - if resp.StatusCode == 200 && content != "" { + if resp.StatusCode == http.StatusOK && content != "" { slog.Debug("Found project access policy", "namespace", namespace, "projectName", projectName) return content, nil } // check if project policy was empty or not found (retrieve org policy if so) - if (resp.StatusCode == 200 && content == "") || resp.StatusCode == 404 { + if (resp.StatusCode == http.StatusOK && content == "") || resp.StatusCode == http.StatusNotFound { slog.Debug("Project access policy not found, falling back to org policy", "organisation", organisation) @@ -233,10 +233,10 @@ func (p DiggerHttpPolicyProvider) GetAccessPolicy(organisation, repo, projectNam return "", fmt.Errorf("error while fetching access policy for organisation: %v", err) } switch resp.StatusCode { - case 200: + case http.StatusOK: slog.Debug("Found organisation access policy", "organisation", organisation) return content, nil - case 404: + case http.StatusNotFound: slog.Debug("Organisation access policy not found, using default", "organisation", organisation) return DefaultAccessPolicy, nil default: @@ -271,13 +271,13 @@ func (p DiggerHttpPolicyProvider) GetPlanPolicy(organisation, repo, projectName, } // project policy found - if resp.StatusCode == 200 && content != "" { + if resp.StatusCode == http.StatusOK && content != "" { slog.Debug("Found project plan policy", "namespace", namespace, "projectName", projectName) return content, nil } // check if project policy was empty or not found (retrieve org policy if so) - if (resp.StatusCode == 200 && content == "") || resp.StatusCode == 404 { + if (resp.StatusCode == http.StatusOK && content == "") || resp.StatusCode == http.StatusNotFound { slog.Debug("Project plan policy not found, falling back to org policy", "organisation", organisation) @@ -289,10 +289,10 @@ func (p DiggerHttpPolicyProvider) GetPlanPolicy(organisation, repo, projectName, return "", err } switch resp.StatusCode { - case 200: + case http.StatusOK: slog.Debug("Found organisation plan policy", "organisation", organisation) return content, nil - case 404: + case http.StatusNotFound: slog.Debug("Organisation plan policy not found", "organisation", organisation) return "", nil default: @@ -320,10 +320,10 @@ func (p DiggerHttpPolicyProvider) GetDriftPolicy() (string, error) { return "", err } switch resp.StatusCode { - case 200: + case http.StatusOK: slog.Debug("Found drift policy", "organisation", p.DiggerOrganisation) return content, nil - case 404: + case http.StatusNotFound: slog.Debug("Drift policy not found", "organisation", p.DiggerOrganisation) return "", nil default: diff --git a/libs/scheduler/aws.go b/libs/scheduler/aws.go index bf068ab0e..3f71ba1e9 100644 --- a/libs/scheduler/aws.go +++ b/libs/scheduler/aws.go @@ -324,7 +324,7 @@ func (fetcher *GithubAwsTokenFetcher) GetIdentityToken() ([]byte, error) { slog.Debug("len(ACTIONS_ID_TOKEN_REQUEST_TOKEN)", "length of ACTIONS_ID_TOKEN_REQUEST_TOKEN", len(bearerToken)) slog.Debug("audience (escaped", "audience", audience) - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { slog.Error("Failed to create request for GitHub identity token", "error", err) return nil, err diff --git a/libs/spec/variables_provider.go b/libs/spec/variables_provider.go index dce44d8b3..ab25198de 100644 --- a/libs/spec/variables_provider.go +++ b/libs/spec/variables_provider.go @@ -27,7 +27,7 @@ func (p VariablesProvider) GetVariables(variables []VariableSpec) (map[string]st if err != nil { return nil, fmt.Errorf("could not decrypt value using private key: %v", err) } - res[v.Name] = string(value) + res[v.Name] = value } else if v.IsInterpolated { // if it is an interpolated value we get it form env variable of the variable res[v.Name] = os.Getenv(v.Value) diff --git a/libs/storage/azure_plan_storage.go b/libs/storage/azure_plan_storage.go index 309bd6a55..eb84c6a82 100644 --- a/libs/storage/azure_plan_storage.go +++ b/libs/storage/azure_plan_storage.go @@ -29,7 +29,7 @@ func (psa *PlanStorageAzure) PlanExists(artifactName, storedPlanFilePath string) // Get the blob properties resp, err := blobClient.GetProperties(psa.Context, nil) if err != nil { - if azErr, ok := err.(*azcore.ResponseError); ok && string(azErr.ErrorCode) == string(bloberror.BlobNotFound) { + if azErr, ok := err.(*azcore.ResponseError); ok && azErr.ErrorCode == string(bloberror.BlobNotFound) { slog.Debug("Blob not found", "container", psa.ContainerName, "path", storedPlanFilePath, diff --git a/next/controllers/drift.go b/next/controllers/drift.go index ad4f91a45..c8bd06d9a 100644 --- a/next/controllers/drift.go +++ b/next/controllers/drift.go @@ -134,7 +134,7 @@ func (d DiggerController) TriggerCronForMatchingProjects(c *gin.Context) { } // Create a new request - req, err := http.NewRequest("POST", driftUrl, bytes.NewBuffer(jsonPayload)) + req, err := http.NewRequest(http.MethodPost, driftUrl, bytes.NewBuffer(jsonPayload)) if err != nil { fmt.Println("Process Drift: Error creating request:", err) return diff --git a/next/services/spec.go b/next/services/spec.go index 29808557b..5c1776912 100644 --- a/next/services/spec.go +++ b/next/services/spec.go @@ -49,7 +49,7 @@ func GetVCSTokenFromJob(job model.DiggerJob, gh utils.GithubClientProvider) (*st func RefreshVariableSpecForJob(job *model.DiggerJob) error { var jobSpec scheduler.JobJson - err := json.Unmarshal([]byte(job.JobSpec), &jobSpec) + err := json.Unmarshal(job.JobSpec, &jobSpec) if err != nil { log.Printf("could not unmarshal job string: %v", err) return fmt.Errorf("could not marshal json string: %v", err) @@ -126,7 +126,7 @@ func RefreshVariableSpecForJob(job *model.DiggerJob) error { func GetRunNameFromJob(job model.DiggerJob) (*string, error) { var jobSpec scheduler.JobJson - err := json.Unmarshal([]byte(job.JobSpec), &jobSpec) + err := json.Unmarshal(job.JobSpec, &jobSpec) if err != nil { log.Printf("could not unmarshal job string: %v", err) return nil, fmt.Errorf("could not marshal json string: %v", err) From 904f06968bcee3461f047ec4d1d35284f9238da5 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:28:15 +0530 Subject: [PATCH 31/33] misspell: add linter and locale = UK english --- .golangci.yml | 4 ++++ backend/utils/ai.go | 4 ++-- libs/ci/github/github.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 273857361..cce015a2b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -32,3 +32,7 @@ linters: - unconvert - usestdlibvars - misspell + +settings: + misspell: + locale: UK diff --git a/backend/utils/ai.go b/backend/utils/ai.go index 48442066b..b082a8d69 100644 --- a/backend/utils/ai.go +++ b/backend/utils/ai.go @@ -67,7 +67,7 @@ func GenerateTerraformCode(appCode, generationEndpoint, apiToken string) (string "statusCode", resp.StatusCode, "response", string(body), ) - return "", fmt.Errorf("unexpected error occured while generating code") + return "", fmt.Errorf("unexpected error occurred while generating code") } type GeneratorResponse struct { @@ -147,7 +147,7 @@ func GetAiSummaryFromTerraformPlans(plans, summaryEndpoint, apiToken string) (st "statusCode", resp.StatusCode, "response", string(body), ) - return "", fmt.Errorf("unexpected error occured while generating code") + return "", fmt.Errorf("unexpected error occurred while generating code") } type GeneratorResponse struct { diff --git a/libs/ci/github/github.go b/libs/ci/github/github.go index 0238c28c9..30f3d7eaa 100644 --- a/libs/ci/github/github.go +++ b/libs/ci/github/github.go @@ -426,7 +426,7 @@ func (svc GithubService) GetHeadCommitFromBranch(branch string) (string, string, branchInfo, _, err := svc.Client.Repositories.GetBranch(context.Background(), svc.Owner, svc.RepoName, branch, 0) if err != nil { slog.Error("error fetching branch", "error", err, "branch", branch) - return "", "", fmt.Errorf("could not retrive branch details: %v", err) + return "", "", fmt.Errorf("could not retrieve branch details: %v", err) } headCommit := branchInfo.GetCommit() From 76a2b2b17345cbbf8a3ab4497b2c568932d386d2 Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:30:32 +0530 Subject: [PATCH 32/33] tagalign: add linter --- .golangci.yml | 1 + backend/controllers/connections.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index cce015a2b..11506847c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -32,6 +32,7 @@ linters: - unconvert - usestdlibvars - misspell + - tagalign settings: misspell: diff --git a/backend/controllers/connections.go b/backend/controllers/connections.go index 5d2c92938..cf6cb01d7 100644 --- a/backend/controllers/connections.go +++ b/backend/controllers/connections.go @@ -61,7 +61,7 @@ func CreateVCSConnectionApi(c *gin.Context) { } type CreateVCSConnectionRequest struct { - VCS string `json:"type" binding:"required"` + VCS string `binding:"required" json:"type"` Name string `json:"connection_name"` BitbucketAccessToken string `json:"bitbucket_access_token"` BitbucketWebhookSecret string `json:"bitbucket_webhook_secret"` From 6dc94a7592c6b77cd0383debf779b400bb65f7dd Mon Sep 17 00:00:00 2001 From: Saurabh2402 Date: Thu, 24 Jul 2025 01:34:03 +0530 Subject: [PATCH 33/33] thelper: add linter and lint _test.go files --- .golangci.yml | 1 + backend/controllers/github_test.go | 2 ++ backend/models/scheduler_test.go | 1 + cli/pkg/integration/integration_test.go | 1 + libs/digger_config/terragrunt/atlantis/generate_test.go | 1 + libs/locking/gcp/gcp_lock_test.go | 1 + 6 files changed, 7 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 11506847c..13ad387e4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -33,6 +33,7 @@ linters: - usestdlibvars - misspell - tagalign + - thelper settings: misspell: diff --git a/backend/controllers/github_test.go b/backend/controllers/github_test.go index 315c6503c..527055410 100644 --- a/backend/controllers/github_test.go +++ b/backend/controllers/github_test.go @@ -568,6 +568,7 @@ var installationCreatedEvent = ` }` func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) { + tb.Helper() // database file name dbName := "database_test.db" @@ -660,6 +661,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) { models.DB = database // Return a function to teardown the test return func(tb testing.TB) { + tb.Helper() err = os.Remove(dbName) if err != nil { panic(err) diff --git a/backend/models/scheduler_test.go b/backend/models/scheduler_test.go index 9c79de535..14b509cdf 100644 --- a/backend/models/scheduler_test.go +++ b/backend/models/scheduler_test.go @@ -12,6 +12,7 @@ import ( ) func setupSuiteScheduler(tb testing.TB) (func(tb testing.TB), *Database) { + tb.Helper() // database file name dbName := "database_test.db" diff --git a/cli/pkg/integration/integration_test.go b/cli/pkg/integration/integration_test.go index 1a529673a..594f0afad 100644 --- a/cli/pkg/integration/integration_test.go +++ b/cli/pkg/integration/integration_test.go @@ -29,6 +29,7 @@ import ( ) func SkipCI(t *testing.T) { + t.Helper() if os.Getenv("CI") != "" { t.Skip("Skipping testing in CI environment") } diff --git a/libs/digger_config/terragrunt/atlantis/generate_test.go b/libs/digger_config/terragrunt/atlantis/generate_test.go index 18f567be5..9f168ef7f 100644 --- a/libs/digger_config/terragrunt/atlantis/generate_test.go +++ b/libs/digger_config/terragrunt/atlantis/generate_test.go @@ -27,6 +27,7 @@ func resetForRun() error { } func runTest(t *testing.T, goldenFile, testPath string, createProjectName bool, workflowName string, withWorkspace, parallel, ignoreParentTerragrunt, ignoreDependencyBlocks, cascadeDependencies bool) { + t.Helper() resetForRun() atlantisConfig, _, err := Parse( testPath, diff --git a/libs/locking/gcp/gcp_lock_test.go b/libs/locking/gcp/gcp_lock_test.go index e4d37c891..ccffe2aaa 100644 --- a/libs/locking/gcp/gcp_lock_test.go +++ b/libs/locking/gcp/gcp_lock_test.go @@ -24,6 +24,7 @@ func randomString(length int) string { } func SkipCI(t *testing.T) { + t.Helper() if os.Getenv("CI") != "" { t.Skip("Skipping testing in CI environment") }