v3: add /orgs endpoint, fixes travis-pro/api-v3#1
This commit is contained in:
parent
a9ffd2bef9
commit
737a31ad23
|
@ -4,8 +4,8 @@ module Travis
|
|||
V3 = self
|
||||
|
||||
def load_dir(dir, recursive: true)
|
||||
Dir.glob("#{dir}/*.rb").each { |f| require f[%r[(?<=lib/).+(?=\.rb$)]] }
|
||||
Dir.glob("#{dir}/*").each { |dir| load_dir(dir) } if recursive
|
||||
Dir.glob("#{dir}/*.rb").sort.each { |f| require f[%r[(?<=lib/).+(?=\.rb$)]] }
|
||||
Dir.glob("#{dir}/*").sort.each { |dir| load_dir(dir) } if recursive
|
||||
end
|
||||
|
||||
def response(payload, headers = {}, content_type: 'application/json'.freeze, status: 200)
|
||||
|
|
7
lib/travis/api/v3/queries/organizations.rb
Normal file
7
lib/travis/api/v3/queries/organizations.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module Travis::API::V3
|
||||
class Queries::Organizations < Query
|
||||
def for_member(user)
|
||||
::Organization.joins(:users).where(users: user_condition(user))
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,17 +6,6 @@ module Travis::API::V3
|
|||
all.joins(:users).where(users: user_condition(user))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_condition(value)
|
||||
case value
|
||||
when String then { login: value }
|
||||
when Integer then { id: value }
|
||||
when ::User then { id: value.id }
|
||||
else raise WrongParams
|
||||
end
|
||||
end
|
||||
|
||||
def all
|
||||
@all ||= begin
|
||||
all = ::Repository
|
||||
|
|
|
@ -15,5 +15,14 @@ module Travis::API::V3
|
|||
return false if value == 'false'.freeze
|
||||
!!value
|
||||
end
|
||||
|
||||
def user_condition(value)
|
||||
case value
|
||||
when String then { login: value }
|
||||
when Integer then { id: value }
|
||||
when ::User then { id: value.id }
|
||||
else raise WrongParams
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
14
lib/travis/api/v3/renderer/organization.rb
Normal file
14
lib/travis/api/v3/renderer/organization.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Travis::API::V3
|
||||
module Renderer::Organization
|
||||
DIRECT_ATTRIBUTES = %i[id login name github_id]
|
||||
extend self
|
||||
|
||||
def render(organization)
|
||||
{ :@type => 'organization'.freeze, **direct_attributes(organization) }
|
||||
end
|
||||
|
||||
def direct_attributes(repository)
|
||||
DIRECT_ATTRIBUTES.map { |a| [a, repository.public_send(a)] }.to_h
|
||||
end
|
||||
end
|
||||
end
|
9
lib/travis/api/v3/renderer/organizations.rb
Normal file
9
lib/travis/api/v3/renderer/organizations.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module Travis::API::V3
|
||||
module Renderer::Organizations
|
||||
extend self
|
||||
|
||||
def render(repositories)
|
||||
Renderer[:collection].render(:organizations, :organization, repositories)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -12,5 +12,10 @@ module Travis::API::V3
|
|||
route '/repos'
|
||||
get :repositories_for_current_user
|
||||
end
|
||||
|
||||
resource :organizations do
|
||||
route '/orgs'
|
||||
get :organizations_for_current_user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
10
lib/travis/api/v3/services/organizations_for_current_user.rb
Normal file
10
lib/travis/api/v3/services/organizations_for_current_user.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
module Travis::API::V3
|
||||
class Services::OrganizationsForCurrentUser < Service
|
||||
result_type :organizations
|
||||
|
||||
def run!
|
||||
raise LoginRequired unless access_control.logged_in?
|
||||
query.for_member(access_control.user)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,7 +11,9 @@ describe Travis::API::V3::ServiceIndex do
|
|||
"repository" => {
|
||||
"find" => [{"request-method"=>"GET", "uri-template"=>"#{path}repo/{repository.id}"}] },
|
||||
"repositories" => {
|
||||
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] }
|
||||
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] },
|
||||
"organizations" => {
|
||||
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}orgs"}] }
|
||||
}}
|
||||
|
||||
describe 'with /v3 prefix' do
|
||||
|
|
31
spec/v3/services/organizations_for_current_user_spec.rb
Normal file
31
spec/v3/services/organizations_for_current_user_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Travis::API::V3::Services::FindRepository do
|
||||
let(:repo) { Repository.by_slug('svenfuchs/minimal').first }
|
||||
|
||||
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
|
||||
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
|
||||
before { Permission.create(repository: repo, user: repo.owner, pull: true) }
|
||||
before { repo.update_attribute(:private, true) }
|
||||
after { repo.update_attribute(:private, false) }
|
||||
|
||||
let(:org) { Organization.new(login: 'example-org') }
|
||||
before { org.save! }
|
||||
before { org.memberships.create(user: repo.owner) }
|
||||
after { org.delete }
|
||||
|
||||
describe "authenticated as user with access" do
|
||||
before { get("/v3/orgs", {}, headers) }
|
||||
example { expect(last_response).to be_ok }
|
||||
example { expect(JSON.load(body)).to be == {
|
||||
"@type" => "organizations",
|
||||
"organizations" => [{
|
||||
"@type" => "organization",
|
||||
"id" => org.id,
|
||||
"login" => "example-org",
|
||||
"name" => nil,
|
||||
"github_id" => nil
|
||||
}]
|
||||
}}
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user