1 # $Id$ |
2 |
3 require 'hudson_api_error' |
4 require 'hudson_exceptions' |
5 |
6 class Hudson |
7 unloadable |
8 |
9 include HudsonHelper |
10 include RexmlHelper |
11 |
12 attr_accessor :project_id, :settings, :jobs |
13 attr_reader :project, :hudson_api_errors |
14 |
15 def api_url_for(type = :user) |
16 return "" unless @settings |
17 return "" if @settings.url_for(type) == nil || @settings.url_for(type).length == 0 |
18 return "#{@settings.url_for(type)}api" |
19 end |
20 |
21 def initialize(project_id) |
22 @project_id = project_id |
23 @project = Project.find(project_id) |
24 @settings = HudsonSettings.find_by_project_id(@project_id) |
25 find_jobs |
26 clear_hudson_api_errors |
27 end |
28 |
29 def fetch |
30 clear_hudson_api_errors |
31 |
32 fetch_jobs |
33 |
34 # ���������JOB������������������������������������������������ |
35 find_jobs |
36 |
37 return unless @hudson_api_errors.empty? |
38 |
39 fetch_buildresults |
40 rescue HudsonApiException => error |
41 @hudson_api_errors << HudsonApiError.new(self.class.name, "fetch", error) |
42 end |
43 |
44 def get_job(job_name) |
45 job = self.jobs.find{|job| job.name == job_name } |
46 return HudsonNoJob.new(:name => job_name, :settings => @settings) unless job |
47 return job |
48 end |
49 |
50 def add_job(job_name) |
51 retval = HudsonJob.new() |
52 retval.name = job_name |
53 retval.project_id = self.project_id |
54 retval.hudson_id = self.settings.id |
55 retval.project = self.project |
56 self.jobs << retval |
57 return retval |
58 end |
59 |
60 private |
61 def clear_hudson_api_errors |
62 @hudson_api_errors = [] |
63 end |
64 |
65 def fetch_jobs |
66 content = "" |
67 begin |
68 # job/build, view, primaryView ��������� |
69 api_url = "#{api_url_for(:plugin)}/xml?depth=1" + |
70 "&xpath=/hudson" + |
71 "&exclude=/hudson/view" + |
72 "&exclude=/hudson/primaryView" + |
73 "&exclude=/hudson/job/build" + |
74 "&exclude=/hudson/job/lastCompletedBuild" + |
75 "&exclude=/hudson/job/lastStableBuild" + |
76 "&exclude=/hudson/job/lastSuccessfulBuild" |
77 content = open_hudson_api(api_url, @settings.auth_user, @settings.auth_password) |
78 rescue HudsonApiException => error |
79 raise error |
80 end |
81 |
82 doc = REXML::Document.new content |
83 |
84 doc.elements.each("hudson/job") do |element| |
85 job_name = get_element_value(element, "name") |
86 next unless self.settings.job_include?(job_name) |
87 |
88 job = self.get_job(job_name) |
89 job = add_job(job_name) if job.is_a?(HudsonNoJob) |
90 |
91 job.update_by_xml(element) |
92 job.update_health_report_by_xml(element) |
93 job.save! |
94 end |
95 |
96 end |
97 |
98 def fetch_buildresults |
99 |
100 self.jobs.each do |job| |
101 next unless self.settings.job_include?(job.name) |
102 |
103 job.fetch_builds |
104 |
105 @hudson_api_errors += job.hudson_api_errors unless job.hudson_api_errors.empty? |
106 |
107 end |
108 |
109 end |
110 |
111 def find_jobs |
112 @jobs = HudsonJob.find :all, |
113 :order => "#{HudsonJob.table_name}.name", |
114 :conditions => ["#{HudsonJob.table_name}.project_id = ?", @project_id], |
115 :include => :job_settings |
116 end |
117 |
118 end |
119 |
120 def Hudson.find(*args) |
121 case args.first |
122 when :all then |
123 retval = [] |
124 HudsonSettings.find(*args).each do |settings| |
125 next unless Project.find_by_id(settings.project_id) |
126 retval << Hudson.new(settings.project_id) |
127 end |
128 return retval |
129 else |
130 settings = HudsonSettings.find(*args) |
131 return nil unless Project.find_by_id(settings.project_id) |
132 retval = Hudson.new(settings.project_id) |
133 return retval |
134 end |
135 end |
136 |
137 def Hudson.find_by_project_id(project_id) |
138 retval = Hudson.new(project_id) |
139 return retval |
140 end |
141 |
142 def Hudson.fetch |
143 hudsons = find(:all) |
144 hudsons.each do |hudson| |
145 hudson.fetch |
146 next if hudson.hudson_api_errors.empty? |
147 hudson.hudson_api_errors.each do |error| |
148 $stderr.print "redmine_hudson: #{hudson.project.name}(#{hudson.settings.url_for(:plugin)}) #{error.class_name}:#{error.method_name} #{error.exception.message}\n" |
149 end |
150 end |
151 end |
152 |
153 def Hudson.autofetch? |
154 return false unless Setting.plugin_redmine_hudson['autofetch'] |
155 return false if Setting.plugin_redmine_hudson['autofetch'] == "" |
156 return true |
157 end |
158 |
159 def Hudson.job_description_format |
160 return "hudson" unless Setting.plugin_redmine_hudson['job_description_format'] |
161 return "hudson" if Setting.plugin_redmine_hudson['job_description_format'] == "" |
162 return Setting.plugin_redmine_hudson['job_description_format'] |
163 end |
164 |
165 def Hudson.query_limit_builds_each_job |
166 return 100 unless Setting.plugin_redmine_hudson['query_limit_builds_each_job'] |
167 return 100 if Setting.plugin_redmine_hudson['query_limit_builds_each_job'] !~ /^[0-9]+$/ |
168 return Setting.plugin_redmine_hudson['query_limit_builds_each_job'].to_i |
169 end |
170 |
171 def Hudson.query_limit_changesets_each_job |
172 return 100 unless Setting.plugin_redmine_hudson['query_limit_changesets_each_job'] |
173 return 100 if Setting.plugin_redmine_hudson['query_limit_changesets_each_job'] !~ /^[0-9]+$/ |
174 return Setting.plugin_redmine_hudson['query_limit_changesets_each_job'].to_i |
175 end |
176 |