mirror of
https://github.com/harness/drone.git
synced 2025-05-31 11:43:15 +00:00
[MAINT] fix linter errors. (#18)
* fix linter errors * fix exhaust linter error * fix exhaust linter error
This commit is contained in:
parent
f03528e862
commit
560c08dcca
@ -31,7 +31,7 @@ func Disect(path string) (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Concatinates two paths together (takes care of leading / trailing '/')
|
* Concatinate two paths together (takes care of leading / trailing '/')
|
||||||
* e.g. (space1/, /space2/) -> space1/space2
|
* e.g. (space1/, /space2/) -> space1/space2
|
||||||
*
|
*
|
||||||
* NOTE: "//" is not a valid path, so all '/' will be trimmed.
|
* NOTE: "//" is not a valid path, so all '/' will be trimmed.
|
||||||
|
@ -5,19 +5,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/internal/api/handler/account"
|
||||||
|
handlerrepo "github.com/harness/gitness/internal/api/handler/repo"
|
||||||
|
handlerspace "github.com/harness/gitness/internal/api/handler/space"
|
||||||
|
"github.com/harness/gitness/internal/api/handler/system"
|
||||||
|
"github.com/harness/gitness/internal/api/handler/user"
|
||||||
|
"github.com/harness/gitness/internal/api/middleware/repo"
|
||||||
|
"github.com/harness/gitness/internal/api/middleware/space"
|
||||||
|
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
|
|
||||||
"github.com/harness/gitness/internal/api/guard"
|
"github.com/harness/gitness/internal/api/guard"
|
||||||
"github.com/harness/gitness/internal/api/handler/account"
|
|
||||||
handler_repo "github.com/harness/gitness/internal/api/handler/repo"
|
|
||||||
handler_space "github.com/harness/gitness/internal/api/handler/space"
|
|
||||||
"github.com/harness/gitness/internal/api/handler/system"
|
|
||||||
"github.com/harness/gitness/internal/api/handler/user"
|
|
||||||
"github.com/harness/gitness/internal/api/middleware/accesslog"
|
"github.com/harness/gitness/internal/api/middleware/accesslog"
|
||||||
middleware_authn "github.com/harness/gitness/internal/api/middleware/authn"
|
middlewareauthn "github.com/harness/gitness/internal/api/middleware/authn"
|
||||||
"github.com/harness/gitness/internal/api/middleware/encode"
|
"github.com/harness/gitness/internal/api/middleware/encode"
|
||||||
"github.com/harness/gitness/internal/api/middleware/repo"
|
|
||||||
"github.com/harness/gitness/internal/api/middleware/space"
|
|
||||||
"github.com/harness/gitness/internal/api/request"
|
"github.com/harness/gitness/internal/api/request"
|
||||||
"github.com/harness/gitness/internal/auth/authn"
|
"github.com/harness/gitness/internal/auth/authn"
|
||||||
"github.com/harness/gitness/internal/auth/authz"
|
"github.com/harness/gitness/internal/auth/authz"
|
||||||
@ -64,7 +65,7 @@ func newAPIHandler(
|
|||||||
r.Use(corsHandler(config))
|
r.Use(corsHandler(config))
|
||||||
|
|
||||||
// for now always attempt auth - enforced per operation
|
// for now always attempt auth - enforced per operation
|
||||||
r.Use(middleware_authn.Attempt(authenticator))
|
r.Use(middlewareauthn.Attempt(authenticator))
|
||||||
|
|
||||||
r.Route("/v1", func(r chi.Router) {
|
r.Route("/v1", func(r chi.Router) {
|
||||||
setupRoutesV1(r, systemStore, userStore, spaceStore, repoStore, authenticator, g)
|
setupRoutesV1(r, systemStore, userStore, spaceStore, repoStore, authenticator, g)
|
||||||
@ -93,75 +94,79 @@ func corsHandler(config *types.Config) func(http.Handler) http.Handler {
|
|||||||
).Handler
|
).Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupRoutesV1(
|
func setupRoutesV1(r chi.Router, systemStore store.SystemStore, userStore store.UserStore, spaceStore store.SpaceStore,
|
||||||
r chi.Router,
|
repoStore store.RepoStore, _ authn.Authenticator, guard *guard.Guard) {
|
||||||
systemStore store.SystemStore,
|
setupSpaces(r, spaceStore, repoStore, guard)
|
||||||
userStore store.UserStore,
|
setupRepos(r, spaceStore, repoStore, guard)
|
||||||
spaceStore store.SpaceStore,
|
setupUsers(r, userStore, guard)
|
||||||
repoStore store.RepoStore,
|
setupAdmin(r, guard)
|
||||||
_ authn.Authenticator,
|
setupAuth(r, userStore, systemStore)
|
||||||
guard *guard.Guard) {
|
setupSystem(r)
|
||||||
// SPACES
|
}
|
||||||
|
|
||||||
|
func setupSpaces(r chi.Router, spaceStore store.SpaceStore, repoStore store.RepoStore, guard *guard.Guard) {
|
||||||
r.Route("/spaces", func(r chi.Router) {
|
r.Route("/spaces", func(r chi.Router) {
|
||||||
// Create takes path and parentId via body, not uri
|
// Create takes path and parentId via body, not uri
|
||||||
r.Post("/", handler_space.HandleCreate(guard, spaceStore))
|
r.Post("/", handlerspace.HandleCreate(guard, spaceStore))
|
||||||
|
|
||||||
r.Route(fmt.Sprintf("/{%s}", request.SpaceRefParamName), func(r chi.Router) {
|
r.Route(fmt.Sprintf("/{%s}", request.SpaceRefParamName), func(r chi.Router) {
|
||||||
// resolves the space and stores in the context
|
// resolves the space and stores in the context
|
||||||
r.Use(space.Required(spaceStore))
|
r.Use(space.Required(spaceStore))
|
||||||
|
|
||||||
// space operations
|
// space operations
|
||||||
r.Get("/", handler_space.HandleFind(guard, spaceStore))
|
r.Get("/", handlerspace.HandleFind(guard, spaceStore))
|
||||||
r.Put("/", handler_space.HandleUpdate(guard, spaceStore))
|
r.Put("/", handlerspace.HandleUpdate(guard, spaceStore))
|
||||||
r.Delete("/", handler_space.HandleDelete(guard, spaceStore))
|
r.Delete("/", handlerspace.HandleDelete(guard, spaceStore))
|
||||||
|
|
||||||
r.Post("/move", handler_space.HandleMove(guard, spaceStore))
|
r.Post("/move", handlerspace.HandleMove(guard, spaceStore))
|
||||||
r.Get("/spaces", handler_space.HandleList(guard, spaceStore))
|
r.Get("/spaces", handlerspace.HandleList(guard, spaceStore))
|
||||||
r.Get("/repos", handler_space.HandleListRepos(guard, repoStore))
|
r.Get("/repos", handlerspace.HandleListRepos(guard, repoStore))
|
||||||
|
|
||||||
// Child collections
|
// Child collections
|
||||||
r.Route("/paths", func(r chi.Router) {
|
r.Route("/paths", func(r chi.Router) {
|
||||||
r.Get("/", handler_space.HandleListPaths(guard, spaceStore))
|
r.Get("/", handlerspace.HandleListPaths(guard, spaceStore))
|
||||||
r.Post("/", handler_space.HandleCreatePath(guard, spaceStore))
|
r.Post("/", handlerspace.HandleCreatePath(guard, spaceStore))
|
||||||
|
|
||||||
// per path operations
|
// per path operations
|
||||||
r.Route(fmt.Sprintf("/{%s}", request.PathIDParamName), func(r chi.Router) {
|
r.Route(fmt.Sprintf("/{%s}", request.PathIDParamName), func(r chi.Router) {
|
||||||
r.Delete("/", handler_space.HandleDeletePath(guard, spaceStore))
|
r.Delete("/", handlerspace.HandleDeletePath(guard, spaceStore))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// REPOS
|
func setupRepos(r chi.Router, spaceStore store.SpaceStore, repoStore store.RepoStore, guard *guard.Guard) {
|
||||||
r.Route("/repos", func(r chi.Router) {
|
r.Route("/repos", func(r chi.Router) {
|
||||||
// Create takes path and parentId via body, not uri
|
// Create takes path and parentId via body, not uri
|
||||||
r.Post("/", handler_repo.HandleCreate(guard, spaceStore, repoStore))
|
r.Post("/", handlerrepo.HandleCreate(guard, spaceStore, repoStore))
|
||||||
|
|
||||||
r.Route(fmt.Sprintf("/{%s}", request.RepoRefParamName), func(r chi.Router) {
|
r.Route(fmt.Sprintf("/{%s}", request.RepoRefParamName), func(r chi.Router) {
|
||||||
// resolves the repo and stores in the context
|
// resolves the repo and stores in the context
|
||||||
r.Use(repo.Required(repoStore))
|
r.Use(repo.Required(repoStore))
|
||||||
|
|
||||||
// repo level operations
|
// repo level operations
|
||||||
r.Get("/", handler_repo.HandleFind(guard, repoStore))
|
r.Get("/", handlerrepo.HandleFind(guard, repoStore))
|
||||||
r.Put("/", handler_repo.HandleUpdate(guard, repoStore))
|
r.Put("/", handlerrepo.HandleUpdate(guard, repoStore))
|
||||||
r.Delete("/", handler_repo.HandleDelete(guard, repoStore))
|
r.Delete("/", handlerrepo.HandleDelete(guard, repoStore))
|
||||||
|
|
||||||
r.Post("/move", handler_repo.HandleMove(guard, repoStore, spaceStore))
|
r.Post("/move", handlerrepo.HandleMove(guard, repoStore, spaceStore))
|
||||||
|
|
||||||
// Child collections
|
// Child collections
|
||||||
r.Route("/paths", func(r chi.Router) {
|
r.Route("/paths", func(r chi.Router) {
|
||||||
r.Get("/", handler_repo.HandleListPaths(guard, repoStore))
|
r.Get("/", handlerrepo.HandleListPaths(guard, repoStore))
|
||||||
r.Post("/", handler_repo.HandleCreatePath(guard, repoStore))
|
r.Post("/", handlerrepo.HandleCreatePath(guard, repoStore))
|
||||||
|
|
||||||
// per path operations
|
// per path operations
|
||||||
r.Route(fmt.Sprintf("/{%s}", request.PathIDParamName), func(r chi.Router) {
|
r.Route(fmt.Sprintf("/{%s}", request.PathIDParamName), func(r chi.Router) {
|
||||||
r.Delete("/", handler_repo.HandleDeletePath(guard, repoStore))
|
r.Delete("/", handlerrepo.HandleDeletePath(guard, repoStore))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// USER - SELF OPERATIONS
|
func setupUsers(r chi.Router, userStore store.UserStore, guard *guard.Guard) {
|
||||||
r.Route("/user", func(r chi.Router) {
|
r.Route("/user", func(r chi.Router) {
|
||||||
// enforce user authenticated
|
// enforce user authenticated
|
||||||
r.Use(guard.EnforceAuthenticated)
|
r.Use(guard.EnforceAuthenticated)
|
||||||
@ -170,8 +175,16 @@ func setupRoutesV1(
|
|||||||
r.Patch("/", user.HandleUpdate(userStore))
|
r.Patch("/", user.HandleUpdate(userStore))
|
||||||
r.Post("/token", user.HandleToken(userStore))
|
r.Post("/token", user.HandleToken(userStore))
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// USERS - ADMIN OPERATIONS
|
func setupSystem(r chi.Router) {
|
||||||
|
r.Route("/system", func(r chi.Router) {
|
||||||
|
r.Get("/health", system.HandleHealth)
|
||||||
|
r.Get("/version", system.HandleVersion)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupAdmin(r chi.Router, guard *guard.Guard) {
|
||||||
r.Route("/users", func(r chi.Router) {
|
r.Route("/users", func(r chi.Router) {
|
||||||
// enforce system admin
|
// enforce system admin
|
||||||
r.Use(guard.EnforceAdmin)
|
r.Use(guard.EnforceAdmin)
|
||||||
@ -191,14 +204,9 @@ func setupRoutesV1(
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// SYSTEM MGMT ENDPOINTS
|
func setupAuth(r chi.Router, userStore store.UserStore, systemStore store.SystemStore) {
|
||||||
r.Route("/system", func(r chi.Router) {
|
|
||||||
r.Get("/health", system.HandleHealth)
|
|
||||||
r.Get("/version", system.HandleVersion)
|
|
||||||
})
|
|
||||||
|
|
||||||
// USER LOGIN & REGISTRATION
|
|
||||||
r.Post("/login", account.HandleLogin(userStore, systemStore))
|
r.Post("/login", account.HandleLogin(userStore, systemStore))
|
||||||
r.Post("/register", account.HandleRegister(userStore, systemStore))
|
r.Post("/register", account.HandleRegister(userStore, systemStore))
|
||||||
}
|
}
|
||||||
|
@ -269,6 +269,8 @@ func ListPaths(ctx context.Context, db *sqlx.DB, targetType enum.PathTargetType,
|
|||||||
stmt = stmt.OrderBy("path_id " + opts.Order.String())
|
stmt = stmt.OrderBy("path_id " + opts.Order.String())
|
||||||
case enum.PathAttrPath:
|
case enum.PathAttrPath:
|
||||||
stmt = stmt.OrderBy("path_value" + opts.Order.String())
|
stmt = stmt.OrderBy("path_value" + opts.Order.String())
|
||||||
|
case enum.PathAttrNone:
|
||||||
|
// no sorting required
|
||||||
}
|
}
|
||||||
|
|
||||||
sql, _, err := stmt.ToSql()
|
sql, _, err := stmt.ToSql()
|
||||||
|
@ -258,6 +258,8 @@ func (s *RepoStore) List(ctx context.Context, spaceID int64, opts *types.RepoFil
|
|||||||
stmt = stmt.OrderBy("repo_displayName " + opts.Order.String())
|
stmt = stmt.OrderBy("repo_displayName " + opts.Order.String())
|
||||||
case enum.RepoAttrPath:
|
case enum.RepoAttrPath:
|
||||||
stmt = stmt.OrderBy("repo_path " + opts.Order.String())
|
stmt = stmt.OrderBy("repo_path " + opts.Order.String())
|
||||||
|
case enum.RepoAttrNone:
|
||||||
|
// no order need here
|
||||||
}
|
}
|
||||||
|
|
||||||
sql, _, err := stmt.ToSql()
|
sql, _, err := stmt.ToSql()
|
||||||
|
@ -38,17 +38,27 @@ const (
|
|||||||
PathAttrUpdated
|
PathAttrUpdated
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
id = "id"
|
||||||
|
path = "path"
|
||||||
|
name = "name"
|
||||||
|
created = "created"
|
||||||
|
createdAt = "created_at"
|
||||||
|
updated = "updated"
|
||||||
|
updatedAt = "updated_at"
|
||||||
|
)
|
||||||
|
|
||||||
// ParsePathAttr parses the path attribute string
|
// ParsePathAttr parses the path attribute string
|
||||||
// and returns the equivalent enumeration.
|
// and returns the equivalent enumeration.
|
||||||
func ParsePathAttr(s string) PathAttr {
|
func ParsePathAttr(s string) PathAttr {
|
||||||
switch strings.ToLower(s) {
|
switch strings.ToLower(s) {
|
||||||
case "id":
|
case id:
|
||||||
return PathAttrID
|
return PathAttrID
|
||||||
case "path":
|
case path:
|
||||||
return PathAttrPath
|
return PathAttrPath
|
||||||
case "created", "created_at":
|
case created, createdAt:
|
||||||
return PathAttrCreated
|
return PathAttrCreated
|
||||||
case "updated", "updated_at":
|
case updated, updatedAt:
|
||||||
return PathAttrUpdated
|
return PathAttrUpdated
|
||||||
default:
|
default:
|
||||||
return PathAttrNone
|
return PathAttrNone
|
||||||
|
@ -24,17 +24,17 @@ const (
|
|||||||
// and returns the equivalent enumeration.
|
// and returns the equivalent enumeration.
|
||||||
func ParseRepoAtrr(s string) RepoAttr {
|
func ParseRepoAtrr(s string) RepoAttr {
|
||||||
switch strings.ToLower(s) {
|
switch strings.ToLower(s) {
|
||||||
case "id":
|
case id:
|
||||||
return RepoAttrID
|
return RepoAttrID
|
||||||
case "name":
|
case name:
|
||||||
return RepoAttrName
|
return RepoAttrName
|
||||||
case "path":
|
case path:
|
||||||
return RepoAttrPath
|
return RepoAttrPath
|
||||||
case "displayName":
|
case "displayName":
|
||||||
return RepoAttrDisplayName
|
return RepoAttrDisplayName
|
||||||
case "created", "created_at":
|
case created, createdAt:
|
||||||
return RepoAttrCreated
|
return RepoAttrCreated
|
||||||
case "updated", "updated_at":
|
case updated, updatedAt:
|
||||||
return RepoAttrUpdated
|
return RepoAttrUpdated
|
||||||
default:
|
default:
|
||||||
return RepoAttrNone
|
return RepoAttrNone
|
||||||
|
Loading…
x
Reference in New Issue
Block a user