class ConfluenceReporter::Reporter

Attributes

base_url[RW]
body_message[RW]
password[RW]
user[RW]

Public Class Methods

new(base_url, user, password) click to toggle source
# 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

Public Instance Methods

append_to_page(page_id, parrent_page_id) click to toggle source
# 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() click to toggle source
# File lib/confluence_reporter.rb, line 199
def clear_log
    @body_message = ""
end
create_page(title, space, parrent_page_id=nil) click to toggle source

Creates new page with title set, if parrent_page_id is provided it adjusts ancestor accordingly and the same space short key

# 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("&&", "&amp;&amp;").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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/confluence_reporter.rb, line 42
def log(message, print=true)
    if print
        puts message
    end
    @body_message = @body_message + "<p>#{message.gsub("\\n", "<br/>").gsub("<", "&lt;").gsub(">","&gt;").gsub("&", "&amp;")}</p>"
end
log_structured_code(message, print=true) click to toggle source
# File lib/confluence_reporter.rb, line 49
def log_structured_code(message, print=true)
    if print
        puts message
    end
    message.split("\n").each do |line|
            @body_message = @body_message + "<pre>#{line}</pre>"
    end
end
report_event(name, parrent_page_id=nil, space=nil) click to toggle source

appends the log to confluence page if found, if not creates new page clears the log

# 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