svn-campfire-notifier 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,2 @@
1
+ Copyright (c) 2010, Fingertips, Manfred Stienstra <manfred@fngtps.com>
2
+ License: http://www.opensource.org/licenses/mit-license.php
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if $0 == __FILE__
4
+ require 'rubygems'
5
+ $:.unshift(File.expand_path('../../lib', __FILE__))
6
+ end
7
+
8
+ $KCODE = 'u'
9
+
10
+ require 'svn_campfire_notifier'
11
+ require 'option_parser'
12
+
13
+ SVNCampfireNotifier.run(*OptionParser.parse(ARGV))
@@ -0,0 +1,66 @@
1
+ require 'broach'
2
+ require 'rest'
3
+ require 'json'
4
+
5
+ require 'svn_campfire_notifier/core_ext'
6
+
7
+ class SVNCampfireNotifier
8
+ class ConfigurationError < StandardError; end
9
+
10
+ autoload :GoogleImage, 'svn_campfire_notifier/google_image'
11
+ autoload :Message, 'svn_campfire_notifier/message'
12
+ autoload :Project, 'svn_campfire_notifier/project'
13
+ autoload :Wrap, 'svn_campfire_notifier/wrap'
14
+
15
+ attr_reader :project
16
+
17
+ def initialize(test_mode, repository_path, revision)
18
+ @test_mode = test_mode
19
+ @project = Project.new(repository_path, revision)
20
+
21
+ @settings = YAML.load_file(File.expand_path('~/.svn_campfire_notifier.yml'))
22
+ @room = @settings['room']
23
+ Broach.settings = {
24
+ 'account' => @settings['account'],
25
+ 'token' => @settings['token'],
26
+ 'use_ssl' => @settings['use_ssl']
27
+ }
28
+ end
29
+
30
+ def test_mode?
31
+ @test_mode
32
+ end
33
+
34
+ def run
35
+ if test_mode?
36
+ Message.new(project).parts.each do |part|
37
+ puts part.first
38
+ end
39
+ elsif room = Broach::Room.find_by_name(@room)
40
+ Message.new(project).parts.each do |part|
41
+ room.speak(*part)
42
+ end
43
+ else
44
+ raise ConfigurationError, "Can't find a room with the name `#{@room}'"
45
+ end
46
+ end
47
+
48
+ def self.usage
49
+ puts "Usage: #{File.basename($0)} <repository> <revision>"
50
+ puts ""
51
+ puts "Options:"
52
+ puts " -v, --verbose: Print informative messages"
53
+ puts " -t, --test: Don't send to Campfire, just print what you would send"
54
+ end
55
+
56
+ def self.run(options, args)
57
+ if args.length == 2
58
+ new(
59
+ options.keys.include?('t') || options.keys.include?('test'),
60
+ *args
61
+ ).run
62
+ else
63
+ usage
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,15 @@
1
+ class Array
2
+ def random
3
+ self[Kernel::rand(self.length)]
4
+ end
5
+ end
6
+
7
+ class String
8
+ def chars
9
+ unpack('U*')
10
+ end
11
+
12
+ def self.from_chars(chars)
13
+ chars.pack('U*')
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ class SVNCampfireNotifier
2
+ class GoogleImage
3
+ URL = 'http://images.google.com/images?safe=active&q=%s'
4
+ RESULTS_REGEXP = /setResults\((\[\[.+\]\])\);/
5
+
6
+ def initialize(query)
7
+ @query = query
8
+ end
9
+
10
+ def query
11
+ "kitty+#{@query}"
12
+ end
13
+
14
+ def url
15
+ URL % query
16
+ end
17
+
18
+ def feeling_lucky
19
+ response = REST.get(url)
20
+ if response.ok? and match = RESULTS_REGEXP.match(response.body)
21
+ data = JSON.parse(match[1]).first
22
+ image_url = data[3]
23
+ token = data[2]
24
+ image_host = data[14]
25
+ "#{image_host}?q=tbn:#{token}#{image_url}"
26
+ end
27
+ end
28
+
29
+ def self.feeling_lucky(query)
30
+ new(query).feeling_lucky
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ class SVNCampfireNotifier
2
+ class Message
3
+ SALUTATIONS = %w(Hi Heej Faffie Kus-kus O~hai Gütentag Bonjour Trouwens Bananen Perenijsjes Hihi Accordions ☹ ☻ ☺ Moo Woof Moof!) +
4
+ ['Je weet zelf', 'Je weet toch', 'Weet je', 'Prijs de heer', 'Voor de verandering', 'Deşteaptă-te, române!', 'Alea iacta est'] +
5
+ ['Zin in!', 'Not zin in!', 'Kiek \'m gaaaan'] +
6
+ ['Dear Sir/Madame', 'Madame, Monsieur', 'Sehr geehrte Damen und Herren', 'Sayın Yetkili', 'Chiranjeevi', 'Гражданин'] +
7
+ ['Intindiquinchu mananchu! Ichaqa']
8
+
9
+ attr_reader :project
10
+
11
+ def initialize(project)
12
+ @project = project
13
+ end
14
+
15
+ def parts
16
+ parts = []
17
+ parts << ["#{SALUTATIONS.random}, #{project.author.capitalize} just committed revision #{project.revision} of #{project.name}: https://fngtps.com/browser/#{project.name}/changesets/#{project.revision}"]
18
+ parts << ["#{Wrap.wrap(project.log)}\n\n#{project.changed.strip}", {:type => :paste}]
19
+ if image_url = GoogleImage.feeling_lucky(project.revision)
20
+ parts << [image_url]
21
+ end
22
+ parts
23
+ # room.speak(IMAGE_EXCEPTIONS.has_key?(revision) ? IMAGE_EXCEPTIONS[revision] : Google::Images.feeling_lucky(revision).thumbnail_url)
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,26 @@
1
+ class SVNCampfireNotifier
2
+ class Project
3
+ attr_reader :repository_path, :revision
4
+
5
+ def initialize(repository_path, revision)
6
+ @repository_path = repository_path
7
+ @revision = revision.to_i
8
+ end
9
+
10
+ def name
11
+ @repository_path.split('/').last
12
+ end
13
+
14
+ def author
15
+ @author ||= `svnlook author -r #{revision} #{repository_path}`.strip
16
+ end
17
+
18
+ def changed
19
+ @changes ||= `svnlook changed -r #{revision} #{repository_path}`.strip
20
+ end
21
+
22
+ def log
23
+ @log ||= `svnlook log -r #{revision} #{repository_path}`.strip
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module Wrap
2
+ def self.wrap(str, max_size=80)
3
+ str.split("\n").map do |line|
4
+ String.from_chars wrap_chars(line.chars, max_size)
5
+ end.join("\n")
6
+ end
7
+
8
+ def self.wrap_chars(chars, max_size=80)
9
+ space_char = " ".chars.first
10
+ if chars.length > max_size and chars.include?(space_char)
11
+ # If we find a space left of max_size
12
+ if last_whitespace_index = chars[0...max_size].rindex(space_char)
13
+ left = chars[0...last_whitespace_index]
14
+ right = wrap_chars(chars[last_whitespace_index+1..-1], max_size)
15
+ left.empty? ? right : left + " \n".chars + right
16
+ # If we find a space right of max_size
17
+ elsif last_whitespace_index = chars[max_size..-1].index(space_char)
18
+ last_whitespace_index += max_size
19
+ left = wrap_chars(chars[0...last_whitespace_index], max_size)
20
+ right = wrap_chars(chars[last_whitespace_index+1..-1], max_size)
21
+ left.empty? ? right : left + " \n".chars + right
22
+ else
23
+ raise "We lost characters from the string, did it changes inbetween?"
24
+ end
25
+ else
26
+ chars
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svn-campfire-notifier
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Manfred Stienstra
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-08 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: broach
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 2
33
+ version: "0.2"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: " A post-commit hook for Subversion that notifies Campfire of commits.\n"
51
+ email: manfred@fngtps.com
52
+ executables:
53
+ - svn-campfire-notifier
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE
58
+ files:
59
+ - LICENSE
60
+ - lib/svn_campfire_notifier.rb
61
+ - lib/svn_campfire_notifier/core_ext.rb
62
+ - lib/svn_campfire_notifier/google_image.rb
63
+ - lib/svn_campfire_notifier/message.rb
64
+ - lib/svn_campfire_notifier/project.rb
65
+ - lib/svn_campfire_notifier/wrap.rb
66
+ - bin/svn-campfire-notifier
67
+ has_rdoc: true
68
+ homepage:
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=utf-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: A post-commit hook for Subversion that notifies Campfire of commits.
101
+ test_files: []
102
+