class GitTrelloPostCommit::Hook

Public Class Methods

new(config) click to toggle source
# File lib/git_trello_post_commit.rb, line 77
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

Public Instance Methods

run() click to toggle source
# File lib/git_trello_post_commit.rb, line 90
def run

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

  parser = MessageParser.new(commit.message)

  parser.instructions.each do |card_id, action|

    puts "Trello: Commenting on card ##{card_id}"

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

    # Determine the action to take
    target_list_id = case action
      when :in_progress then @list_id_in_progress
      when :done        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!(/\(\)$/, "")
    message.gsub!(/Signed-off-by: (.*) <(.*)>/,"")
    @http.add_comment(results["id"], message)

    if target_list_id
      to_update = {}
      unless results["idList"] == target_list_id
        puts "Trello: Moving card ##{card_id} to list #{target_list_id}"
        to_update[:idList] = target_list_id
        to_update[:pos]    = "top"
        @http.update_card(results["id"], to_update)
      end
    end

  end
      
end