├── .gitignore ├── config.ru ├── config.yml.sample ├── Gemfile ├── config.yml.heroku_sample ├── README.md └── gh.rb /.gitignore: -------------------------------------------------------------------------------- 1 | config.yml 2 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | require './gh.rb' 2 | run Sinatra::Application 3 | -------------------------------------------------------------------------------- /config.yml.sample: -------------------------------------------------------------------------------- 1 | user: YOURUSERNAME 2 | token: PASSWORD 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source :gemcutter 2 | gem 'sinatra' 3 | gem 'curb' 4 | gem 'erubis' 5 | -------------------------------------------------------------------------------- /config.yml.heroku_sample: -------------------------------------------------------------------------------- 1 | user: <%= ENV['GITHUB_USERNAME'] %> 2 | token: <%= ENV['GITHUB_PASSWORD'] %> 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | Move config.yml.sample to config.yml and enter your details 3 | 4 | Start sinatra and point your repo's hook to the sinatra ip/port! 5 | 6 | # How to assign a user 7 | 8 | git commit -a -m 'unicorns are awesome, here you go =bluescripts' 9 | 10 | # How to add labels 11 | git commit -a -m 'unicorns are awesome ~unicorns ~awesome' 12 | 13 | # Mix and match! 14 | git commit -a -m 'unicorns are awesome ~unicorns here you go =bluescripts' 15 | -------------------------------------------------------------------------------- /gh.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra' 2 | require 'yaml' 3 | require 'json' 4 | require 'curb' 5 | require 'erubis' 6 | 7 | config = YAML::load(Erubis::Eruby.new(File.open('config.yml').read).result) 8 | 9 | set :sessions, true 10 | set :logging, true 11 | set :port, 3000 12 | set :gh_user, config['user'] 13 | set :gh_token, config['token'] 14 | set :gh_api, "https://github.com/api/v2/json/" 15 | set :gh_apiv3, "https://api.github.com/" 16 | set :gh_issue, "issues/show/:user/:repo/:number" 17 | set :gh_add_label, "issues/label/add/:user/:repo/:label/:number" 18 | set :gh_edit_issue, "repos/:user/:repo/issues/:number" 19 | #view_issue = "issues/show/dmsfl/fleet/23" 20 | 21 | 22 | def get_labels(user, repo, issue) 23 | endpoint = options.gh_issue.gsub(':user', user).gsub(':repo', repo).gsub(':number', issue) 24 | c = Curl::Easy.new(options.gh_api + endpoint) 25 | c.http_auth_types = :basic 26 | c.username = options.gh_user 27 | c.password = options.gh_token 28 | c.perform 29 | json = JSON.parse(c.body_str) 30 | json['issue']['labels'] 31 | end 32 | 33 | def add_label(user, repo, issue, label) 34 | endpoint = options.gh_add_label.gsub(':user', user).gsub(':repo', repo).gsub(':number', issue).gsub(':label', label) 35 | c = Curl::Easy.new(options.gh_api + endpoint) 36 | c.http_auth_types = :basic 37 | c.username = options.gh_user 38 | c.password = options.gh_token 39 | c.perform 40 | p c.body_str 41 | end 42 | 43 | def assign_issue(user, repo, issue, assignee) 44 | endpoint = options.gh_edit_issue.gsub(':user', user).gsub(':repo', repo).gsub(':number', issue) 45 | curl = Curl::Easy.http_post(options.gh_apiv3 + endpoint,{:assignee => assignee}.to_json) do |c| 46 | c.http_auth_types = :basic 47 | c.username = options.gh_user 48 | c.password = options.gh_token 49 | end 50 | p curl.body_str 51 | end 52 | 53 | get '/test' do 54 | p 'hello world' 55 | end 56 | 57 | post '/' do 58 | push = JSON.parse(params[:payload]) 59 | repo = push['repository']['name'] 60 | owner = push['repository']['owner']['name'] 61 | push['commits'].each do |c| 62 | m = c['message'] 63 | issue = m.scan(/[^\#][0-9]+/) 64 | if issue.size == 1 #only check for other goodies if an issue is mentioned 65 | begin 66 | user = m.scan(/\=[a-zA-Z0-9]+/)[0].split(//)[1..-1].join 67 | assign_issue(owner, repo, issue[0], user) 68 | rescue => e 69 | p e.to_s 70 | end 71 | labels = m.scan(/\~[a-zA-Z0-9]+/) 72 | labels.each do |l| 73 | add_label(owner, repo, issue[0], l.gsub('~', '')) 74 | end 75 | end 76 | end 77 | end 78 | --------------------------------------------------------------------------------