Skip to content
Browse files

port tagging feature to new models

  • Loading branch information...
1 parent 3fd9ae3 commit bfd3106ae250d75f1071bcb0810e398dc1621e1e @svenfuchs svenfuchs committed
View
2 app/models/build.rb
@@ -1,7 +1,7 @@
require 'core_ext/active_record/base'
class Build < ActiveRecord::Base
- include Matrix, Notifications, SimpleStates, Tagging, Travis::Notifications
+ include Matrix, Notifications, SimpleStates, Travis::Notifications
PER_PAGE = 10
View
31 app/models/build/tagging.rb
@@ -1,31 +0,0 @@
-class Build
- module Tagging
- def add_tags
- tags_to_add =[]
- # Load rules.yml file from config
- rules.each do |key, rule|
- # Try to match each rule on log or config Build attribute
- if log.to_s + config.to_s =~ /#{rule['pattern']}/
- tags_to_add << key
- end
- end
- update_attribute(:tags, tags_to_add.uniq.join(',')) unless tags_to_add.empty?
- end
-
- def rules
- rules = read_yml_rules
- end
-
- def read_yml_rules
- YAML.load_file(File.expand_path(path))
- end
-
- def path(environment = nil)
- filename = 'rules.yml'
- path = "./config/#{filename}"
- return path if File.exists?(path)
- raise "Could not find a configuration file. Valid paths are: #{paths.join(', ')}"
- end
-
- end
-end
View
20 app/models/task/tagging.rb
@@ -0,0 +1,20 @@
+module Task::Tagging
+ class << self
+ def rules
+ @@rules ||= YAML.load_file('./config/tagging.yml')
+ end
+ end
+
+ def add_tags
+ subject = log.to_s + config.to_s
+ tags = Task::Tagging.rules.inject([]) do |result, rule|
+ result << rule['tag'] if subject =~ /#{rule['pattern']}/
+ result
+ end
+ self.tags = tags.uniq.join(',') if tags.present?
+ end
+
+ def rules
+ rules = read_yml_rules
+ end
+end
View
4 app/models/task/test.rb
@@ -1,9 +1,11 @@
# This task is owned by a Build
class Task::Test < Task
+ include Tagging
+
states :created, :started, :cloned, :installed, :finished
event :start, :to => :started, :after => :propagate
- event :finish, :to => :finished, :after => :propagate
+ event :finish, :to => :finished, :after => [:add_tags, :propagate]
def start(data = {})
self.started_at = data[:started_at]
View
6 config/rules.yml
@@ -1,6 +0,0 @@
-MissingTravisFile:
- pattern: no Travis file
- message: We havent found any travis.yml file
-MissingRakeFile:
- pattern: no RakeFile
- message: We haven't found any RakeFile
View
5 config/tagging.yml
@@ -0,0 +1,5 @@
+- tag: rake_not_bundled
+ pattern: rake is not part of the bundle
+- tag: database_missing
+ pattern: database "[^"]*" does not exist
+
View
9 db/migrate/20110819232908_add_tags_to_build.rb
@@ -1,9 +0,0 @@
-class AddTagsToBuild < ActiveRecord::Migration
- def self.up
- add_column :builds, :tags,:text
- end
-
- def self.down
- remove_column :builds, :tags
- end
-end
View
9 db/migrate/20110819232908_tasks_add_tags.rb
@@ -0,0 +1,9 @@
+class TasksAddTags < ActiveRecord::Migration
+ def self.up
+ add_column :tasks, :tags, :text
+ end
+
+ def self.down
+ remove_column :tasks, :tags
+ end
+end
View
14 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110805030147) do
+ActiveRecord::Schema.define(:version => 20110819232908) do
create_table "builds", :force => true do |t|
t.integer "repository_id"
@@ -45,17 +45,6 @@
t.string "author_email"
t.datetime "created_at"
t.datetime "updated_at"
-<<<<<<< HEAD
-=======
- t.integer "parent_id"
- t.text "config"
- t.string "ref"
- t.string "branch"
- t.text "github_payload"
- t.string "compare_url"
- t.string "token"
- t.text "tags"
->>>>>>> 41a5023... Adding tags based on regexp pattern on Build.log and Build.config
end
create_table "rails_admin_histories", :force => true do |t|
@@ -121,6 +110,7 @@
t.datetime "finished_at"
t.datetime "created_at"
t.datetime "updated_at"
+ t.text "tags"
end
create_table "tokens", :force => true do |t|
View
23 spec/models/build/tagging_spec.rb
@@ -1,23 +0,0 @@
-require 'spec_helper'
-
-describe Build::Tagging do
- let(:build) { Factory(:build) }
-
- it "Automatic MissingTravisFile tag addition " do
- log = "$ no Travis file git clone --depth=1000 --quiet git://github.com/intridea/omniauth.git ~/builds/intridea/omniauth\n"
- build.update_attributes!(:log => log)
-
- build.add_tags
- assert_equal build.tags, "MissingTravisFile"
- end
-
- it "Automatic MissingRakeFile tag addition " do
- log = "$ no RakeFile git clone --depth=1000 --quiet git://github.com/intridea/omniauth.git ~/builds/intridea/omniauth\n"
- build.update_attributes!(:log => log)
-
- build.add_tags
- assert_equal build.tags, "MissingRakeFile"
- end
-end
-
-
View
29 spec/models/task/tagging.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Task::Tagging do
+ before :each do
+ rules = YAML.load <<-yml
+ - tag: rake_not_bundled
+ pattern: rake is not part of the bundle
+ - tag: database_missing
+ pattern: database "[^"]*" does not exist
+ yml
+ Task::Tagging.stubs(:rules).returns(rules)
+ end
+
+ let(:test) { Factory(:build).matrix.first }
+
+ describe :add_tags do
+ it 'tags the task according to the rules' do
+ test.update_attributes! :log => <<-log
+ in `block in replace_gem': rake is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
+ PGError: FATAL: database "data_migrations_test" does not exist
+ log
+
+ test.add_tags
+ test.tags.should == 'rake_not_bundled,database_missing'
+ end
+ end
+end
+
+

0 comments on commit bfd3106

Please sign in to comment.
Something went wrong with that request. Please try again.