diff --git a/Gemfile.lock b/Gemfile.lock index c73b46c5..ee5ce520 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -73,7 +73,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-migrations.git - revision: 3f6bb84800b0222ceba95a4b1368969eb5ede8e0 + revision: bf360857ef7830f7e3ff12de181ab58c33fb29f1 specs: travis-migrations (0.0.1) @@ -426,3 +426,6 @@ DEPENDENCIES travis-yaml! unicorn yard-sinatra! + +BUNDLED WITH + 1.11.2 diff --git a/lib/travis/api/v3/models/repository.rb b/lib/travis/api/v3/models/repository.rb index 22c26d71..b462db88 100644 --- a/lib/travis/api/v3/models/repository.rb +++ b/lib/travis/api/v3/models/repository.rb @@ -10,6 +10,7 @@ module Travis::API::V3 belongs_to :owner, polymorphic: true belongs_to :last_build, class_name: 'Travis::API::V3::Models::Build'.freeze + belongs_to :current_build, class_name: 'Travis::API::V3::Models::Build'.freeze has_one :default_branch, foreign_key: [:repository_id, :name], diff --git a/lib/travis/api/v3/queries/repositories.rb b/lib/travis/api/v3/queries/repositories.rb index 97d4cfc5..7132fb00 100644 --- a/lib/travis/api/v3/queries/repositories.rb +++ b/lib/travis/api/v3/queries/repositories.rb @@ -2,6 +2,9 @@ module Travis::API::V3 class Queries::Repositories < Query params :active, :private, :starred, prefix: :repository sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active), :'default_branch.last_build' => 'builds.started_at' + sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active), + :'default_branch.last_build' => 'builds.started_at', + :current_build => "current_build_id %{order} NULLS LAST" def for_member(user, **options) all(user: user, **options).joins(:users).where(users: user_condition(user), invalidated_at: nil) @@ -35,6 +38,7 @@ module Travis::API::V3 end list = list.includes(default_branch: :last_build) + list = list.includes(:current_build) list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze sort list end diff --git a/lib/travis/api/v3/query.rb b/lib/travis/api/v3/query.rb index 97d803ba..a9812e0c 100644 --- a/lib/travis/api/v3/query.rb +++ b/lib/travis/api/v3/query.rb @@ -150,7 +150,7 @@ module Travis::API::V3 def sort_by(collection, field, order: nil, first: false, sql: nil, **) raise ArgumentError, 'cannot sort by that' unless sort_by?(field, order) actual = sql || self.class.sort_by.fetch(field) - line = "#{actual} #{order.upcase}" + line = add_order(actual, order) if sort_join?(collection, actual) collection = collection.joins(actual.to_sym) @@ -181,5 +181,14 @@ module Travis::API::V3 else raise WrongParams end end + + def add_order(field, order) + order = order.upcase + if field =~ /%{order}/ + field % { order: order } + else + "#{field} #{order}" + end + end end end diff --git a/lib/travis/api/v3/renderer/repository.rb b/lib/travis/api/v3/renderer/repository.rb index 46072e36..c1c3bf30 100644 --- a/lib/travis/api/v3/renderer/repository.rb +++ b/lib/travis/api/v3/renderer/repository.rb @@ -3,7 +3,7 @@ require 'travis/api/v3/renderer/model_renderer' module Travis::API::V3 class Renderer::Repository < Renderer::ModelRenderer representation(:minimal, :id, :name, :slug) - representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred) + representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build) def active !!model.active diff --git a/spec/v3/services/owner/find_spec.rb b/spec/v3/services/owner/find_spec.rb index f47f0b89..6ad095da 100644 --- a/spec/v3/services/owner/find_spec.rb +++ b/spec/v3/services/owner/find_spec.rb @@ -81,7 +81,8 @@ describe Travis::API::V3::Services::Owner::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }] }} end @@ -129,7 +130,8 @@ describe Travis::API::V3::Services::Owner::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation"=> "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }] }} end diff --git a/spec/v3/services/repositories/for_current_user_spec.rb b/spec/v3/services/repositories/for_current_user_spec.rb index d414783a..83ffc360 100644 --- a/spec/v3/services/repositories/for_current_user_spec.rb +++ b/spec/v3/services/repositories/for_current_user_spec.rb @@ -4,6 +4,7 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } let(:build) { repo.builds.first } 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(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }} @@ -12,6 +13,7 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do after { repo.update_attribute(:private, false) } 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) } example { expect(last_response).to be_ok } example { expect(JSON.load(body)).to be == { @@ -63,7 +65,19 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "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 diff --git a/spec/v3/services/repositories/for_owner_spec.rb b/spec/v3/services/repositories/for_owner_spec.rb index 953f57e1..b3bb8adc 100644 --- a/spec/v3/services/repositories/for_owner_spec.rb +++ b/spec/v3/services/repositories/for_owner_spec.rb @@ -64,7 +64,8 @@ describe Travis::API::V3::Services::Repositories::ForOwner do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }]}} end @@ -141,7 +142,8 @@ describe Travis::API::V3::Services::Repositories::ForOwner do "@href" => "/v3/repo/1/branch/master", "@representation"=>"minimal", "name" => "master" }, - "starred" => false }, { + "starred" => false, + "current_build" => nil }, { "@type" => "repository", "@href" => "/v3/repo/#{repo2.id}", "@representation" => "standard", @@ -170,6 +172,7 @@ describe Travis::API::V3::Services::Repositories::ForOwner do "@href" => "/v3/repo/#{repo2.id}/branch/master", "@representation"=>"minimal", "name" =>"master" }, - "starred"=>false}]} + "starred" => false, + "current_build" => nil}]} end end diff --git a/spec/v3/services/repository/find_spec.rb b/spec/v3/services/repository/find_spec.rb index b3c03556..1e602988 100644 --- a/spec/v3/services/repository/find_spec.rb +++ b/spec/v3/services/repository/find_spec.rb @@ -55,7 +55,8 @@ describe Travis::API::V3::Services::Repository::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }} end @@ -133,7 +134,8 @@ describe Travis::API::V3::Services::Repository::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }} end @@ -196,7 +198,8 @@ describe Travis::API::V3::Services::Repository::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }} end @@ -265,7 +268,8 @@ describe Travis::API::V3::Services::Repository::Find do "@href" => "/v3/repo/#{repo.id}/branch/master", "@representation" => "minimal", "name" => "master"}, - "starred" => false + "starred" => false, + "current_build" => nil }} end