foca / integrity

Don't mind this, go to http://github.com/integrity/integrity

This URL has Read+Write access

integrity / lib / integrity / project.rb
100644 88 lines (72 sloc) 2.32 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module Integrity
  class Project
    include DataMapper::Resource
 
    property :id, Integer, :serial => true
    property :name, String, :nullable => false
    property :permalink, String
    property :uri, URI, :nullable => false, :length => 255
    property :branch, String, :nullable => false, :default => "master"
    property :command, String, :nullable => false, :length => 255, :default => "rake"
    property :public, Boolean, :default => true
    property :building, Boolean, :default => false
    property :created_at, DateTime
    property :updated_at, DateTime
 
    has n, :builds, :class_name => "Integrity::Build"
    has n, :notifiers, :class_name => "Integrity::Notifier"
 
    before :save, :set_permalink
    before :destroy, :delete_code
 
    validates_is_unique :name
 
    def build(commit_identifier="HEAD")
      return if building?
      update_attributes(:building => true)
      Builder.new(self).build(commit_identifier)
    ensure
      update_attributes(:building => false)
      send_notifications
    end
 
    def last_build
      builds.last
    end
 
    def previous_builds
      return [] if builds.size <= 1
      builds.all(:order => [:created_at.desc], :offset => 1, :limit => builds.size - 1)
    end
 
    def status
      last_build && last_build.status
    end
 
    def public=(flag)
      attribute_set(:public, !!flag)
    end
    
    def config_for(notifier)
      notifier = notifiers.first(:name => notifier.to_s.split(/::/).last)
      notifier.blank? ? {} : notifier.config
    end
    
    def notifies?(notifier)
      !notifiers.first(:name => notifier.to_s.split(/::/).last).blank?
    end
    
    def enable_notifiers(*args)
      Notifier.enable_notifiers(id, *args)
    end
    
    private
      def set_permalink
        self.permalink = (name || "").downcase.
          gsub(/'s/, "s").
          gsub(/&/, "and").
          gsub(/[^a-z0-9]+/, "-").
          gsub(/-*$/, "")
      end
 
      def delete_code
        builds.destroy!
        Builder.new(self).delete_code
      end
      
      def send_notifications
        notifiers.each do |notifier|
          begin
            notifier.notify_of_build last_build
          rescue Timeout::Error
            next
          end
        end
      end
  end
end