Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint errors #69

Merged
merged 6 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ jobs:
go-version: 1.18
- name: Integration test
env:
POSTGRES_HOST: localhost
POSTGRES_DB: mlp
POSTGRES_USER: mlp
POSTGRES_PASSWORD: mlp
DATABASE_HOST: localhost
DATABASE_NAME: mlp
DATABASE_USER: mlp
DATABASE_PASSWORD: mlp
KETO_URL: http://localhost:4466
run: make it-test-api-ci

Expand Down
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
run:
build-tags:
- integration
skip-dirs:
- api/client

linters:
enable:
- bodyclose
- deadcode
- errcheck
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- revive
- staticcheck
- structcheck
- unused
- varcheck

linters-settings:
gocyclo:
# Min code complexity to report, 30 by default (recommended 10-20)
min-complexity: 25
lll:
line-length: 120
staticcheck:
checks:
- all
- '-SA1019' # disable SA1019: "github.com/newrelic/go-agent" is deprecated: This package has been supplanted by version 3
18 changes: 12 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ all: setup init-dep lint test clean build run
# ============================================================
.PHONY: setup
setup:
@echo "> Setting up tools ..."
@test -x $(shell go env GOPATH)/bin/golint || go install golang.org/x/lint/golint
@echo "> Setting up tools..."
@test -x $(shell go env GOPATH)/bin/golangci-lint || \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/v1.48.0/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.48.0

.PHONY: init-dep
init-dep: init-dep-ui init-dep-api
Expand All @@ -42,10 +43,15 @@ lint-ui:
@echo "> Linting the UI source code ..."
@cd ${UI_PATH} && yarn lint

.PHONY: fmt
fmt:
@echo "Formatting code..."
gofmt -s -w ${SRC_ROOT}

.PHONY: lint-api
lint-api:
@echo "> Analyzing API source code..."
@cd ${API_PATH} && golint ${API_ALL_PACKAGES}
lint-api: setup
@echo "Linting code..."
golangci-lint -v run --timeout 3m $(if $(filter true,$(fix)),--fix,) ${API_PATH}/...

# ============================================================
# Testing recipes
Expand All @@ -62,7 +68,7 @@ test-api: init-dep-api
.PHONY: it-test-api-local
it-test-api-local: local-db start-keto
@echo "> API integration testing locally..."
@cd ${API_PATH} && go test -race -short -cover -coverprofile cover.out -tags integration_local ${API_ALL_PACKAGES}
@cd ${API_PATH} && go test -race -short -cover -coverprofile cover.out -tags integration ${API_ALL_PACKAGES}
@cd ${API_PATH} && go tool cover -func cover.out
@make stop-docker

Expand Down
210 changes: 0 additions & 210 deletions api/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion api/api/applications_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ApplicationsController struct {
*AppContext
}

func (c *ApplicationsController) ListApplications(r *http.Request, _ map[string]string, _ interface{}) *ApiResponse {
func (c *ApplicationsController) ListApplications(r *http.Request, _ map[string]string, _ interface{}) *Response {
applications, err := c.ApplicationService.List()
if err != nil {
return InternalServerError(err.Error())
Expand Down
36 changes: 20 additions & 16 deletions api/api/projects_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ProjectsController struct {
*AppContext
}

func (c *ProjectsController) ListProjects(r *http.Request, vars map[string]string, _ interface{}) *ApiResponse {
func (c *ProjectsController) ListProjects(r *http.Request, vars map[string]string, _ interface{}) *Response {
projects, err := c.ProjectsService.ListProjects(vars["name"])
if err != nil {
return InternalServerError(err.Error())
Expand All @@ -31,7 +31,7 @@ func (c *ProjectsController) ListProjects(r *http.Request, vars map[string]strin
return Ok(projects)
}

func (c *ProjectsController) CreateProject(r *http.Request, vars map[string]string, body interface{}) *ApiResponse {
func (c *ProjectsController) CreateProject(r *http.Request, vars map[string]string, body interface{}) *Response {
userAgent := strings.ToLower(r.Header.Get("User-Agent"))
if strings.Contains(userAgent, "swagger") {
return Forbidden("Project creation from SDK is disabled. Use the MLP console to create a project.")
Expand Down Expand Up @@ -61,9 +61,9 @@ func (c *ProjectsController) CreateProject(r *http.Request, vars map[string]stri
return Created(project)
}

func (c *ProjectsController) UpdateProject(r *http.Request, vars map[string]string, body interface{}) *ApiResponse {
projectID, _ := models.ParseId(vars["project_id"])
project, err := c.ProjectsService.FindById(projectID)
func (c *ProjectsController) UpdateProject(r *http.Request, vars map[string]string, body interface{}) *Response {
projectID, _ := models.ParseID(vars["project_id"])
project, err := c.ProjectsService.FindByID(projectID)
if err != nil {
if gorm.IsRecordNotFoundError(err) {
return NotFound(fmt.Sprintf("Project id %s not found", projectID))
Expand Down Expand Up @@ -91,9 +91,9 @@ func (c *ProjectsController) UpdateProject(r *http.Request, vars map[string]stri
return Ok(project)
}

func (c *ProjectsController) GetProject(r *http.Request, vars map[string]string, body interface{}) *ApiResponse {
projectID, _ := models.ParseId(vars["project_id"])
project, err := c.ProjectsService.FindById(projectID)
func (c *ProjectsController) GetProject(r *http.Request, vars map[string]string, body interface{}) *Response {
projectID, _ := models.ParseID(vars["project_id"])
project, err := c.ProjectsService.FindByID(projectID)
if err != nil {
if gorm.IsRecordNotFoundError(err) {
return NotFound(fmt.Sprintf("Project id %s not found", projectID))
Expand All @@ -105,13 +105,17 @@ func (c *ProjectsController) GetProject(r *http.Request, vars map[string]string,
return Ok(project)
}

func (c *ProjectsController) filterAuthorizedProjects(user string, projects []*models.Project, action string) ([]*models.Project, error) {
func (c *ProjectsController) filterAuthorizedProjects(
user string,
projects []*models.Project,
action string,
) ([]*models.Project, error) {
if c.AuthorizationEnabled {
projectIds := make([]string, 0, 0)
allowedProjects := make([]*models.Project, 0, 0)
projectIds := make([]string, 0)
allowedProjects := make([]*models.Project, 0)
projectMap := make(map[string]*models.Project)
for _, project := range projects {
projectID := fmt.Sprintf("projects:%s", project.Id)
projectID := fmt.Sprintf("projects:%s", project.ID)
projectIds = append(projectIds, projectID)
projectMap[projectID] = project
}
Expand All @@ -121,14 +125,14 @@ func (c *ProjectsController) filterAuthorizedProjects(user string, projects []*m
return nil, err
}

for _, projectId := range allowedProjectIds {
allowedProjects = append(allowedProjects, projectMap[projectId])
for _, projectID := range allowedProjectIds {
allowedProjects = append(allowedProjects, projectMap[projectID])
}

return allowedProjects, nil
} else {
return projects, nil
}

return projects, nil
}

// addRequester add requester to users slice if it doesn't exists
Expand Down
Loading