v3: allow expanding build.commit

This commit is contained in:
Konstantin Haase 2015-04-30 17:08:52 +02:00
parent fb8e6825db
commit 61ae7e669e
7 changed files with 66 additions and 4 deletions

View File

@ -2,5 +2,19 @@ module Travis::API::V3
class Models::Commit < Model class Models::Commit < Model
belongs_to :repository belongs_to :repository
has_one :request has_one :request
has_many :builds
has_one :branch,
foreign_key: [:repository_id, :name],
primary_key: [:repository_id, :branch],
class_name: 'Travis::API::V3::Models::Branch'.freeze
def branch_name
read_attribute(:branch)
end
def branch_name=(value)
write_attribute(:branch, value)
end
end end
end end

View File

@ -17,9 +17,15 @@ module Travis::API::V3
def filter(list) def filter(list)
list = list.where(active: bool(active)) unless active.nil? list = list.where(active: bool(active)) unless active.nil?
list = list.where(private: bool(private)) unless private.nil? list = list.where(private: bool(private)) unless private.nil?
list = list.includes(:owner) if includes? 'repository.owner'.freeze list = list.includes(:owner) if includes? 'repository.owner'.freeze
list = list.includes(:last_build) if includes? 'repository.last_build'.freeze
if includes? 'repository.last_build'.freeze
list = list.includes(:last_build)
list = list.includes(last_build: :commit) if includes? 'build.commit'.freeze
end
list = list.includes(default_branch: :last_build) list = list.includes(default_branch: :last_build)
list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze
list list
end end
end end

View File

@ -1,11 +1,20 @@
module Travis::API::V3 module Travis::API::V3
class Queries::User < Query class Queries::User < Query
params :id, :login params :id, :login, :email
def find def find
return Models::User.find_by_id(id) if id return Models::User.find_by_id(id) if id
return Models::User.where('lower(login) = ?'.freeze, login.downcase).first if login return Models::User.where('lower(login) = ?'.freeze, login.downcase).first if login
return find_by_email(email) if email
raise WrongParams, 'missing user.id or user.login'.freeze raise WrongParams, 'missing user.id or user.login'.freeze
end end
def find_by_email(email)
if email_model = Models::Email.find_by_email(email)
email_model.user
else
User.find_by_email(email)
end
end
end end
end end

View File

@ -13,6 +13,7 @@ module Travis::API::V3
when has(:gravatar_url) then object.gravatar_url when has(:gravatar_url) then object.gravatar_url
when has(:gravatar_id) then GRAVATAR_URL % object.gravatar_id when has(:gravatar_id) then GRAVATAR_URL % object.gravatar_id
when has(:email) then GRAVATAR_URL % Digest::MD5.hexdigest(object.email) when has(:email) then GRAVATAR_URL % Digest::MD5.hexdigest(object.email)
when String then GRAVATAR_URL % Digest::MD5.hexdigest(object)
end end
end end

View File

@ -3,6 +3,6 @@ require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3 module Travis::API::V3
class Renderer::Build < Renderer::ModelRenderer class Renderer::Build < Renderer::ModelRenderer
representation(:minimal, :id, :number, :state, :duration, :event_type, :previous_state, :started_at, :finished_at) representation(:minimal, :id, :number, :state, :duration, :event_type, :previous_state, :started_at, :finished_at)
representation(:standard, *representations[:minimal], :repository, :branch) representation(:standard, *representations[:minimal], :repository, :branch, :commit)
end end
end end

View File

@ -0,0 +1,31 @@
require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3
class Renderer::Commit < Renderer::ModelRenderer
representation(:minimal, :id, :sha, :ref, :message, :compare_url, :committed_at)
representation(:standard, *representations[:minimal], :repository, :branch, :committer, :author)
def sha
model.commit
end
def committer
user_data(model.committer_name, model.committer_email)
end
def author
user_data(model.author_name, model.author_email)
end
private
def user_data(name, email)
# query(:user).find_by_email(email) <= this triggers an N+1 query
{
#:@type => 'user'.freeze,
:name => name,
:avatar_url => Renderer::AvatarURL.avatar_url(email)
}
end
end
end

View File

@ -1,5 +1,6 @@
module Travis::API::V3 module Travis::API::V3
class Services::Owner::Repositories < Service class Services::Owner::Repositories < Service
params :active, :private, prefix: :repository
result_type :repositories result_type :repositories
def run! def run!