git-trello 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #This is gem, no need to require rubygems
4
+ #require 'rubygems'
5
+ require 'json'
6
+ require 'grit'
7
+ require_relative 'git-trello/trello-http'
8
+
9
+
10
+ class GitHook
11
+ def initialize(config)
12
+ @api_key = config[:api_key]
13
+ @oauth_token = config[:oauth_token]
14
+ @repodir = Dir.pwd
15
+ @board_id = config[:board_id]
16
+ @list_id_in_progress = config[:list_id_in_progress]
17
+ @list_id_done = config[:list_id_done]
18
+ @commit_url_prefix = config[:commit_url_prefix]
19
+
20
+ @http = Trello::HTTP.new(@oauth_token, @api_key)
21
+ @repo = Grit::Repo.new(@repodir)
22
+ end
23
+ def test
24
+ puts 'git-trello post receive hook trigered just fine'
25
+ end
26
+ def post_receive
27
+ while msg = gets
28
+ #get the data out of the input
29
+ old_sha, new_sha, ref = msg.split(' ', 3)
30
+
31
+ #get the commit out of the new_sha
32
+ commit = @repo.commit(new_sha)
33
+
34
+ # Figure out the card short id
35
+ match = commit.message.match(/((case|card|close|fix)e?s? \D?([0-9]+))/i)
36
+ next unless match and match[3].to_i > 0
37
+
38
+ puts "Trello: Commenting on the card ##{match[3].to_i}"
39
+
40
+ results = @http.get_card(@board_id, match[3].to_i)
41
+ unless results
42
+ puts "Trello: Cannot find card matching ID #{match[3]}"
43
+ next
44
+ end
45
+ results = JSON.parse(results)
46
+
47
+ # Determine the action to take
48
+ target_list_id = ""
49
+ target_list_id = case match[2].downcase
50
+ when "case", "card" then @list_id_in_progress
51
+ when "close", "fix" then @list_id_done
52
+ end
53
+
54
+ puts "Trello: Moving card ##{match[3].to_i} to list #{target_list_id}"
55
+
56
+ # Add the commit comment
57
+ message = "#{commit.author.name}:\n#{commit.message}"
58
+ message << "\n\n#{@commit_url_prefix}#{new_sha}" unless @commit_url_prefix.nil?
59
+ message.gsub!(match[1], "")
60
+ message.gsub!(/\(\)$/, "")
61
+ message.gsub!(/Signed-off-by: (.*) <(.*)>/,"")
62
+ @http.add_comment(results["id"], message)
63
+
64
+ unless target_list_id == ""
65
+ to_update = {}
66
+ unless results["idList"] == target_list_id
67
+ to_update[:idList] = target_list_id
68
+ @http.update_card(results["id"], to_update)
69
+ end
70
+ end
71
+ ""
72
+ end
73
+ end
74
+ end
75
+
@@ -0,0 +1,60 @@
1
+ #Copyright (c) 2011 Zachary Anker
2
+ #Minor modifications by Zeljko Milojkovic
3
+
4
+ require "cgi"
5
+ require "net/https"
6
+
7
+ module Trello
8
+ class HTTP
9
+ def initialize(token, api_key)
10
+ @token, @api_key = token, api_key
11
+ @uri = URI("https://api.trello.com")
12
+ end
13
+
14
+ def get_card(board_id, card_id)
15
+ http_request(:get, "/1/boards/#{board_id}/cards/#{card_id}", :params => {:fields => "idList,closed"})
16
+ end
17
+
18
+ def update_card(card_id, params)
19
+ http_request(:put, "/1/cards/#{card_id}", :params => params)
20
+ end
21
+
22
+ def add_comment(card_id, comment)
23
+ http_request(:post, "/1/cards/#{card_id}/actions/comments", :body => "text=#{CGI::escape(comment)}")
24
+ end
25
+
26
+ def get_cards(list_id)
27
+ http_request(:get, "/1/lists/#{list_id}/cards", :params => {:fields => "idList"})
28
+ end
29
+
30
+ private
31
+ def http_request(method, request_path, args={})
32
+ request_path << "?"
33
+ args[:params] ||= {}
34
+ args[:params][:key] = @api_key
35
+ args[:params][:token] = @token
36
+ args[:params].each {|k, v| request_path << "#{k}=#{CGI::escape(v.to_s)}&"}
37
+
38
+ http = Net::HTTP.new(@uri.host, @uri.port)
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+
42
+ http.start
43
+
44
+ if method == :get
45
+ response = http.request_get(request_path)
46
+ elsif method == :post
47
+ response = http.request_post(request_path, args.delete(:body))
48
+ elsif method == :put
49
+ response = http.request_put(request_path, args.delete(:body))
50
+ end
51
+
52
+ unless response.code == "200" or response.code == "201"
53
+ raise Net::HTTPError.new("#{response.body == "" ? response.message : response.body.strip} (#{response.code})", response)
54
+ end
55
+
56
+ response.body
57
+ end
58
+ end
59
+ end
60
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-trello
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zeljko Milojkovic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &11576340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *11576340
25
+ - !ruby/object:Gem::Dependency
26
+ name: grit
27
+ requirement: &11571120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *11571120
36
+ description: ! 'This gem should be used by post-receive hook in any Git repository
37
+ to comment on and move Trello cards in a specified board. What will be done is based
38
+ on a Git commit message:for example, if a message contains ''Card #20'', post-receive
39
+ will add the rest of comment message as a comment on card #20.'
40
+ email: zeljko@zwr.fi
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/git-trello.rb
46
+ - lib/git-trello/trello-http.rb
47
+ homepage: http://zwr.fi/git-trello
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.11
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A gem for Git hooks.
71
+ test_files: []