Skip to content

Instantly share code, notes, and snippets.

@migrs
Last active April 19, 2018 05:55
Show Gist options
  • Save migrs/d15ebcbee1cdf0c21a5efffb9f7498f3 to your computer and use it in GitHub Desktop.
Save migrs/d15ebcbee1cdf0c21a5efffb9f7498f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# <bitbar.title>Redmine My Issues</bitbar.title>
# <bitbar.version>v1.0.0</bitbar.version>
# <bitbar.author>mig</bitbar.author>
# <bitbar.author.github>migrs</bitbar.author.github>
# <bitbar.desc></bitbar.desc>
# <bitbar.image></bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/migrs</bitbar.abouturl>
require 'net/http'
require 'uri'
require 'zlib'
require 'base64'
require 'json'
require 'date'
begin
require 'dotenv'
Dotenv.load File.dirname(__FILE__) + '/.env'
rescue LoadError
end
TOKEN = ENV["REDMINE_TOKEN"] || 'PUT-YOUR-REDMINE-TOKEN'
REDMINE_URL = ENV["REDMINE_URL"] || 'https://redmine.your-company.com'
MENU_COLOR = ENV['REDMINE_MENU_COLOR'] || '#bb2200'
ISSUES_URL = "#{REDMINE_URL}/issues.json?key=#{TOKEN}&limit=100&status_id=open&assigned_to_id=me"
# icon = `curl -s #{REDMINE_URL}/favicon.ico | base64 -w 0`
# icon = Base64.strict_encode64 Zlib::Deflate.deflate icon, Zlib::BEST_COMPRESSION
# if RUBY_PLATFORM =~ /linux/
# icon = 'eNrtWkt76roO/UEMHEi4hcEdKDbvBJK2tJs9K1BcSjdQoITk11+thLSwC/QF59wBHvAh4kiyLEtLMkRkE7lU4k+iEtXInuBrU/OHrTv4JEk0pBFR3SoxrWuY6pOmhvb52x/FcxsUz8TQlD6n5Mfk5/PYNSa1Hhuvek0kH4yA6XLpa+//qm2/3w1AZ5geCtDLAtP12ZeYNhru9vtOhWld1K+0gDz9Jaby4emv90fgfwd97TnTT2WeFcrCl7iuHtlFG6V78OuzN04U006hTaScAdPhi8Xuvbz9LL8sdTD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bX1nnXf95n9ymH6B9/cK+//4QvH/Czb0Y3qO/vLVXAZq2P0/vDfPB/N1P/88zuN4Q3wwaIAGcBX9YTv+78obTSmtLyMhorFm2tOeyFCBp2qPH1kqqIqMnSmJTG3wpypsBmwDXWXAXLOFJZPusu8Jy5eeEBMyBb+NbnRm8E6Twn//B2ylDAI='
# icon = Zlib::Inflate.inflate(Base64.decode64(icon))
# end
icon = nil
unfolded_status = ['Open', 'Ready', 'In Progress', 'Feedback']
def li text = '---', args = {}
args = args.dup
fold = args.delete(:fold) ? '--' : ''
args_set = (alt = args.delete(:alt)) ? [args, args.merge(alt).merge(alternate: true)] : [args]
args_set.each do |_|
puts fold + _.delete(:prepend).to_s + (_.delete(:text) || text).to_s + _.delete(:append).to_s +
(_.empty? ? '' : ' | ' + _.select {|k,v|!v.nil?}.map {|k,v| "#{k}=#{v}"}.join(' '))
end
end
begin
now = Time.now
uri = URI ISSUES_URL
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) {|_|_.get uri.request_uri }
raise "error #{res.code} #{res.message}" if res.code != '200'
result = JSON.parse res.body, symbolize_names: true
li "● #{result[:total_count]}", color: (result[:total_count] == 0 ? '#7d7d7d' : MENU_COLOR), image: icon, imageWidth: 18
li
#li "Last updated: #{now.strftime('%Y-%m-%d %H:%M')}", color: '#aaaa99', refresh: true
li "Redmine Tasks (#{result[:total_count]})", color: '#58BE89', href: "#{REDMINE_URL}/issues?utf8=%E2%9C%93&set_filter=1&f[]=status_id&op[status_id]=o&f[]=assigned_to_id&op[assigned_to_id]=%3D&v[assigned_to_id][]=me"
result[:issues].group_by {|_|_[:project][:id]}.each do |_, issues|
li
project = issues.first[:project]
li "#{project[:name]} (#{issues.count})", color: '#888888'
issues.group_by {|_|_[:status][:id]}.each do |_, issues|
status = issues.first[:status]
li "➠ #{status[:name]} (#{issues.count})", color: '#33BFDB'
fold = !unfolded_status.include?(status[:name])
issues.group_by {|_|_[:tracker][:id]}.each do |_, issues|
tracker = issues.first[:tracker]
color = nil # case tracker[:name] when 'Task' then '#ffffcc' when 'Defect' then '#ffcccc' else nil end
issues.each do |_|
priority = case _[:priority][:name] when 'High' then '#ff6666' when 'Low' then '#66ffff' else nil end
issue_id = priority ? "##{_[:id]} #{_[:priority][:name]}" : "##{_[:id]}"
title = "#{issue_id} #{_[:subject]} by #{_[:author][:name]}"
updated_on = DateTime.parse(_[:updated_on]).new_offset(Rational(9, 24)).strftime('%m-%d %H:%M')
li title, href: "#{REDMINE_URL}/issues/#{_[:id]}", color: color, fold: fold, alt: { prepend: "#{updated_on} | " }
end
end
end
end
rescue
li '!', color: '#ECB935', image: icon, imageWidth: 18
li
li "Exception: #{$!}", refresh: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment