mirror of https://github.com/harness/drone.git
feat: [CODE-2862]: support integer ref for repo finder (#3205)
parent
887379b94a
commit
3d5feb0315
|
@ -17,6 +17,7 @@ package refcache
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/harness/gitness/app/paths"
|
||||
"github.com/harness/gitness/app/store"
|
||||
|
@ -39,6 +40,15 @@ func NewRepoFinder(
|
|||
}
|
||||
|
||||
func (r RepoFinder) FindByRef(ctx context.Context, repoRef string) (*types.Repository, error) {
|
||||
if id, err := strconv.ParseInt(repoRef, 10, 64); err == nil && id > 0 {
|
||||
repo, err := r.repoStore.Find(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get repository by ID: %w", err)
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
spacePath, repoIdentifier, err := paths.DisectLeaf(repoRef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to disect extract repo idenfifier from path: %w", err)
|
||||
|
@ -54,10 +64,21 @@ func (r RepoFinder) FindByRef(ctx context.Context, repoRef string) (*types.Repos
|
|||
return nil, fmt.Errorf("failed to get repository by parent space ID and UID: %w", err)
|
||||
}
|
||||
|
||||
repo.Version = -1 // destroy the repo version so that it can't be used for update
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
func (r RepoFinder) FindDeletedByRef(ctx context.Context, repoRef string, deleted int64) (*types.Repository, error) {
|
||||
if id, err := strconv.ParseInt(repoRef, 10, 64); err == nil && id > 0 {
|
||||
repo, err := r.repoStore.FindDeleted(ctx, id, &deleted)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get repository by ID: %w", err)
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
spacePath, repoIdentifier, err := paths.DisectLeaf(repoRef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to disect extract repo idenfifier from path: %w", err)
|
||||
|
@ -73,5 +94,7 @@ func (r RepoFinder) FindDeletedByRef(ctx context.Context, repoRef string, delete
|
|||
return nil, fmt.Errorf("failed to get deleted repository by parent space ID and UID: %w", err)
|
||||
}
|
||||
|
||||
repo.Version = -1 // destroy the repo version so that it can't be used for update
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
|
|
@ -90,5 +90,7 @@ func (g pathToSpaceCacheGetter) Find(ctx context.Context, spaceRef string) (*typ
|
|||
return nil, fmt.Errorf("failed to find space by id: %w", err)
|
||||
}
|
||||
|
||||
space.Version = -1 // destroy the space version so that it can't be used for update
|
||||
|
||||
return space, nil
|
||||
}
|
||||
|
|
|
@ -241,6 +241,9 @@ type (
|
|||
// Find the repo by id.
|
||||
Find(ctx context.Context, id int64) (*types.Repository, error)
|
||||
|
||||
// FindDeleted the deleted repo by id.
|
||||
FindDeleted(ctx context.Context, id int64, deleted *int64) (*types.Repository, error)
|
||||
|
||||
// FindActiveByUID finds a non-deleted repo by UID.
|
||||
FindActiveByUID(ctx context.Context, parentSpaceID int64, uid string) (*types.Repository, error)
|
||||
|
||||
|
|
|
@ -117,11 +117,11 @@ const (
|
|||
|
||||
// Find finds the repo by id.
|
||||
func (s *RepoStore) Find(ctx context.Context, id int64) (*types.Repository, error) {
|
||||
return s.find(ctx, id, nil)
|
||||
return s.FindDeleted(ctx, id, nil)
|
||||
}
|
||||
|
||||
// find is a wrapper to find a repo by id w/o deleted timestamp.
|
||||
func (s *RepoStore) find(ctx context.Context, id int64, deletedAt *int64) (*types.Repository, error) {
|
||||
// FindDeleted finds a repo by id and deleted timestamp.
|
||||
func (s *RepoStore) FindDeleted(ctx context.Context, id int64, deletedAt *int64) (*types.Repository, error) {
|
||||
stmt := database.Builder.
|
||||
Select(repoColumnsForJoin).
|
||||
From("repositories").
|
||||
|
@ -437,7 +437,7 @@ func (s *RepoStore) updateOptLock(
|
|||
return nil, err
|
||||
}
|
||||
|
||||
repo, err = s.find(ctx, repo.ID, repo.Deleted)
|
||||
repo, err = s.FindDeleted(ctx, repo.ID, repo.Deleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue