Skip to content

Commit 5a09a0c

Browse files
Add admin repos API (#601)
* Add admin repos API * Fix swagger API * Fix mock
1 parent 0115000 commit 5a09a0c

File tree

10 files changed

+270
-16
lines changed

10 files changed

+270
-16
lines changed

_mocks/opencsg.com/csghub-server/aigateway/token/mock_CounterFactory.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_mocks/opencsg.com/csghub-server/builder/store/database/mock_RepoStore.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_mocks/opencsg.com/csghub-server/component/mock_RepoComponent.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/handler/repo.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,3 +3015,40 @@ func (h *RepoHandler) ChangePath(ctx *gin.Context) {
30153015
}
30163016
httpbase.OK(ctx, nil)
30173017
}
3018+
3019+
// GetRepos godoc
3020+
// @Security ApiKey
3021+
// @Summary Get repo paths with search query
3022+
// @Tags Repository
3023+
// @Accept json
3024+
// @Produce json
3025+
// @Param current_user query string false "current user name"
3026+
// @Param search query string true "search query"
3027+
// @Param type query string true "repository type query" enums(model, dataset, code, space, mcpserver)
3028+
// @Success 200 {object} types.Response{data=[]string} "OK"
3029+
// @Failure 400 {object} types.APIBadRequest "Bad request"
3030+
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
3031+
// @Router /repos [get]
3032+
func (h *RepoHandler) GetRepos(ctx *gin.Context) {
3033+
currentUser := httpbase.GetCurrentUser(ctx)
3034+
search := ctx.Query("search")
3035+
repositoryType := ctx.Query("type")
3036+
repoType := types.RepositoryType(repositoryType)
3037+
if repoType == types.UnknownRepo {
3038+
httpbase.BadRequest(ctx, "Unknown repository type")
3039+
return
3040+
}
3041+
3042+
repos, err := h.c.GetRepos(ctx.Request.Context(), search, currentUser, repoType)
3043+
if err != nil {
3044+
slog.Error(
3045+
"Failed to get repos",
3046+
slog.Any("error", err))
3047+
httpbase.ServerError(ctx, err)
3048+
return
3049+
}
3050+
slog.Debug(
3051+
"Get repos succeed",
3052+
slog.String("search", search))
3053+
httpbase.OK(ctx, repos)
3054+
}

api/handler/repo_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,15 @@ func TestRepoHandler_CommitFiles(t *testing.T) {
15281528
t, 200, tester.OKText, nil,
15291529
)
15301530
}
1531+
1532+
func TestRepoHandler_GetRepos(t *testing.T) {
1533+
tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc {
1534+
return rp.GetRepos
1535+
})
1536+
tester.mocks.repo.EXPECT().GetRepos(mock.Anything, "search", "u", types.ModelRepo).Return([]string{}, nil).Once()
1537+
tester.WithQuery("type", "model").WithQuery("search", "search").WithUser().Execute()
1538+
1539+
tester.ResponseEq(
1540+
t, 200, tester.OKText, []string{},
1541+
)
1542+
}

