Skip to content

Instantly share code, notes, and snippets.

@sturdy5
Last active July 3, 2017 16:07
Show Gist options
  • Save sturdy5/15a542122b198449903d to your computer and use it in GitHub Desktop.
Save sturdy5/15a542122b198449903d to your computer and use it in GitHub Desktop.
Dashing BitBucket Repo Stats

Description

This is a Dashing widget that is based on the Dashing GitHub Stats widget. This widget displays Last Activity, Open Issues, Open Pulls, Forks, and Watchers of a given set of BitBucket repositories.

Setup

  1. You can either copy and paste these files on your own, or you can use dashing install 15a542122b198449903d to copy the files into your dashing directory in place.
  2. Edit the bitbucket.yml file to configure the widget. An example configuration is below.
  3. Add bitbucket_rest_api and actionview gems to your Gemfile and run bundle install
  4. Add the widget to your dashboard erb file. The data-id value is the repository you want to display information for. An example is below.
repos:
  - atlassian/atlasboard
  - atlassian/atlasboard-atlassian-package

login: my_bitbucket_user
password: my_bitbucket_user

Example 1: bitbucket.yml file example

<ul>
  <li> data-row="1" data-col="1" data-sizex="1" data-sizey="1">
    <div data-id="atlassian/atlasboard" data-view="BitbucketStats"></div>
    <i class="icon-bitbucket icon-background"></i>
  </li>
</ul>

Example 2: dashboard erb widget placement

repos:
- atlassian/atlasboard
- atlassian/atlasboard-atlassian-package
login: my_bitbucket_user
password: my_bitbucket_password
class Dashing.BitbucketStats 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`
$(@node).fadeOut().fadeIn()
<div class="title"><span class="repo_name" data-bind="repo"></span></div>
<div class="main">
<div class="activity">
<span class="label">Last Activity:</span>
<span class="date" data-bind="activity"></span>
</div>
<div class="issues">
<span class="label">Open Issues:</span>
<span class="count" data-bind="issues"></span>
</div>
<div class="pulls">
<span class="label">Open Pulls:</span>
<span class="count" data-bind="pulls"></span>
</div>
<div class="forks">
<span class="label">Forks:</span>
<span class="count" data-bind="forks"></span>
</div>
<div class="watchers">
<span class="label">Watchers:</span>
<span class="count" data-bind="watchers"></span>
</div>
</div>
require 'bitbucket_rest_api'
require 'action_view'
include ActionView::Helpers::DateHelper
class Time
def acts_like?(duck)
respond_to? :"acts_like_#{duck}?"
end
end
config = YAML::load_file('bitbucket.yml')
bitbucket = BitBucket.new :basic_auth => "#{config["login"]}:#{config["password"]}"
SCHEDULER.every '30m', :first_in => 0 do |job|
config["repos"].each do |name|
# break up the repo by the / to get the username and the repo name
repoparts = name.split("/")
# issues
begin
newIssueCount = bitbucket.issues.list_repo(repoparts[0], repoparts[1], {"limit" => 50, "status" => "new"}).size
openIssueCount = bitbucket.issues.list_repo(repoparts[0], repoparts[1], {"limit" => 50, "status" => "open"}).size
rescue BitBucket::Error::NotFound
# the issue tracker has been disabled for this repo
newIssueCount = "N/"
openIssueCount = "A"
end
# pulls
pulls = bitbucket.repos.pull_request.list(repoparts[0], repoparts[1])["size"]
# forks
forks = bitbucket.repos.forks.list(repoparts[0], repoparts[1])["size"]
# watchers
watchers = bitbucket.repos.following.followers(repoparts[0], repoparts[1])["count"]
# activity
lastTimestamp = bitbucket.repos.changesets.list(repoparts[0], repoparts[1], {"limit" => 1})["changesets"][0]["timestamp"]
timestamp = DateTime.parse(lastTimestamp)
send_event(name, {
repo: repoparts[1],
issues: newIssueCount + openIssueCount,
pulls: pulls,
forks: forks,
watchers: watchers,
activity: time_ago_in_words(timestamp).capitalize
})
end
end
.widget.widget-bitbucket-stats {
vertical-align: text-top;
background-color: #55A;
background-size: contain;
background-position: center;
background-repeat: no-repeat;
line-height: 1em;
.title {
color: white;
padding: 10px;
font-size: 1.5em;
.repo_name {
font-weight: bolder;
}
}
.main {
text-align: left;
color: white;
}
.label {
display: inline-block;
font-size: 1em;
padding: 10px;
width: 45%;
text-align: right;
}
.count {
display: inline-block;
width: 35%;
font-size: 1em;
padding: 10px;
text-align: left;
font-weight: bolder;
}
.date {
display: inline-block;
width: 35%;
font-size: 1em;
padding: 10px;
text-align: left;
font-weight: bolder;
}
}
@ntende
Copy link

ntende commented Apr 20, 2017

Can it be customised for a standalone bitbucket server ?

@sturdy5
Copy link
Author

sturdy5 commented Jul 3, 2017

I really need to setup notifications so I see these comments sooner...

Oh well, I am using a ruby library to hit the bitbucket apis. It can be found here. From the documentation it doesn't appear to be able to hit standalone bitbucket servers, but you might be able to extend the api to meet your needs.

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