Skip to content

Commit 737a31a

Browse files
committed
v3: add /orgs endpoint, fixes travis-pro/api-v3#1
1 parent a9ffd2b commit 737a31a

10 files changed

+92
-16
lines changed

lib/travis/api/v3.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module V3
44
V3 = self
55

66
def load_dir(dir, recursive: true)
7-
Dir.glob("#{dir}/*.rb").each { |f| require f[%r[(?<=lib/).+(?=\.rb$)]] }
8-
Dir.glob("#{dir}/*").each { |dir| load_dir(dir) } if recursive
7+
Dir.glob("#{dir}/*.rb").sort.each { |f| require f[%r[(?<=lib/).+(?=\.rb$)]] }
8+
Dir.glob("#{dir}/*").sort.each { |dir| load_dir(dir) } if recursive
99
end
1010

1111
def response(payload, headers = {}, content_type: 'application/json'.freeze, status: 200)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Travis::API::V3
2+
class Queries::Organizations < Query
3+
def for_member(user)
4+
::Organization.joins(:users).where(users: user_condition(user))
5+
end
6+
end
7+
end

lib/travis/api/v3/queries/repositories.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ def for_member(user)
66
all.joins(:users).where(users: user_condition(user))
77
end
88

9-
private
10-
11-
def user_condition(value)
12-
case value
13-
when String then { login: value }
14-
when Integer then { id: value }
15-
when ::User then { id: value.id }
16-
else raise WrongParams
17-
end
18-
end
19-
209
def all
2110
@all ||= begin
2211
all = ::Repository

lib/travis/api/v3/query.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@ def bool(value)
1515
return false if value == 'false'.freeze
1616
!!value
1717
end
18+
19+
def user_condition(value)
20+
case value
21+
when String then { login: value }
22+
when Integer then { id: value }
23+
when ::User then { id: value.id }
24+
else raise WrongParams
25+
end
26+
end
1827
end
1928
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Travis::API::V3
2+
module Renderer::Organization
3+
DIRECT_ATTRIBUTES = %i[id login name github_id]
4+
extend self
5+
6+
def render(organization)
7+
{ :@type => 'organization'.freeze, **direct_attributes(organization) }
8+
end
9+
10+
def direct_attributes(repository)
11+
DIRECT_ATTRIBUTES.map { |a| [a, repository.public_send(a)] }.to_h
12+
end
13+
end
14+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Travis::API::V3
2+
module Renderer::Organizations
3+
extend self
4+
5+
def render(repositories)
6+
Renderer[:collection].render(:organizations, :organization, repositories)
7+
end
8+
end
9+
end

lib/travis/api/v3/routes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@ module Routes
1212
route '/repos'
1313
get :repositories_for_current_user
1414
end
15+
16+
resource :organizations do
17+
route '/orgs'
18+
get :organizations_for_current_user
19+
end
1520
end
1621
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Travis::API::V3
2+
class Services::OrganizationsForCurrentUser < Service
3+
result_type :organizations
4+
5+
def run!
6+
raise LoginRequired unless access_control.logged_in?
7+
query.for_member(access_control.user)
8+
end
9+
end
10+
end

spec/v3/service_index_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
describe "custom json entry point" do
1010
let(:expected_resources) {{
11-
"repository" => {
11+
"repository" => {
1212
"find" => [{"request-method"=>"GET", "uri-template"=>"#{path}repo/{repository.id}"}] },
13-
"repositories" => {
14-
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] }
13+
"repositories" => {
14+
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] },
15+
"organizations" => {
16+
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}orgs"}] }
1517
}}
1618

1719
describe 'with /v3 prefix' do
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
describe Travis::API::V3::Services::FindRepository do
4+
let(:repo) { Repository.by_slug('svenfuchs/minimal').first }
5+
6+
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
7+
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
8+
before { Permission.create(repository: repo, user: repo.owner, pull: true) }
9+
before { repo.update_attribute(:private, true) }
10+
after { repo.update_attribute(:private, false) }
11+
12+
let(:org) { Organization.new(login: 'example-org') }
13+
before { org.save! }
14+
before { org.memberships.create(user: repo.owner) }
15+
after { org.delete }
16+
17+
describe "authenticated as user with access" do
18+
before { get("/v3/orgs", {}, headers) }
19+
example { expect(last_response).to be_ok }
20+
example { expect(JSON.load(body)).to be == {
21+
"@type" => "organizations",
22+
"organizations" => [{
23+
"@type" => "organization",
24+
"id" => org.id,
25+
"login" => "example-org",
26+
"name" => nil,
27+
"github_id" => nil
28+
}]
29+
}}
30+
end
31+
end

0 commit comments

Comments
 (0)