From 8a7ff5c6b80a0f05526eb460adbbe40c2ae07cb0 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 13 Oct 2015 16:52:15 +0200 Subject: [PATCH 1/4] v3: add sorting to builds --- lib/travis/api/v3/queries/builds.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/travis/api/v3/queries/builds.rb b/lib/travis/api/v3/queries/builds.rb index fbd7e08e..ccb6f0fe 100644 --- a/lib/travis/api/v3/queries/builds.rb +++ b/lib/travis/api/v3/queries/builds.rb @@ -3,8 +3,10 @@ module Travis::API::V3 params :state, :event_type, :previous_state, prefix: :build params :name, prefix: :branch, method_name: :branch_name + sortable_by :id, :started_at, :finished_at + def find(repository) - filter(repository.builds) + sort filter(repository.builds) end def filter(list) From 2ebfa1d85bb2958a21b9a87ad0a0940132b693cd Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 13 Oct 2015 17:09:18 +0200 Subject: [PATCH 2/4] v3: add pagination and sorting to orgs endpoint --- lib/travis/api/v3/queries/organizations.rb | 4 +++- .../services/organizations/for_current_user.rb | 2 ++ spec/v3/service_index_spec.rb | 2 +- .../organizations/for_current_user_spec.rb | 18 +++++++++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/travis/api/v3/queries/organizations.rb b/lib/travis/api/v3/queries/organizations.rb index faa3f68b..29b50a39 100644 --- a/lib/travis/api/v3/queries/organizations.rb +++ b/lib/travis/api/v3/queries/organizations.rb @@ -1,7 +1,9 @@ module Travis::API::V3 class Queries::Organizations < Query + sortable_by :id, :login, :name, :github_id + def for_member(user) - Models::Organization.joins(:users).where(users: user_condition(user)) + sort Models::Organization.joins(:users).where(users: user_condition(user)) end end end diff --git a/lib/travis/api/v3/services/organizations/for_current_user.rb b/lib/travis/api/v3/services/organizations/for_current_user.rb index b99af28f..dcc52ad1 100644 --- a/lib/travis/api/v3/services/organizations/for_current_user.rb +++ b/lib/travis/api/v3/services/organizations/for_current_user.rb @@ -1,5 +1,7 @@ module Travis::API::V3 class Services::Organizations::ForCurrentUser < Service + paginate(default_limit: 100) + def run! raise LoginRequired unless access_control.logged_in? query.for_member(access_control.user) diff --git a/spec/v3/service_index_spec.rb b/spec/v3/service_index_spec.rb index 1d430b6e..d46784ae 100644 --- a/spec/v3/service_index_spec.rb +++ b/spec/v3/service_index_spec.rb @@ -110,7 +110,7 @@ describe Travis::API::V3::ServiceIndex do describe "for_current_user action" do let(:action) { resource.fetch("actions").fetch("for_current_user") } - specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}orgs{?include}") } + specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}orgs{?include,limit,offset,sort_by}") } end end diff --git a/spec/v3/services/organizations/for_current_user_spec.rb b/spec/v3/services/organizations/for_current_user_spec.rb index eb978cac..92fe0cfa 100644 --- a/spec/v3/services/organizations/for_current_user_spec.rb +++ b/spec/v3/services/organizations/for_current_user_spec.rb @@ -20,7 +20,23 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do example { expect(JSON.load(body)).to be == { "@type" => "organizations", "@href" => "/v3/orgs", - "@representation" => "standard", + "@representation" => "standard", + "@pagination" => { + "limit" => 100, + "offset" => 0, + "count" => 1, + "is_first" => true, + "is_last" => true, + "next" => nil, + "prev" => nil, + "first" => { + "@href" => "/v3/orgs", + "offset" => 0, + "limit" => 100}, + "last" => { + "@href" => "/v3/orgs", + "offset" => 0, + "limit" => 100}}, "organizations" => [{ "@type" => "organization", "@href" => "/v3/org/#{org.id}", From 571af140674f47280ff41329e2370c714827aff8 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 13 Oct 2015 17:34:31 +0200 Subject: [PATCH 3/4] v3: add pagination and sortability to repo endpoints --- lib/travis/api/v3/queries/repositories.rb | 3 ++- .../api/v3/services/owner/repositories.rb | 1 + .../services/repositories/for_current_user.rb | 1 + spec/v3/service_index_spec.rb | 2 +- spec/v3/services/owner/repositories_spec.rb | 16 ++++++++++++++++ .../repositories/for_current_user_spec.rb | 18 +++++++++++++++++- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/travis/api/v3/queries/repositories.rb b/lib/travis/api/v3/queries/repositories.rb index 1a683ffc..9c75c5b6 100644 --- a/lib/travis/api/v3/queries/repositories.rb +++ b/lib/travis/api/v3/queries/repositories.rb @@ -1,6 +1,7 @@ module Travis::API::V3 class Queries::Repositories < Query params :active, :private, prefix: :repository + sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active) def for_member(user) all.joins(:users).where(users: user_condition(user), invalidated_at: nil) @@ -27,7 +28,7 @@ module Travis::API::V3 list = list.includes(default_branch: :last_build) list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze - list + sort list end end end diff --git a/lib/travis/api/v3/services/owner/repositories.rb b/lib/travis/api/v3/services/owner/repositories.rb index 6942c383..7d376fda 100644 --- a/lib/travis/api/v3/services/owner/repositories.rb +++ b/lib/travis/api/v3/services/owner/repositories.rb @@ -2,6 +2,7 @@ module Travis::API::V3 class Services::Owner::Repositories < Service params :active, :private, prefix: :repository result_type :repositories + paginate(default_limit: 100) def run! unfiltered = query(:repositories).for_owner(find(:owner)) diff --git a/lib/travis/api/v3/services/repositories/for_current_user.rb b/lib/travis/api/v3/services/repositories/for_current_user.rb index 925a605c..b72c9396 100644 --- a/lib/travis/api/v3/services/repositories/for_current_user.rb +++ b/lib/travis/api/v3/services/repositories/for_current_user.rb @@ -1,6 +1,7 @@ module Travis::API::V3 class Services::Repositories::ForCurrentUser < Service params :active, :private, prefix: :repository + paginate(default_limit: 100) def run! raise LoginRequired unless access_control.logged_in? diff --git a/spec/v3/service_index_spec.rb b/spec/v3/service_index_spec.rb index d46784ae..8559b637 100644 --- a/spec/v3/service_index_spec.rb +++ b/spec/v3/service_index_spec.rb @@ -64,7 +64,7 @@ describe Travis::API::V3::ServiceIndex do describe "for_current_user action" do let(:action) { resource.fetch("actions").fetch("for_current_user") } - specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,private,repository.active,repository.private}") } + specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,limit,offset,private,repository.active,repository.private,sort_by}") } end end diff --git a/spec/v3/services/owner/repositories_spec.rb b/spec/v3/services/owner/repositories_spec.rb index 0fb9b0be..8fb3ee59 100644 --- a/spec/v3/services/owner/repositories_spec.rb +++ b/spec/v3/services/owner/repositories_spec.rb @@ -18,6 +18,22 @@ describe Travis::API::V3::Services::Owner::Repositories do "@type" => "repositories", "@href" => "/v3/owner/svenfuchs/repos", "@representation" => "standard", + "@pagination" => { + "limit" => 100, + "offset" => 0, + "count" => 1, + "is_first" => true, + "is_last" => true, + "next" => nil, + "prev" => nil, + "first" => { + "@href" => "/v3/owner/svenfuchs/repos", + "offset" => 0, + "limit" => 100}, + "last" => { + "@href" => "/v3/owner/svenfuchs/repos", + "offset" => 0, + "limit" => 100}}, "repositories" => [{ "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", diff --git a/spec/v3/services/repositories/for_current_user_spec.rb b/spec/v3/services/repositories/for_current_user_spec.rb index fb9e9ff2..d696df18 100644 --- a/spec/v3/services/repositories/for_current_user_spec.rb +++ b/spec/v3/services/repositories/for_current_user_spec.rb @@ -17,7 +17,23 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do example { expect(JSON.load(body)).to be == { "@type" => "repositories", "@href" => "/v3/repos", - "@representation" => "standard", + "@representation" => "standard", + "@pagination" => { + "limit" => 100, + "offset" => 0, + "count" => 1, + "is_first" => true, + "is_last" => true, + "next" => nil, + "prev" => nil, + "first" => { + "@href" => "/v3/repos", + "offset" => 0, + "limit" => 100}, + "last" => { + "@href" => "/v3/repos", + "offset" => 0, + "limit" => 100}}, "repositories" => [{ "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", From df8f2537ccde2a96780bc2465247c9ef8fc3f97f Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 13 Oct 2015 17:40:58 +0200 Subject: [PATCH 4/4] v3: list sortable fields in service index --- lib/travis/api/v3/service_index.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/travis/api/v3/service_index.rb b/lib/travis/api/v3/service_index.rb index 4ee23a1e..7bc0903f 100644 --- a/lib/travis/api/v3/service_index.rb +++ b/lib/travis/api/v3/service_index.rb @@ -96,6 +96,13 @@ module Travis::API::V3 pattern = sub_route ? resource.route + sub_route : resource.route factory = Services[resource.identifier][service] + if factory.params and factory.params.include? "sort_by".freeze + query = Queries[resource.identifier] + if query and query.sortable? + resources[resource.identifier][:sortable_by] = query.sort_by.keys + end + end + pattern.to_templates.each do |template| params = factory.params if request_method == 'GET'.freeze params &&= params.reject { |p| p.start_with? ?@.freeze }