Class: OrgUpdateMatrix::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/orgupdatematrix.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo, file_path, auth_token) ⇒ Updater

Returns a new instance of Updater.



9
10
11
12
13
# File 'lib/orgupdatematrix.rb', line 9

def initialize(repo, file_path, auth_token)
  @repo = repo
  @file_path = file_path
  @client = Octokit::Client.new(access_token: auth_token)
end

Instance Method Details

#get_file_contentsObject



15
16
17
# File 'lib/orgupdatematrix.rb', line 15

def get_file_contents
  @client.contents(@repo, path: @file_path)
end

#log(message, indent = 6) ⇒ Object



19
20
21
# File 'lib/orgupdatematrix.rb', line 19

def log(message, indent = 6)
  puts("#{' ' * indent}#{message}")
end

#sort_data(hash) ⇒ Object



23
24
25
# File 'lib/orgupdatematrix.rb', line 23

def sort_data(hash)
  hash.sort.to_h.each { |app, stage| hash[app] = stage.sort.to_h }
end

#update(app, stage, branch, commit, app_url, repo_url, extras = nil) ⇒ Object



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
# File 'lib/orgupdatematrix.rb', line 34

def update(app, stage, branch, commit, app_url, repo_url, extras = nil)
  app = app.to_s
  stage = stage.to_s
  state = {
    'branch' => branch.to_s,
    'commit' => commit,
    'updated' => Time.now.utc.to_s,
    'app_url' => app_url,
    'github_url' => repo_url.gsub('git@', 'https://').gsub(/(\w):(\w)/, '\1/\2'),
  }
  state['extras'] = extras if extras

  log("app: #{app}", 9)
  log("stage: #{stage}", 9)
  state.each { |key, value| log("#{key}: #{value}", 9) }
  message = "update deploy matrix: #{app}:#{stage}"

  Octopoller.poll(wait: :exponentially, retries: 6) do
    begin
      file = get_file_contents
      data = update_yaml(file, app, stage, state)
      result = @client.update_contents(@repo, @file_path, message, file[:sha], data.to_yaml)
      log('Update complete!') if result[:commit]
    rescue StandardError => e
      log("Error: #{e.message}")
      log('Retrying with latest commit...')
      :re_poll
    end
  end
end

#update_yaml(file, app, stage, state) ⇒ Object



27
28
29
30
31
32
# File 'lib/orgupdatematrix.rb', line 27

def update_yaml(file, app, stage, state)
  data = YAML::load(Base64.decode64(file.content))
  data[app] ||= {}
  data[app][stage] = state
  sort_data(data)
end