wesabe / wesabot

Wesabe's Campfire bot framework

This URL has Read+Write access

wesabot / campfire / polling_bot / plugins / deploy_plugin.rb
100644 95 lines (78 sloc) 2.842 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Plugin to get a list of commits that are on deck to be deployed
class DeployPlugin < Campfire::PollingBot::Plugin
  accepts :text_message, :addressed_to_me => true
 
  def process(message)
    case message.command
    when /on deck(?: for ([^\s\?]+))?/
      project = $1
 
      if not projects.any?
        bot.say("Sorry #{message.person}, I don't know about any projects. Please configure the deploy plugin.")
        return HALT
      end
 
      project ||= default_project
      if project.nil?
        bot.say("Sorry #{message.person}, I don't have a default project. Here are the projects I do know about:")
        bot.paste(projects.keys.sort.join("\n"))
        return HALT
      end
      project.downcase!
 
      info = project_info(project)
      if info.nil?
        bot.say("Sorry #{message.person}, I don't know anything about #{project}. Here are the projects I do know about:")
        bot.paste(projects.keys.sort.join("\n"))
        return HALT
      end
 
      range = nil
      begin
        range = "#{deployed_revision(project)}..HEAD"
        puts "asking for shortlog in range #{range}" if bot.debug
        shortlog = project_shortlog(project, range)
      rescue => e
        bot.say("Sorry #{message.person}, I couldn't get what's on deck for #{project}, got a #{e.class}:")
        bot.paste("#{e.message}\n\n#{e.backtrace.map{|l| " #{l}\n"}}")
        return HALT
      end
 
      if shortlog.nil? || shortlog =~ /\A\s*\Z/
        bot.say("There's nothing on deck for #{project} right now.")
        return HALT
      end
 
      bot.say("Here's what's on deck for #{project}:")
      bot.paste("$ git shortlog #{range}\n\n#{shortlog}")
 
      return HALT
    end
  end
 
  def help
    help_lines = [["what's on deck for <project>?", "get the shortlog of to-be-deployed changes for a specific project"]]
    help_lines << ["what's on deck?", "get shortlog of to-be-deployed changes for #{default_project}"] unless default_project.nil?
    return help_lines
  end
 
  def projects
    (config && config['project']) || {}
  end
 
  def default_project
    (config && config['default_project']) ||
      (projects.size == 1 ? projects.keys.first : nil)
  end
 
  private
 
  def project_info(project)
    projects[project]
  end
 
  def project_shortlog(project, treeish)
    info = project_info(project)
    return nil if info.nil?
    result = %x{ cd #{repository_path(project)}; git shortlog #{treeish} }
    if $?.exitstatus.zero?
      return result
    else
      raise "got non-zero exit status from git shortlog: #{$?.exitstatus}"
    end
  end
 
  def deployed_revision(project)
    info = project_info(project)
    return nil if info.nil?
    return bot.get_content(info["deployed_revision_url"])
  end
 
  def repository_path(project)
    File.join(config["repository_base_path"], "#{project}.git")
  end
end