api/router/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func NewRouter(config *config.Config, enableSwagger bool) (*gin.Engine, error) {
188188
versionHandler := handler.NewVersionHandler()
189189
apiGroup.GET("/version", versionHandler.Version)
190190

191+
// Admin user get repo path list
192+
apiGroup.GET("/repos", middlewareCollection.Auth.NeedAdmin, repoCommonHandler.GetRepos)
193+
191194
// TODO:use middleware to handle common response
192195
//
193196
memoryStore := persist.NewMemoryStore(1 * time.Minute)

builder/store/database/repository.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type RepoStore interface {
9595
FindByRepoTypeAndPaths(ctx context.Context, repoType types.RepositoryType, path []string) ([]Repository, error)
9696
FindUnhashedRepos(ctx context.Context, batchSize int, lastID int64) ([]Repository, error)
9797
UpdateRepoSensitiveCheckStatus(ctx context.Context, repoID int64, status types.SensitiveCheckStatus) error
98+
GetReposBySearch(ctx context.Context, search string, repoType types.RepositoryType, page, pageSize int) ([]*Repository, int, error)
9899
}
99100

100101
func (s *repoStoreImpl) UpdateRepoSensitiveCheckStatus(ctx context.Context, repoID int64, status types.SensitiveCheckStatus) error {
@@ -1395,3 +1396,18 @@ func (s *repoStoreImpl) FindUnhashedRepos(ctx context.Context, batchSize int, la
13951396
Scan(ctx)
13961397
return res, err
13971398
}
1399+
1400+
func (s *repoStoreImpl) GetReposBySearch(ctx context.Context, search string, repoType types.RepositoryType, page, pageSize int) ([]*Repository, int, error) {
1401+
var (
1402+
res []*Repository
1403+
count int
1404+
err error
1405+
)
1406+
count, err = s.db.Operator.Core.NewSelect().
1407+
Model(&res).
1408+
Where("path like ? and repository_type = ?", fmt.Sprintf("%%%s%%", search), repoType).
1409+
Offset((page - 1) * pageSize).
1410+
Limit(pageSize).
1411+
ScanAndCount(ctx)
1412+
return res, count, err
1413+
}

builder/store/database/repository_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,3 +1821,33 @@ func TestRepoStore_UpdateRepoSensitiveCheckStatus(t *testing.T) {
18211821
require.Nil(t, err)
18221822
require.Equal(t, types.SensitiveCheckPass, rp.SensitiveCheckStatus)
18231823
}
1824+
1825+
func TestRepoStore_GetReposBySearch(t *testing.T) {
1826+
db := tests.InitTestDB()
1827+
defer db.Close()
1828+
ctx := context.TODO()
1829+
1830+
// insert a new repo
1831+
rs := database.NewRepoStoreWithDB(db)
1832+
for i := 0; i < 10; i++ {
1833+
_, err := db.Operator.Core.NewInsert().Model(&database.Repository{
1834+
UserID: 1,
1835+
Path: fmt.Sprintf("%s/%d", "path", i),
1836+
GitPath: fmt.Sprintf("datasets_%s/%d", "path", i),
1837+
Name: fmt.Sprintf("name_%d", i),
1838+
DefaultBranch: "main",
1839+
Nickname: "ww",
1840+
Description: "ww",
1841+
Private: false,
1842+
RepositoryType: types.DatasetRepo,
1843+
Source: "opencsg",
1844+
Hashed: false,
1845+
}).Exec(ctx)
1846+
require.Nil(t, err)
1847+
}
1848+
1849+
repos, total, err := rs.GetReposBySearch(ctx, "path/1", types.DatasetRepo, 1, 10)
1850+
require.Nil(t, err)
1851+
require.NotNil(t, repos)
1852+
require.Equal(t, 1, total)
1853+
}

component/repo.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type RepoComponent interface {
190190
BatchMigrateRepoToHashedPath(ctx context.Context, auto bool, batchSize int, lastID int64) (int64, error)
191191
GetMirrorTaskStatusAndSyncStatus(repo *database.Repository) (types.MirrorTaskStatus, types.RepositorySyncStatus)
192192
CheckDeployPermissionForUser(ctx context.Context, deployReq types.DeployActReq) (*database.User, *database.Deploy, error)
193+
GetRepos(ctx context.Context, search, currentUser string, repoType types.RepositoryType) ([]string, error)
193194
IsXnetEnabled(ctx context.Context, repoType types.RepositoryType, namespace, name, username string) (*types.XetEnabled, error)
194195
}
195196

@@ -4073,3 +4074,15 @@ func (c *repoComponentImpl) GetMirrorTaskStatusAndSyncStatus(repo *database.Repo
40734074
func (c *repoComponentImpl) RandomPath() []string {
40744075
return strings.SplitN(uuid.NewString(), "-", 2)
40754076
}
4077+
4078+
func (c *repoComponentImpl) GetRepos(ctx context.Context, search, currentUser string, repoType types.RepositoryType) ([]string, error) {
4079+
var repoPaths []string
4080+
repos, _, err := c.repoStore.GetReposBySearch(ctx, search, repoType, 1, 10)
4081+
if err != nil {
4082+
return repoPaths, fmt.Errorf("failed to get repos, error: %w", err)
4083+
}
4084+
for _, repo := range repos {
4085+
repoPaths = append(repoPaths, repo.Path)
4086+
}
4087+
return repoPaths, nil
4088+
}

component/repo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,3 +3244,16 @@ func TestRepoComponent_SendAssetManagementMsg(t *testing.T) {
32443244
require.Nil(t, err)
32453245
wg.Wait()
32463246
}
3247+
3248+
func TestRepoComponent_GetRepos(t *testing.T) {
3249+
ctx := context.Background()
3250+
3251+
repoComp := initializeTestRepoComponent(ctx, t)
3252+
3253+
repoComp.mocks.stores.RepoMock().EXPECT().GetReposBySearch(ctx, "search", types.ModelRepo, 1, 10).
3254+
Return([]*database.Repository{{ID: 1, Path: "ns/name"}}, 1, nil).Once()
3255+
paths, err := repoComp.GetRepos(ctx, "search", "u", types.ModelRepo)
3256+
require.NoError(t, err)
3257+
require.Equal(t, 1, len(paths))
3258+
require.Equal(t, "ns/name", paths[0])
3259+
}

0 commit comments

Comments
 (0)