1 # $Id$ |
2 # To change this template, choose Tools | Templates |
3 # and open the template in the editor. |
4 |
5 require "uri" |
6 require 'net/http' |
7 |
8 module HudsonHelper |
9 |
10 def open_hudson_api( uri, auth_user, auth_password ) |
11 |
12 begin |
13 http = create_http_connection(uri) |
14 request = create_http_request(uri, auth_user, auth_password) |
15 rescue => error |
16 raise HudsonApiException.new(error) |
17 end |
18 |
19 begin |
20 response = http.request(request) |
21 rescue Timeout::Error, StandardError => error |
22 raise HudsonApiException.new(error) |
23 end |
24 |
25 case response |
26 when Net::HTTPSuccess, Net::HTTPFound |
27 return response.body |
28 else |
29 raise HudsonApiException.new(response) |
30 end |
31 end |
32 |
33 def check_box_to_boolean(item) |
34 return false unless item |
35 return false if "0" == item |
36 return false if "false" == item |
37 return true |
38 end |
39 |
40 def is_today?(value) |
41 return false unless value |
42 |
43 value_time = Time.parse(value.to_s, 0) rescue nil |
44 return false unless value_time |
45 |
46 today = Time.now |
47 return today.strftime("%Y/%m/%d") == value_time.strftime("%Y/%m/%d") |
48 end |
49 |
50 def create_http_connection(uri) |
51 |
52 param = URI.parse( URI.escape(uri) ) |
53 |
54 if "https" == param.scheme then |
55 param.port = 443 if param.port == nil || param.port == "" |
56 end |
57 |
58 retval = Net::HTTP.new(param.host, param.port) |
59 |
60 if "https" == param.scheme then |
61 retval.use_ssl = true |
62 retval.verify_mode = OpenSSL::SSL::VERIFY_NONE |
63 end |
64 |
65 return retval |
66 |
67 end |
68 |
69 def create_http_request(uri, auth_user, auth_password) |
70 |
71 param = URI.parse( URI.escape(uri) ) |
72 |
73 getpath = param.path |
74 getpath += "?" + param.query if param.query != nil && param.query.length > 0 |
75 |
76 retval = Net::HTTP::Get.new(getpath) |
77 retval.basic_auth(auth_user, auth_password) if auth_user != nil && auth_user.length > 0 |
78 |
79 return retval |
80 |
81 end |
82 |
83 def generate_atom_content(job) |
84 tag = "" |
85 tag = job.latest_build.error if "" != job.latest_build.error |
86 if "" == job.latest_build.error |
87 |
88 icon = "#{job.state}.gif" |
89 icon = "grey.gif" if job.state == "disabled" |
90 |
91 tag << image_tag("#{job.settings.url}images/24x24/#{icon}") |
92 tag << " " |
93 |
94 if "" != job.latest_build.number |
95 tag << link_to("##{job.latest_build.number}",job.latest_build.url_for(:user)) |
96 tag << " " |
97 tag << content_tag("span", job.latest_build.result, |
98 :class => "result #{job.latest_build.result.downcase}") if true != job.latest_build.building? && "" != job.latest_build.result |
99 tag " " |
100 tag << content_tag("span", l(:notice_building), :class => "result") if job.latest_build.building? |
101 tag << " " |
102 tag << content_tag("span", job.latest_build.finished_at.localtime.strftime("%Y/%m/%d %H:%M:%S")) |
103 end |
104 tag << l(:notice_no_builds) if "" == job.latest_build.number |
105 end |
106 |
107 tag << "<ul class=\"job-health-reports\">" |
108 job.health_reports.each do |report| |
109 tag << "<li>#{link_to(report.description, report.url)} #{report.score}" if report.url != "" |
110 tag << "<li>#{report.description} #{report.score}%" if report.url == "" |
111 end |
112 tag << "</ul>" |
113 return tag |
114 end |
115 |
116 end |