Sha256: d3e7a044a123d85d8a077c516d51f386f58ff31619ee0d18cf1cd0ce59da34cd

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require "orgupdatematrix/version"
require "octokit"
require "octopoller"
require "yaml"
require "base64"

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

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

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

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

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
orgupdatematrix-0.2.0 lib/orgupdatematrix.rb