Skip to content

Instantly share code, notes, and snippets.

@gavinbunney
Last active July 5, 2019 08:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gavinbunney/10617832 to your computer and use it in GitHub Desktop.
Save gavinbunney/10617832 to your computer and use it in GitHub Desktop.

Bamboo Plan Dashing Widget

Dashing widget to display plan build details from your Bamboo instance.

Preview

Bamboo Dashing Widget

Installation

Automatic

To install this widget, run this command from your Dashing directory:

dashing install 10617832

Then copy the bamboo-logo.png file into the assets\images folder

Manual

  1. Copy the below coffee/scss/html files into a widget folder named bamboo_build
  2. Copy the bamboo_build.rb file into the jobs folder
  3. Copy the bamboo-logo.png file into the assets\images folder

Usage

Update job

Update the configuration element in the bamboo_build.rb file with your bamboo instance uri and the plans you wish to monitor.

configuration = {
    :bamboo_uri => 'http://bamboo:8085',
    :rest_endpoint => '/rest/api/latest',
    :credentials => {
         :username => '',
         :password => ''
     },
    :refresh_rate => '10s',
    :plan_keys => %w[PLAN-ONE PLAN-TWO PLAN-THREE]
}

Add to dashboard

To include this widget in your dashboard, add the following snippet of code to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="PLAN-ONE" data-view="BambooBuild" data-title="Plan 1 Build"></div>
</li>

You'll need to update the data-id with the name of your plan to show as defined in the bamboo_build.rb file.

class Dashing.BambooBuild extends Dashing.Widget
@accessor 'value', Dashing.AnimatedValue
constructor: ->
super
refreshWidgetState: =>
node = $(@node)
node.removeClass('successful failed unknown')
node.addClass(@get('state').toLowerCase())
ready: ->
@refreshWidgetState()
onData: (data) ->
@refreshWidgetState()
<h1 class="title" data-bind="title"></h1>
<h2 class="large-font" data-bind="number"></h2>
<h2 class="state" data-bind="state"></h2>
<p class="duration" data-bind="duration"></p>
<p class="finished" data-bind="finished"></p>
require 'net/https'
require 'json'
#--------------------------------------------------------------------------------
# Configuration
#--------------------------------------------------------------------------------
configuration = {
:bamboo_uri => 'http://bamboo:8085',
:rest_endpoint => '/rest/api/latest',
:credentials => {
:username => '',
:password => ''
},
:refresh_rate => '10s',
:plan_keys => %w[PLAN-ONE PLAN-TWO PLAN-THREE]
}
#--------------------------------------------------------------------------------
class BambooBuild
def initialize(base_uri, rest_endpoint, credentials)
@base_uri = base_uri
@rest_endpoint = rest_endpoint
@credentials = credentials
end
def plan_status(plan_key)
begin
uri = URI.parse(@base_uri)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(result_endpoint(plan_key))
if @credentials && @credentials[:username].length > 0
request.basic_auth @credentials[:username], @credentials[:password]
end
response = http.request(request)
response_json = JSON.parse(response.body)
latest_build = response_json['results']['result'][0]
return {
:state => latest_build['state'],
:number => latest_build['number'],
:duration => latest_build['buildDurationDescription'],
:finished => latest_build['buildRelativeTime']
}
rescue => e
puts "Error getting bamboo plan status: #{e}"
return {
:state => 'Unknown',
:number => '?',
:duration => '',
:finished => ''
}
end
end
private
def result_endpoint(plan_key)
auth_param = ''
if @credentials && @credentials[:username].length > 0
auth_param = 'os_authType=basic&'
end
"#{@rest_endpoint}/result/#{plan_key}.json?#{auth_param}expand=results.result&max-results=1"
end
end
def get_plan_status(bamboo_uri, rest_endpoint, credentials, job)
build = BambooBuild.new(bamboo_uri, rest_endpoint, credentials)
build.plan_status(job)
end
configuration[:plan_keys].each do |plan_key|
SCHEDULER.every configuration[:refresh_rate], :first_in => 0 do |_|
status = get_plan_status(configuration[:bamboo_uri], configuration[:rest_endpoint], configuration[:credentials], plan_key)
send_event(plan_key, status)
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$state-color: #fff;
$background-color: #4b4b4b;
$background-failed-color: #d26771;
$background-successful-color: #96bf48;
$background-unknown-color: #999;
$title-color: rgba(255, 255, 255, 1);
$label-color: rgba(255, 255, 255, 0.7);
$duration-color: rgba(255, 255, 255, 0.7);
$finished-color: rgba(0, 0, 0, 0.3);
.widget-bamboo-build {
background: url("/assets/bamboo-logo.png") no-repeat center;
background-size: contain;
vertical-align: top;
.title {
color: $title-color;
}
.state {
text-align: center;
display: block;
font-weight: 600;
color: $state-color;
font-size: 40px;
word-wrap: break-word;
}
.finished {
color: $finished-color;
position: absolute;
bottom: 24px;
left: 0;
right: 0;
}
.duration {
color: $duration-color;
position: absolute;
bottom: 48px;
left: 0;
right: 0;
}
&.failed {
background-color: $background-failed-color;
}
&.unknown {
background-color: $background-unknown-color;
}
&.successful {
background-color: $background-successful-color;
}
}
@dawogfather
Copy link

add this in place of line 28 of bamboo_build.rb to support bamboo which is behind https...

      if @base_uri.include? "https"
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      request = Net::HTTP::Get.new(result_endpoint(plan_key))

@gavinbunney
Copy link
Author

Thanks @dawogfather - I've added HTTPS support to the main job

@sagarjauhari
Copy link

I have my server with HTTPS and I'm continuously getting

Error getting bamboo plan status: Operation timed out - connect(2)

@hmcmanus
Copy link

Hey widget is great, I've been working on one for stash pull requests, just putting out the number as opposed to the existing one that's out there. Quick question, how did you make the logo? Was thinking of creating something similar for stash's logo...

@igoratencompass
Copy link

Hi,
How about proxy support? Our bamboo runs behind a remote proxy together with bunch of other services so it's accessible via https://proxy.example.com/bamboo but looks like currently the URI from the URL is being stripped off.

@igoratencompass
Copy link

Proxy access logs, we can see how /bamboo/ is missing in the beginning of the requests:

x.x.x.x - - [19/Jul/2015:13:06:46 +1000] "GET /rest/api/latest/result/MY-PLAN-1.json?os_authType=basic&expand=results.result&max-results=1 HTTP/1.1" 404 4468 "-" "Ruby"
x.x.x.x - - [19/Jul/2015:13:06:46 +1000] "GET /rest/api/latest/result/MY-PLAN-2.json?os_authType=basic&expand=results.result&max-results=1 HTTP/1.1" 404 4463 "-" "Ruby"

@gavinbunney
Copy link
Author

@igoratencompass - I've moved the rest_endpoint into a configuration variable... so you can now add your bamboo/ top level route 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment