Skip to content

Instantly share code, notes, and snippets.

@dragonai
Last active October 9, 2019 13:00
Show Gist options
  • Save dragonai/bfa70e8e85a5f64b44aa to your computer and use it in GitHub Desktop.
Save dragonai/bfa70e8e85a5f64b44aa to your computer and use it in GitHub Desktop.
Semaphore Branch Status Monitor

##Preview

Description

Simple Dashing widget that displays the build statuses of all branches of a Semaphore repository, reflecting each branch's latest build status through text color.

##Usage

#####Dependencies

Add semaphoreapp to the gemfile:

gem 'semaphoreapp'

and then just run

$ bundle install

#####Setup

To install this widget, simply run dashing install bfa70e8e85a5f64b44aa.

Then substitute the following placeholders in semaphore_branches.rb with the appropriate values:

  • YOUR_SEMAPHORE_AUTH_KEY => your Semaphore auth token
  • LIST_WIDGET_DATA_ID => the target HTML element's data-id attribute in your layout
  • REPO_TITLE_TO_DISPLAY => the name of the repo to be displayed on the widget (doesn't need to match Semaphore)
  • exact_repo_name_here => the exact name of the repo to monitor on Semaphore itself (does need to match Semaphore)

Finally, to include the widget on a dashboard, drop the following snippet into your layout:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-view="BranchList" data-id="whatever_id_you_like"></div>
</li>
class Dashing.BranchList extends Dashing.Widget
ready: ->
# This is fired when the widget is done being rendered
onData: (data) ->
# Handle incoming data
# You can access the html node of this widget with `@node`
# Example: $(@node).fadeOut().fadeIn() will make the node flash each time data comes in.
<h1 class="title" data-bind="title"></h1>
<ul class="list-nostyle">
<li data-foreach-item="items">
<span class="label" data-bind="item.label" data-bind-class="item.branch_status"></span>
</li>
</ul>
<p class="more-info" data-bind="moreinfo"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: gray;
$value-color: #fff;
$pass-color: #8fb347;
$fail-color: #ad3b0e;
$else-color: #9CB4F4;
$title-color: rgba(255, 255, 255, 0.7);
$label-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-branch-list styles
// ----------------------------------------------------------------------------
.widget-branch-list {
background-color: white;
vertical-align: top;
.title {
color: #222222;
}
ol, ul {
margin: 0 15px;
text-align: center;
color: $label-color;
}
ol {
list-style-position: inside;
}
li {
margin-bottom: 5px;
}
.list-nostyle {
list-style: none;
}
.label {
color: $label-color;
}
.value {
float: right;
margin-left: 12px;
font-weight: 600;
color: $value-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
.more-info {
color: $moreinfo-color;
}
.passed
{
color: $pass-color;
}
.failed
{
color: $fail-color;
}
.pending
{
color: $else-color;
}
}
require 'semaphoreapp'
class SemaphoreConnector
def initialize auth_key="YOUR_SEMAPHORE_AUTH_KEY"
Semaphoreapp.auth_token = auth_key
end
def project_list
Semaphoreapp::Project.all.map {|project| project.name }
end
def get_project project_name
Semaphoreapp::Project.find_by_name(project_name)
end
def get_branches project_name
Semaphoreapp::Project.find_by_name(project_name).branches.map {|branch| branch.branch_name }
end
def branch_status project_name, branch_title
requested_branch = get_project(project_name).branches.find {|branch| branch.branch_name == branch_title }
requested_branch_info = {
"name" => requested_branch.branch_name,
"build_number" => requested_branch.build_number,
"build_status" => requested_branch.result,
"last_build" => requested_branch.build_url,
"last_build_time" => requested_branch.finished_at,
"latest_commit_author" => requested_branch.commit.author_name,
"latest_commit_message" => requested_branch.commit.message,
"latest_commit_link" => requested_branch.commit.url
}
end
def number_of_branches project_name
get_branches(project_name).count
end
end
@semaphore = SemaphoreConnector.new
def build_branch_list project
branch_list = @semaphore.get_branches(project).map { |branch|
this_branch = @semaphore.branch_status(project, branch)
{ :label => this_branch['name'], :branch_status => this_branch['build_status'], :last_build => this_branch['last_build_time'] }
}
branch_list.sort_by! { |branch| branch[:last_build] }.reverse!
end
SCHEDULER.every '1m', :first_in => 0 do |job|
send_event('LIST_WIDGET_DATA_ID', {
title: "REPO_TITLE_TO_DISPLAY",
items: build_branch_list("exact_repo_name_here")
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment