projects = Project.where(id: project_ids)
result[:projects] = projects.search(query).limit(20)
- # Search inside singe project
+ # Search inside single project
project = projects.first if projects.length == 1
if params[:search_code].present?
post do
required_attributes! [:name]
attrs = attributes_for_keys [:name,
- :path,
- :description,
- :default_branch,
- :issues_enabled,
- :wall_enabled,
- :merge_requests_enabled,
- :wiki_enabled,
- :snippets_enabled,
- :namespace_id,
- :public]
+ :path,
+ :description,
+ :default_branch,
+ :issues_enabled,
+ :wall_enabled,
+ :merge_requests_enabled,
+ :wiki_enabled,
+ :snippets_enabled,
+ :namespace_id,
+ :public]
@project = ::Projects::CreateContext.new(current_user, attrs).execute
if @project.saved?
present @project, with: Entities::Project
authenticated_as_admin!
user = User.find(params[:user_id])
attrs = attributes_for_keys [:name,
- :description,
- :default_branch,
- :issues_enabled,
- :wall_enabled,
- :merge_requests_enabled,
- :wiki_enabled,
- :snippets_enabled,
- :public]
+ :description,
+ :default_branch,
+ :issues_enabled,
+ :wall_enabled,
+ :merge_requests_enabled,
+ :wiki_enabled,
+ :snippets_enabled,
+ :public]
@project = ::Projects::CreateContext.new(user, attrs).execute
if @project.saved?
present @project, with: Entities::Project
end
end
-
# Get a project team members
#
# Parameters:
{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, defaults to 20
+ # offset (optional) - the offset in pages to retrieve
+ # Example Request:
+ # GET /projects/search/:query
+ get "/search/:query" do
+ limit = (params[:per_page] || 20).to_i
+ offset = (params[:page] || 0).to_i * limit
+ ids = current_user.authorized_projects.map(&:id)
+ projects = Project.where("(id in (?) OR public = true) AND (name LIKE (?))", ids, "%#{params[:query]}%").limit(limit).offset(offset)
+ present projects, with: Entities::Project
+ end
end
end
end
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