diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..262c08ba --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--colour +--tty +--format documentation diff --git a/Gemfile b/Gemfile index cf09b3c1..da99c923 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ ruby '2.1.7' if ENV.key?('DYNO') gem 's3', github: 'travis-ci/s3' -gem 'travis-core', github: 'travis-ci/travis-core', ref: 'sf-ar-te' +gem 'travis-core', github: 'travis-ci/travis-core' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-amqp', github: 'travis-ci/travis-amqp' gem 'travis-config', '~> 0.1.0' diff --git a/Gemfile.lock b/Gemfile.lock index 665485d0..f7765800 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,8 +48,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: fdcd69981dc9ccb6f85452213d8bdc096f4308be - ref: sf-ar-te + revision: a66c345d44fd9c28884d694acfff3b1a0fbc5232 specs: travis-core (0.0.1) actionmailer (~> 3.2.19) @@ -391,4 +390,4 @@ DEPENDENCIES yard-sinatra! BUNDLED WITH - 1.12.0.pre.1 + 1.11.2 diff --git a/README.md b/README.md index 890a583d..56e8af0c 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ $ bundle exec rspec ```sh-session $ bundle exec script/server ``` + If you have problems with Nginx because the websocket is already in use, try restarting your computer. + ## Contributing 1. Fork it diff --git a/lib/travis/api/attack.rb b/lib/travis/api/attack.rb index 17fcd44a..d210b633 100644 --- a/lib/travis/api/attack.rb +++ b/lib/travis/api/attack.rb @@ -50,9 +50,9 @@ class Rack::Attack # Ban time: 5 hours # Ban after: 10 POST requests within five minutes to /auth/github blacklist('hammering /auth/github') do |request| - Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 2, findtime: 5.minutes, bantime: bantime(5.hours)) do - request.post? and request.path == '/auth/github' - end + Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 2, findtime: 5.minutes, bantime: bantime(5.hours)) do + request.post? and request.path == '/auth/github' + end end #### @@ -60,9 +60,9 @@ class Rack::Attack # Ban time: 1 hour # Ban after: 10 POST requests within 30 seconds blacklist('spamming with POST requests') do |request| - Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 10, findtime: 30.seconds, bantime: bantime(1.hour)) do - request.post? and not POST_WHITELISTED.include? request.path - end + Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 10, findtime: 30.seconds, bantime: bantime(1.hour)) do + request.post? and not POST_WHITELISTED.include? request.path + end end diff --git a/lib/travis/api/v3/models/repository.rb b/lib/travis/api/v3/models/repository.rb index ed35b5a2..22c26d71 100644 --- a/lib/travis/api/v3/models/repository.rb +++ b/lib/travis/api/v3/models/repository.rb @@ -62,5 +62,9 @@ module Travis::API::V3 __send__(name, *args, &block) end + + def settings + @settings ||= JSON.load(super) + end end end diff --git a/lib/travis/api/v3/services/requests/create.rb b/lib/travis/api/v3/services/requests/create.rb index c3353daf..ea3827de 100644 --- a/lib/travis/api/v3/services/requests/create.rb +++ b/lib/travis/api/v3/services/requests/create.rb @@ -1,7 +1,7 @@ module Travis::API::V3 class Services::Requests::Create < Service TIME_FRAME = 1.hour - LIMIT = 10 + LIMIT = 10 private_constant :TIME_FRAME, :LIMIT result_type :request @@ -22,14 +22,19 @@ module Travis::API::V3 accepted(remaining_requests: remaining, repository: repository, request: payload) end - def limit - Travis.config.requests_create_api_limit || LIMIT + def limit(repository) + if repository.settings.nil? + Travis.config.requests_create_api_limit || LIMIT + else + repository.settings["api_builds_rate_limit"] || Travis.config.requests_create_api_limit || LIMIT + end end def remaining_requests(repository) - return limit if access_control.full_access? + api_builds_rate_limit = limit(repository) + return api_builds_rate_limit if access_control.full_access? count = query(:requests).count(repository, TIME_FRAME) - count > limit ? 0 : limit - count + count > api_builds_rate_limit ? 0 : api_builds_rate_limit - count end end end diff --git a/spec/v3/services/requests/create_spec.rb b/spec/v3/services/requests/create_spec.rb index 23fd8460..c0dfb035 100644 --- a/spec/v3/services/requests/create_spec.rb +++ b/spec/v3/services/requests/create_spec.rb @@ -239,6 +239,26 @@ describe Travis::API::V3::Services::Requests::Create do } end + describe "overrides default request limit if included in repository.settings" do + before { repo.update_attribute(:settings, { api_builds_rate_limit: 12 }.to_json) } + + before { 10.times { repo.requests.create(event_type: 'api', result: 'accepted') } } + before { post("/v3/repo/#{repo.id}/requests", {}, headers) } + + example { expect(last_response.status).to be == 202 } + example { expect(JSON.load(body).to_s).to include( + "@type", + "repository", + "remaining_requests", + "2", + "request", + "representation", + "minimal", + "slug", + "svenfuchs/minimal") + } + end + describe "passing the token in params" do let(:params) {{ request: { token: 'foo-bar' }}} example { expect(sidekiq_params[:credentials]).to be == {