Skip to content

Instantly share code, notes, and snippets.

@davecowart
Created July 19, 2013 18:16
Show Gist options
  • Save davecowart/6041208 to your computer and use it in GitHub Desktop.
Save davecowart/6041208 to your computer and use it in GitHub Desktop.
Github branch watcher
require 'open-uri'
require 'json'
class GithubWatcher
ROOT_URL = 'https://api.github.com'
TOKEN = '[API TOKEN]'
def compareBranches
branches = JSON.parse(open("#{ROOT_URL}/repos/[ORGANIZATION]/[REPO]/branches?access_token=#{TOKEN}").read)
active_branches = branches.select{|b| !['master','uat','qa'].include?(b['name'])}
comparisons = []
master = branches.find{|b| b['name'] == 'master'}
uat = branches.find{|b| b['name'] == 'uat'}
qa = branches.find{|b| b['name'] == 'qa'}
active_branches.each do |branch|
masterComparison = compareBranch(master, branch)
uatComparison = compareBranch(uat, branch)
qaComparison = compareBranch(qa, branch)
comparisons << {
:name => branch['name'],
:mergedIntoMaster => !masterComparison[:ahead],
:mergedIntoUAT => !uatComparison[:ahead],
:mergedIntoQA => !qaComparison[:ahead],
:safeToMerge => !masterComparison[:behind]
}
end
comparisons
end
def compareBranch(base, head)
comparison = JSON.parse(open("#{ROOT_URL}/repos/[ORGANIZATION]/[REPO]/compare/#{base['commit']['sha']}...#{head['commit']['sha']}?access_token=#{TOKEN}").read)
{
:ahead => comparison['ahead_by'] > 0,
:behind => comparison['behind_by'] > 0
}
end
end
- @comparisons.each do |comp|
.branch
%h4= comp[:name]
%ul
%li= "Merged Into Master: #{comp[:mergedIntoMaster]}"
- if !comp[:mergedIntoMaster]
%a(href="https://github.com/[ORGANIZATION]/[REPO]/pull/new/[ORGANIZATION]:master...[ORGANIZATION]:#{comp[:name]}") Create Pull Request
%li= "Merged Into UAT: #{comp[:mergedIntoUAT]}"
- if !comp[:mergedIntoUAT]
%a(href="https://github.com/[ORGANIZATION]/[REPO]/pull/new/[ORGANIZATION]:uat...[ORGANIZATION]:#{comp[:name]}") Create Pull Request
%li= "Merged Into QA: #{comp[:mergedIntoQA]}"
- if !comp[:mergedIntoQA]
%a(href="https://github.com/[ORGANIZATION]/[REPO]/pull/new/[ORGANIZATION]:qa...[ORGANIZATION]:#{comp[:name]}") Create Pull Request
.merged_branches
- @comparisons.each do |comp|
- if comp[:mergedIntoMaster] && comp[:mergedIntoUAT] && comp[:mergedIntoQA]
%span= ":#{comp[:name]} "
class ProjectsController < ApplicationController
def index
@comparisons = GithubWatcher.new.compareBranches
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment