mirror of
https://github.com/harness/drone.git
synced 2025-05-04 18:12:37 +08:00
Minor fixes and refactoring
This commit is contained in:
parent
25031d113a
commit
f5084697b1
@ -36,7 +36,7 @@ func (c *Controller) Delete(ctx context.Context, session *auth.Session, spaceRef
|
|||||||
func (c *Controller) DeleteNoAuth(ctx context.Context, session *auth.Session, spaceID int64) error {
|
func (c *Controller) DeleteNoAuth(ctx context.Context, session *auth.Session, spaceID int64) error {
|
||||||
filter := &types.SpaceFilter{
|
filter := &types.SpaceFilter{
|
||||||
Page: 1,
|
Page: 1,
|
||||||
Size: int(math.MaxInt),
|
Size: math.MaxInt,
|
||||||
Query: "",
|
Query: "",
|
||||||
Order: enum.OrderAsc,
|
Order: enum.OrderAsc,
|
||||||
Sort: enum.SpaceAttrNone,
|
Sort: enum.SpaceAttrNone,
|
||||||
|
@ -10,13 +10,17 @@ import (
|
|||||||
|
|
||||||
apiauth "github.com/harness/gitness/internal/api/auth"
|
apiauth "github.com/harness/gitness/internal/api/auth"
|
||||||
"github.com/harness/gitness/internal/auth"
|
"github.com/harness/gitness/internal/auth"
|
||||||
|
"github.com/harness/gitness/store/database/dbtx"
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ListSpaces lists the child spaces of a space.
|
// ListSpaces lists the child spaces of a space.
|
||||||
func (c *Controller) ListSpaces(ctx context.Context, session *auth.Session,
|
func (c *Controller) ListSpaces(ctx context.Context,
|
||||||
spaceRef string, filter *types.SpaceFilter) ([]*types.Space, int64, error) {
|
session *auth.Session,
|
||||||
|
spaceRef string,
|
||||||
|
filter *types.SpaceFilter,
|
||||||
|
) ([]types.Space, int64, error) {
|
||||||
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
|
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
@ -28,20 +32,30 @@ func (c *Controller) ListSpaces(ctx context.Context, session *auth.Session,
|
|||||||
return c.ListSpacesNoAuth(ctx, space.ID, filter)
|
return c.ListSpacesNoAuth(ctx, space.ID, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List spaces WITHOUT checking PermissionSpaceView.
|
// ListSpacesNoAuth lists spaces WITHOUT checking PermissionSpaceView.
|
||||||
func (c *Controller) ListSpacesNoAuth(
|
func (c *Controller) ListSpacesNoAuth(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
spaceID int64,
|
spaceID int64,
|
||||||
filter *types.SpaceFilter,
|
filter *types.SpaceFilter,
|
||||||
) ([]*types.Space, int64, error) {
|
) ([]types.Space, int64, error) {
|
||||||
count, err := c.spaceStore.Count(ctx, spaceID, filter)
|
var spaces []types.Space
|
||||||
if err != nil {
|
var count int64
|
||||||
return nil, 0, fmt.Errorf("failed to count child spaces: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
spaces, err := c.spaceStore.List(ctx, spaceID, filter)
|
err := dbtx.New(c.db).WithTx(ctx, func(ctx context.Context) (err error) {
|
||||||
|
count, err = c.spaceStore.Count(ctx, spaceID, filter)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to count child spaces: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
spaces, err = c.spaceStore.List(ctx, spaceID, filter)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to list child spaces: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, dbtx.TxDefaultReadOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("failed to list child spaces: %w", err)
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -7,16 +7,13 @@ package space
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/harness/gitness/internal/api/controller/repo"
|
|
||||||
"github.com/harness/gitness/internal/api/controller/space"
|
"github.com/harness/gitness/internal/api/controller/space"
|
||||||
"github.com/harness/gitness/internal/api/render"
|
"github.com/harness/gitness/internal/api/render"
|
||||||
"github.com/harness/gitness/internal/api/request"
|
"github.com/harness/gitness/internal/api/request"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
// HandleDelete handles the delete space HTTP API.
|
||||||
* Deletes a space.
|
func HandleDelete(spaceCtrl *space.Controller) http.HandlerFunc {
|
||||||
*/
|
|
||||||
func HandleDelete(spaceCtrl *space.Controller, repoCtrl *repo.Controller) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
session, _ := request.AuthSessionFrom(ctx)
|
session, _ := request.AuthSessionFrom(ctx)
|
||||||
|
@ -122,7 +122,7 @@ func setupRoutesV1(r chi.Router,
|
|||||||
principalCtrl principal.Controller,
|
principalCtrl principal.Controller,
|
||||||
checkCtrl *check.Controller,
|
checkCtrl *check.Controller,
|
||||||
) {
|
) {
|
||||||
setupSpaces(r, spaceCtrl, repoCtrl)
|
setupSpaces(r, spaceCtrl)
|
||||||
setupRepos(r, repoCtrl, pullreqCtrl, webhookCtrl, checkCtrl)
|
setupRepos(r, repoCtrl, pullreqCtrl, webhookCtrl, checkCtrl)
|
||||||
setupUser(r, userCtrl)
|
setupUser(r, userCtrl)
|
||||||
setupServiceAccounts(r, saCtrl)
|
setupServiceAccounts(r, saCtrl)
|
||||||
@ -134,7 +134,7 @@ func setupRoutesV1(r chi.Router,
|
|||||||
setupResources(r)
|
setupResources(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupSpaces(r chi.Router, spaceCtrl *space.Controller, repoCtrl *repo.Controller) {
|
func setupSpaces(r chi.Router, spaceCtrl *space.Controller) {
|
||||||
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("/", handlerspace.HandleCreate(spaceCtrl))
|
r.Post("/", handlerspace.HandleCreate(spaceCtrl))
|
||||||
@ -143,7 +143,7 @@ func setupSpaces(r chi.Router, spaceCtrl *space.Controller, repoCtrl *repo.Contr
|
|||||||
// space operations
|
// space operations
|
||||||
r.Get("/", handlerspace.HandleFind(spaceCtrl))
|
r.Get("/", handlerspace.HandleFind(spaceCtrl))
|
||||||
r.Patch("/", handlerspace.HandleUpdate(spaceCtrl))
|
r.Patch("/", handlerspace.HandleUpdate(spaceCtrl))
|
||||||
r.Delete("/", handlerspace.HandleDelete(spaceCtrl, repoCtrl))
|
r.Delete("/", handlerspace.HandleDelete(spaceCtrl))
|
||||||
|
|
||||||
r.Post("/move", handlerspace.HandleMove(spaceCtrl))
|
r.Post("/move", handlerspace.HandleMove(spaceCtrl))
|
||||||
r.Get("/spaces", handlerspace.HandleListSpaces(spaceCtrl))
|
r.Get("/spaces", handlerspace.HandleListSpaces(spaceCtrl))
|
||||||
|
@ -187,7 +187,7 @@ type (
|
|||||||
Count(ctx context.Context, id int64, opts *types.SpaceFilter) (int64, error)
|
Count(ctx context.Context, id int64, opts *types.SpaceFilter) (int64, error)
|
||||||
|
|
||||||
// List returns a list of child spaces in a space.
|
// List returns a list of child spaces in a space.
|
||||||
List(ctx context.Context, id int64, opts *types.SpaceFilter) ([]*types.Space, error)
|
List(ctx context.Context, id int64, opts *types.SpaceFilter) ([]types.Space, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RepoStore defines the repository data storage.
|
// RepoStore defines the repository data storage.
|
||||||
|
@ -399,7 +399,7 @@ func (s *MembershipStore) mapToMembershipSpaces(ctx context.Context,
|
|||||||
res := make([]types.MembershipSpace, len(ms))
|
res := make([]types.MembershipSpace, len(ms))
|
||||||
for i, m := range ms {
|
for i, m := range ms {
|
||||||
res[i].Membership = mapToMembership(&m.membership)
|
res[i].Membership = mapToMembership(&m.membership)
|
||||||
res[i].Space = *mapToSpace(&m.space)
|
res[i].Space = mapToSpace(&m.space)
|
||||||
if addedBy, ok := infoMap[m.membership.CreatedBy]; ok {
|
if addedBy, ok := infoMap[m.membership.CreatedBy]; ok {
|
||||||
res[i].AddedBy = *addedBy
|
res[i].AddedBy = *addedBy
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,9 @@ func (s *SpaceStore) Find(ctx context.Context, id int64) (*types.Space, error) {
|
|||||||
return nil, database.ProcessSQLErrorf(err, "Failed to find space")
|
return nil, database.ProcessSQLErrorf(err, "Failed to find space")
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapToSpace(dst), nil
|
result := mapToSpace(dst)
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindByRef finds the space using the spaceRef as either the id or the space path.
|
// FindByRef finds the space using the spaceRef as either the id or the space path.
|
||||||
@ -113,6 +115,10 @@ func (s *SpaceStore) FindByRef(ctx context.Context, spaceRef string) (*types.Spa
|
|||||||
|
|
||||||
// Create a new space.
|
// Create a new space.
|
||||||
func (s *SpaceStore) Create(ctx context.Context, space *types.Space) error {
|
func (s *SpaceStore) Create(ctx context.Context, space *types.Space) error {
|
||||||
|
if space == nil {
|
||||||
|
return errors.New("space is nil")
|
||||||
|
}
|
||||||
|
|
||||||
const sqlQuery = `
|
const sqlQuery = `
|
||||||
INSERT INTO spaces (
|
INSERT INTO spaces (
|
||||||
space_version
|
space_version
|
||||||
@ -134,14 +140,9 @@ func (s *SpaceStore) Create(ctx context.Context, space *types.Space) error {
|
|||||||
,:space_updated
|
,:space_updated
|
||||||
) RETURNING space_id`
|
) RETURNING space_id`
|
||||||
|
|
||||||
dbSpace, err := mapToInternalSpace(space)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to map space: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
db := dbtx.GetAccessor(ctx, s.db)
|
db := dbtx.GetAccessor(ctx, s.db)
|
||||||
|
|
||||||
query, args, err := db.BindNamed(sqlQuery, dbSpace)
|
query, args, err := db.BindNamed(sqlQuery, mapToInternalSpace(space))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return database.ProcessSQLErrorf(err, "Failed to bind space object")
|
return database.ProcessSQLErrorf(err, "Failed to bind space object")
|
||||||
}
|
}
|
||||||
@ -153,8 +154,12 @@ func (s *SpaceStore) Create(ctx context.Context, space *types.Space) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the space details.
|
// Update updates the space details.
|
||||||
func (s *SpaceStore) Update(ctx context.Context, space *types.Space) error {
|
func (s *SpaceStore) Update(ctx context.Context, space *types.Space) error {
|
||||||
|
if space == nil {
|
||||||
|
return errors.New("space is nil")
|
||||||
|
}
|
||||||
|
|
||||||
const sqlQuery = `
|
const sqlQuery = `
|
||||||
UPDATE spaces
|
UPDATE spaces
|
||||||
SET
|
SET
|
||||||
@ -166,10 +171,7 @@ func (s *SpaceStore) Update(ctx context.Context, space *types.Space) error {
|
|||||||
,space_is_public = :space_is_public
|
,space_is_public = :space_is_public
|
||||||
WHERE space_id = :space_id AND space_version = :space_version - 1`
|
WHERE space_id = :space_id AND space_version = :space_version - 1`
|
||||||
|
|
||||||
dbSpace, err := mapToInternalSpace(space)
|
dbSpace := mapToInternalSpace(space)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to map space: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// update Version (used for optimistic locking) and Updated time
|
// update Version (used for optimistic locking) and Updated time
|
||||||
dbSpace.Version++
|
dbSpace.Version++
|
||||||
@ -205,7 +207,8 @@ func (s *SpaceStore) Update(ctx context.Context, space *types.Space) error {
|
|||||||
// UpdateOptLock updates the space using the optimistic locking mechanism.
|
// UpdateOptLock updates the space using the optimistic locking mechanism.
|
||||||
func (s *SpaceStore) UpdateOptLock(ctx context.Context,
|
func (s *SpaceStore) UpdateOptLock(ctx context.Context,
|
||||||
space *types.Space,
|
space *types.Space,
|
||||||
mutateFn func(space *types.Space) error) (*types.Space, error) {
|
mutateFn func(space *types.Space) error,
|
||||||
|
) (*types.Space, error) {
|
||||||
for {
|
for {
|
||||||
dup := *space
|
dup := *space
|
||||||
|
|
||||||
@ -229,7 +232,7 @@ func (s *SpaceStore) UpdateOptLock(ctx context.Context,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the space.
|
// Delete deletes a space.
|
||||||
func (s *SpaceStore) Delete(ctx context.Context, id int64) error {
|
func (s *SpaceStore) Delete(ctx context.Context, id int64) error {
|
||||||
const sqlQuery = `
|
const sqlQuery = `
|
||||||
DELETE FROM spaces
|
DELETE FROM spaces
|
||||||
@ -271,7 +274,7 @@ func (s *SpaceStore) Count(ctx context.Context, id int64, opts *types.SpaceFilte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List returns a list of spaces under the parent space.
|
// List returns a list of spaces under the parent space.
|
||||||
func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter) ([]*types.Space, error) {
|
func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter) ([]types.Space, error) {
|
||||||
stmt := database.Builder.
|
stmt := database.Builder.
|
||||||
Select(spaceColumnsForJoin).
|
Select(spaceColumnsForJoin).
|
||||||
From("spaces").
|
From("spaces").
|
||||||
@ -310,7 +313,7 @@ func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter
|
|||||||
|
|
||||||
db := dbtx.GetAccessor(ctx, s.db)
|
db := dbtx.GetAccessor(ctx, s.db)
|
||||||
|
|
||||||
dst := []*space{}
|
var dst []*space
|
||||||
if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
|
if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
|
||||||
return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query")
|
return nil, database.ProcessSQLErrorf(err, "Failed executing custom list query")
|
||||||
}
|
}
|
||||||
@ -318,8 +321,8 @@ func (s *SpaceStore) List(ctx context.Context, id int64, opts *types.SpaceFilter
|
|||||||
return mapToSpaces(dst), nil
|
return mapToSpaces(dst), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapToSpace(s *space) *types.Space {
|
func mapToSpace(s *space) types.Space {
|
||||||
res := &types.Space{
|
res := types.Space{
|
||||||
ID: s.ID,
|
ID: s.ID,
|
||||||
Version: s.Version,
|
Version: s.Version,
|
||||||
UID: s.UID,
|
UID: s.UID,
|
||||||
@ -339,21 +342,16 @@ func mapToSpace(s *space) *types.Space {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapToSpaces(spaces []*space) []*types.Space {
|
func mapToSpaces(spaces []*space) []types.Space {
|
||||||
res := make([]*types.Space, len(spaces))
|
res := make([]types.Space, len(spaces))
|
||||||
for i := range spaces {
|
for i := range spaces {
|
||||||
res[i] = mapToSpace(spaces[i])
|
res[i] = mapToSpace(spaces[i])
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapToInternalSpace(s *types.Space) (*space, error) {
|
func mapToInternalSpace(s *types.Space) space {
|
||||||
// space comes from outside.
|
res := space{
|
||||||
if s == nil {
|
|
||||||
return nil, fmt.Errorf("space is nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
res := &space{
|
|
||||||
ID: s.ID,
|
ID: s.ID,
|
||||||
Version: s.Version,
|
Version: s.Version,
|
||||||
UID: s.UID,
|
UID: s.UID,
|
||||||
@ -371,5 +369,5 @@ func mapToInternalSpace(s *types.Space) (*space, error) {
|
|||||||
res.ParentID = null.IntFrom(s.ParentID)
|
res.ParentID = null.IntFrom(s.ParentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res
|
||||||
}
|
}
|
||||||
|
@ -529,10 +529,10 @@ func (mr *MockSpaceStoreMockRecorder) FindByRef(arg0, arg1 interface{}) *gomock.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List mocks base method.
|
// List mocks base method.
|
||||||
func (m *MockSpaceStore) List(arg0 context.Context, arg1 int64, arg2 *types.SpaceFilter) ([]*types.Space, error) {
|
func (m *MockSpaceStore) List(arg0 context.Context, arg1 int64, arg2 *types.SpaceFilter) ([]types.Space, error) {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
ret := m.ctrl.Call(m, "List", arg0, arg1, arg2)
|
ret := m.ctrl.Call(m, "List", arg0, arg1, arg2)
|
||||||
ret0, _ := ret[0].([]*types.Space)
|
ret0, _ := ret[0].([]types.Space)
|
||||||
ret1, _ := ret[1].(error)
|
ret1, _ := ret[1].(error)
|
||||||
return ret0, ret1
|
return ret0, ret1
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ func TestParseUserAttr(t *testing.T) {
|
|||||||
text string
|
text string
|
||||||
want UserAttr
|
want UserAttr
|
||||||
}{
|
}{
|
||||||
{"id", UserAttrUID},
|
{"uid", UserAttrUID},
|
||||||
{"name", UserAttrName},
|
{"name", UserAttrName},
|
||||||
{"email", UserAttrEmail},
|
{"email", UserAttrEmail},
|
||||||
{"created", UserAttrCreated},
|
{"created", UserAttrCreated},
|
||||||
|
Loading…
Reference in New Issue
Block a user