Skip to content

Commit

Permalink
Merge pull request #88 from thegalactiks/ci-lint
Browse files Browse the repository at this point in the history
Add golangci lint
  • Loading branch information
emmanuelgautier authored Oct 6, 2024
2 parents d149dd4 + b47fa6a commit eeff61a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 ./...

Expand Down
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
linters:
enable:
- errcheck
- goconst
- gocritic
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused

issues:
exclude-files:
- ".+_test.go"
12 changes: 10 additions & 2 deletions api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions cmd/serve/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
},
})
Expand Down
1 change: 0 additions & 1 deletion gitlab/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
5 changes: 0 additions & 5 deletions gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit eeff61a

Please sign in to comment.