Skip to content

Commit 96eaf52

Browse files
committed
Make search api more general
1 parent e2c10a0 commit 96eaf52

File tree

3 files changed

+33
-47
lines changed

3 files changed

+33
-47
lines changed

integrations/api_repo_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,18 @@ func TestAPISearchRepo(t *testing.T) {
119119
nil: {count: 1},
120120
user: {count: 1},
121121
user4: {count: 2, includesPrivate: true}}},
122+
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork/Exclusive", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "fork"), expectedResults: expectedResults{
123+
nil: {count: 1},
124+
user: {count: 1},
125+
user4: {count: 2, includesPrivate: true}}},
122126
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "mirror"), expectedResults: expectedResults{
123127
nil: {count: 2},
124128
user: {count: 2},
125129
user4: {count: 4, includesPrivate: true}}},
130+
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeMirror/Exclusive", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s&exclusive=1", user4.ID, "mirror"), expectedResults: expectedResults{
131+
nil: {count: 1},
132+
user: {count: 1},
133+
user4: {count: 2, includesPrivate: true}}},
126134
{name: "RepositoriesAccessibleAndRelatedToUser4/SearchModeCollaborative", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d&mode=%s", user4.ID, "collaborative"), expectedResults: expectedResults{
127135
nil: {count: 0},
128136
user: {count: 0},

public/swagger.v1.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,13 @@
11201120
"description": "Type of repository to search, related to owner",
11211121
"name": "mode",
11221122
"in": "query"
1123+
},
1124+
{
1125+
"type": "boolean",
1126+
"x-go-name": "OwnerExclusive",
1127+
"description": "Search only owners repositories\nHas effect only if owner is provided and mode is not \"collaborative\"",
1128+
"name": "exclusive",
1129+
"in": "query"
11231130
}
11241131
],
11251132
"responses": {

routers/api/v1/repo/repo.go

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,13 @@ type SearchRepoOption struct { // TODO: Move SearchRepoOption to Gitea SDK
4040
//
4141
// in: query
4242
SearchMode string `json:"mode"`
43+
// Search only owners repositories
44+
// Has effect only if owner is provided and mode is not "collaborative"
45+
//
46+
// in: query
47+
OwnerExclusive bool `json:"exclusive"`
4348
}
4449

45-
// searchMode is repository filtering mode identifier
46-
type searchMode int
47-
48-
const (
49-
searchModeAny searchMode = iota + 1
50-
searchModeFork
51-
searchModeMirror
52-
searchModeSource
53-
searchModeCollaborative
54-
)
55-
5650
// Search repositories via options
5751
func Search(ctx *context.APIContext) {
5852
// swagger:route GET /repos/search repository repoSearch
@@ -69,35 +63,32 @@ func Search(ctx *context.APIContext) {
6963
Keyword: strings.Trim(ctx.Query("q"), " "),
7064
OwnerID: ctx.QueryInt64("uid"),
7165
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
72-
Collaborate: util.OptionalBoolFalse,
66+
Collaborate: util.OptionalBoolNone,
7367
}
7468

75-
var modeQuery = ctx.Query("mode")
76-
var mode searchMode
77-
switch modeQuery {
69+
if ctx.QueryBool("exclusive") {
70+
opts.Collaborate = util.OptionalBoolFalse
71+
}
72+
73+
var mode = ctx.Query("mode")
74+
switch mode {
7875
case "source":
79-
mode = searchModeSource
8076
opts.Fork = util.OptionalBoolFalse
8177
opts.Mirror = util.OptionalBoolFalse
8278
case "fork":
83-
mode = searchModeFork
8479
opts.Fork = util.OptionalBoolTrue
8580
case "mirror":
86-
mode = searchModeMirror
8781
opts.Mirror = util.OptionalBoolTrue
8882
case "collaborative":
89-
mode = searchModeCollaborative
9083
opts.Mirror = util.OptionalBoolFalse
84+
opts.Collaborate = util.OptionalBoolTrue
9185
case "":
92-
mode = searchModeAny
93-
}
94-
95-
err := validateSearchInput(opts.OwnerID, modeQuery, mode)
96-
if err != nil {
97-
ctx.Error(http.StatusUnprocessableEntity, "", err)
86+
default:
87+
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid search mode: \"%s\"", mode))
9888
return
9989
}
10090

91+
var err error
10192
if opts.OwnerID > 0 {
10293
var repoOwner *models.User
10394
if ctx.User != nil && ctx.User.ID == opts.OwnerID {
@@ -113,12 +104,8 @@ func Search(ctx *context.APIContext) {
113104
}
114105
}
115106

116-
if !repoOwner.IsOrganization() {
117-
if mode == searchModeCollaborative {
118-
opts.Collaborate = util.OptionalBoolTrue
119-
} else if mode != searchModeSource && mode != searchModeFork {
120-
opts.Collaborate = util.OptionalBoolNone
121-
}
107+
if repoOwner.IsOrganization() {
108+
opts.Collaborate = util.OptionalBoolFalse
122109
}
123110

124111
// Check visibility.
@@ -168,22 +155,6 @@ func Search(ctx *context.APIContext) {
168155
})
169156
}
170157

171-
func validateSearchInput(ownerID int64, modeQuery string, mode searchMode) error {
172-
var errors []string
173-
if mode == 0 {
174-
errors = append(errors, fmt.Sprintf("Invalid search mode: \"%s\"", modeQuery))
175-
}
176-
177-
if ownerID <= 0 && (mode == searchModeFork || mode == searchModeSource) {
178-
errors = append(errors, fmt.Sprintf("Invalid combination of input params: \"mode=%s\" has to be combined with \"uid\"", modeQuery))
179-
}
180-
181-
if len(errors) > 0 {
182-
return fmt.Errorf(strings.Join(errors, " "))
183-
}
184-
return nil
185-
}
186-
187158
// CreateUserRepo create a repository for a user
188159
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
189160
repo, err := models.CreateRepository(ctx.User, owner, models.CreateRepoOptions{

0 commit comments

Comments
 (0)