class TcieMigrateToApps::Cli

Constants

HEADERS
MSGS
PER_PAGE
URIS
USAGE

Attributes

api_endpoint[R]
github_api_endpoint[R]
installation[R]

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/tcie_migrate_to_apps/cli.rb, line 52
def initialize(*)
  super
  to_h.keys.each do |key|
    if key == :api_endpoint
      @api_endpoint = to_h[key] ? to_h[key] :  'https://api.travis-ci.com'
    elsif key == :github_api_endpoint
      @github_api_endpoint = to_h[key] ? to_h[key] :  'https://api.github.com'
    else
      missing_arg(key) unless send(key)
    end
  end
end

Public Instance Methods

run() click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 65
def run
  msg :start, owner_name, color: :yellow
  validate
  migrate_repos
  msg :done, color: :green
end

Private Instance Methods

error(key, *args) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 136
def error(key, *args)
  abort colored(:red, MSGS[key] % args)
end
fetch_installation() click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 102
def fetch_installation
  msg :fetch_installation, owner_name
  uri = uri(:travis, :installation, api_endpoint, owner_name)
  data = request(:get, uri, headers(:travis))
  data['installation']
end
fetch_repos(repos = [], page = 1) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 109
def fetch_repos(repos = [], page = 1)
  offset = (page - 1) * PER_PAGE
  uri    = uri(:travis, :repositories, api_endpoint, owner_name, PER_PAGE, offset)
  data   = request(:get, uri, headers(:travis))
  repos += data['repositories'].map { |repo| only(repo, 'name', 'github_id') }
  repos  = fetch_repos(repos, page + 1) unless data['@pagination']['is_last']
  repos
end
headers(target) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 122
def headers(target)
  args = { token: send(:"#{target}_access_token") }
  HEADERS[target].map { |key, value| [key, value % args] }.to_h
end
migrate_repo(repo) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 95
def migrate_repo(repo)
  msg :migrating_repo, repo['name'], nl: false
  uri = uri(:github, :installation_repos, github_api_endpoint, installation['github_id'], repo['github_id'])
  request(:put, uri, headers(:github))
  msg :migrated_repo, repo['name']
end
migrate_repos() click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 90
def migrate_repos
  msg :migrate_repos, repos.count
  repos.each { |repo| migrate_repo(repo) }
end
missing_arg(key) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 148
def missing_arg(key)
  puts colored(:red, "No #{key} given")
  puts USAGE
  abort
end
msg(key, *args) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 140
def msg(key, *args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  msg = MSGS[key] % args
  msg = colored(opts[:color], msg) if opts[:color]
  method = opts[:nl].is_a?(FalseClass) ? :print : :puts
  send(method, msg)
end
only(hash, *keys) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 154
def only(hash, *keys)
  hash.select { |key, _| keys.include?(key) }
end
repos() click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 78
def repos
  @repos ||= begin
    msg :fetch_repos, owner_name
    fetch_repos
  end
end
request(method, uri, headers) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 127
def request(method, uri, headers)
  req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
  headers.each { |key, value| req[key] = value }
  http = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true)
  res = http.request(req)
  error :request_failed, method, uri, res.code, res.body unless res.is_a?(Net::HTTPSuccess)
  JSON.parse(res.body) if method == :get
end
uri(target, resource, *args) click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 118
def uri(target, resource, *args)
  URI(URIS[target][resource] % args)
end
validate() click to toggle source
# File lib/tcie_migrate_to_apps/cli.rb, line 85
def validate
  error :missing_installation, owner_name unless installation
  error :missing_repos                    unless repos.any?
end