From 3943b5316387f77d01ab990b9f6062aabfbb28e6 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Sun, 17 Sep 2023 02:55:17 +0200 Subject: [PATCH] Enhance Go CI (#99) --- .github/workflows/go.yml | 70 ++++++++++++++++++++++++---------------- Makefile | 16 ++++++++- go.mod | 5 ++- go.sum | 6 +--- internal/db/db.go | 4 ++- internal/git/commands.go | 9 ++++-- 6 files changed, 70 insertions(+), 40 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4163bb6..3417748 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,16 +1,52 @@ -name: "Go" +name: "Go CI" on: push: branches: - master + - 'dev-*' pull_request: jobs: - checks: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Go 1.19 + uses: actions/setup-go@v4 + with: + go-version: 1.19 + + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 + skip-pkg-cache: true + args: --out-format=colored-line-number --timeout=20m + + - name: Format + run: make fmt check_changes + + check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Go 1.19 + uses: actions/setup-go@v4 + with: + go-version: 1.19 + + - name: Check + run: make go_mod check_changes + + test: strategy: fail-fast: false matrix: - os: ["ubuntu-latest", "macOS-latest"] + os: ["ubuntu-latest", "macOS-latest", "windows-latest"] go: ["1.19", "1.20"] runs-on: ${{ matrix.os }} steps: @@ -18,32 +54,10 @@ jobs: uses: actions/checkout@v3 - name: Set up Go ${{ matrix.go }} - uses: WillAbides/setup-go-faster@v1.8.0 + uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} - - name: Cache Go modules - uses: actions/cache@v3 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + - name: Run tests + run: make test - - name: Cache Go build cache - uses: actions/cache@v3 - with: - path: ~/.cache/go-build - key: ${{ runner.os }}-go-build-${{ matrix.go }} - restore-keys: | - ${{ runner.os }}-go-build- - - - name: Run go vet - run: "go vet ./..." - - - name: Run Staticcheck - uses: dominikh/staticcheck-action@v1.3.0 - with: - version: "2023.1.1" - install-go: false - cache-key: ${{ matrix.go }} diff --git a/Makefile b/Makefile index bf29fb3..80c713b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all install build_frontend build_backend build build_docker watch_frontend watch_backend watch clean clean_docker +.PHONY: all install build_frontend build_backend build build_docker watch_frontend watch_backend watch clean clean_docker check_changes go_mod fmt test # Specify the name of your Go binary output BINARY_NAME := opengist @@ -44,3 +44,17 @@ clean: clean_docker: @echo "Cleaning up Docker image..." @docker rmi $(BINARY_NAME) + +check_changes: + @echo "Checking for changes..." + @git --no-pager diff --exit-code || (echo "There are unstaged changes detected." && exit 1) + +go_mod: + @go mod download + @go mod tidy + +fmt: + @go fmt ./... + +test: + @go test ./... -p 1 diff --git a/go.mod b/go.mod index 8ce05be..57a9c69 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,12 @@ require ( github.com/go-playground/validator/v10 v10.11.0 github.com/google/uuid v1.3.0 github.com/gorilla/sessions v1.2.1 + github.com/hashicorp/go-memdb v1.3.4 github.com/labstack/echo/v4 v4.10.0 github.com/markbates/goth v1.77.0 github.com/mattn/go-sqlite3 v1.14.13 github.com/rs/zerolog v1.29.0 + github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.2.0 golang.org/x/text v0.7.0 gopkg.in/yaml.v3 v3.0.1 @@ -27,7 +29,6 @@ require ( github.com/gorilla/mux v1.6.2 // indirect github.com/gorilla/securecookie v1.1.1 // indirect github.com/hashicorp/go-immutable-radix v1.3.0 // indirect - github.com/hashicorp/go-memdb v1.3.4 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect @@ -36,8 +37,6 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/net v0.7.0 // indirect diff --git a/go.sum b/go.sum index 0bc637a..92be277 100644 --- a/go.sum +++ b/go.sum @@ -132,6 +132,7 @@ github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrh github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c= github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg= +github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -195,16 +196,11 @@ github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= diff --git a/internal/db/db.go b/internal/db/db.go index 79394f6..38e983f 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -45,7 +45,9 @@ func Setup(dbPath string, sharedCache bool) error { return err } - ApplyMigrations(db) + if err = ApplyMigrations(db); err != nil { + return err + } // Default admin setting values return initAdminSettings(map[string]string{ diff --git a/internal/git/commands.go b/internal/git/commands.go index c3a94ff..29206f4 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -170,9 +170,14 @@ func GetLog(user string, gist string, skip int) ([]*Commit, error) { if err != nil { return nil, err } - defer cmd.Wait() + defer func(cmd *exec.Cmd) { + waitErr := cmd.Wait() + if waitErr != nil { + err = waitErr + } + }(cmd) - return parseLog(stdout, truncateLimit), nil + return parseLog(stdout, truncateLimit), err } func CloneTmp(user string, gist string, gistTmpId string, email string) error {