Skip to content

Commit

Permalink
mathew: fix linting
Browse files Browse the repository at this point in the history
Signed-off-by: Mathew Wicks <[email protected]>
  • Loading branch information
thesuperzapper committed Oct 10, 2024
1 parent 0320ecd commit 539b356
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 79 deletions.
40 changes: 40 additions & 0 deletions workspaces/backend/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
run:
timeout: 5m
allow-parallel-runners: true

issues:
# don't skip warning about doc comments
# don't exclude the default set of lint
exclude-use-default: false
# restore some of the defaults
# (fill in the rest as needed)
exclude-rules:
- path: "api/*"
linters:
- lll
- path: "internal/*"
linters:
- dupl
- lll
linters:
disable-all: true
enable:
- dupl
- errcheck
- exportloopref
- goconst
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- prealloc
- staticcheck
- typecheck
- unconvert
- unparam
- unused
13 changes: 7 additions & 6 deletions workspaces/backend/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package api

import (
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
"log/slog"
"net/http"

Expand All @@ -26,22 +25,24 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

const (
Version = "1.0.0"
PathPrefix = "/api/v1"

// healthcheck
HealthCheckPath = PathPrefix + "/healthcheck"
//workspaces

// workspaces
AllWorkspacesPath = PathPrefix + "/workspaces"
NamespacePathParam = "namespace"
WorkspaceNamePathParam = "name"
WorkspacesByNamespacePath = AllWorkspacesPath + "/:" + NamespacePathParam
WorkspacesByNamePath = AllWorkspacesPath + "/:" + NamespacePathParam + "/:" + WorkspaceNamePathParam

WorkspaceNamePathParam = "name"
WorkspacesByNamePath = AllWorkspacesPath + "/:" + NamespacePathParam + "/:" + WorkspaceNamePathParam

//workspacekinds
// workspacekinds
AllWorkspaceKindsPath = PathPrefix + "/workspacekinds"
WorkspaceKindNamePathParam = "name"
WorkspaceKindsByNamePath = AllWorkspaceKindsPath + "/:" + WorkspaceNamePathParam
Expand Down
6 changes: 1 addition & 5 deletions workspaces/backend/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ func (a *App) badRequestResponse(w http.ResponseWriter, r *http.Request, err err
}

func (a *App) errorResponse(w http.ResponseWriter, r *http.Request, error *HTTPError) {

env := ErrorEnvelope{Error: error}

err := a.WriteJSON(w, error.StatusCode, env, nil)

if err != nil {
a.LogError(r, err)
w.WriteHeader(error.StatusCode)
Expand All @@ -84,7 +82,6 @@ func (a *App) serverErrorResponse(w http.ResponseWriter, r *http.Request, err er
}

func (a *App) notFoundResponse(w http.ResponseWriter, r *http.Request) {

httpError := &HTTPError{
StatusCode: http.StatusNotFound,
ErrorResponse: ErrorResponse{
Expand All @@ -96,7 +93,6 @@ func (a *App) notFoundResponse(w http.ResponseWriter, r *http.Request) {
}

func (a *App) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request) {

httpError := &HTTPError{
StatusCode: http.StatusMethodNotAllowed,
ErrorResponse: ErrorResponse{
Expand All @@ -109,11 +105,11 @@ func (a *App) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request) {

// nolint:unused
func (a *App) failedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string) {

message, err := json.Marshal(errors)
if err != nil {
message = []byte("{}")
}

httpError := &HTTPError{
StatusCode: http.StatusUnprocessableEntity,
ErrorResponse: ErrorResponse{
Expand Down
7 changes: 3 additions & 4 deletions workspaces/backend/api/healthcheck__handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package api

import (
"encoding/json"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -28,6 +26,8 @@ import (
"github.com/stretchr/testify/assert"

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

func TestHealthCheckHandler(t *testing.T) {
Expand All @@ -46,8 +46,7 @@ func TestHealthCheckHandler(t *testing.T) {

app.HealthcheckHandler(rr, req, nil)
rs := rr.Result()

defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

body, err := io.ReadAll(rs.Body)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion workspaces/backend/api/healthcheck_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (a *App) HealthcheckHandler(w http.ResponseWriter, r *http.Request, ps http
}

err = a.WriteJSON(w, http.StatusOK, healthCheck, nil)

if err != nil {
a.serverErrorResponse(w, r, err)
}
Expand Down
2 changes: 0 additions & 2 deletions workspaces/backend/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Envelope[D any] struct {
func (a *App) WriteJSON(w http.ResponseWriter, status int, data any, headers http.Header) error {

js, err := json.MarshalIndent(data, "", "\t")

if err != nil {
return err
}
Expand Down Expand Up @@ -62,7 +61,6 @@ func (a *App) ReadJSON(w http.ResponseWriter, r *http.Request, dst any) error {
dec.DisallowUnknownFields()

err := dec.Decode(dst)

if err != nil {
var syntaxError *json.SyntaxError
var unmarshalTypeError *json.UnmarshalTypeError
Expand Down
4 changes: 3 additions & 1 deletion workspaces/backend/api/workspacekinds_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ package api
import (
"errors"
"fmt"
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
"net/http"
)

type WorkspaceKindsEnvelope Envelope[[]models.WorkspaceKindModel]
Expand Down
12 changes: 3 additions & 9 deletions workspaces/backend/api/workspaces_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

type WorkspacesEnvelope Envelope[[]models.WorkspaceModel]
type WorkspaceEnvelope Envelope[models.WorkspaceModel]

func (a *App) GetWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

namespace := ps.ByName(NamespacePathParam)
workspaceName := ps.ByName(WorkspaceNamePathParam)

Expand All @@ -47,7 +47,6 @@ func (a *App) GetWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps htt
}

workspace, err = a.repositories.Workspace.GetWorkspace(r.Context(), namespace, workspaceName)

if err != nil {
if errors.Is(err, repositories.ErrWorkspaceNotFound) {
a.notFoundResponse(w, r)
Expand All @@ -62,15 +61,13 @@ func (a *App) GetWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps htt
}

err = a.WriteJSON(w, http.StatusOK, modelRegistryRes, nil)

if err != nil {
a.serverErrorResponse(w, r, err)
}

}

func (a *App) GetWorkspacesHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

namespace := ps.ByName(NamespacePathParam)

var workspaces []models.WorkspaceModel
Expand All @@ -80,7 +77,6 @@ func (a *App) GetWorkspacesHandler(w http.ResponseWriter, r *http.Request, ps ht
} else {
workspaces, err = a.repositories.Workspace.GetWorkspaces(r.Context(), namespace)
}

if err != nil {
a.serverErrorResponse(w, r, err)
return
Expand All @@ -91,11 +87,9 @@ func (a *App) GetWorkspacesHandler(w http.ResponseWriter, r *http.Request, ps ht
}

err = a.WriteJSON(w, http.StatusOK, modelRegistryRes, nil)

if err != nil {
a.serverErrorResponse(w, r, err)
}

}

func (a *App) CreateWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand Down
30 changes: 16 additions & 14 deletions workspaces/backend/api/workspaces_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"

"github.com/julienschmidt/httprouter"
"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"net/http"
"net/http/httptest"
"strings"

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

var _ = Describe("Workspaces Handler", func() {
Expand Down Expand Up @@ -165,7 +167,7 @@ var _ = Describe("Workspaces Handler", func() {
rr := httptest.NewRecorder()
a.GetWorkspacesHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code")
Expect(rs.StatusCode).To(Equal(http.StatusOK), "Expected HTTP status 200 OK")
Expand Down Expand Up @@ -224,7 +226,7 @@ var _ = Describe("Workspaces Handler", func() {
rr := httptest.NewRecorder()
a.GetWorkspacesHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code")
Expect(rs.StatusCode).To(Equal(http.StatusOK), "Expected HTTP status 200 OK")
Expand Down Expand Up @@ -291,7 +293,7 @@ var _ = Describe("Workspaces Handler", func() {
rr := httptest.NewRecorder()
a.GetWorkspacesHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code")
Expect(rs.StatusCode).To(Equal(http.StatusOK), "Expected HTTP status 200 OK")
Expand Down Expand Up @@ -417,7 +419,7 @@ var _ = Describe("Workspaces Handler", func() {

a.CreateWorkspaceHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code for creation")
Expect(rs.StatusCode).To(Equal(http.StatusCreated), "Expected HTTP status 201 Created")
Expand All @@ -443,7 +445,7 @@ var _ = Describe("Workspaces Handler", func() {

a.GetWorkspaceHandler(rr, req, ps)
rs = rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code for retrieval")
Expect(rs.StatusCode).To(Equal(http.StatusOK), "Expected HTTP status 200 OK")
Expand Down Expand Up @@ -477,7 +479,7 @@ var _ = Describe("Workspaces Handler", func() {
rr = httptest.NewRecorder()
a.DeleteWorkspaceHandler(rr, req, ps)
rs = rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code for deletion")
Expect(rs.StatusCode).To(Equal(http.StatusNoContent), "Expected HTTP status 204 No Content")
Expand All @@ -489,7 +491,7 @@ var _ = Describe("Workspaces Handler", func() {

a.GetWorkspaceHandler(rr, req, ps)
rs = rr.Result()
defer rs.Body.Close()
defer rs.Body.Close() // nolint: errcheck

By("verifying the HTTP response status code for not found")
Expect(rs.StatusCode).To(Equal(http.StatusNotFound), "Expected HTTP status 200 OK")
Expand Down
Loading

0 comments on commit 539b356

Please sign in to comment.