Peter Kurfer
c1c1667d9f
lint-pr runs golangci-lint, captures the result and uses it directly without temporary files It also allows to automatically retrieve the PRs base commit and run golangci-lint only against changes in the current PR
99 lines
1.9 KiB
Go
99 lines
1.9 KiB
Go
//go:build mage
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/exp/slices"
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
var (
|
|
WorkingDir string
|
|
GoSourceFiles []string
|
|
GeneratedMockFiles []string
|
|
IsReleaseBuild bool
|
|
IsPRBuild bool
|
|
|
|
repoOwner = "prskr"
|
|
repoName = "nitter"
|
|
prIndex int64 = 1
|
|
|
|
dirsToIgnore = []string{
|
|
".git",
|
|
"build",
|
|
".run",
|
|
".task",
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr)))
|
|
|
|
IsReleaseBuild = strings.EqualFold(os.Getenv("DRONE_BUILD_EVENT"), "tag")
|
|
IsPRBuild = strings.EqualFold(os.Getenv("DRONE_BUILD_EVENT"), "pull_request")
|
|
|
|
if wd, err := os.Getwd(); err != nil {
|
|
slog.Error("Failed to get working directory", err)
|
|
os.Exit(1)
|
|
} else {
|
|
WorkingDir = wd
|
|
}
|
|
|
|
if owner := os.Getenv("DRONE_REPO_NAMESPACE"); owner != "" {
|
|
repoOwner = owner
|
|
}
|
|
|
|
if name := os.Getenv("DRONE_REPO_NAME"); name != "" {
|
|
repoName = name
|
|
}
|
|
|
|
if prIdxRaw := os.Getenv("DRONE_PULL_REQUEST"); prIdxRaw != "" {
|
|
if parsed, err := strconv.ParseInt(prIdxRaw, 10, 64); err != nil {
|
|
slog.Error("Failed to parse PR index", err)
|
|
os.Exit(1)
|
|
} else {
|
|
prIndex = parsed
|
|
}
|
|
}
|
|
if err := initSourceFiles(); err != nil {
|
|
slog.Error("Failed to init source files", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
slog.Info("Completed initialization")
|
|
}
|
|
|
|
func initSourceFiles() error {
|
|
return filepath.WalkDir(WorkingDir, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.IsDir() {
|
|
if slices.Contains(dirsToIgnore, filepath.Base(path)) {
|
|
return fs.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
|
|
_, ext, found := strings.Cut(filepath.Base(path), ".")
|
|
if !found {
|
|
return nil
|
|
}
|
|
|
|
switch ext {
|
|
case "mock.go":
|
|
GeneratedMockFiles = append(GeneratedMockFiles, path)
|
|
case "go":
|
|
GoSourceFiles = append(GoSourceFiles, path)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|