Merge pull request #173 from travis-ci/fetch-by-slug
Allow to fetch repository by slug
This commit is contained in:
commit
0219308a08
|
@ -108,7 +108,6 @@ module Travis::Api
|
|||
|
||||
use Travis::Api::App::Cors # if Travis.env == 'development' ???
|
||||
use Raven::Rack if Travis.env == 'production' || Travis.env == 'staging'
|
||||
use Rack::Protection::PathTraversal
|
||||
use Rack::SSL if Endpoint.production?
|
||||
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
||||
use ActiveRecord::QueryCache
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
module Travis::API::V3
|
||||
class Queries::Repository < Query
|
||||
params :id
|
||||
params :id, :slug
|
||||
|
||||
def find
|
||||
return by_slug if slug
|
||||
return Models::Repository.find_by_id(id) if id
|
||||
raise WrongParams, 'missing repository.id'.freeze
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def by_slug
|
||||
owner_name, name = slug.split('/')
|
||||
Models::Repository.where(owner_name: owner_name, name: name).first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,7 +15,8 @@ module Travis::API::V3
|
|||
end
|
||||
|
||||
resource :repository do
|
||||
route '/repo/{repository.id}'
|
||||
route '/repo/({repository.id}|{repository.slug})',
|
||||
capture: { :"repository.id" => :digit }
|
||||
get :find
|
||||
|
||||
post :enable, '/enable'
|
||||
|
|
|
@ -33,8 +33,8 @@ module Travis::API::V3
|
|||
@current_resource = resource_was
|
||||
end
|
||||
|
||||
def route(value)
|
||||
current_resource.route = Mustermann.new(prefix) + Mustermann.new(value)
|
||||
def route(value, options = {})
|
||||
current_resource.route = Mustermann.new(prefix) + Mustermann.new(value, options)
|
||||
end
|
||||
|
||||
def get(*args)
|
||||
|
|
|
@ -151,12 +151,12 @@ describe Travis::API::V3::ServiceIndex do
|
|||
describe 'with /v3 prefix' do
|
||||
let(:headers) { { 'HTTP_ACCEPT' => 'application/json-home' } }
|
||||
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
|
||||
|
||||
describe 'with Travis-API-Version header' do
|
||||
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
|
||||
|
|
|
@ -74,4 +74,4 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do
|
|||
example { expect(last_response) .to be_ok }
|
||||
example { expect(JSON.load(body)['repositories']) .to be == [] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,6 +4,23 @@ describe Travis::API::V3::Services::Repository::Find do
|
|||
let(:repo) { Repository.by_slug('svenfuchs/minimal').first }
|
||||
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
|
||||
before { get("/v3/repo/#{repo.id}") }
|
||||
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(parsed_body).to include("last_build") }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user