diff --git a/lib/travis/api/v3/access_control/generic.rb b/lib/travis/api/v3/access_control/generic.rb index f5ea01a6..8578491e 100644 --- a/lib/travis/api/v3/access_control/generic.rb +++ b/lib/travis/api/v3/access_control/generic.rb @@ -20,6 +20,10 @@ module Travis::API::V3 protected + def organization_visible?(organization) + unrestricted_api? + end + def repository_visible?(repository) return true if unrestricted_api? and not repository.private? private_repository_visible?(repository) diff --git a/lib/travis/api/v3/queries/organization.rb b/lib/travis/api/v3/queries/organization.rb new file mode 100644 index 00000000..a88f412c --- /dev/null +++ b/lib/travis/api/v3/queries/organization.rb @@ -0,0 +1,10 @@ +module Travis::API::V3 + class Queries::Organization < Query + params :id + + def find + return ::Organization.find_by_id(id) if id + raise WrongParams + end + end +end diff --git a/lib/travis/api/v3/renderer/organization.rb b/lib/travis/api/v3/renderer/organization.rb index e6d0daf3..33c0886a 100644 --- a/lib/travis/api/v3/renderer/organization.rb +++ b/lib/travis/api/v3/renderer/organization.rb @@ -3,8 +3,12 @@ module Travis::API::V3 DIRECT_ATTRIBUTES = %i[id login name github_id] extend self - def render(organization, **) - { :@type => 'organization'.freeze, **direct_attributes(organization) } + def render(organization, script_name: nil, **) + { + :@type => 'organization'.freeze, + :@href => Renderer.href(:organization, id: organization.id, script_name: script_name), + **direct_attributes(organization) + } end def direct_attributes(repository) diff --git a/lib/travis/api/v3/routes.rb b/lib/travis/api/v3/routes.rb index d1158278..6c78a64f 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -19,6 +19,11 @@ module Travis::API::V3 get :for_current_user end + resource :organization do + route '/org/{organization.id}' + get :find + end + resource :organizations do route '/orgs' get :for_current_user diff --git a/lib/travis/api/v3/service_helpers/organization.rb b/lib/travis/api/v3/service_helpers/organization.rb new file mode 100644 index 00000000..5ce58f81 --- /dev/null +++ b/lib/travis/api/v3/service_helpers/organization.rb @@ -0,0 +1,13 @@ +module Travis::API::V3 + module ServiceHelpers::Organization + def organization + @organization ||= find_organization + end + + def find_organization + not_found(true, :organization) unless org = query(:organization).find + not_found(false, :organization) unless access_control.visible? org + org + end + end +end diff --git a/lib/travis/api/v3/services.rb b/lib/travis/api/v3/services.rb index 4cdc4d22..3148f9d8 100644 --- a/lib/travis/api/v3/services.rb +++ b/lib/travis/api/v3/services.rb @@ -2,6 +2,7 @@ module Travis::API::V3 module Services extend ConstantResolver + Organization = Module.new { extend Services } Organizations = Module.new { extend Services } Repositories = Module.new { extend Services } Repository = Module.new { extend Services } diff --git a/lib/travis/api/v3/services/organization/find.rb b/lib/travis/api/v3/services/organization/find.rb new file mode 100644 index 00000000..21280be9 --- /dev/null +++ b/lib/travis/api/v3/services/organization/find.rb @@ -0,0 +1,9 @@ +module Travis::API::V3 + class Services::Organization::Find < Service + helpers :organization + + def run! + organization + end + end +end diff --git a/spec/v3/service_index_spec.rb b/spec/v3/service_index_spec.rb index 4b840436..6143d8a3 100644 --- a/spec/v3/service_index_spec.rb +++ b/spec/v3/service_index_spec.rb @@ -14,6 +14,8 @@ describe Travis::API::V3::ServiceIndex do "for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] }, "organizations" => { "for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}orgs"}] }, + "organization" => { + "find" => [{"request-method"=>"GET", "uri-template"=>"#{path}org/{organization.id}"}] }, "requests" => { "find" => [{"request-method"=>"GET", "uri-template"=>"#{path}repo/{repository.id}/requests"}], "create" => [{"request-method"=>"POST", "uri-template"=>"#{path}repo/{repository.id}/requests"}]} diff --git a/spec/v3/services/organization/find_spec.rb b/spec/v3/services/organization/find_spec.rb new file mode 100644 index 00000000..cc3b248a --- /dev/null +++ b/spec/v3/services/organization/find_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Travis::API::V3::Services::Organization::Find do + let(:org) { Organization.new(login: 'example-org') } + before { org.save! } + after { org.delete } + + describe 'existing org, public api' do + before { get("/v3/org/#{org.id}") } + example { expect(last_response).to be_ok } + example { expect(JSON.load(body)).to be == { + "@type" => "organization", + "@href" => "/v3/org/#{org.id}", + "id" => org.id, + "login" => "example-org", + "name" => nil, + "github_id" => nil + }} + end +end diff --git a/spec/v3/services/organizations/organizations_for_current_user_spec.rb b/spec/v3/services/organizations/for_current_user_spec.rb similarity index 75% rename from spec/v3/services/organizations/organizations_for_current_user_spec.rb rename to spec/v3/services/organizations/for_current_user_spec.rb index 601d8d14..d4123ac5 100644 --- a/spec/v3/services/organizations/organizations_for_current_user_spec.rb +++ b/spec/v3/services/organizations/for_current_user_spec.rb @@ -18,14 +18,15 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do before { get("/v3/orgs", {}, headers) } example { expect(last_response).to be_ok } example { expect(JSON.load(body)).to be == { - "@type" => "organizations", - "@href" => "/v3/orgs", - "organizations" => [{ - "@type" => "organization", - "id" => org.id, - "login" => "example-org", - "name" => nil, - "github_id" => nil + "@type" => "organizations", + "@href" => "/v3/orgs", + "organizations" => [{ + "@type" => "organization", + "@href" => "/v3/org/#{org.id}", + "id" => org.id, + "login" => "example-org", + "name" => nil, + "github_id" => nil }] }} end