From 1d783129ced9437f592c836427ab859280257f43 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 13 Oct 2015 12:42:03 +0200 Subject: [PATCH] v3: allow filtering branches by exists_on_github --- lib/travis/api/v3/queries/branches.rb | 9 ++++++++- lib/travis/api/v3/services/branches/find.rb | 1 + spec/v3/services/branches/find_spec.rb | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/travis/api/v3/queries/branches.rb b/lib/travis/api/v3/queries/branches.rb index 6566a1a8..6eb36728 100644 --- a/lib/travis/api/v3/queries/branches.rb +++ b/lib/travis/api/v3/queries/branches.rb @@ -1,5 +1,7 @@ module Travis::API::V3 class Queries::Branches < Query + params :exists_on_github, prefix: :branch + sortable_by :name, last_build: "builds.started_at".freeze, exists_on_github: "(case when branches.exists_on_github then 1 else 2 end)".freeze @@ -7,7 +9,12 @@ module Travis::API::V3 default_sort "last_build:desc" def find(repository) - sort repository.branches + sort filter(repository.branches) + end + + def filter(list) + list = list.where(exists_on_github: bool(exists_on_github)) unless exists_on_github.nil? + list end end end diff --git a/lib/travis/api/v3/services/branches/find.rb b/lib/travis/api/v3/services/branches/find.rb index cd618c70..34cf0ef1 100644 --- a/lib/travis/api/v3/services/branches/find.rb +++ b/lib/travis/api/v3/services/branches/find.rb @@ -1,5 +1,6 @@ module Travis::API::V3 class Services::Branches::Find < Service + params :exists_on_github, prefix: :branch paginate def run! diff --git a/spec/v3/services/branches/find_spec.rb b/spec/v3/services/branches/find_spec.rb index 68a45a86..a6a2db39 100644 --- a/spec/v3/services/branches/find_spec.rb +++ b/spec/v3/services/branches/find_spec.rb @@ -172,6 +172,20 @@ describe Travis::API::V3::Services::Branches::Find do } end + describe "filtering by exists_on_github" do + describe "false" do + before { get("/v3/repo/#{repo.id}/branches?branch.exists_on_github=false") } + example { expect(last_response).to be_ok } + example { expect(parsed_body["branches"]).to be_empty } + end + + describe "true" do + before { get("/v3/repo/#{repo.id}/branches?branch.exists_on_github=true") } + example { expect(last_response).to be_ok } + example { expect(parsed_body["branches"]).not_to be_empty } + end + end + describe "sorting by name" do before { get("/v3/repo/#{repo.id}/branches?sort_by=name&limit=1") } example { expect(last_response).to be_ok }