public
Description: A web hook that takes GitHub commits and turns them into Unfuddle changesets posting using the Unfuddle API.
Homepage:
Clone URL: git://github.com/mbleigh/github-unfuddle.git
github-unfuddle / github_unfuddle.rb
100644 76 lines (60 sloc) 2.077 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'rubygems'
require 'sinatra'
require 'json'
require 'yaml'
require 'net/http'
 
CONFIG = YAML.load_file("config.yml")
 
post '/' do
  push = JSON.parse(params[:payload])
  build_unfuddle_xml_from(push)
end
 
def build_unfuddle_xml_from(push)
  @repository = push["repository"]
  @commits = push["commits"]
  
  raise "Must specify Unfuddle subdomain, user, and password." unless CONFIG["unfuddle"]["subdomain"] && CONFIG["unfuddle"]["user"] && CONFIG["unfuddle"]["password"]
  raise "Could not map from GitHub project name to Unfuddle Project ID." unless unfuddle_project_id
  
  successes = []
  
  @commits.each do |id, commit|
    timestamp = Time.parse(commit['timestamp'])
    xml = <<-XML
<changeset>
#{"<author-id type=\"integer\">#{unfuddle_author_id(commit)}</author-id>" if unfuddle_author_id(commit)}
<message>#{commit["message"]}
<revision type="integer">#{timestamp.strftime("%y%m%d%H%M")}</revision>
Details: #{commit["url"]}</message>
</changeset>
XML
    
    successes << post_changeset_to_unfuddle(xml)
  end
  
  successes.inspect
end
 
def post_changeset_to_unfuddle(xml)
  http = Net::HTTP.new("#{CONFIG["unfuddle"]["subdomain"]}.unfuddle.com", CONFIG["unfuddle"]["use_ssl"] ? 443 : 80)
 
  # if using ssl, then set it up
  if CONFIG["unfuddle"]["use_ssl"]
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  
  begin
    path = "/api/v1/projects/#{unfuddle_project_id}/changesets.xml"
    
    request = Net::HTTP::Post.new(path, {'Content-type' => 'application/xml'})
    request.basic_auth CONFIG["unfuddle"]["user"], CONFIG["unfuddle"]["password"]
    request.body = xml
 
    response = http.request(request)
    
    if response.code == "201"
      return response['Location']
    else
      puts response.body
      return false
    end
  rescue => e
    puts e.message
    return false
  end
end
 
def unfuddle_project_id
  CONFIG["repositories"][@repository["name"]]["unfuddle_project_id"]
end
 
def unfuddle_author_id(commit)
  CONFIG["unfuddle"]["people"][commit["author"]["email"]]
end