murmurs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +0 -0
  2. data/bin/murmurs +62 -0
  3. data/bin/murmurs-git +56 -0
  4. data/lib/murmurs.rb +86 -0
  5. metadata +72 -0
data/README.md ADDED
File without changes
data/bin/murmurs ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'murmurs'
5
+ require 'optparse'
6
+
7
+ options = {
8
+ :murmurs_url => ENV['MINGLE_MURMURS_URL'],
9
+ :access_key_id => ENV['MINGLE_ACCESS_KEY_ID'],
10
+ :access_secret_key => ENV['MINGLE_ACCESS_SECRET_KEY'],
11
+ :skip_ssl_verify => false,
12
+ :git => false,
13
+ :git_branch => 'master'
14
+ }
15
+
16
+ NOTES = %Q{
17
+ You can also setup the following environment variables as options:
18
+ MINGLE_MURMURS_URL => --murmurs_url
19
+ MINGLE_ACCESS_KEY_ID => --mingle_access_key_id
20
+ MINGLE_ACCESS_SECRET_KEY => --mingle_access_secret_key
21
+
22
+ For further information about Mingle user access key id and secure key, please read:
23
+ http://www.thoughtworks.com/products/docs/mingle/current/help/configuring_hmac_authentication.html
24
+ }
25
+
26
+ opts = OptionParser.new do |opts|
27
+ opts.banner = "Usage: murmurs [options] MESSAGE"
28
+
29
+ opts.on("-m", "--murmurs_url MINGLE_MURMURS_URL", "Mingle project murmurs url") do |v|
30
+ options[:murmurs_url] = v
31
+ end
32
+
33
+ opts.on("-k", "--mingle_access_key_id MINGLE_ACCESS_KEY_ID", "Mingle user access key id") do |v|
34
+ options[:access_key_id] = v
35
+ end
36
+
37
+ opts.on("-s", "--mingle_access_secret_key MINGLE_SECURE_ACCESS_KEY", "Mingle user access secure key") do |v|
38
+ options[:access_secret_key] = v
39
+ end
40
+
41
+ opts.on_tail('-g', '--git', 'Process standard input of Git hook post-receive, and murmur each commits; default is false') do
42
+ options[:git] = true
43
+ end
44
+
45
+ opts.on("-b", "--git_branch GIT_BRANCH", "Murmur Git commits in branch, default is master") do |v|
46
+ options[:git_branch] = v
47
+ end
48
+
49
+ opts.on_tail("-i", "--skip_ssl_verify", "Skip SSL verify in case the server is using self signed SSL certificate, default is false") do
50
+ options[:skip_ssl_verify] = true
51
+ end
52
+
53
+ opts.on_tail('-h', '--help') do
54
+ puts opts
55
+ puts NOTES
56
+ exit(0)
57
+ end
58
+
59
+ end.parse!
60
+
61
+ include Murmurs
62
+ murmur(options.delete(:murmurs_url), ARGV[0] || ARGF.read, options)
data/bin/murmurs-git ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if ARGV[0] == '-h' || ARGV[0] == '--help'
4
+ puts "Usage murmurs-git GIT_REPOSITORY_PATH"
5
+ puts ""
6
+ puts "Install a git post-receive hook in hooks directory inside given git repository path"
7
+
8
+ exit(0)
9
+ end
10
+
11
+ def git_hooks_dir(git_repo_dir)
12
+ hooks = File.join(git_repo_dir, 'hooks')
13
+ if File.exists?(hooks)
14
+ hooks
15
+ else
16
+ hooks = File.join(git_repo_dir, '.git', 'hooks')
17
+ if File.exists?(hooks)
18
+ hooks
19
+ else
20
+ raise "Could not find hooks dir or .git/hooks dir in #{git_repo_dir}"
21
+ end
22
+ end
23
+ end
24
+
25
+ require 'fileutils'
26
+
27
+ hooks = git_hooks_dir(ARGV[0])
28
+ hook = File.join(hooks, 'post-receive')
29
+
30
+ if File.exists?(hook)
31
+ puts "There is #{hook} file existing, please backup / remove it."
32
+ exit
33
+ end
34
+
35
+ File.open(hook, 'w') do |f|
36
+ f.write <<-BASH
37
+ #!/usr/bin/env bash
38
+
39
+ mingle_murmurs_url=$(git config hooks.minglemurmursurl)
40
+ mingle_access_key_id=$(git config hooks.mingleaccesskeyid)
41
+ mingle_access_secret_key=$(git config hooks.mingleaccesssecretkey)
42
+
43
+ echo "$(cat)" | murmurs -g -b master -m "$mingle_murmurs_url" -k "$mingle_access_key_id" -s "$mingle_access_secret_key"
44
+ BASH
45
+ end
46
+ FileUtils.chmod('+x', hook)
47
+ puts "Installed #{hook}"
48
+ puts "Please setup the following git config:"
49
+ puts %Q{
50
+ git config hooks.minglemurmursurl "https://<your_site>.mingle.thoughtworks.com/api/v2/projects/<your_project>/murmurs.xml"
51
+ git config hooks.mingleaccesskeyid "MINGLE ACCESS KEY ID"
52
+ git config hooks.mingleaccesssecretkey "MINGLE ACCESS SECRET KEY"
53
+
54
+ For further information, please read this:
55
+ http://www.thoughtworks.com/products/docs/mingle/current/help/configuring_hmac_authentication.html
56
+ }
data/lib/murmurs.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'time'
4
+ require 'api-auth'
5
+
6
+ module Murmurs
7
+ def murmur(url, msg, options={})
8
+ if options[:git]
9
+ msg = git_commits_murmur(msg, options[:git_branch])
10
+ end
11
+
12
+ if url.to_s !~ /\Ahttps?\:\/\/.+\/projects\/[^\/]+\/murmurs/
13
+ raise "Invalid murmurs URL: #{url.inspect}"
14
+ end
15
+ if msg.nil? || msg.empty?
16
+ puts "Nothing to murmur." unless options[:git]
17
+ return
18
+ end
19
+ t = 20
20
+ Array(msg).each do |m|
21
+ git_log(options[:git], m)
22
+ http_post(url, {'murmur[body]' => m}, options)
23
+ end
24
+ end
25
+
26
+ def git_log(log, m)
27
+ if log
28
+ if m.size > t
29
+ puts "murmur #{m[0..t]}..."
30
+ else
31
+ puts "murmur #{m}"
32
+ end
33
+ end
34
+ end
35
+
36
+ # input: git post receive stdin string
37
+ # branch: git branch
38
+ def git_commits_murmur(input, branch)
39
+ data = input.split("\n").map do |l|
40
+ l.split
41
+ end.find do |l|
42
+ l[2] =~ /\Arefs\/heads\/#{branch}\z/
43
+ end
44
+
45
+ return if data.nil?
46
+
47
+ null_rev = '0' * 40
48
+ from_rev, to_rev, _ = data
49
+ if to_rev == null_rev # delete branch
50
+ "Someone deleted branch #{branch}."
51
+ else
52
+ revs = if from_rev == null_rev # new branch
53
+ to_rev
54
+ else
55
+ "#{from_rev}..#{to_rev}"
56
+ end
57
+ `git rev-list #{revs}`.split("\n").map do |rev|
58
+ `git log -n 1 #{rev}`
59
+ end
60
+ end
61
+ end
62
+
63
+ def http_post(url, params, options={})
64
+ uri = URI.parse(url)
65
+ http = Net::HTTP.new(uri.host, uri.port)
66
+ if uri.scheme == 'https'
67
+ http.use_ssl = true
68
+ if options[:skip_ssl_verify]
69
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
70
+ end
71
+ end
72
+
73
+ request = Net::HTTP::Post.new(uri.request_uri)
74
+ request.form_data = params if params
75
+
76
+ if options[:access_key_id]
77
+ ApiAuth.sign!(request, options[:access_key_id], options[:access_secret_key])
78
+ end
79
+
80
+ response = http.request(request)
81
+
82
+ if response.code.to_i > 300
83
+ response.error!
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: murmurs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ThoughtWorks Mingle Team
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: api-auth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
30
+ description: ! 'Use HMAC to create murmur in Mingle project.
31
+
32
+ Git post-receive hook to post murmurs when new commits pushed to Git server.
33
+
34
+ '
35
+ email:
36
+ - mingle-dev@thoughtworks.com
37
+ executables:
38
+ - murmurs
39
+ - murmurs-git
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - README.md
44
+ - bin/murmurs
45
+ - bin/murmurs-git
46
+ - lib/murmurs.rb
47
+ homepage: https://github.com/ThoughtWorksStudios/murmurs
48
+ licenses:
49
+ - MIT
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.23
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Post murmur to Mingle project murmurs
72
+ test_files: []