From 4cfbfbb845680f2df67110ca15f2479cba8e2441 Mon Sep 17 00:00:00 2001 From: Izaak Alpert Date: Sun, 22 Sep 2013 00:50:18 -0400 Subject: Added search for projects by name to api GITLAB-1283 (GITLAB-869) Change-Id: I611e7e93f6292de08e1edc8d3ea77cf9087b6ded Conflicts: config/initializers/1_settings.rb --- app/contexts/search_context.rb | 2 +- lib/api/projects.rb | 53 ++++++++++++++++++++++++-------------- spec/requests/api/projects_spec.rb | 38 +++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/app/contexts/search_context.rb b/app/contexts/search_context.rb index 0817d52cd00..48def0784fd 100644 --- a/app/contexts/search_context.rb +++ b/app/contexts/search_context.rb @@ -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 single project project = projects.first if projects.length == 1 if params[:search_code].present? diff --git a/lib/api/projects.rb b/lib/api/projects.rb index c6ff524cb06..d17b791dccc 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -73,16 +73,16 @@ module API 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 @@ -113,14 +113,14 @@ module API 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 @@ -165,7 +165,6 @@ module API end end - # Get a project team members # # Parameters: @@ -262,6 +261,22 @@ 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, 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 diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 2de3dc55e40..b8c0b6f33ed 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -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 -- cgit v1.2.1 From 9d06921ffb2082cdcb6bcce3e420d53011a3b71d Mon Sep 17 00:00:00 2001 From: Izaak Alpert Date: Mon, 23 Sep 2013 19:18:29 -0400 Subject: Added missing API documentation Change-Id: I1337ee7ff51d018d6f62d447345032597e84269f --- doc/api/projects.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/api/projects.md b/doc/api/projects.md index 9afffcbaa80..380f6f21ce8 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -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, defaults to 20 ++ offset (optional) - the offset in pages to retrieve -- cgit v1.2.1 From 04c7c262a7be469a4ae4865464b003c4b522a895 Mon Sep 17 00:00:00 2001 From: Izaak Alpert Date: Tue, 24 Sep 2013 09:22:46 -0400 Subject: Used pagnation function from api helpers Change-Id: I1bdd3608d3b46924b5da3ae282c99f85ee4e0dab --- doc/api/projects.md | 4 ++-- lib/api/projects.rb | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/api/projects.md b/doc/api/projects.md index 380f6f21ce8..5150331e7d7 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -497,5 +497,5 @@ GET /projects/search/:query 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 ++ per_page (optional) - number of projects to return per page ++ page (optional) - the page to retrieve diff --git a/lib/api/projects.rb b/lib/api/projects.rb index d17b791dccc..cf357b23c40 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -266,16 +266,14 @@ module API # # 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 + # 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 - 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 + projects = Project.where("(id in (?) OR public = true) AND (name LIKE (?))", ids, "%#{params[:query]}%") + present paginate(projects), with: Entities::Project end end end -- cgit v1.2.1