Skip to content

Improvement/formatting setup #2041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions backend/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"time"

"github.com/diggerhq/digger/backend/config"
"github.com/diggerhq/digger/backend/segment"
"github.com/diggerhq/digger/backend/utils"
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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion backend/ci_backends/ci_backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion backend/ci_backends/github_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion backend/config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

import (
"github.com/spf13/cast"
"os"
"strings"
"time"

"github.com/spf13/cast"

"github.com/spf13/viper"
)

Expand Down
3 changes: 2 additions & 1 deletion backend/controllers/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions backend/controllers/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions backend/controllers/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 12 additions & 13 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/diggerhq/digger/libs/git_utils"
"log/slog"
"math/rand"
"net/http"
Expand All @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion backend/controllers/github_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
3 changes: 2 additions & 1 deletion backend/controllers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package controllers

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/gin-gonic/gin"
)

func Home(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions backend/controllers/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions backend/controllers/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion backend/controllers/policies_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading