Skip to content

Commit

Permalink
feat(workflows): add basic golangci-lint setup (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i authored Jul 25, 2023
1 parent 45b66fc commit e10f327
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 91 deletions.
39 changes: 29 additions & 10 deletions .github/workflows/run-tests-and-sonar-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,32 @@ on:
- main
pull_request:
types: [opened, synchronize, reopened]
env:
GOLANG_VERSION: '1.20.4'

jobs:
golangci:
name: GolangCI
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version: ${{ env.GOLANG_VERSION }}
cache: false

- name: Install project dependencies
run: make install-deps mocks docs

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53

test:
name: Test
runs-on: ubuntu-latest
Expand All @@ -32,18 +57,12 @@ jobs:
with:
fetch-depth: 0

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20.4'

- name: Install swag
run: go install github.com/swaggo/swag/cmd/swag@latest

- name: Generate swagger docs
run: make docs
go-version: ${{ env.GOLANG_VERSION }}

- name: Install mockgen
run: go install github.com/golang/mock/mockgen@v1.6.0
- name: Install project dependencies
run: make install-deps mocks docs

- name: Run tests
env:
Expand Down
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
issues:
max-per-linter: 0
max-same-issues: 0

linters:
disable-all: true
enable:
- durationcheck
- errcheck
- exportloopref
- forcetypeassert
- godot
- gofmt
- gosimple
- ineffassign
- makezero
- misspell
- nilerr
- predeclared
- staticcheck
- tenv
- unconvert
- unparam
- unused
- vet
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ help: ## Print this help
@echo "Usage: make [target]"
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: install-deps
install-deps: ## Install dependencies
@echo "===> Installing dependencies"
@go install github.com/swaggo/swag/cmd/swag@latest
@go install go.uber.org/mock/mockgen@latest
@echo "===> Done"

.PHONY: test
test: mocks ## Run tests
@ARGO_TIMEOUT=1 go test -v ./... -count=1 -coverprofile coverage.out `go list ./... | egrep -v '(test|mock)'`
Expand Down
2 changes: 1 addition & 1 deletion cmd/argo-watcher/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"regexp"
"testing"

"github.com/golang/mock/gomock"
"github.com/shini4i/argo-watcher/cmd/argo-watcher/mock"
"github.com/shini4i/argo-watcher/internal/models"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

const loggedInUsername = "unit-test"
Expand Down
42 changes: 4 additions & 38 deletions cmd/argo-watcher/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewServerConfig(t *testing.T) {
// Set up the required environment variables
if err := os.Setenv("ARGO_URL", "https://example.com"); err != nil {
t.Fatal(err)
}

if err := os.Setenv("ARGO_TOKEN", "secret-token"); err != nil {
t.Fatal(err)
}

if err := os.Setenv("STATE_TYPE", "postgres"); err != nil {
t.Fatal(err)
}

// Cleanup the environment variables after the test
defer func() {
if err := os.Unsetenv("ARGO_URL"); err != nil {
t.Fatal(err)
}

if err := os.Unsetenv("ARGO_TOKEN"); err != nil {
t.Fatal(err)
}

if err := os.Unsetenv("STATE_TYPE"); err != nil {
t.Fatal(err)
}
}()
t.Setenv("ARGO_URL", "https://example.com")
t.Setenv("ARGO_TOKEN", "secret-token")
t.Setenv("STATE_TYPE", "postgres")

// Call the NewServerConfig function
cfg, err := NewServerConfig()
Expand All @@ -50,17 +26,7 @@ func TestNewServerConfig(t *testing.T) {
}

func TestNewServerConfig_RequiredFieldsMissing(t *testing.T) {
// Set up environment variables with missing required fields
if err := os.Setenv("ARGO_URL", "https://example.com"); err != nil {
t.Fatal(err)
}

// Cleanup the environment variables after the test
defer func() {
if err := os.Unsetenv("ARGO_URL"); err != nil {
t.Fatal(err)
}
}()
t.Setenv("ARGO_URL", "https://example.com")

// Call the NewServerConfig function
cfg, err := NewServerConfig()
Expand Down
14 changes: 7 additions & 7 deletions cmd/argo-watcher/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Env struct {
metrics *Metrics
}

// initialize router
// initialize router.
func (env *Env) CreateRouter() *gin.Engine {
docs.SwaggerInfo.Title = "Argo-Watcher API"
docs.SwaggerInfo.Version = version
Expand Down Expand Up @@ -84,7 +84,7 @@ func prometheusHandler() gin.HandlerFunc {
// @Description Get the version of the server
// @Tags frontend
// @Success 200 {string} string
// @Router /api/v1/version [get]
// @Router /api/v1/version [get].
func (env *Env) getVersion(c *gin.Context) {
c.JSON(http.StatusOK, version)
}
Expand All @@ -97,7 +97,7 @@ func (env *Env) getVersion(c *gin.Context) {
// @Produce json
// @Param task body models.Task true "Task"
// @Success 202 {object} models.TaskStatus
// @Router /api/v1/tasks [post]
// @Router /api/v1/tasks [post].
func (env *Env) addTask(c *gin.Context) {
var task models.Task

Expand Down Expand Up @@ -139,7 +139,7 @@ func (env *Env) addTask(c *gin.Context) {
// @Param from_timestamp query int true "From timestamp" default(1648390029)
// @Param to_timestamp query int false "To timestamp"
// @Success 200 {array} models.Task
// @Router /api/v1/tasks [get]
// @Router /api/v1/tasks [get].
func (env *Env) getState(c *gin.Context) {
startTime, _ := strconv.ParseFloat(c.Query("from_timestamp"), 64)
endTime, _ := strconv.ParseFloat(c.Query("to_timestamp"), 64)
Expand All @@ -158,7 +158,7 @@ func (env *Env) getState(c *gin.Context) {
// @Tags backend
// @Produce json
// @Success 200 {object} models.TaskStatus
// @Router /api/v1/tasks/{id} [get]
// @Router /api/v1/tasks/{id} [get].
func (env *Env) getTaskStatus(c *gin.Context) {
id := c.Param("id")
task, err := env.argo.state.GetTask(id)
Expand Down Expand Up @@ -188,7 +188,7 @@ func (env *Env) getTaskStatus(c *gin.Context) {
// @Description Get the list of apps
// @Tags frontend
// @Success 200 {array} string
// @Router /api/v1/apps [get]
// @Router /api/v1/apps [get].
func (env *Env) getApps(c *gin.Context) {
c.JSON(http.StatusOK, env.argo.GetAppList())
}
Expand All @@ -200,7 +200,7 @@ func (env *Env) getApps(c *gin.Context) {
// @Produce json
// @Success 200 {object} models.HealthStatus
// @Failure 503 {object} models.HealthStatus
// @Router /healthz [get]
// @Router /healthz [get].
func (env *Env) healthz(c *gin.Context) {
if env.argo.SimpleHealthCheck() {
c.JSON(http.StatusOK, models.HealthStatus{
Expand Down
5 changes: 2 additions & 3 deletions cmd/argo-watcher/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (

// initLogs initializes the logging configuration based on the provided log level.
// It parses the log level string and sets the global log level accordingly using the zerolog library.
// If the log level string is invalid, it sets the log level to the default InfoLevel.
// If the log level string is invalid, it falls back to the default InfoLevel.
func initLogs(logLevel string) {
if logLevel, err := zerolog.ParseLevel(logLevel); err != nil {
log.Warn().Msgf("Couldn't parse log level. Got the following error: %s", err)
logLevel = zerolog.InfoLevel
} else {
log.Info().Msgf("Setting log level to %s", logLevel)
zerolog.SetGlobalLevel(logLevel)
log.Debug().Msgf("Configured log level: %s", logLevel)
}
}

Expand Down
24 changes: 0 additions & 24 deletions cmd/argo-watcher/state/in_memory_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,6 @@ func TestInMemoryState_GetAppListEmpty(t *testing.T) {
}

func TestInMemoryState_ProcessObsoleteTasks(t *testing.T) {
tasks := []models.Task{
{
Id: "d4776428-6a95-4a54-a3f4-509aafb4f444",
Created: float64(time.Now().Unix()),
Updated: float64(time.Now().Unix()),
Images: []models.Image{{Image: "image1", Tag: "tag1"}},
Status: "app not found",
},
{
Id: "df43ec06-4e47-46bf-b526-a24c3b0fe58f",
Created: float64(time.Now().Unix()),
Updated: float64(time.Now().Unix() - 7200), // Older than 1 hour
Images: []models.Image{{Image: "image2", Tag: "tag2"}},
Status: "in progress",
},
{
Id: "231f576b-d9bf-463c-b233-d30a7c12e10e",
Created: float64(time.Now().Unix()),
Updated: float64(time.Now().Unix()),
Images: []models.Image{{Image: "image3", Tag: "tag3"}},
Status: "deployed",
},
}

state.ProcessObsoleteTasks(1)

// Call the function under test
Expand Down
2 changes: 1 addition & 1 deletion cmd/argo-watcher/state/postgres_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (state *PostgresState) Connect(serverConfig *config.ServerConfig) {

// Add inserts a new task into the PostgreSQL database with the provided details.
// It takes a models.Task parameter and returns an error if the insertion fails.
// The method executes an INSERT query to add a new record with the task details, including the current UTC time
// The method executes an INSERT query to add a new record with the task details, including the current UTC time.
func (state *PostgresState) Add(task models.Task) error {
images, err := json.Marshal(task.Images)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19
github.com/gin-gonic/gin v1.9.1
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/lib/pq v1.10.6
Expand All @@ -17,6 +16,7 @@ require (
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.2
github.com/swaggo/swag v1.16.1
go.uber.org/mock v0.2.0
)

require (
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -1237,6 +1236,8 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -153,10 +152,7 @@ func TestGetImagesList(t *testing.T) {
},
}

err := os.Setenv("IMAGES", "example/app,example/web")
if err != nil {
return
}
t.Setenv("IMAGES", "example/app,example/web")

images := getImagesList()

Expand Down

0 comments on commit e10f327

Please sign in to comment.