Class: BubBot::Slack::Command::Deploy

Inherits:
BubBot::Slack::Command show all
Defined in:
lib/bub_bot/slack/commands/deploy.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BubBot::Slack::Command

can_handle?, #initialize

Constructor Details

This class inherits a constructor from BubBot::Slack::Command

Class Method Details

.aliasesObject



2
3
4
# File 'lib/bub_bot/slack/commands/deploy.rb', line 2

def self.aliases
  %w(deploy ship push)
end

Instance Method Details

#branches(target = nil) ⇒ Object

All known branches for the given target. Returns all branches for all targets if target is nil.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bub_bot/slack/commands/deploy.rb', line 108

def branches(target = nil)
  puts 'deploy.branches'
  @_branch_cache ||= {}
  if target == nil
    targets.flat_map do |target|
      branches(target)
    end
  else
    @_branch_cache[target] ||= deployer.branches(target)
  end
end

#runObject

bub deploy cannoli core kk_foo



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
96
97
98
99
100
101
102
103
104
# File 'lib/bub_bot/slack/commands/deploy.rb', line 7

def run
  puts 'deploying'
  server = nil
  deploys = {}
  iterator = create_token_iterator

  # Skip the command name itself
  iterator.next

  while token = iterator.peek
    puts "token loop #{token}"
    if token == 'and'
      iterator.next
      next
    end

    # TODO: strip trailing commas (need to do this in the iterator)
    #token = token.sub!(/,$/, '')

    if servers.names.include?(token)
      puts 'servers.names'
      server = iterator.next

    # Handle 'kk_some_fix [to] core'
    elsif branches.include?(token)
      puts 'branches.include'
      
      branch = iterator.next
      target = iterator.next

      # Skip connector words
      target = iterator.next if %w(to on with).include?(target)

      deploys[target] = branch

    # Handle 'core kk_some_fix'
    elsif targets.include?(token)
      puts 'else targets.inclue'
      target = iterator.next
      branch = iterator.next
      deploys[target] = branch
    else
      raise RespondableError.new("I didn't recognize #{token}")
    end
  end

  # Validate all the potential deploys before we start
  deploys.each do |target, branch|
    puts 'deploys.each'
    # TODO: infer targets, etc
    unless targets.include?(target)
      raise RespondableError.new("Unknown deploy target #{token}. Try one of #{targets.join(', ')}")
    end
    unless branches(target).include?(branch)
      raise RespondableError.new("Deploy target #{target} doesn't have a branch named #{branch}.  Maybe you forgot to push that branch?")
    end
  end

  server = servers.first_unclaimed unless server

  unless server
    return respond('No servers available')
  end

  unless deploys.any?
    return respond("Please specify a target and a branch, eg `#{bot_name} deploy #{server} core kk_add_lasers`");
  end

  # TODO:
  # - default to deploying develop

  claim_data = servers.list[server]

  if claim_data['user'] && claim_data['user'] != source_user_name
    raise RespondableError.new("Server already claimed by #{claim_data['user']}.  Use the 'take' command first to override their claim.")
  elsif
    # Extend our current claim on this server to 1 hour from now unless we
    # already have it claimed for longer
    if !claim_data['expires_at'] || claim_data['expires_at'] < 1.hour.from_now
      servers.take(
        user: source_user_name,
        duration: 1.hour,
        server_name: server
      )
    end
  end

  message_segments = deploys.map do |target, branch|
    "branch '#{branch}' to target '#{target}'"
  end
  respond("Deploying on server #{server}: #{message_segments.join('; ')}").deliver

  deploys.each do |target, branch|
    deployer.deploy(server, target, branch)
  end

  respond("Finished deploying on server #{server}: #{message_segments.join('; ')}");
end

#targetsObject



120
121
122
# File 'lib/bub_bot/slack/commands/deploy.rb', line 120

def targets
  deployer.target_names
end