Skip to content

Instantly share code, notes, and snippets.

@warmfusion
Last active March 17, 2017 20:58
Show Gist options
  • Save warmfusion/5923565 to your computer and use it in GitHub Desktop.
Save warmfusion/5923565 to your computer and use it in GitHub Desktop.
stash-pull-requests widget for Dashing

Description

Simple Dashing widget (and associated job) to display open pull requests for a specific Project and/or repository from Atlassian Stash

See the example.png below for an idea on how this widget looks on a dashboard.

Dependencies

None

Usage

This widget provides two presentation styles;

  1. A Pull request summary for a project, where all repositories are listed along with the number of open pull requests
  2. A pull request detail for a specific repository, where the title and original author is displayed

To use this widget, copy stash_pull_requests.html, stash_pull_requests.coffee, and stash_pull_requests.scss into the /widgets/StashPullRequests directory. Put the stash_pull_requests.rb file in your /jobs folder.

Summary Display

To include the summary widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="stash-pull-requests" data-view="StashPullRequests" data-title="Pull Request Summary"></div>
</li>

Detailed Display

To include the detailed widget in a dashboard, you include the following snippet in your dashboard, where the data-id indicates the project and repository slugs you'd like to show requests for;

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="stash-pull-requests-myproject-myrepo" data-view="StashPullRequests" data-title="Open MyRepo Pulls" style="background-color:#96bf48;"></div>
</li>    

For example, in the snippet above, the view would show all open pull requests for the "myrepo" repository contained within the "myproject" project.

Settings

The following values can be updated to reflect your own configuration requirements;

  • STASH_URI = URI.parse("http://stash:7990")
  • STASH_USER = 'stash'
  • STASH_PWD = 'stash'

Stash is checked every 30 minutes, but you can change that by editing the job schedule.

class Dashing.StashPullRequests extends Dashing.Widget
<h1 data-bind="title"></h1>
<ul>
<li data-foreach-item="pullRequests">
<span class="label" data-bind="item.title | truncate 16"></span>
<span class="reviewer" data-foreach-reviewer="item.reviewers">
<img class="gravatar" data-bind-src="reviewer.user.gravatar | append '?s=20&d=retro'" data-bind-title="reviewer.user.displayName"/>
</span>
</li>
</ul>
<ol>
<li data-foreach-repo="summary">
<span class="rotate project" data-bind="repo.project.key"></span>
<span class="label" data-bind="repo.name | truncate 18"></span>
<span class="value" data-bind="repo.pulls"></span>
</li>
</ol>
require 'net/http'
require 'json'
require 'digest/md5'
STASH_URI = URI.parse("http://stash")
STASH_USER = 'username'
STASH_PWD = 'password'
PROJECT_API = "/rest/api/1.0/projects/"
REPO_API = "/rest/api/1.0/projects/%s/repos"
PULL_API = "/rest/api/1.0/projects/%s/repos/%s/pull-requests"
SCHEDULER.every '30m', :first_in => 0 do |job|
req = Net::HTTP::Get.new(STASH_URI.to_s + PROJECT_API)
# Connect to stash, even behind self-signed SSL servers... WARNING: not necessarily secure
response = Net::HTTP.start(STASH_URI.host, STASH_URI.port, :use_ssl => STASH_URI.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
#Comment out the following if you are using a public stash project
req.basic_auth STASH_USER, STASH_PWD
https.request(req)
end
summary = Array.new
projects= JSON.parse(response.body)['values']
projects.each do |project|
project['key'].downcase!
puts "Getting repo list from %s" % project['key']
req = Net::HTTP::Get.new(STASH_URI.to_s + REPO_API % project['key'])
# Connect to stash, even behind self-signed SSL servers... WARNING: not necessarily secure
response = Net::HTTP.start(STASH_URI.host, STASH_URI.port, :use_ssl => STASH_URI.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
#Comment out the following if you are using a public stash project
req.basic_auth STASH_USER, STASH_PWD
https.request(req)
end
repos = JSON.parse(response.body)['values']
if repos
repos.each do |repo|
puts " Getting pull requests for %s:%s" % [project['key'], repo['slug'] ]
req = Net::HTTP::Get.new(STASH_URI.to_s + PULL_API % [project['key'], repo['slug']])
response = Net::HTTP.start(STASH_URI.host, STASH_URI.port, :use_ssl => STASH_URI.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
req.basic_auth STASH_USER, STASH_PWD
https.request(req )
end
pulls = JSON.parse(response.body)['values']
if pulls
summary << { project: project, name: repo['slug'], pulls: pulls.count }
# Do you have lots of pull requests? If so, uncomment this to limit to 8
# pulls = pulls[0..7]
# Build a gravatar link to pass to the UI.
# NOTE: Stash allows you to specify your own gravatar servers; if you use this
# functionality, ensure you change this path too.
pulls.each do |pull|
pull['reviewers'].each do |reviewer|
digest = Digest::MD5.hexdigest( reviewer['user']['emailAddress']).to_s
reviewer['user']['gravatar'] = '//secure.gravatar.com/avatar/%s.jpg' % digest
end
end
puts " Sending event for %s " % 'stash-pull-requests-%s-%s' % [ project['key'], repo['slug'] ]
send_event('stash-pull-requests-%s-%s' % [ project['key'], repo['slug'] ], { pullRequests: pulls })
end
end # pull loop
end # repo loop
end # project loop
puts "Sending summary event for stash-pull-requests"
send_event('stash-pull-requests' , { summary: summary})
end
$background-color: #12b0c5;
$value-color: #fff;
$label-color: rgba(255, 255, 255, 0.8);
.widget-stash-pull_requests {
background-color: $background-color;
vertical-align: top;
ol, ul {
margin: 0 15px;
text-align: left;
color: $label-color;
list-style: none;
}
ol {
list-style-position: inside;
}
li {
margin-bottom: 5px;
}
.list-nostyle {
}
.label {
color: $label-color;
}
.value {
float: right;
margin-left: 12px;
font-weight: 600;
color: $value-color;
}
.author {
float: left;
font-size: 50%;
}
.gravatar{
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
.reviewer{
float:right;
display: inline;
list-style-type: none;
padding-right: 1px;
margin: 0;
}
.project {
font-size:60%;
font-weight:bold;
position:absolute;
left:10px;
color: darken( $label-color, 10%)
}
.rotate {
display: block;
/* Safari */
-webkit-transform: rotate(-45deg);
/* Firefox */
-moz-transform: rotate(-45deg);
/* IE */
-ms-transform: rotate(-45deg);
/* Opera */
-o-transform: rotate(-45deg);
/* Internet Explorer */
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}
}
@avanderberg
Copy link

I had to name the directory widgets/stash_pull_requests/ instead of widgets/StashPullRequests/ to make it work.

@hfaing
Copy link

hfaing commented May 7, 2015

in the scss file, it should be widget-stash-pull-requests instead of widget-stash-pull_requests (dash instead of underscore)

@hfaing
Copy link

hfaing commented May 7, 2015

after scss change (underscore to dash), my project name overlaps my repo name, so I added margin-left: 38px; to .label

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