Class: ConfluenceReporter::Reporter
- Inherits:
-
Object
- Object
- ConfluenceReporter::Reporter
- Defined in:
- lib/confluence_reporter.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#body_message ⇒ Object
Returns the value of attribute body_message.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #append_to_page(page_id, parrent_page_id) ⇒ Object
- #clear_log ⇒ Object
-
#create_page(title, space, parrent_page_id = nil) ⇒ Object
Creates new page with title set, if parrent_page_id is provided it adjusts ancestor accordingly and the same space short key.
- #find_page_by_id(page_id) ⇒ Object
- #find_page_by_name(name, parrent_page_id = nil) ⇒ Object
-
#initialize(base_url, user, password) ⇒ Reporter
constructor
A new instance of Reporter.
- #log(message, print = true) ⇒ Object
- #log_structured_code(message, print = true) ⇒ Object
-
#report_event(name, parrent_page_id = nil, space = nil) ⇒ Object
appends the log to confluence page if found, if not creates new page clears the log.
Constructor Details
#initialize(base_url, user, password) ⇒ Reporter
Returns a new instance of Reporter.
29 30 31 32 33 34 |
# File 'lib/confluence_reporter.rb', line 29 def initialize(base_url, user, password) @body_message = "<h2>#{Time.now}</h2>" @base_url = base_url @user = user @password = password end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
27 28 29 |
# File 'lib/confluence_reporter.rb', line 27 def base_url @base_url end |
#body_message ⇒ Object
Returns the value of attribute body_message.
27 28 29 |
# File 'lib/confluence_reporter.rb', line 27 def @body_message end |
#password ⇒ Object
Returns the value of attribute password.
27 28 29 |
# File 'lib/confluence_reporter.rb', line 27 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
27 28 29 |
# File 'lib/confluence_reporter.rb', line 27 def user @user end |
Instance Method Details
#append_to_page(page_id, parrent_page_id) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/confluence_reporter.rb', line 174 def append_to_page(page_id, parrent_page_id) page = find_page_by_id(page_id) page["version"]["number"] = page["version"]["number"].to_i + 1 page["body"]["storage"]["value"] = (page["body"]["storage"]["value"] + "#{ @body_message.to_json.gsub(/\\u001b.../, " ") }").force_encoding('UTF-8') page['ancestors'] = [{'type' => 'page', 'id' => parrent_page_id}] uri = URI.parse(@base_url + "#{page_id}") https = Net::HTTP.new(uri.host,uri.port) https.use_ssl = true # https.set_debug_output $stderr req = Net::HTTP::Put.new(uri.path, initheader = {'Content-Type' =>'application/json'}) req.basic_auth(@user, @password) req['Accept'] = 'application/json' req.body = "#{page.to_json}" response = https.request(req) response = JSON.parse(response.body) if response["statusCode"] == 400 puts response.inspect puts req.body.inspect puts "Append to page: Error reporting to confluence: #{response["message"]}" raise "Append to page: Error reporting to confluence: #{response["message"]}" else puts "Reported page update." end end |
#clear_log ⇒ Object
199 200 201 |
# File 'lib/confluence_reporter.rb', line 199 def clear_log @body_message = "" end |
#create_page(title, space, parrent_page_id = nil) ⇒ Object
Creates new page with title set, if parrent_page_id is provided it adjusts ancestor accordingly and the same space short key
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 169 170 171 172 |
# File 'lib/confluence_reporter.rb', line 139 def create_page(title, space, parrent_page_id=nil) params = { 'type' => 'page', 'title' => title, 'space' => {'key' => space}, 'body' => { 'storage' => { 'value' => ("#{ @body_message.to_json.gsub("&&", "&&").gsub(/\\u001b.../, " ") }").force_encoding('UTF-8'), 'representation' => 'storage' } } } if parrent_page_id params['ancestors'] = [{'type' => 'page', 'id' => parrent_page_id}] end uri = URI.parse(@base_url) https = Net::HTTP.new(uri.host,uri.port) https.use_ssl = true # https.set_debug_output $stderr req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'}) req.basic_auth(@user, @password) req['Accept'] = 'application/json' req.body = "#{params.to_json}" response = https.request(req) response = JSON.parse(response.body) if response["statusCode"] == 400 puts response.inspect puts req.body.inspect puts "Create page: Error reporting to confluence: #{response["message"]}" raise "Create page: Error reporting to confluence: #{response["message"]}" else puts "Reported page creation." end end |
#find_page_by_id(page_id) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/confluence_reporter.rb', line 109 def find_page_by_id(page_id) uri = URI.parse(@base_url + page_id) params={"expand" => "body,body.storage,version"} uri.query = URI.encode_www_form(params) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@user, @password) request['Accept'] = 'application/json' response = https.request(request) JSON.parse(response.body) end |
#find_page_by_name(name, parrent_page_id = nil) ⇒ Object
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 |
# File 'lib/confluence_reporter.rb', line 58 def find_page_by_name(name, parrent_page_id=nil) found = nil if parrent_page_id uri = URI.parse(@base_url + parrent_page_id + "/child/page") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true # https.set_debug_output $stderr next_url = uri.request_uri # Confluence limits the result to only 25 while (next_url and !found) request = Net::HTTP::Get.new(next_url) request.basic_auth(@user, @password) request['Accept'] = 'application/json' response = https.request(request) resp = JSON.parse(response.body) resp["results"].each do |res| if res["title"] == name found = res break end end if resp["_links"]["next"] if next_url == (resp["_links"]["base"] + resp["_links"]["next"]) next_url = nil else next_url = resp["_links"]["base"] + resp["_links"]["next"] end else next_url = nil end end else uri = URI.parse(@base_url) params={"start" => "0", "limit" => "40", "title" => name, "type" => "page", "expand" => "id,version,ancestors"} uri.query = URI.encode_www_form(params) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@user, @password) request['Accept'] = 'application/json' response = https.request(request) resp = JSON.parse(response.body) found = resp["results"] end found end |
#log(message, print = true) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/confluence_reporter.rb', line 42 def log(, print=true) if print puts end @body_message = @body_message + "<p>#{.gsub("\\n", "<br/>").gsub("<", "<").gsub(">",">").gsub("&", "&")}</p>" end |
#log_structured_code(message, print = true) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/confluence_reporter.rb', line 49 def log_structured_code(, print=true) if print puts end .split("\n").each do |line| @body_message = @body_message + "<pre>#{line}</pre>" end end |
#report_event(name, parrent_page_id = nil, space = nil) ⇒ Object
appends the log to confluence page if found, if not creates new page clears the log
126 127 128 129 130 131 132 133 134 |
# File 'lib/confluence_reporter.rb', line 126 def report_event(name, parrent_page_id=nil, space=nil) page = find_page_by_name(name, parrent_page_id) if page append_to_page(page["id"], parrent_page_id) else create_page(name, space, parrent_page_id) end clear_log end |