From 62a5e602c5b99dc0e6b5680e6e5c0e604b9cd206 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 27 Sep 2013 13:50:43 +0200 Subject: [PATCH] Implement simple repository settings API --- Gemfile | 2 +- Gemfile.lock | 3 ++- lib/travis/api/app/endpoint/repos.rb | 24 ++++++++++++++++++++++++ spec/integration/v2/repositories_spec.rb | 11 ++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 58bc4852..9125a034 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ ruby '2.0.0' source 'https://rubygems.org' gemspec -gem 'travis-core', github: 'travis-ci/travis-core' +gem 'travis-core', github: 'travis-ci/travis-core', branch: 'repository-settings' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil, ref: 'cde9741' gem 'sinatra' diff --git a/Gemfile.lock b/Gemfile.lock index 7108fe04..cfc6673c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,7 +23,8 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: bf2520962f9445db529313b2ac8d8589a9d150b4 + revision: 6a5b230b06e927cf953a1b7bb20dcf7fd45ee978 + branch: repository-settings specs: travis-core (0.0.1) actionmailer (~> 3.2.12) diff --git a/lib/travis/api/app/endpoint/repos.rb b/lib/travis/api/app/endpoint/repos.rb index fd4a36a5..a062ffda 100644 --- a/lib/travis/api/app/endpoint/repos.rb +++ b/lib/travis/api/app/endpoint/repos.rb @@ -41,6 +41,30 @@ class Travis::Api::App respond_with service(:find_repo, params.merge(schema: 'cc')) end + # Get settings for a given repository + # + get '/:id/settings' do + settings = service(:find_repo_settings, params).run + if settings + respond_with({ settings: settings.obfuscated }, version: :v2) + else + status 404 + end + end + + put '/:id/settings' do + settings = service(:find_repo_settings, params).run + if settings + settings.merge(params[:settings]) + # TODO: I would like to have better API here, but leaving this + # for testing to not waste too much time before I can play with it + settings.repository.save + respond_with({ settings: settings.obfuscated }, version: :v2) + else + status 404 + end + end + # Get the public key for the repository with the given id. # # This can be used to encrypt secure variables in the build configuration. See diff --git a/spec/integration/v2/repositories_spec.rb b/spec/integration/v2/repositories_spec.rb index fcf89a1f..0d8d53e3 100644 --- a/spec/integration/v2/repositories_spec.rb +++ b/spec/integration/v2/repositories_spec.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require 'spec_helper' describe 'Repos' do @@ -9,7 +10,7 @@ describe 'Repos' do let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) } let(:headers) { { 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json', 'HTTP_AUTHORIZATION' => "token #{token}" } } - before { user.permissions.create!(:repository_id => repo.id, :admin => true) } + before { user.permissions.create!(:repository_id => repo.id, :admin => true, :push => true) } it 'POST /repos/:id/key' do expect { @@ -22,6 +23,14 @@ describe 'Repos' do response = post "/repos/#{repo.slug}/key", {}, headers }.to change { repo.reload.key.private_key } end + + it 'allows to get settings' do + repo.settings.replace('foo' => { 'type' => 'password', 'value' => 'abc123' }) + repo.save + + response = get "repos/#{repo.id}/settings", {}, headers + JSON.parse(response.body).should == { 'settings' => { 'foo' => { 'type' => 'password', 'value' => '∗∗∗∗∗∗' } } } + end end describe 'without authenticated user' do