Skip to content

Instantly share code, notes, and snippets.

@jsyeo
Forked from gavinbunney/README.md
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsyeo/c80c1759aa78e4d7a7da to your computer and use it in GitHub Desktop.
Save jsyeo/c80c1759aa78e4d7a7da 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 c80c1759aa78e4d7a7da

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',
    :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>
<div class="aligner">
<img data-bind-src="avatar_url" />
<h2 class="large-font" data-bind="number"></h2>
</div>
<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 = {
:base_uri => 'https://bamboo:8888',
:credentials => {
:username => '',
:password => ''
},
:refresh_rate => '10s',
:plan_keys => %w[PLAN-ONE PLAN-TWO PLAN-THREE]
}
#--------------------------------------------------------------------------------
class BambooBuild
def initialize(base_uri, credentials)
@base_uri = base_uri
@credentials = credentials
end
def plan_status(plan_key)
begin
build = latest_build(plan_key)
build_reason = build['buildReason']
author_avatar_url = build_reason.match("\/user\/([a-zA-Z]+)") do |match|
avatar_url match[1]
end
author_avatar_url ||= ''
return {
:state => build['state'],
:number => build['number'],
:duration => build['buildDurationDescription'],
:finished => build['buildRelativeTime'],
:avatar_url => author_avatar_url
}
rescue => e
puts "Error getting bamboo plan status: #{e}"
return {
:state => 'Unknown',
:number => '?',
:duration => '',
:finished => ''
}
end
end
private
def latest_build(plan_key)
response = make_request(result_endpoint(plan_key))
response_json = JSON.parse(response.body)
response_json['results']['result'][0]
end
def setup_http
@http ||=
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
http
end
end
def make_request(endpoint)
http = setup_http
request = Net::HTTP::Get.new(endpoint)
if @credentials && @credentials[:username].length > 0
request.basic_auth @credentials[:username], @credentials[:password]
end
response = http.request(request)
end
def rest_endpoint
'/bamboo/rest/api/latest'
end
def auth_param
if @credentials && @credentials[:username].length > 0
'os_authType=basic'
else
''
end
end
def result_endpoint(plan_key)
"#{rest_endpoint}/result/#{plan_key}.json?#{auth_param}&expand=results.result&max-results=1"
end
def avatar_url(username)
jira_url = "#{@base_uri}/jira/secure/useravatar?size=xlarge&ownerId=#{username}"
response = make_request(jira_url)
response['Location']
end
end
def get_plan_status(base_uri, credentials, job)
build = BambooBuild.new(base_uri, 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[:base_uri], 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;
}
.aligner {
display: flex;
align-items: center;
justify-content: center;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment