Manage GitHub repository branch freezes from the Mac menu bar.
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'json'
# <xbar.title>Merge Freeze</xbar.title>
# <xbar.version>v0.2</xbar.version>
# <xbar.author>Ryan Kulp</xbar.author>
# <xbar.author.github>ryanckulp</xbar.author.github>
# <xbar.desc>Manage GitHub repository branch freezes from the Mac menu bar.</xbar.desc>
# <xbar.image>https://user-images.githubusercontent.com/3083888/188286665-131e3cb8-9b1d-45e2-af4a-2852201c334e.png</xbar.image>
# <xbar.dependencies>ruby</xbar.dependencies>
# <xbar.abouturl>https://github.com/Merge-Freeze/mac-menu-plugin</xbar.abouturl>
# Variables become preferences in the app:
# <xbar.var>string(API_KEY=""): Organization-level API key from MergeFreeze.com.</xbar.var>
# <xbar.var>string(ORGANIZATION_SLUG=""): Organization "slug" username on GitHub.com.</xbar.var>
module Icons
# red fill, 1+ repos are frozen
def frozen_icon
"iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA4ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDcuMi1jMDAwIDc5LjU2NmViYzViNCwgMjAyMi8wNS8wOS0wODoyNTo1NSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMu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ZdZU1b/QfeLx29HQoGJmxsAUJTK4thaPPXLrwnwg4PcSutQEVrFO4vInB3biy8PDlGKOe7uvp1BdUnmPFW1VYGkbN7Z4bSVjF1GSEgmZs7iQkds6/q/lcvoSujHPB+46jiWLsbLiTp06i9xsfjGhRRvHB37zWmWi0ukZnEmkvD2Vhe26jkUiZBmEnS+7JMXl79Qk9r9/tIKCFn4eHsfwSNJjRdkknyeJMbIiS7eVSlpwHVMn2czPkp5O0F1b50vThabV4IMPv0NDvYpHH74FtUSrp3feVVVA7/T24eChQW+uXNIyjJadnyTMKQnsGHOpQa50XCOlr7xA3rxn91foPzqKO27bhNbWoEcrKT79R0ZxmsRIU2uqE8Z9MPTkj5ZlFIV8TjLpwT0Nwabtmta0TkpfmZdSL0ZGxnHypzHwUqwogJ4mqyX+ulTB8iIkDySjuelseqCHeiDZ9JPU6X9MZr7oUsKdz2laNKaIQIhULyAFhcLyH0+TY9NDkbet/IRhJE9eyPywR+G504rwg5mmifHxJOQTRdmkQ1Sp0Y3klrb6Y+pYDiXftgx6TA2T3hJI0La2NvwrwACK6ehm4BCh9gAAAABJRU5ErkJggg=="
end
end
class MergeFreezeApplet
include Icons
BASE_URL = 'https://www.mergefreeze.com'.freeze
attr_accessor :api_key, :organization_slug, :repositories
def initialize
@api_key = ENV['API_KEY']
@organization_slug = ENV['ORGANIZATION_SLUG']
end
def call
if (api_key && api_key.length > 0) && (organization_slug && organization_slug.length > 0)
get_repositories
set_icon
insert_separator
build_menu
else
puts "Merge Freeze - Setup"
insert_separator
puts "Press ⌘E to add config keys | disabled=true"
end
end
def get_repositories
uri = URI("#{BASE_URL}/api/branches/#{@organization_slug}?access_token=#{@api_key}")
res = Net::HTTP.get_response(uri)
@repositories = JSON.parse(res.body)
end
def sort_and_print(collection)
repositories[collection].sort_by { |k| k['name'] }.each do |repo| # list alphabetically
puts "-- #{repo['name']} | href=#{repo['url']}"
end
end
def frozen_repos_exist?
repositories['frozen'].count > 0
end
def set_icon
puts "|image=#{frozen_repos_exist? ? frozen_icon : unfrozen_icon}"
end
def insert_separator
puts "---"
end
def build_menu
if frozen_repos_exist?
puts "Frozen repos"
sort_and_print('frozen')
else
puts "All repos are clear!"
end
puts "Unfrozen repos"
sort_and_print('unfrozen')
puts ""
puts "Contribute / Docs | href=https://github.com/Merge-Freeze/mac-menu-plugin"
end
end
# check for ENV vars, fetch projects, build interactive menu
MergeFreezeApplet.new.call