Skip to content

Commit 62076a3

Browse files
committed
Add repo type option to /api/repo/search
1 parent ab5d7cb commit 62076a3

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

models/repo_list.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,28 @@ type SearchRepoOptions struct {
109109
// maximum: setting.ExplorePagingNum
110110
// in: query
111111
PageSize int `json:"limit"` // Can be smaller than or equal to setting.ExplorePagingNum
112+
// Type of repository to search (related to owner if present)
113+
//
114+
// in: query
115+
RepoType RepoType `json:"type"`
112116
}
113117

118+
// RepoType is repository filtering type identifier
119+
type RepoType string
120+
121+
const (
122+
// RepoTypeAny any type (default)
123+
RepoTypeAny RepoType = ""
124+
// RepoTypeFork fork type
125+
RepoTypeFork = "FORK"
126+
// RepoTypeMirror mirror type
127+
RepoTypeMirror = "MIRROR"
128+
// RepoTypeSource source type
129+
RepoTypeSource = "SOURCE"
130+
// RepoTypeCollaborative collaborative type
131+
RepoTypeCollaborative = "COLLABORATIVE"
132+
)
133+
114134
//SearchOrderBy is used to sort the result
115135
type SearchOrderBy string
116136

@@ -172,11 +192,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
172192
}
173193
} else {
174194
// Set user access conditions
195+
var accessCond builder.Cond = builder.NewCond()
196+
175197
// Add Owner ID to access conditions
176-
var accessCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
198+
if opts.RepoType != RepoTypeCollaborative {
199+
accessCond = accessCond.Or(builder.Eq{"owner_id": opts.OwnerID})
200+
}
177201

178202
// Include collaborative repositories
179-
if opts.Collaborate {
203+
if opts.Collaborate && (opts.RepoType == RepoTypeAny || opts.RepoType == RepoTypeMirror || opts.RepoType == RepoTypeCollaborative) {
180204
// Add repositories where user is set as collaborator directly
181205
accessCond = accessCond.Or(builder.And(
182206
builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID),
@@ -192,6 +216,18 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
192216
opts.OrderBy = SearchOrderByAlphabetically
193217
}
194218

219+
// Add general filters for repository
220+
if opts.RepoType != RepoTypeAny {
221+
cond = cond.And(builder.Eq{"is_mirror": opts.RepoType == RepoTypeMirror})
222+
223+
switch opts.RepoType {
224+
case RepoTypeFork:
225+
cond = cond.And(builder.Eq{"is_fork": true})
226+
case RepoTypeSource:
227+
cond = cond.And(builder.Eq{"is_fork": false})
228+
}
229+
}
230+
195231
sess := x.NewSession()
196232
defer sess.Close()
197233

public/swagger.v1.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,13 @@
11131113
"description": "Limit of result\n\nmaximum: setting.ExplorePagingNum",
11141114
"name": "limit",
11151115
"in": "query"
1116+
},
1117+
{
1118+
"type": "string",
1119+
"x-go-name": "RepoType",
1120+
"description": "Type of repository to search (related to owner if present)",
1121+
"name": "type",
1122+
"in": "query"
11161123
}
11171124
],
11181125
"responses": {

routers/api/v1/repo/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ func Search(ctx *context.APIContext) {
2828
// Responses:
2929
// 200: SearchResults
3030
// 500: SearchError
31+
repoTypes := map[string]models.RepoType{
32+
"fork": models.RepoTypeFork,
33+
"mirror": models.RepoTypeMirror,
34+
"source": models.RepoTypeSource,
35+
"collaborative": models.RepoTypeCollaborative,
36+
}
3137

3238
opts := &models.SearchRepoOptions{
3339
Keyword: strings.Trim(ctx.Query("q"), " "),
3440
OwnerID: ctx.QueryInt64("uid"),
3541
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
42+
RepoType: repoTypes[ctx.Query("type")],
3643
}
3744

3845
// Include collaborative and private repositories

0 commit comments

Comments
 (0)