class TravisMigrateToApps::Cli
Constants
- HEADERS
- MSGS
- PER_PAGE
- URIS
- USAGE
Attributes
installation[R]
Public Class Methods
new(*)
click to toggle source
Calls superclass method
# File lib/travis_migrate_to_apps/cli.rb, line 50 def initialize(*) super to_h.keys.each do |key| missing_arg(key) unless send(key) end end
Public Instance Methods
run()
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 57 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/travis_migrate_to_apps/cli.rb, line 128 def error(key, *args) abort colored(:red, MSGS[key] % args) end
fetch_installation()
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 94 def fetch_installation msg :fetch_installation, owner_name uri = uri(:travis, :installation, owner_name) data = request(:get, uri, headers(:travis)) data['installation'] end
fetch_repos(repos = [], page = 1)
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 101 def fetch_repos(repos = [], page = 1) offset = (page - 1) * PER_PAGE uri = uri(:travis, :repositories, 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/travis_migrate_to_apps/cli.rb, line 114 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/travis_migrate_to_apps/cli.rb, line 87 def migrate_repo(repo) msg :migrating_repo, repo['name'], nl: false uri = uri(:github, :installation_repos, 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/travis_migrate_to_apps/cli.rb, line 82 def migrate_repos msg :migrate_repos, repos.count repos.each { |repo| migrate_repo(repo) } end
missing_arg(key)
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 140 def missing_arg(key) puts colored(:red, "No #{key} given") puts USAGE abort end
msg(key, *args)
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 132 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/travis_migrate_to_apps/cli.rb, line 146 def only(hash, *keys) hash.select { |key, _| keys.include?(key) } end
repos()
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 70 def repos @repos ||= begin msg :fetch_repos, owner_name fetch_repos end end
request(method, uri, headers)
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 119 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/travis_migrate_to_apps/cli.rb, line 110 def uri(target, resource, *args) URI(URIS[target][resource] % args) end
validate()
click to toggle source
# File lib/travis_migrate_to_apps/cli.rb, line 77 def validate error :missing_installation, owner_name unless installation error :missing_repos unless repos.any? end