Skip to content

Commit

Permalink
Add golangci-lint for BFF linting (kubeflow#433)
Browse files Browse the repository at this point in the history
Signed-off-by: Griffin-Sullivan <[email protected]>
  • Loading branch information
Griffin-Sullivan authored Oct 1, 2024
1 parent 87e2018 commit edbc250
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ui-bff-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:
working-directory: clients/ui/bff
run: make clean

- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.57.2
working-directory: clients/ui/bff/

- name: Build
working-directory: clients/ui/bff
run: make build
Expand Down
38 changes: 38 additions & 0 deletions clients/ui/bff/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ fmt:
clean:
rm -Rf ./bin

.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter
$(GOLANGCI_LINT) run

.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix

.PHONY: vet
vet: .
go vet ./...
Expand All @@ -38,3 +46,33 @@ run: fmt vet
.PHONY: docker-build
docker-build:
$(CONTAINER_TOOL) build -t ${IMG} .

##@ Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
GOLANGCI_LINT_VERSION ?= v1.57.2

.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})


# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f $(1) ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
}
endef
11 changes: 11 additions & 0 deletions clients/ui/bff/docs/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

## Running the linter locally
The BFF directory uses golangci-lint to combine multiple linters for a more comprehensive linting process. To install and run simply use:

```shell
cd clients/ui/bff
make lint
```

For more information on configuring golangci-lint see the [documentation](https://golangci-lint.run/).
6 changes: 4 additions & 2 deletions clients/ui/bff/internal/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package api
import (
"encoding/json"
"fmt"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations"
"net/http"
"strconv"

"github.com/kubeflow/model-registry/ui/bff/internal/integrations"
)

type HTTPError struct {
Expand Down Expand Up @@ -91,7 +92,8 @@ func (app *App) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request)
app.errorResponse(w, r, httpError)
}

func (app *App) failedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string) {
// TODO remove nolint comment below when we use this method
func (app *App) failedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string) { //nolint:unused

message, err := json.Marshal(errors)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions clients/ui/bff/internal/data/model_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package data

import (
"encoding/json"
"net/http"
"net/url"
"testing"

"github.com/brianvoe/gofakeit/v7"
"github.com/kubeflow/model-registry/ui/bff/internal/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"net/http"
"net/url"
"testing"
)

func TestGetModelVersion(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelVersion()

Expand All @@ -37,7 +38,7 @@ func TestGetModelVersion(t *testing.T) {
}

func TestCreateModelVersion(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelVersion()

Expand All @@ -62,7 +63,7 @@ func TestCreateModelVersion(t *testing.T) {
}

func TestUpdateModelVersion(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelVersion()

Expand Down Expand Up @@ -90,7 +91,7 @@ func TestUpdateModelVersion(t *testing.T) {
}

func TestGetModelArtifactsByModelVersion(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelArtifactList()

Expand All @@ -116,7 +117,7 @@ func TestGetModelArtifactsByModelVersion(t *testing.T) {
}

func TestCreateModelArtifactByModelVersion(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelArtifact()

Expand Down
19 changes: 10 additions & 9 deletions clients/ui/bff/internal/data/registered_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package data

import (
"encoding/json"
"net/http"
"net/url"
"testing"

"github.com/brianvoe/gofakeit/v7"
"github.com/kubeflow/model-registry/ui/bff/internal/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"net/http"
"net/url"
"testing"
)

func TestGetAllRegisteredModels(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockRegisteredModelList()

Expand All @@ -36,7 +37,7 @@ func TestGetAllRegisteredModels(t *testing.T) {
}

func TestCreateRegisteredModel(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockRegisteredModel()

Expand All @@ -61,7 +62,7 @@ func TestCreateRegisteredModel(t *testing.T) {
}

func TestGetRegisteredModel(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockRegisteredModel()

Expand All @@ -83,7 +84,7 @@ func TestGetRegisteredModel(t *testing.T) {
}

func TestUpdateRegisteredModel(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockRegisteredModel()

Expand Down Expand Up @@ -111,7 +112,7 @@ func TestUpdateRegisteredModel(t *testing.T) {
}

func TestGetAllModelVersions(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelVersionList()

Expand Down Expand Up @@ -139,7 +140,7 @@ func TestGetAllModelVersions(t *testing.T) {
}

func TestCreateModelVersionForRegisteredModel(t *testing.T) {
gofakeit.Seed(0)
gofakeit.Seed(0) //nolint:errcheck

expected := mocks.GenerateMockModelVersion()

Expand Down

0 comments on commit edbc250

Please sign in to comment.