From b155e54255984913b97daed86901059893b52bda Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Sun, 6 Oct 2024 17:57:00 +0200 Subject: [PATCH 1/2] ci: add golangci lint --- .github/workflows/ci.yml | 9 ++++++++- .golangci.yml | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc83c56..5387e37 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,9 +16,13 @@ concurrency: cancel-in-progress: true jobs: - build: + test: runs-on: ubuntu-latest + permissions: + contents: read + checks: write + steps: - uses: actions/checkout@v4 @@ -27,6 +31,9 @@ jobs: with: go-version: ${{ env.GO_VERSION }} + - name: Lint + uses: golangci/golangci-lint-action@v6 + - name: Build run: go build -v ./... diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..dfbe904 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,18 @@ +linters: + enable: + - errcheck + - goconst + - gocritic + - gofmt + - goimports + - gosec + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + +issues: + exclude-files: + - ".+_test.go" From b47fa6ac8e11c0570491fea9ffe768c139bddf72 Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Sun, 6 Oct 2024 18:07:41 +0200 Subject: [PATCH 2/2] fix: fix or ignore lint errors --- api/files.go | 12 ++++++++++-- api/middleware.go | 14 ++++++++------ cmd/serve/root.go | 10 ++++++++-- gitlab/commit.go | 1 - gitlab/gitlab.go | 5 ----- internal/config/config.go | 3 ++- internal/logging/logger.go | 1 + 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/api/files.go b/api/files.go index f23e1e9..6a61cd0 100644 --- a/api/files.go +++ b/api/files.go @@ -97,7 +97,11 @@ func (h *Handler) CreateFile(ctx *gin.Context) { Path: uri.Path, Content: &form.Content, } - file.SetEncoding(form.Encoding) + if _, err := file.SetEncoding(form.Encoding); err != nil { + RespondError(ctx, http.StatusBadRequest, HTTPRequestValidationFailed, err) + return + } + _, commit, err := hostingService.CreateFile(ctx, repo, &file, &hosting.CreateFileOpts{ SHA: queryForm.SHA, Branch: &queryForm.Branch, @@ -147,7 +151,11 @@ func (h *Handler) UpdateFile(ctx *gin.Context) { Path: uri.Path, Content: &form.Content, } - file.SetEncoding(form.Encoding) + if _, err := file.SetEncoding(form.Encoding); err != nil { + RespondError(ctx, http.StatusBadRequest, HTTPRequestValidationFailed, err) + return + } + _, commit, err := hostingService.UpdateFile(ctx, repo, &file, &hosting.UpdateFileOpts{ SHA: queryForm.SHA, Branch: &queryForm.Branch, diff --git a/api/middleware.go b/api/middleware.go index dd197bd..945c425 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -47,32 +47,34 @@ func HostingMiddleware(githubService *github.GithubService) gin.HandlerFunc { hostingParam := ctx.Param("hosting") ownerParam := ctx.Param("owner") var service hosting.GitHostingService - if hostingParam == GithubHost { - if token != nil { + switch { + case hostingParam == GithubHost: + switch { + case token != nil: service, err = githubService.WithAuthToken(ctx, *token) if err != nil { RespondError(ctx, http.StatusUnauthorized, "invalid github token", err) ctx.Abort() return } - } else if ownerParam != "" && githubService.IsKnownInstallation(ownerParam) { + case ownerParam != "" && githubService.IsKnownInstallation(ownerParam): service, err = githubService.WithInstallationOwner(ownerParam) if err != nil { RespondError(ctx, http.StatusUnauthorized, "invalid github installation", err) ctx.Abort() return } - } else { + default: service = githubService } - } else if hostingParam == GitlabHost { + case hostingParam == GitlabHost: service, err = gitlab.NewGitlabService(*token) if err != nil { RespondError(ctx, http.StatusUnauthorized, "invalid gitlab token", err) ctx.Abort() return } - } else { + default: RespondError(ctx, http.StatusBadRequest, "unknown git provider") ctx.Abort() return diff --git a/cmd/serve/root.go b/cmd/serve/root.go index b4f1d48..60358c8 100644 --- a/cmd/serve/root.go +++ b/cmd/serve/root.go @@ -59,6 +59,7 @@ func NewServeCmd() (serveCmd *cobra.Command) { Level: zapcore.Level(cfg.LoggingConfig.Level), Development: cfg.LoggingConfig.Development, }) + // nolint:errcheck defer logging.DefaultLogger().Sync() tp := otel.InitTracerProvider() @@ -99,6 +100,7 @@ func newHTTPServer(lc fx.Lifecycle, tp *oteltrace.TracerProvider, mp *otelmetric r := gin.New() logger := otelzap.New(zap.NewExample()) + // nolint:errcheck defer logger.Sync() undo := otelzap.ReplaceGlobals(logger) @@ -124,8 +126,9 @@ func newHTTPServer(lc fx.Lifecycle, tp *oteltrace.TracerProvider, mp *otelmetric r.Use(gin.Recovery()) srv := &http.Server{ - Addr: fmt.Sprintf(":%d", cfg.ServeConfig.Port), - Handler: r, + Addr: fmt.Sprintf(":%d", cfg.ServeConfig.Port), + ReadTimeout: 5 * time.Second, + Handler: r, } lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { @@ -138,9 +141,12 @@ func newHTTPServer(lc fx.Lifecycle, tp *oteltrace.TracerProvider, mp *otelmetric return nil }, OnStop: func(ctx context.Context) error { + // nolint:errcheck tp.Shutdown(ctx) + // nolint:errcheck mp.Shutdown(ctx) logging.FromContext(ctx).Info("server shutdown") + // nolint:errcheck return srv.Shutdown(ctx) }, }) diff --git a/gitlab/commit.go b/gitlab/commit.go index 1826a36..f731882 100644 --- a/gitlab/commit.go +++ b/gitlab/commit.go @@ -28,7 +28,6 @@ func mapCommit(c *gitlab.Commit) *hosting.Commit { } func (h *GitlabService) GetCommits(ctx context.Context, repo *hosting.Repository, opts *hosting.GetCommitsOpts) ([]hosting.Commit, error) { - h.client.Projects.GetProject(ctx, &gitlab.GetProjectOptions{}) gitlabCommits, _, err := h.client.Commits.ListCommits(createPid(repo), &gitlab.ListCommitsOptions{ RefName: opts.Ref, }) diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index 69e9299..7a6cd72 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -29,11 +29,6 @@ func createPid(repo *hosting.Repository) string { return fmt.Sprintf("%s/%s", repo.Owner, repo.Name) } -func newTrue() *bool { - b := true - return &b -} - func newFalse() *bool { b := false return &b diff --git a/internal/config/config.go b/internal/config/config.go index 4b93cef..9a480b7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,7 +35,7 @@ type ServeConfig struct { } type LoggingConfig struct { - Level int `json:"level"` + Level int8 `json:"level"` Encoding string `json:"encoding"` Development bool `json:"development"` } @@ -72,6 +72,7 @@ func New(configFilePath string) (*Config, error) { // load from env err = k.Load(env.Provider(_defaultPrefix, ".", func(s string) string { + // nolint:gocritic return strings.Replace(strings.ToLower( strings.TrimPrefix(s, _defaultPrefix)), "_", ".", -1) }), nil) diff --git a/internal/logging/logger.go b/internal/logging/logger.go index c4b22b5..d988365 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -78,6 +78,7 @@ func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context if gCtx, ok := ctx.(*gin.Context); ok { ctx = gCtx.Request.Context() } + // nolint:staticcheck return context.WithValue(ctx, loggerKey, logger) }