Skip to content

Commit

Permalink
working on a command pattern for the commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rauhryan committed Oct 28, 2011
1 parent d5b490e commit c622da2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
8 changes: 5 additions & 3 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'sinatra'
require 'json'
require 'crack'
require './lib/github'


get '/:user/:repo/milestones' do
return Dashboard::Github.milestones(params[:user],params[:repo]).to_json
end
Expand All @@ -10,10 +12,10 @@
return Dashboard::Pebble.board(params[:user], params[:repo]).to_json
end

post 'webhook' do
Dashboard::Pebble.responds_to_message(Crack::Json.parse(params[:payload])) do |commit, hash|
post '/webhook' do
puts "webhook"
Dashboard::Pebble.responds_to_message(Crack::JSON.parse(params[:payload])) do |commit, hash|
puts commit
puts hash
end
end

Expand Down
25 changes: 17 additions & 8 deletions lib/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.board(user_name = "DovetailSoftware", repo = "blue")
issues = Github.get_issues(user_name, repo)

issues_by_label = issues.group_by { |issue| issue["current_state"]["name"] }

all_labels = Github.labels(user_name, repo)

all_labels = all_labels.map do |label|
Expand All @@ -24,14 +24,23 @@ def self.board(user_name = "DovetailSoftware", repo = "blue")

end

def self.responds_to_message(payload)
def self.register(command, &block)
@@sub ||= {}
@@sub[command] = block
end

payload["commits"].each do |commit|
r = /[Pp]ush (gh|GH)-(?<issue_number>\d+)/
yield commit, r.match(commit["message"]) if r.match(commit["message"])
end
def self.deliver(payload)
consumers = @@sub
r = /^(?<command>[A-Z]+) GH-(?<issue>[0-9]+)/
payload["commits"].each do |c|
match = r.match c["message"]
next if r.match match.nil?
next unless consumers.has_key? match[:command]
consumers[match[:command]].call payload, match[:issue]
end
end


end


Expand Down Expand Up @@ -75,8 +84,8 @@ def self.get_issues(user_name = "DovetailSoftware", repo = "blue")
def self.current_state(issue)

r = /(?<id>\d+) *- *(?<name>.+)/
issue["labels"].find {|x| r.match(x["name"])} || {"name" => "none"}

issue["labels"].find {|x| r.match(x["name"])} || {"name" => "none"}

end

Expand Down
6 changes: 1 addition & 5 deletions script/deliver_payload
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
require 'rubygems'
require "json"

unless ARGV[0]
puts "Usage: ./script/deliver_payload [label]"
exit 1
end

payload_file = File.new("#{File.dirname(__FILE__)}/sample_payload")
hash = eval(payload_file.read)

data_json = JSON.generate(hash["data"])
payload_json = JSON.generate(hash["payload"])

exec "curl --data-binary 'data=#{data_json}&payload=#{payload_json}' http://localhost:9393/label/refer/#{ARGV[0]}/token"
exec "curl --data-binary 'data=#{data_json}&payload=#{payload_json}' http://localhost:9393/webhook"
2 changes: 1 addition & 1 deletion script/sample_payload
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"removed" => [],
"message" => "clean up heads test. closes gh-2",
"message" => "clean up heads test. push gh-2",
"added" => [],
"timestamp" => "2007-10-10T00:18:20-07:00",
"modified" => ["test/test_grit.rb"],
Expand Down

0 comments on commit c622da2

Please sign in to comment.