Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
iv
gitlab-ce
Commits
1165759b
Commit
1165759b
authored
Sep 25, 2013
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5146 from karlhungus/feature-api-search-for-projects-by-name
Added search for projects by name to api
parents
203bc7bb
04c7c262
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
86 additions
and
20 deletions
+86
-20
app/contexts/search_context.rb
app/contexts/search_context.rb
+1
-1
doc/api/projects.md
doc/api/projects.md
+15
-0
lib/api/projects.rb
lib/api/projects.rb
+32
-19
spec/requests/api/projects_spec.rb
spec/requests/api/projects_spec.rb
+38
-0
No files found.
app/contexts/search_context.rb
View file @
1165759b
...
...
@@ -13,7 +13,7 @@ class SearchContext
projects
=
Project
.
where
(
id:
project_ids
)
result
[
:projects
]
=
projects
.
search
(
query
).
limit
(
20
)
# Search inside singe project
# Search inside sing
l
e project
project
=
projects
.
first
if
projects
.
length
==
1
if
params
[
:search_code
].
present?
...
...
doc/api/projects.md
View file @
1165759b
...
...
@@ -484,3 +484,18 @@ DELETE /projects/:id/fork
Parameter:
+
`id`
(required) - The ID of the project
## Search for projects by name
Search for projects by name which are public or the calling user has access to
```
GET /projects/search/:query
```
Parameters:
+
query (required) - A string contained in the project name
+
per_page (optional) - number of projects to return per page
+
page (optional) - the page to retrieve
lib/api/projects.rb
View file @
1165759b
...
...
@@ -165,7 +165,6 @@ module API
end
end
# Get a project team members
#
# Parameters:
...
...
@@ -262,6 +261,20 @@ module API
{
message:
"Access revoked"
,
id:
params
[
:user_id
].
to_i
}
end
end
# search for projects current_user has access to
#
# Parameters:
# query (required) - A string contained in the project name
# per_page (optional) - number of projects to return per page
# page (optional) - the page to retrieve
# Example Request:
# GET /projects/search/:query
get
"/search/:query"
do
ids
=
current_user
.
authorized_projects
.
map
(
&
:id
)
projects
=
Project
.
where
(
"(id in (?) OR public = true) AND (name LIKE (?))"
,
ids
,
"%
#{
params
[
:query
]
}
%"
)
present
paginate
(
projects
),
with:
Entities
::
Project
end
end
end
end
spec/requests/api/projects_spec.rb
View file @
1165759b
...
...
@@ -692,4 +692,42 @@ describe API::API do
end
end
end
describe
"GET /projects/search/:query"
do
let!
(
:query
)
{
'query'
}
let!
(
:search
)
{
create
(
:project
,
name:
query
,
creator_id:
user
.
id
,
namespace:
user
.
namespace
)
}
let!
(
:pre
)
{
create
(
:project
,
name:
"pre_
#{
query
}
"
,
creator_id:
user
.
id
,
namespace:
user
.
namespace
)
}
let!
(
:post
)
{
create
(
:project
,
name:
"
#{
query
}
_post"
,
creator_id:
user
.
id
,
namespace:
user
.
namespace
)
}
let!
(
:pre_post
)
{
create
(
:project
,
name:
"pre_
#{
query
}
_post"
,
creator_id:
user
.
id
,
namespace:
user
.
namespace
)
}
let!
(
:unfound
)
{
create
(
:project
,
name:
'unfound'
,
creator_id:
user
.
id
,
namespace:
user
.
namespace
)
}
let!
(
:public
)
{
create
(
:project
,
name:
"another
#{
query
}
"
,
public:
true
)
}
let!
(
:unfound_public
)
{
create
(
:project
,
name:
'unfound public'
,
public:
true
)
}
context
"when unauthenticated"
do
it
"should return authentication error"
do
get
api
(
"/projects/search/
#{
query
}
"
)
response
.
status
.
should
==
401
end
end
context
"when authenticated"
do
it
"should return an array of projects"
do
get
api
(
"/projects/search/
#{
query
}
"
,
user
)
response
.
status
.
should
==
200
json_response
.
should
be_an
Array
json_response
.
size
.
should
==
5
json_response
.
each
{
|
project
|
project
[
'name'
].
should
=~
/.*query.*/
}
end
end
context
"when authenticated as a different user"
do
it
"should return matching public projects"
do
get
api
(
"/projects/search/
#{
query
}
"
,
user2
)
response
.
status
.
should
==
200
json_response
.
should
be_an
Array
json_response
.
size
.
should
==
1
json_response
.
first
[
'name'
].
should
==
"another
#{
query
}
"
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment