class IssueExporting::Importer

Public Class Methods

new(files, owner, repo, token) click to toggle source
# File lib/issue_exporter/import.rb, line 6
def initialize(files, owner, repo, token)
  @files = files
  @owner = owner
  @repo = repo
  @token = token
end

Public Instance Methods

import() click to toggle source
# File lib/issue_exporter/import.rb, line 13
def import
  @files.each do |file|
    file_contents = read_file file
    import_json(file_contents) unless file_contents.nil?
  end
end

Private Instance Methods

allowed_properties() click to toggle source
# File lib/issue_exporter/import.rb, line 56
def allowed_properties
  ["title", "body", "assignee", "milestone", "labels"]
end
create_issue(json_obj) click to toggle source
# File lib/issue_exporter/import.rb, line 46
def create_issue(json_obj)
  uri = IssueExporting.make_uri @owner, @repo, @token
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  request = Net::HTTP::Post.new uri.request_uri
  request.content_type = "application/json"
  request.body = json_obj.to_json
  handle_response http.request(request)
end
filter_json(json_hash) click to toggle source
# File lib/issue_exporter/import.rb, line 42
def filter_json(json_hash)
  json_hash.select { |k, _v| allowed_properties.include? k }
end
full_path(filename) click to toggle source
# File lib/issue_exporter/import.rb, line 71
def full_path(filename)
  return filename if File.dirname(filename) != "."
  "#{Dir.pwd}/#{filename}"
end
handle_missing_file(filename) click to toggle source
# File lib/issue_exporter/import.rb, line 83
def handle_missing_file(filename)
  puts "Cannot open file: #{filename}"
end
handle_response(response) click to toggle source
# File lib/issue_exporter/import.rb, line 76
def handle_response(response)
  error_handler = ErrorHandler.new
  if error_handler.response_has_error response
    error_handler.handle_error error_handler.error_message(response.body), true
  end
end
import_json(json_obj) click to toggle source
# File lib/issue_exporter/import.rb, line 22
def import_json(json_obj)
  if json_obj.class == Hash
    import_json_hash json_obj
  elsif json_obj.class == Array
    import_json_array json_obj
  else
    puts "Unknown object: #{json_obj.class}"
  end
end
import_json_array(json_array) click to toggle source
# File lib/issue_exporter/import.rb, line 32
def import_json_array(json_array)
  json_array.each do |json_hash|
    import_json_hash json_hash
  end
end
import_json_hash(json_hash) click to toggle source
# File lib/issue_exporter/import.rb, line 38
def import_json_hash(json_hash)
  create_issue(filter_json json_hash)
end
read_file(filename) click to toggle source
# File lib/issue_exporter/import.rb, line 60
def read_file(filename)
  path = full_path filename
  if File.exist? path
    raw_text = File.read path
    JSON.parse raw_text
  else
    handle_missing_file path
    nil
  end
end