Class: Buildmeister::Base

Inherits:
Object
  • Object
show all
Includes:
GitUtils, StringUtils
Defined in:
lib/buildmeister/base.rb

Constant Summary collapse

RETRY_COUNT =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitUtils

#current_branch, #generate_timed_branch, #local_branches, #new_experimental, #new_hotfix, #pull_bin, #remote_branches

Methods included from StringUtils

#divider

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



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
# File 'lib/buildmeister/base.rb', line 65

def initialize(options = {})
  @options = options
        
  # Lighthouse setup
  @config  = Buildmeister::Base.load_config
  @account = Lighthouse::.new(@config['account'], @config['token'])
  
  self.projects = []
  
  projects.extend Finder
  
  if @options[:project]
    @config['projects'] = @config['projects'].select { |p| p['name'] == @options[:project] } 
    raise "#{@options[:project]} did not match any projects in the config file" if (@config['projects'] || []).empty?
  end
  
  @config['projects'].each do |project_name|
    self.projects << Buildmeister::Project.new(project_name, @account, @options)
  end
  
  self.notification_interval = @config['notification_interval']
  
  # Did we pass in a command?
  self.command = options[:command]
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



63
64
65
# File 'lib/buildmeister/base.rb', line 63

def args
  @args
end

#commandObject

Returns the value of attribute command.



63
64
65
# File 'lib/buildmeister/base.rb', line 63

def command
  @command
end

#notification_intervalObject

Returns the value of attribute notification_interval.



63
64
65
# File 'lib/buildmeister/base.rb', line 63

def notification_interval
  @notification_interval
end

#projectsObject

Returns the value of attribute projects.



63
64
65
# File 'lib/buildmeister/base.rb', line 63

def projects
  @projects
end

Class Method Details

.load_configObject



269
270
271
# File 'lib/buildmeister/base.rb', line 269

def self.load_config
  YAML.load_file(File.expand_path('~/.buildmeister_config.yml'))
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/buildmeister/base.rb', line 95

def changed?
  projects.any?(&:changed?)
end

#go!Object



91
92
93
# File 'lib/buildmeister/base.rb', line 91

def go!
  send command if command
end

#list_staged_ticketsObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/buildmeister/base.rb', line 183

def list_staged_tickets
  # First, fetch origin to make sure we have all the necessary information
  system('git fetch origin')
  shas = `git log origin/#{@options[:compare_branch]}..HEAD --pretty=format:%H`.split

  # Generate an array of arrays - the inner array lists all branches that contain
  # each of the SHAs listed above
  contains_info = shas.map do |sha|
    `git branch -a --contains #{sha}`.split
  end

  staged_branches = Set.new

  contains_info.each do |branches|
    branches.each do |branch|
      # First, sanitize
      branch.gsub!('remotes/origin/', '')

      # Only match branch names that start with a digit (we're assuming
      # that these all reference tickets)
      staged_branches << branch if branch =~ /^\d+/
    end
  end

  tickets = staged_branches.to_a.map(&:to_i)
  puts "Staged: #{tickets.join(',')}"

  tickets
end

#move_allObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/buildmeister/base.rb', line 157

def move_all
  bin_name = @options[:move_from]
  
  if projects.size > 1
    puts "#{projects.size} projects are loaded (#{projects.map(&:name).join(', ')})"
    puts "Do you really want to move tickets in all projects? [y/n]"
    
    choice = gets
    
    if choice.downcase.strip == 'n'
      puts "aborting..."
      return
    end
  end
  
  projects.each do |project|
    project.bins.named(bin_name).tickets.each do |ticket|
      puts "processing #{project.name}: #{ticket.id}"
      ticket.state = @options[:to_state]
      ticket.save
    end
  end

  puts "All tickets from bin #{@options[:move_from]} have been moved to #{@options[:to_state]}"
end

#move_staged_ticketsObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/buildmeister/base.rb', line 213

def move_staged_tickets
  unless projects.one?
    puts "Please specify one project!"
    return
  end

  # Grab the actual lighthouse project
  project = projects.first

  tickets = project.find_tickets(*list_staged_tickets)
  tickets.each do |ticket|
    unless ticket.state == 'verified'
      ticket.state = @options[:to_state]
      ticket.save
    end
  end
end

#notifyObject



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/buildmeister/base.rb', line 103

def notify
  puts "Starting Buildmeister Notify..."

  retry_count = RETRY_COUNT

  loop do
    begin
      body = ''

      body << projects.map do |project|
        "#{project.display}\n"
      end.join("\n")

      puts "Updated notification at #{Time.now.strftime("%m/%d %I:%M %p")}"

      if changed? || @force
        Buildmeister::Notifier.post(title, body)
        @force = false
      end

      sleep notification_interval * 60

      refresh!

      # Reset the retry count, since we successfully completed this iteration
      retry_count = RETRY_COUNT

    rescue StandardError => e        
      if retry_count < 1
        puts "Retried #{RETRY_COUNT} times... I give up!"
        raise e
      else
        # Exponential falloff...
        sleep_time = (50.0 * (1 / (retry_count / 2.0))).to_i

        puts "Caught error: #{e.class.name}: #{e.message}"
        puts "#{retry_count} more tries... sleeping #{sleep_time} seconds..."

        sleep sleep_time

        retry_count -= 1

        retry
      end
    rescue Interrupt => i
      puts "\rPress ^C again to quit..."
      sleep 3
      @force = true
      refresh!
      retry
    end
  end
end

#refresh!Object



265
266
267
# File 'lib/buildmeister/base.rb', line 265

def refresh!
  projects.each(&:refresh!)
end

#summaryObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/buildmeister/base.rb', line 231

def summary
  body = ''
  
  projects.each do |project|
    bin = project.bins.detect {|b| b.name =~ /^#{Regexp.escape(@options[:bin_name])}$/i}
    
    unless bin
      puts "No ticket bin found matching \"#{@options[:bin_name]}\" in #{project.name}"
      next 
    end
    
    tickets = bin.tickets.sort_by(&:id)
    
    body << "=== #{project.name} ===\n\n" if tickets.length > 0
    
    tickets.each do |ticket|
      body << title = "##{ticket.number} - #{ticket.title}\n"
      body << "-" * title.length + "\n\n"
      
      if ticket.original_body.to_s.strip.empty?
        body << "(no description)" + "\n" * 4
      else
        body << "#{ticket.original_body}"  + "\n" * 4
      end
      
    end
  end
  
  puts body
  
  `echo "#{body.gsub('"', '\"')}" | pbcopy`
  puts "Summary copied to the clipboard"
end

#titleObject



99
100
101
# File 'lib/buildmeister/base.rb', line 99

def title
  "Buildmeister: #{Time.now.strftime("%m/%d %I:%M %p")}"
end