lib/slamboo/slamboo.rb
# posts a message to Slack
# options
posts a message to Slack
options
posts a message to Slack options
def message
            bambooURL=options[:bDomain]
            bambooKEY=options[:bPlanKey]
            uriStringBamboo = "https://#{bambooURL}/builds/rest/api/latest/result/#{bambooKEY}"
            uriBamboo = URI(uriStringBamboo)
            r = Resources::Request.new(uriBamboo, options[:bUser], options[:bPass])
            req=r.create_get_request_header
            res = Net::HTTP.start(uriBamboo.hostname, 
                :use_ssl => uriBamboo.scheme == 'https') { |http|
                http.request(req)
            }
            if res.code != "200"
                puts res.code
                puts res.message
            else 
                result = XmlSimple.xml_in res.body
                puts result["key"]
                puts result['planName'][0]
                puts result["projectName"][0]
                puts result["buildNumber"][0]
                puts result["buildState"][0]
                reasonSummary = result["reasonSummary"][0].gsub! "a href=\"", ""
                reasonSummary = reasonSummary.gsub! "\">", "|"
                reasonSummary = reasonSummary.gsub! "</a>", ">"
                puts result["reasonSummary"]
                slackMsg = JSON.parse("{}")
                options[:user] != nil ? slackMsg["username"] = options[:user] : nil
                options[:channel] != nil ? slackMsg["channel"] = "\##{options[:channel]}" : nil
                options[:userIconURL] != nil ? slackMsg["icon_url"] = options[:userIconURL] : nil
                options[:userEmoji] != nil ? slackMsg["icon_emoji"] = options[:userEmoji] : nil
                rand = Random.new
                emojiSuccess = [":white_check_mark:", ":eight_spoked_asterisk:", ":bowtie:", ":sunglasses:", ":+1:"]
                emojiFail = [":trollface:", ":bangbang:", ":x:", ":do_not_litter:", ":x:", ":no_entry_sign:", ":no_entry:", ":sos:"]
                if (result["buildState"][0] == "Failed")
                    resultEmoji = emojiFail[rand.rand(emojiFail.length)]
                    resultColor = "#E02D19"
                    options[:failedChannel] != nil ? slackMsg["channel"] = "\##{options[:failedChannel]}" : nil
                else
                    resultEmoji = emojiSuccess[rand.rand(emojiSuccess.length)] 
                    resultColor = "#0DB542"
                end
                slackMsg["attachments"] = []
                slackMsg["attachments"][0] = JSON.parse("{}")
                slackMsg["attachments"][0]["fallback"] ="#{result["buildState"][0]} &gt; #{result['planName'][0]}"
                slackMsg["attachments"][0]["title"] = "#{result["projectName"][0]} &gt; #{result['planName'][0]}"
                slackMsg["attachments"][0]["title_link"] = "https://#{bambooURL}/builds/browse/#{result["key"]}"
                slackMsg["attachments"][0]["fields"] = []
                slackMsg["attachments"][0]["fields"][0] = JSON.parse("{}")
                slackMsg["attachments"][0]["fields"][0]["title"] = result["buildState"][0]
                slackMsg["attachments"][0]["fields"][0]["value"] = "Build number: #{result["buildNumber"][0]}"
                slackMsg["attachments"][0]["fields"][0]["short"] = true
                slackMsg["attachments"][0]["fields"][1] = JSON.parse("{}")
                slackMsg["attachments"][0]["fields"][1]["title"] = "Reason:"
                slackMsg["attachments"][0]["fields"][1]["value"] = result["reasonSummary"][0]
                slackMsg["attachments"][0]["fields"][1]["short"] = true
                slackMsg["attachments"][0]["color"] = resultColor
                puts ">>>>>>>>>>>>>>>>>>>>>>"
                puts slackMsg
                puts ">>>>>>>>>>>>>>>>>>>>>>"
                slackURL=options[:sURL]
                uriStringSlack = slackURL
                uriSlack = URI(uriStringSlack)
                s = Resources::Request.new(uriSlack, nil, nil)
                sreq = s.create_post_request_header(JSON.generate(slackMsg))
                sres = Net::HTTP.start(uriSlack.hostname, 
                    :use_ssl => uriSlack.scheme == 'https') { |http|
                    http.request(sreq)
                }
                if sres.code != "200"
                    puts sres.code
                else
                    puts sres
                end
            end
        end
