Class: GitTrelloPostCommit::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/git_trello_post_commit.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Hook

Returns a new instance of Hook.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/git_trello_post_commit.rb', line 10

def initialize(config)
  @api_key = config[:api_key]
  @oauth_token = config[:oauth_token]
  @repodir = config[: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 = GitTrelloPostCommit::Trello::HTTP.new(@oauth_token, @api_key)
  @repo = Git.open(@repodir)
end

Instance Method Details

#runObject



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
# File 'lib/git_trello_post_commit.rb', line 23

def run

  #get the commit and it's sha from HEAD
  commit  = @repo.gcommit('HEAD')
  new_sha = commit.sha

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

  puts "Trello: Commenting on 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]}"
    return
  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

  # 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
      puts "Trello: Moving card ##{match[3].to_i} to list #{target_list_id}"
      to_update[:idList] = target_list_id
      @http.update_card(results["id"], to_update)
    end
  end
      
end