Merge pull request #173 from travis-ci/fetch-by-slug

Allow to fetch repository by slug
This commit is contained in:
Konstantin Haase 2015-05-21 14:08:48 +02:00
commit 0219308a08
7 changed files with 34 additions and 9 deletions

View File

@ -108,7 +108,6 @@ module Travis::Api
use Travis::Api::App::Cors # if Travis.env == 'development' ??? use Travis::Api::App::Cors # if Travis.env == 'development' ???
use Raven::Rack if Travis.env == 'production' || Travis.env == 'staging' use Raven::Rack if Travis.env == 'production' || Travis.env == 'staging'
use Rack::Protection::PathTraversal
use Rack::SSL if Endpoint.production? use Rack::SSL if Endpoint.production?
use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache use ActiveRecord::QueryCache

View File

@ -1,10 +1,18 @@
module Travis::API::V3 module Travis::API::V3
class Queries::Repository < Query class Queries::Repository < Query
params :id params :id, :slug
def find def find
return by_slug if slug
return Models::Repository.find_by_id(id) if id return Models::Repository.find_by_id(id) if id
raise WrongParams, 'missing repository.id'.freeze raise WrongParams, 'missing repository.id'.freeze
end end
private
def by_slug
owner_name, name = slug.split('/')
Models::Repository.where(owner_name: owner_name, name: name).first
end
end end
end end

View File

@ -15,7 +15,8 @@ module Travis::API::V3
end end
resource :repository do resource :repository do
route '/repo/{repository.id}' route '/repo/({repository.id}|{repository.slug})',
capture: { :"repository.id" => :digit }
get :find get :find
post :enable, '/enable' post :enable, '/enable'

View File

@ -33,8 +33,8 @@ module Travis::API::V3
@current_resource = resource_was @current_resource = resource_was
end end
def route(value) def route(value, options = {})
current_resource.route = Mustermann.new(prefix) + Mustermann.new(value) current_resource.route = Mustermann.new(prefix) + Mustermann.new(value, options)
end end
def get(*args) def get(*args)

View File

@ -151,12 +151,12 @@ describe Travis::API::V3::ServiceIndex do
describe 'with /v3 prefix' do describe 'with /v3 prefix' do
let(:headers) { { 'HTTP_ACCEPT' => 'application/json-home' } } let(:headers) { { 'HTTP_ACCEPT' => 'application/json-home' } }
let(:path) { '/v3/' } let(:path) { '/v3/' }
specify(:resources) { expect(json['resources']).to include("http://schema.travis-ci.com/rel/repository/find") } specify(:resources) { expect(json['resources']).to include("http://schema.travis-ci.com/rel/repository/find/by_repository.id") }
end end
describe 'with Travis-API-Version header' do describe 'with Travis-API-Version header' do
let(:headers) { { 'HTTP_ACCEPT' => 'application/json-home', 'HTTP_TRAVIS_API_VERSION' => '3' } } let(:headers) { { 'HTTP_ACCEPT' => 'application/json-home', 'HTTP_TRAVIS_API_VERSION' => '3' } }
specify(:resources) { expect(json['resources']).to include("http://schema.travis-ci.com/rel/repository/find") } specify(:resources) { expect(json['resources']).to include("http://schema.travis-ci.com/rel/repository/find/by_repository.id") }
end end
end end
end end

View File

@ -74,4 +74,4 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do
example { expect(last_response) .to be_ok } example { expect(last_response) .to be_ok }
example { expect(JSON.load(body)['repositories']) .to be == [] } example { expect(JSON.load(body)['repositories']) .to be == [] }
end end
end end

View File

@ -4,6 +4,23 @@ describe Travis::API::V3::Services::Repository::Find do
let(:repo) { Repository.by_slug('svenfuchs/minimal').first } let(:repo) { Repository.by_slug('svenfuchs/minimal').first }
let(:parsed_body) { JSON.load(body) } let(:parsed_body) { JSON.load(body) }
describe "fetching a public repository by slug" do
before { get("/v3/repo/svenfuchs%2Fminimal") }
example { expect(last_response).to be_ok }
example { expect(parsed_body['slug']).to be == 'svenfuchs/minimal' }
end
describe "fetching a non-existing repository by slug" do
before { get("/v3/repo/svenfuchs%2Fminimal1") }
example { expect(last_response).to be_not_found }
example { expect(parsed_body).to be == {
"@type" => "error",
"error_type" => "not_found",
"error_message" => "repository not found (or insufficient access)",
"resource_type" => "repository"
}}
end
describe "public repository" do describe "public repository" do
before { get("/v3/repo/#{repo.id}") } before { get("/v3/repo/#{repo.id}") }
example { expect(last_response).to be_ok } example { expect(last_response).to be_ok }
@ -349,4 +366,4 @@ describe Travis::API::V3::Services::Repository::Find do
example { expect(last_response).to be_ok } example { expect(last_response).to be_ok }
example { expect(parsed_body).to include("last_build") } example { expect(parsed_body).to include("last_build") }
end end
end end