public
Description: Undercover: CIA Ruby agent for GitHub
Homepage:
Clone URL: git://github.com/alloy/undercover.git
alloy (author)
Fri Apr 11 02:02:54 -0700 2008
commit  94482431df1756b8b26811de6ddb22a2c97a9e66
tree    49ede973f4d4c1786f9901c31dafa0a60391b6b5
parent  72ca3295774560c14beb99a20d99c1dc5511d5b1
undercover / undercover.rb
100644 183 lines (159 sloc) 5.774 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Created by Eloy Duran (e.duran@superalloy.nl)
# You can redistribute it and/or modify it under the Ruby's license or the GPL2.
 
require 'xmlrpc/client'
require 'cgi'
 
class Cia
  SERVER = "cia.vc"
  
  def initialize(data)
    @data = data
  end
  
  def send_commit_messages!
    server = XMLRPC::Client.new(SERVER)
    commit_messages.each { |message| server.call("hub.deliver", message) }
  rescue XMLRPC::FaultException => e
    #puts "XMLRPC Error: #{e.message}"
    # FIXME: Might be GitHub specific
  end
  
  def commit_messages
    @data[:commits].collect do |sha, commit|
      %{
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<generator>
<name>Undercover: CIA Ruby agent for GitHub</name>
<version>1.0</version>
</generator>
<source>
<project>#{h @data[:repository][:name]}</project>
<branch>#{h @data[:ref].split('/').last}</branch>
</source>
<timestamp>#{commit[:timestamp].to_i}</timestamp>
<body>
<commit>
<author>#{h "#{commit[:author][:name]} (#{commit[:author][:email]})"}</author>
<revision>#{sha}</revision>
<log>#{h commit[:message]}</log>
<url>#{h commit[:url]}</url>
</commit>
</body>
</message>
}
    end
  end
 
  private
 
  def h(input)
    CGI.escapeHTML(input.to_s)
  end
end
 
if __FILE__ == $0
  # Sample data taken from a GitHub guide.
  TIMESTAMP = Time.now
  DATA = {
    :before => "5aef35982fb2d34e9d9d4502f6ede1072793222d",
    :repository => {
      :url => "http://github.com/alloy/undercover",
      :name => "undercover",
      :owner => {
        :email => "e.duran@superalloy.nl",
        :name => "alloy"
      }
    },
    :commits => {
      '41a212ee83ca127e3c8cf465891ab7216a705f59' => {
        :url => "http://github.com/alloy/undercover/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
        :author => {
          :email => "e.duran@superalloy.nl",
          :name => "Eloy Duran"
        },
        :message => "duck for cover",
        :timestamp => TIMESTAMP
      },
      'de8251ff97ee194a289832576287d6f8ad74e3d0' => {
        :url => "http://github.com/alloy/undercover/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
        :author => {
          :email => "e.duran@superalloy.nl",
          :name => "Eloy Duran"
        },
        :message => "the company posted a message",
        :timestamp => TIMESTAMP
      }
    },
    :after => "de8251ff97ee194a289832576287d6f8ad74e3d0",
    :ref => "refs/heads/master"
  }
  
  # Try it out!
  #
  # cia = Cia.new(DATA)
  # cia.send_commit_messages!
  
  require "test/unit"
  require "rubygems"
  require "mocha"
  require "rexml/document"
  
  class TestUndercover < Test::Unit::TestCase
    def setup
      @cia = Cia.new(DATA)
    end
    
    def test_should_create_a_message_for_each_commit
      assert_equal 2, @cia.commit_messages.length
      @cia.commit_messages.each { |cm| assert_instance_of String, cm }
    end
    
    def test_should_create_xml_which_describes_the_commit
      @cia.commit_messages.each_with_index do |cm, idx|
        @message = REXML::Document.new(cm).root
        
        assert_equal 'undercover', elm('source/project')
        assert_equal 'master', elm('source/branch')
        
        assert_equal TIMESTAMP.to_i.to_s, elm('timestamp')
        
        sha, commit = DATA[:commits].to_a[idx]
        assert_equal "Eloy Duran (e.duran@superalloy.nl)", elm('body/commit/author')
        assert_equal sha, elm('body/commit/revision')
        assert_equal commit[:message], elm('body/commit/log')
        assert_equal commit[:url], elm('body/commit/url')
      end
    end
    
    def test_should_post_the_commit_messages_to_the_cia_server
      server = mock('CIA XMLRPC Server')
      XMLRPC::Client.expects(:new).with(Cia::SERVER).returns(server)
      @cia.commit_messages.each { |message| server.expects(:call).with('hub.deliver', message) }
      
      @cia.send_commit_messages!
    end
    
    def test_should_not_break_if_a_xmlrpc_exception_occurs
      XMLRPC::Client.stubs(:new).raises XMLRPC::FaultException.new('foo', 'bar')
      assert_nothing_raised(Exception) { @cia.send_commit_messages! }
    end
    
    def test_should_escape_certain_data_to_not_break_xml_validity
      cia = Cia.new({
        :before => "5aef35982fb2d34e9d9d4502f6ede1072793222d",
        :repository => {
          :url => "http://github.com/alloy/undercover",
          :name => "under & cover",
          :owner => {
            :email => "e.duran@superalloy.nl",
            :name => "alloy"
          }
        },
        :commits => {
          '41a212ee83ca127e3c8cf465891ab7216a705f59' => {
            :url => "undercover?foo=foo&bar=bar",
            :author => {
              :email => "<monsieur@église.fr>",
              :name => "Mister & Monsieur"
            },
            :message => "<strong>weird</strong>",
            :timestamp => TIMESTAMP
          }
        },
        :after => "de8251ff97ee194a289832576287d6f8ad74e3d0",
        :ref => "refs/heads/under&cover"
      })
      message = cia.commit_messages.first
      
      assert message.include?('<project>under &amp; cover</project>')
      assert message.include?('<author>Mister &amp; Monsieur (&lt;monsieur@église.fr&gt;)</author>')
      assert message.include?('<log>&lt;strong&gt;weird&lt;/strong&gt;</log>')
      assert message.include?('<branch>under&amp;cover</branch>')
      assert message.include?('<url>undercover?foo=foo&amp;bar=bar</url>')
    end
    
    private
    
    def elm(xpath)
      @message.elements[xpath].text
    end
  end
end