def message bambooURL = options [ :bDomain ] bambooKEY = options [ :bPlanKey ] uriStringBamboo = "https://#{bambooURL}/builds/rest/api/latest/result/#{bambooKEY}" uriBamboo = URI ( uriStringBamboo ) r = Resources :: Request . new ( uriBamboo , options [ :bUser ] , options [ :bPass ] ) req = r . create_get_request_header res = Net :: HTTP . start ( uriBamboo . hostname , :use_ssl => uriBamboo . scheme == 'https' ) { | http | http . request ( req ) } if res . code != "200" puts res . code puts res . message else result = XmlSimple . xml_in res . body puts result [ "key" ] puts result [ 'planName' ] [ 0 ] puts result [ "projectName" ] [ 0 ] puts result [ "buildNumber" ] [ 0 ] puts result [ "buildState" ] [ 0 ] reasonSummary = result [ "reasonSummary" ] [ 0 ] . gsub! "a href=\"" , "" reasonSummary = reasonSummary . gsub! "\">" , "|" reasonSummary = reasonSummary . gsub! "</a>" , ">" puts result [ "reasonSummary" ] slackMsg = JSON . parse ( "{}" ) options [ :user ] != nil ? slackMsg [ "username" ] = options [ :user ] : nil options [ :channel ] != nil ? slackMsg [ "channel" ] = "\##{options[:channel]}" : nil options [ :userIconURL ] != nil ? slackMsg [ "icon_url" ] = options [ :userIconURL ] : nil options [ :userEmoji ] != nil ? slackMsg [ "icon_emoji" ] = options [ :userEmoji ] : nil rand = Random . new emojiSuccess = [ ":white_check_mark:" , ":eight_spoked_asterisk:" , ":bowtie:" , ":sunglasses:" , ":+1:" ] emojiFail = [ ":trollface:" , ":bangbang:" , ":x:" , ":do_not_litter:" , ":x:" , ":no_entry_sign:" , ":no_entry:" , ":sos:" ] if ( result [ "buildState" ] [ 0 ] == "Failed" ) resultEmoji = emojiFail [ rand . rand ( emojiFail . length ) ] resultColor = "#E02D19" options [ :failedChannel ] != nil ? slackMsg [ "channel" ] = "\##{options[:failedChannel]}" : nil else resultEmoji = emojiSuccess [ rand . rand ( emojiSuccess . length ) ] resultColor = "#0DB542" end slackMsg [ "attachments" ] = [ ] slackMsg [ "attachments" ] [ 0 ] = JSON . parse ( "{}" ) slackMsg [ "attachments" ] [ 0 ] [ "fallback" ] = "#{result["buildState"][0]} &gt; #{result['planName'][0]}" slackMsg [ "attachments" ] [ 0 ] [ "title" ] = "#{result["projectName"][0]} &gt; #{result['planName'][0]}" slackMsg [ "attachments" ] [ 0 ] [ "title_link" ] = "https://#{bambooURL}/builds/browse/#{result["key"]}" slackMsg [ "attachments" ] [ 0 ] [ "fields" ] = [ ] slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 0 ] = JSON . parse ( "{}" ) slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 0 ] [ "title" ] = result [ "buildState" ] [ 0 ] slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 0 ] [ "value" ] = "Build number: #{result["buildNumber"][0]}" slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 0 ] [ "short" ] = true slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 1 ] = JSON . parse ( "{}" ) slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 1 ] [ "title" ] = "Reason:" slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 1 ] [ "value" ] = result [ "reasonSummary" ] [ 0 ] slackMsg [ "attachments" ] [ 0 ] [ "fields" ] [ 1 ] [ "short" ] = true slackMsg [ "attachments" ] [ 0 ] [ "color" ] = resultColor puts ">>>>>>>>>>>>>>>>>>>>>>" puts slackMsg puts ">>>>>>>>>>>>>>>>>>>>>>" slackURL = options [ :sURL ] uriStringSlack = slackURL uriSlack = URI ( uriStringSlack ) s = Resources :: Request . new ( uriSlack , nil , nil ) sreq = s . create_post_request_header ( JSON . generate ( slackMsg ) ) sres = Net :: HTTP . start ( uriSlack . hostname , :use_ssl => uriSlack . scheme == 'https' ) { | http | http . request ( sreq ) } if sres . code != "200" puts sres . code else puts sres end end end
posts a message to Slack
options
posts a message to Slack options
#puts result.keys #slackMsg["text"] = "> #{resultEmoji} <https://#{bambooURL}/builds/browse/#{result["key"]}|#{result["projectName"][0]} &gt; #{result['planName'][0]} &gt; #{result["buildNumber"][0]} > *#{result["buildState"][0]}*\n> #{result["reasonSummary"][0]}" #slackMsg["attachments"][0]["fallback"] ="#{resultEmoji} <https://#{bambooURL}/builds/browse/#{result["key"]}|#{result["projectName"][0]} &gt; #{result['planName'][0]} &gt; #{result["buildNumber"][0]} > *#{result["buildState"][0]}*\n> #{result["reasonSummary"][0]}" #slackMsg["attachments"][0]["pretext"] = "#{resultEmoji} #{result['planName'][0]}" #options[:message] != nil ? msg = options[:message] : nil