Merge branch 'master' into ar-improve-feature-flag

This commit is contained in:
Ana Rosas 2016-06-23 17:07:36 -05:00 committed by GitHub
commit bfb68bf931
9 changed files with 62 additions and 34 deletions

View File

@ -3,7 +3,15 @@ module Travis::API::V3
params :active, :private, :starred, prefix: :repository params :active, :private, :starred, prefix: :repository
sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active), sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active),
:'default_branch.last_build' => 'builds.started_at', :'default_branch.last_build' => 'builds.started_at',
:current_build_id => "repositories.current_build_id %{order} NULLS LAST" :current_build => "repositories.current_build_id %{order} NULLS LAST"
# this is a hack for a bug in AR that generates invalid query when it tries
# to include `current_build` and join it at the same time. We don't actually
# need the join, but it will be automatically added, because `current_build`
# is an association. This prevents adding the join. We will probably be able
# to remove it once we move to newer AR versions
prevent_sortable_join :current_build
experimental_sortable_by :current_build
def for_member(user, **options) def for_member(user, **options)
all(user: user, **options).joins(:users).where(users: user_condition(user), invalidated_at: nil) all(user: user, **options).joins(:users).where(users: user_condition(user), invalidated_at: nil)
@ -37,7 +45,7 @@ module Travis::API::V3
end end
list = list.includes(default_branch: :last_build) list = list.includes(default_branch: :last_build)
list = list.includes(:current_build) list = list.includes(:current_build) if includes? 'repository.current_build'.freeze
list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze
sort list sort list
end end

View File

@ -70,6 +70,26 @@ module Travis::API::V3
mapping.each { |key, value| sort_by[key.to_s] = prefix(value) } mapping.each { |key, value| sort_by[key.to_s] = prefix(value) }
end end
def self.prevent_sortable_join(*fields)
dont_join.push(*fields.map(&:to_s))
end
@dont_join = []
def self.dont_join
@dont_join ||= superclass.dont_join.dup
end
@experimental_sortable_by = []
def self.experimental_sortable_by(*fields)
@experimental_sortable_by ||= []
if fields.first
@experimental_sortable_by.push(*fields.map(&:to_s))
end
@experimental_sortable_by
end
def self.sort_condition(condition) def self.sort_condition(condition)
if condition.is_a? Hash if condition.is_a? Hash
condition = condition.map { |e| e.map { |v| prefix(v) }.join(" = ".freeze) }.join(" and ".freeze) condition = condition.map { |e| e.map { |v| prefix(v) }.join(" = ".freeze) }.join(" and ".freeze)
@ -172,6 +192,7 @@ module Travis::API::V3
end end
def sort_join?(collection, field) def sort_join?(collection, field)
return if self.class.dont_join.include?(field)
!collection.reflect_on_association(field.to_sym).nil? !collection.reflect_on_association(field.to_sym).nil?
end end

View File

@ -26,6 +26,17 @@ module Travis::API::V3
@representations ||= superclass.representations.dup @representations ||= superclass.representations.dup
end end
@experimental_representations = []
def self.experimental_representations(*representations)
@experimental_representations ||= superclass.experimental_representations.dup
if representations.first
@experimental_representations.push(*representations)
end
@experimental_representations
end
@available_attributes = Set.new @available_attributes = Set.new
def self.available_attributes def self.available_attributes
@available_attributes ||= superclass.available_attributes.dup @available_attributes ||= superclass.available_attributes.dup

View File

@ -3,7 +3,10 @@ require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3 module Travis::API::V3
class Renderer::Repository < Renderer::ModelRenderer class Renderer::Repository < Renderer::ModelRenderer
representation(:minimal, :id, :name, :slug) representation(:minimal, :id, :name, :slug)
representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build) representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred)
representation(:experimental, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build)
experimental_representations(:experimental)
def active def active
!!model.active !!model.active

View File

@ -85,9 +85,17 @@ module Travis::API::V3
data = resources[resource.identifier] ||= { :@type => :resource, :actions => {} } data = resources[resource.identifier] ||= { :@type => :resource, :actions => {} }
data.merge! resource.meta_data data.merge! resource.meta_data
if renderer = Renderer[resource.identifier, false] if renderer = Renderer[resource.identifier, false]
data[:attributes] = renderer.available_attributes if renderer.respond_to? :available_attributes
data[:representations] = renderer.representations if renderer.respond_to? :representations data[:attributes] = renderer.available_attributes if renderer.respond_to? :available_attributes
if renderer.respond_to? :representations
representations = renderer.representations
if renderer.respond_to? :experimental_representations
representations = representations.reject { |k| renderer.experimental_representations.include? k }
end
data[:representations] = representations
end
end end
if permissions = Permissions[resource.identifier, false] if permissions = Permissions[resource.identifier, false]
@ -102,7 +110,7 @@ module Travis::API::V3
if factory.params and factory.params.include? "sort_by".freeze if factory.params and factory.params.include? "sort_by".freeze
query = Queries[resource.identifier] query = Queries[resource.identifier]
if query and query.sortable? if query and query.sortable?
resources[resource.identifier][:sortable_by] = query.sort_by.keys resources[resource.identifier][:sortable_by] = query.sort_by.keys - query.experimental_sortable_by
resources[resource.identifier][:default_sort] = query.default_sort unless query.default_sort.empty? resources[resource.identifier][:default_sort] = query.default_sort unless query.default_sort.empty?
end end
end end

View File

@ -79,8 +79,7 @@ describe Travis::API::V3::Services::Owner::Find, set_app: true do
"@href" => "/v3/repo/#{repo.id}/branch/master", "@href" => "/v3/repo/#{repo.id}/branch/master",
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false
"current_build" => nil
}] }]
}} }}
end end
@ -128,8 +127,7 @@ describe Travis::API::V3::Services::Owner::Find, set_app: true do
"@href" => "/v3/repo/#{repo.id}/branch/master", "@href" => "/v3/repo/#{repo.id}/branch/master",
"@representation"=> "minimal", "@representation"=> "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false
"current_build" => nil
}] }]
}} }}
end end

View File

@ -2,7 +2,6 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser, set_app: true
let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first }
let(:build) { repo.builds.first } let(:build) { repo.builds.first }
let(:jobs) { Travis::API::V3::Models::Build.find(build.id).jobs } let(:jobs) { Travis::API::V3::Models::Build.find(build.id).jobs }
let(:current_build) { repo.builds.first }
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) } let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }} let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
@ -11,7 +10,6 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser, set_app: true
after { repo.update_attribute(:private, false) } after { repo.update_attribute(:private, false) }
describe "private repository, private API, authenticated as user with access" do describe "private repository, private API, authenticated as user with access" do
before { repo.update_attribute(:current_build_id, current_build.id) }
before { get("/v3/repos", {}, headers) } before { get("/v3/repos", {}, headers) }
example { expect(last_response).to be_ok } example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == { example { expect(JSON.load(body)).to be == {
@ -64,18 +62,6 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser, set_app: true
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => {
"@type" => "build",
"@href" => "/v3/build/#{current_build.id}",
"@representation" => "minimal",
"id" => current_build.id.to_i,
"number" => current_build.number,
"state" => current_build.state,
"duration" => current_build.duration,
"event_type" => current_build.event_type,
"previous_state" => current_build.previous_state,
"started_at" => current_build.started_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
"finished_at" => nil},
}] }]
}} }}
end end

View File

@ -63,7 +63,6 @@ describe Travis::API::V3::Services::Repositories::ForOwner, set_app: true do
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => nil
}]}} }]}}
end end
@ -140,8 +139,7 @@ describe Travis::API::V3::Services::Repositories::ForOwner, set_app: true do
"@href" => "/v3/repo/1/branch/master", "@href" => "/v3/repo/1/branch/master",
"@representation"=>"minimal", "@representation"=>"minimal",
"name" => "master" }, "name" => "master" },
"starred" => false, "starred" => false }, {
"current_build" => nil }, {
"@type" => "repository", "@type" => "repository",
"@href" => "/v3/repo/#{repo2.id}", "@href" => "/v3/repo/#{repo2.id}",
"@representation" => "standard", "@representation" => "standard",
@ -170,7 +168,6 @@ describe Travis::API::V3::Services::Repositories::ForOwner, set_app: true do
"@href" => "/v3/repo/#{repo2.id}/branch/master", "@href" => "/v3/repo/#{repo2.id}/branch/master",
"@representation"=>"minimal", "@representation"=>"minimal",
"name" =>"master" }, "name" =>"master" },
"starred" => false, "starred" => false}]}
"current_build" => nil}]}
end end
end end

View File

@ -54,7 +54,6 @@ describe Travis::API::V3::Services::Repository::Find, set_app: true do
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => nil
}} }}
end end
@ -133,7 +132,6 @@ describe Travis::API::V3::Services::Repository::Find, set_app: true do
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => nil
}} }}
end end
@ -197,7 +195,6 @@ describe Travis::API::V3::Services::Repository::Find, set_app: true do
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => nil
}} }}
end end
@ -267,7 +264,6 @@ describe Travis::API::V3::Services::Repository::Find, set_app: true do
"@representation" => "minimal", "@representation" => "minimal",
"name" => "master"}, "name" => "master"},
"starred" => false, "starred" => false,
"current_build" => nil
}} }}
end end