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.



77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

#runObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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