From 734a7b9566c91fcbe6a3367aaa89fc229e4a227c Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sun, 19 Jun 2016 16:00:24 +0200 Subject: [PATCH] use travis-settings, remove travis/settings --- Gemfile | 3 +- Gemfile.lock | 11 +- vendor/travis-core/lib/travis/model/user.rb | 2 +- vendor/travis-core/lib/travis/settings.rb | 65 ------- .../lib/travis/settings/collection.rb | 79 -------- .../lib/travis/settings/encrypted_value.rb | 62 ------ .../travis-core/lib/travis/settings/model.rb | 39 ---- .../lib/travis/settings/model_extensions.rb | 177 ------------------ vendor/travis-core/lib/travis/states_cache.rb | 2 + 9 files changed, 15 insertions(+), 425 deletions(-) delete mode 100644 vendor/travis-core/lib/travis/settings.rb delete mode 100644 vendor/travis-core/lib/travis/settings/collection.rb delete mode 100644 vendor/travis-core/lib/travis/settings/encrypted_value.rb delete mode 100644 vendor/travis-core/lib/travis/settings/model.rb delete mode 100644 vendor/travis-core/lib/travis/settings/model_extensions.rb diff --git a/Gemfile b/Gemfile index 8bdd51f8..ab1dc3bf 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,11 @@ gemspec gem 's3', github: 'travis-ci/s3' -gem 'travis-core', path: 'vendor' +gem 'travis-core', path: 'vendor/travis-core' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-amqp', github: 'travis-ci/travis-amqp' gem 'travis-config', '~> 0.1.0' +gem 'travis-settings', github: 'travis-ci/travis-settings' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs' gem 'travis-yaml', github: 'travis-ci/travis-yaml' diff --git a/Gemfile.lock b/Gemfile.lock index 81285ce2..0dda637b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,6 +45,14 @@ GIT specs: travis-migrations (0.0.2) +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 @@ -93,7 +101,7 @@ PATH virtus (~> 1.0.0) PATH - remote: vendor + remote: vendor/travis-core specs: travis-core (0.0.1) activerecord (~> 3.2.19) @@ -403,6 +411,7 @@ DEPENDENCIES travis-config (~> 0.1.0) travis-core! travis-migrations! + travis-settings! travis-sidekiqs! travis-support! travis-yaml! diff --git a/vendor/travis-core/lib/travis/model/user.rb b/vendor/travis-core/lib/travis/model/user.rb index 692918d0..5a44a7ef 100644 --- a/vendor/travis-core/lib/travis/model/user.rb +++ b/vendor/travis-core/lib/travis/model/user.rb @@ -142,7 +142,7 @@ class User < Travis::Model end def invalid_github_scopes? - Travis.env == 'production' and (github_oauth_token_changed? or github_scopes.blank?) + Travis.env != 'test' and (github_oauth_token_changed? or github_scopes.blank?) end def set_as_recent diff --git a/vendor/travis-core/lib/travis/settings.rb b/vendor/travis-core/lib/travis/settings.rb deleted file mode 100644 index d81e0ab1..00000000 --- a/vendor/travis-core/lib/travis/settings.rb +++ /dev/null @@ -1,65 +0,0 @@ -require 'coercible' -require 'travis/settings/collection' -require 'travis/settings/model' -require 'travis/settings/model_extensions' - -module Travis - class Settings - include Virtus.model - include ActiveModel::Validations - include Travis::Settings::ModelExtensions - - def on_save(&block) - @on_save = block - self - end - - def merge(hash) - hash.each { |k, v| - set(k, v) unless collection?(k) || model?(k) - } - end - - def obfuscated - to_hash - end - - def save - if valid? - @on_save.call - end - end - - def to_json - to_hash.to_json - end - - def to_hash - result = super - - result.each do |key, value| - if value.respond_to?(:to_hash) - result[key] = value.to_hash - end - end - - result - end - end - - module DefaultSettings - def initialize(*) - super - - freeze - end - - def merge(*) - raise "merge is not supported on default settings" - end - - def set(key, value) - raise "setting values is not supported on default settings" - end - end -end diff --git a/vendor/travis-core/lib/travis/settings/collection.rb b/vendor/travis-core/lib/travis/settings/collection.rb deleted file mode 100644 index 8d378756..00000000 --- a/vendor/travis-core/lib/travis/settings/collection.rb +++ /dev/null @@ -1,79 +0,0 @@ -class Travis::Settings - class Collection - include Enumerable - - delegate :each, :<<, :push, :delete, :length, :first, :last, to: '@collection' - attr_accessor :additional_attributes - - class << self - # This feels a bit weird, but I don't know how to do it better. - # Virtus checks for collection type by checking an array member, - # so if you pass Array[String], a collection type will be set to String. - # Here, we already specify what is a model class for a collection. - # In order to not have to specify class twice, I created this method - # which creates just what Virtus needs. - def for_virtus - self[model_class] - end - - def [](*args) - new(*args) - end - - def model(model_name_or_class = nil) - if model_name_or_class - klass = if model_name_or_class.is_a?(String) || model_name_or_class.is_a?(Symbol) - name = model_name_or_class.to_s.classify - self.const_defined?(name, false) ? self.const_get(name, false) : Travis::Settings.const_get(name, false) - else - model_name_or_class - end - - @model_class = klass - else - @model_class - end - end - attr_reader :model_class - end - - delegate :model_class, to: 'self.class' - - def initialize(*args) - @collection = Array[*args] - end - - def create(attributes) - model = model_class.new(attributes) - model.load({}, additional_attributes) - model.id = SecureRandom.uuid unless model.id - push model - model - end - - def find(id) - detect { |model| model.id == id.to_s } - end - - def destroy(id) - record = find(id) - if record - delete record - record - end - end - - def to_hash - @collection.map(&:to_hash) - end - - def load(collection, additional_attributes = {}) - self.additional_attributes = additional_attributes - return unless collection.respond_to?(:each) - - collection.each do |element| - self.push model_class.load(element, additional_attributes) - end - end - end -end diff --git a/vendor/travis-core/lib/travis/settings/encrypted_value.rb b/vendor/travis-core/lib/travis/settings/encrypted_value.rb deleted file mode 100644 index a9b852e6..00000000 --- a/vendor/travis-core/lib/travis/settings/encrypted_value.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'virtus' - -class Travis::Settings - class EncryptedValue - include Virtus.value_object - attr_reader :value, :key - - values do - attribute :value, String - end - - def initialize(value) - if value.is_a? String - # a value is set through the accessor, not loaded in jason, we - # need to encrypt it and put into hash form - value = { value: encrypt(value) } - end - - super value - end - - def to_s - value - end - - def to_str - value - end - - def to_json(*) - as_json.to_json - end - - def as_json(*) - value - end - - def to_hash - value - end - - def inspect - "" - end - - def key - Travis.config.encryption.key - end - - def encrypt(value) - Travis::Model::EncryptedColumn.new(key: key, use_prefix: false).dump(value) - end - - def decrypt - Travis::Model::EncryptedColumn.new(key: key, use_prefix: false).load(value) - end - - def load(value, additional_attributes = nil) - self.instance_variable_set('@value', value) - end - end -end diff --git a/vendor/travis-core/lib/travis/settings/model.rb b/vendor/travis-core/lib/travis/settings/model.rb deleted file mode 100644 index 37b97f7c..00000000 --- a/vendor/travis-core/lib/travis/settings/model.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'virtus' -require 'travis/settings/encrypted_value' -require 'travis/settings/model_extensions' - -class Travis::Settings - class Model - include Virtus.model - include ActiveModel::Validations - include ModelExtensions - include ActiveModel::Serialization - - def attribute?(name) - attributes.keys.include?(name.to_sym) - end - - def read_attribute_for_serialization(name) - self.send(name) if attribute?(name) - end - - def read_attribute_for_validation(name) - return unless attribute?(name) - - value = self.send(name) - value.is_a?(EncryptedValue) ? value.to_s : value - end - - def update(attributes) - self.attributes = attributes - end - - def key - Travis.config.encryption.key - end - - def to_json - to_hash.to_json - end - end -end diff --git a/vendor/travis-core/lib/travis/settings/model_extensions.rb b/vendor/travis-core/lib/travis/settings/model_extensions.rb deleted file mode 100644 index 46f19a47..00000000 --- a/vendor/travis-core/lib/travis/settings/model_extensions.rb +++ /dev/null @@ -1,177 +0,0 @@ -module Travis - class Settings - module AccessorExtensions - def set(instance, value) - if instance.frozen? - raise 'setting values is not supported on default settings' - end - - super - end - - def get(instance) - if type.primitive <= Travis::Settings::EncryptedValue - unless instance.instance_variable_get(instance_variable_name) - value = Travis::Settings::EncryptedValue.new(nil) - if instance.frozen? - return value - else - set(instance, value) - end - end - end - - super instance - end - end - - module ModelExtensions - class Errors < ActiveModel::Errors - # Default behavior of Errors in Active Model is to - # translate symbolized message into full text message, - # using i18n if available. I don't want such a behavior, - # as I want to return error "codes" like :blank, not - # full text like "can't be blank" - def normalize_message(attribute, message, options) - message || :invalid - end - end - - module ClassMethods - def attribute(name, type = nil, options = {}) - options[:finalize] = false - - super name, type, options - - attribute = attribute_set[name] - attribute.extend(AccessorExtensions) - attribute.finalize - attribute.define_accessor_methods(attribute_set) - - self - end - - def load(json, additional_attributes = {}) - instance = new() - - json = JSON.parse(json) if json.is_a?(String) - instance.load json, additional_attributes - instance - end - end - - def self.included(base) - base.extend ClassMethods - end - - def additional_attributes - @additional_attributes || {} - end - - def additional_attributes=(hash = {}) - attribute_set.each do |attribute| - value = get(attribute.name) - if value.respond_to?(:additional_attributes=) - value.additional_attributes = hash - end - end - @additional_attributes = hash - end - - def errors - @errors ||= Errors.new(self) - end - - def attribute?(key) - attributes.keys.include? key.to_sym - end - - def to_hash - attributes.each_with_object({}) do |(name, value), hash| - hash[name] = value.respond_to?(:to_hash) ? value.to_hash : value - end - end - - def collection?(name) - # TODO: I don't like this type of class checking, it will be better to work - # based on an API contract, but it should do for now - if attribute = attribute_set[name.to_sym] - attribute.type.primitive <= Travis::Settings::Collection - end - end - - def encrypted?(name) - if attribute = attribute_set[name.to_sym] - attribute.type.primitive <= Travis::Settings::EncryptedValue - end - end - - def model?(name) - if attribute = attribute_set[name.to_sym] - attribute.type.primitive <= Travis::Settings::Model - end - end - - def primitive(name) - attribute_set[name.to_sym].type.primitive - end - - def get(key) - if attribute?(key) - self.send(key) - end - end - - def set(key, value) - if attribute?(key) - self.send("#{key}=", value) - end - end - - def simple_attributes - attributes.select { |k, v| simple_attribute?(k) } - end - - def simple_attribute?(key) - !(collection?(key) || encrypted?(key) || model?(key)) - end - - def load(hash = {}, additional_attributes = {}) - hash ||= {} - self.additional_attributes = additional_attributes || {} - - hash.merge(self.additional_attributes).each do |key, value| - if collection?(key) || encrypted?(key) || model?(key) - thing = get(key) - thing = set(key, primitive(key).new) if !thing && value - thing.load(value, self.additional_attributes) if thing - elsif attribute?(key) - set(key, value) - end - end - end - - def create(key, attributes) - attributes = (attributes || {}).merge(additional_attributes || {}) - set(key, primitive(key).new(attributes)) - get(key) - end - - def delete(key) - model = get(key) - set(key, nil) - model - end - - def update(key, attributes) - attributes = (attributes || {}).merge(additional_attributes || {}) - if model = get(key) - model.update(attributes) - model - else - create(key, attributes) - end - end - end - end -end diff --git a/vendor/travis-core/lib/travis/states_cache.rb b/vendor/travis-core/lib/travis/states_cache.rb index 145e14c8..03a43390 100644 --- a/vendor/travis-core/lib/travis/states_cache.rb +++ b/vendor/travis-core/lib/travis/states_cache.rb @@ -3,6 +3,8 @@ require 'connection_pool' require 'active_support/core_ext/module/delegation' require 'travis/api/serialize' +# TODO compare commit history to travis-states-cache, and start using it + module Travis class StatesCache class CacheError < StandardError; end