Use travis-settings to manage JSON settings field

Since we use repository.settings as a kind of dump for all
sorts of settings, some user-facing and some not, this lets us
leave the db as it is, but pretend to have separate models for each
"kind" of setting.
This commit is contained in:
Joe Corcoran 2016-06-09 16:21:55 +02:00
parent cf5ea374d4
commit 026dc4cb98
7 changed files with 34 additions and 17 deletions

View File

@ -11,6 +11,7 @@ gem 'travis-settings', github: 'travis-ci/travis-settings'
gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs'
gem 'travis-yaml', github: 'travis-ci/travis-yaml'
gem 'travis-settings', github: 'travis-ci/travis-settings'
gem 'mustermann', github: 'rkh/mustermann'
gem 'sinatra'
gem 'sinatra-contrib', require: nil #github: 'sinatra/sinatra-contrib', require: nil

View File

@ -53,6 +53,14 @@ GIT
activemodel
virtus
GIT
remote: git://github.com/travis-ci/travis-settings.git
revision: d510e63b6c6f059cccae141c265e7a0c7236d1fd
specs:
travis-settings (0.0.1)
activemodel
virtus
GIT
remote: git://github.com/travis-ci/travis-sidekiqs.git
revision: c5d4a4abc6c3737f9c43d3333efb94daa18b9fbb

View File

@ -0,0 +1,5 @@
module Travis::API::V3
class Models::AdminSettings < Travis::Settings::Model
attribute :api_builds_rate_limit, Integer
end
end

View File

@ -65,7 +65,15 @@ module Travis::API::V3
end
def settings
@settings ||= JSON.load(super)
@settings ||= JSON.load(super || '{}'.freeze)
end
def user_settings
@user_settings ||= Models::UserSettings.new(settings)
end
def admin_settings
@admin_settings ||= Models::AdminSettings.new(settings)
end
end
end

View File

@ -1,12 +1,5 @@
module Travis::API::V3
class Models::Settings
DEFAULTS = {
'builds_only_with_travis_yml' => false,
'build_pushes' => true,
'build_pull_requests' => true,
'maximum_number_of_builds' => 0
}.freeze
attr_reader :repository
def initialize(repository)
@ -14,13 +7,11 @@ module Travis::API::V3
end
def to_h
DEFAULTS.merge(repository.settings || {})
repository.user_settings.to_hash
end
def update(settings = {})
settings = to_h.merge(settings)
repository.settings.clear
settings.each { |k, v| repository.settings[k] = v }
repository.user_settings.update(settings)
repository.save!
end
end

View File

@ -0,0 +1,8 @@
module Travis::API::V3
class Models::UserSettings < Travis::Settings::Model
attribute :builds_only_with_travis_yml, Boolean, default: false
attribute :build_pushes, Boolean, default: true
attribute :build_pull_requests, Boolean, default: true
attribute :maximum_number_of_builds, Integer, default: 0
end
end

View File

@ -23,11 +23,7 @@ module Travis::API::V3
end
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
repository.admin_settings.api_builds_rate_limit || Travis.config.requests_create_api_limit || LIMIT
end
def remaining_requests(repository)