forked from appelier/bigtuna
/
project.rb
168 lines (143 loc) · 4.34 KB
/
project.rb
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class Project < ActiveRecord::Base
STATUS_NOT_BUILT = "status_not_built"
attr_accessor :hook_update
has_many :builds, :dependent => :destroy
has_many :step_lists, :dependent => :destroy
before_destroy :remove_build_folder
before_update :rename_build_folder
before_create :set_default_build_counts
after_save :update_hooks
validates :hook_name, :uniqueness => {:allow_blank => true}
validates :name, :presence => true, :uniqueness => true
validates :vcs_type, :inclusion => BigTuna.vcses.map { |e| e::VALUE }
validates :vcs_source, :presence => true
validates :vcs_branch, :presence => true
validate :validate_vcs_incremental_support
acts_as_list
def self.ajax_reload?
case BigTuna.ajax_reload
when "always" then true
when "building" then self.all.map(&:recent_build).compact.map(&:ajax_reload?).include?(true)
else false
end
end
def ajax_reload?
case BigTuna.ajax_reload
when "always" then true
when "building" then self.recent_build.try(:ajax_reload?)
else false
end
end
def recent_build
builds.order("created_at DESC").first
end
def build!
new_total_builds = self.total_builds + 1
ActiveRecord::Base.transaction do
build = self.builds.create!({:scheduled_at => Time.now, :build_no => new_total_builds})
self.update_attributes!(:total_builds => new_total_builds)
remove_project_jobs_in_queue()
Delayed::Job.enqueue(build)
end
end
def hooks
hook_hash = {}
BigTuna.hooks.each do |hook|
hook_hash[hook::NAME] = hook
end
Hook.where(:project_id => self.id)
end
def build_dir
build_dir_from_name(name)
end
def truncate_builds!
builds.order("created_at DESC").offset(self.max_builds).each do |build|
build.destroy
end
end
def max_builds
super || 10
end
def status
build = recent_build
build.nil? ? STATUS_NOT_BUILT : build.status
end
def vcs
return @vcs if @vcs
klass = BigTuna.vcses.find { |e| e::VALUE == vcs_type }
raise ArgumentError.new("VCS not supported: %p" % [vcs_type]) if klass.nil?
@vcs = klass.new(self.vcs_source, self.vcs_branch)
end
def to_param
[self.id, self.name.to_url].join("-")
end
def stability
last_builds = builds.order("created_at DESC").
where(:status => [Build::STATUS_OK, Build::STATUS_FAILED, Build::STATUS_BUILDER_ERROR]).
limit(5)
statuses = last_builds.map { |e| e.status }
return -1 if statuses.size < 5
statuses.count { |e| e == Build::STATUS_OK }
end
def hooks=(hooks)
@_hooks = hooks
end
def fetch_type
if self[:fetch_type]
self[:fetch_type].to_sym
else
:clone
end
end
private
def build_dir_from_name(name)
if BigTuna.build_dir[0] == '/'[0]
File.join(BigTuna.build_dir, name.downcase.gsub(/[^A-Za-z0-9]/, "_"))
else
File.join(Rails.root, BigTuna.build_dir, name.downcase.gsub(/[^A-Za-z0-9]/, "_"))
end
end
def remove_build_folder
if File.directory?(self.build_dir)
FileUtils.rm_rf(self.build_dir)
else
Rails.logger.debug("[BigTuna] Couldn't find build dir: %p" % [self.build_dir])
end
end
def rename_build_folder
if self.name_changed? && self.total_builds != 0
old_dir = build_dir_from_name(self.name_was)
new_dir = build_dir_from_name(self.name)
FileUtils.mv(old_dir, new_dir)
end
end
def set_default_build_counts
self.total_builds = 0
self.failed_builds = 0
end
def update_hooks
return unless self.hook_update
new_hooks = (@_hooks || {}).keys
current_hooks = self.hooks.map { |e| e.hook_name }
to_remove = current_hooks - new_hooks
to_add = new_hooks - current_hooks
Hook.where(:project_id => self.id, :hook_name => to_remove).delete_all
to_add.each do |name|
Hook.create!(:project => self, :hook_name => name)
end
end
def remove_project_jobs_in_queue
jobs_to_destroy = []
Delayed::Job.all.each do |job|
build = job.payload_object
next unless build.is_a?(Build)
if build.status = Build::STATUS_IN_QUEUE && build.project == self
jobs_to_destroy << job
end
end
jobs_to_destroy.each { |job| job.destroy }
end
def validate_vcs_incremental_support
errors.add(:fetch_type, " #{fetch_type} not support by the vcs") if fetch_type == :incremental && !vcs.support_incremental_build?
end
end