Class: GitHook

Inherits:
Object
  • Object
show all
Defined in:
lib/git-trello.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GitHook



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/git-trello.rb', line 11

def initialize(config)
  @api_key = config[:api_key]
  @oauth_token = config[:oauth_token]
  @repodir = Dir.pwd
  @board_id = config[:board_id]
  @list_id_in_progress = config[:list_id_in_progress]
  @list_id_done = config[:list_id_done]
  @commit_url_prefix = config[:commit_url_prefix]
  
  @http = Trello::HTTP.new(@oauth_token, @api_key)
  @repo = Grit::Repo.new(@repodir)
end

Instance Method Details

#post_receiveObject



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
# File 'lib/git-trello.rb', line 26

def post_receive
  while msg = gets
 #get the data out of the input
 old_sha, new_sha, ref = msg.split(' ', 3)

 #get the commit out of the new_sha
 commit = @repo.commit(new_sha)

 # Figure out the card short id
 match = commit.message.match(/((case|card|close|fix)e?s? \D?([0-9]+))/i)
 next unless match and match[3].to_i > 0

 puts "Trello: Commenting on the card ##{match[3].to_i}"

 results = @http.get_card(@board_id, match[3].to_i)
 unless results
puts "Trello: Cannot find card matching ID #{match[3]}"
next
 end
 results = JSON.parse(results)

 # Determine the action to take
 target_list_id = ""
 target_list_id = case match[2].downcase
when "case", "card" then @list_id_in_progress
when "close", "fix" then @list_id_done
 end

 puts "Trello: Moving card ##{match[3].to_i} to list #{target_list_id}"

 # Add the commit comment
 message = "#{commit.author.name}:\n#{commit.message}"
 message << "\n\n#{@commit_url_prefix}#{new_sha}" unless @commit_url_prefix.nil?
 message.gsub!(match[1], "")
 message.gsub!(/\(\)$/, "")
 message.gsub!(/Signed-off-by: (.*) <(.*)>/,"")
 @http.add_comment(results["id"], message)

 unless target_list_id == ""
to_update = {}
unless results["idList"] == target_list_id
  to_update[:idList] = target_list_id
  @http.update_card(results["id"], to_update)
end
 end
 ""
  end
end

#testObject



23
24
25
# File 'lib/git-trello.rb', line 23

def test
  puts 'git-trello post receive hook trigered just fine'
end