class PrMigrator::Create

Attributes

client[RW]
exported_pr_dir[RW]
migration_branch[RW]
repo_name[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/pr_migrator/create.rb, line 9
def initialize(options = {})
  initialize_api_endpoint
  @client           = Octokit::Client.new(access_token: PrMigrator.configuration.destination_access_token)
  @repo_name        = options[:repo_name]
  @exported_pr_dir  = options[:exported_pr_dir]
  @migration_branch = options[:migration_branch]
end

Public Instance Methods

create_pull_request(pr_number) click to toggle source
# File lib/pr_migrator/create.rb, line 50
def create_pull_request(pr_number)
  @client.create_pull_request(@repo_name,
                              'master',
                              "pr_#{pr_number}",
                              "Title",
                              "Body")
end
create_ref_to_temp_file(pr_number, sha) click to toggle source
# File lib/pr_migrator/create.rb, line 58
def create_ref_to_temp_file(pr_number, sha)
  @client.create_ref(@repo_name, "heads/pr_#{pr_number}", sha)
end
create_temp_file(file_name, pr_number) click to toggle source
# File lib/pr_migrator/create.rb, line 42
def create_temp_file(file_name, pr_number)
  @client.create_contents(@repo_name,
                          "pull_request_#{Time.now.to_i}_#{SecureRandom.hex}.md",
                          "Migrating pull request #{pr_number}",
                          "This Includes a temporary content for the PR migration of #{file_name} #{Time.now.to_i}_#{SecureRandom.hex}" ,
                          branch: @migration_branch)
end
delete_ref_to_temp_file(pr_number) click to toggle source
# File lib/pr_migrator/create.rb, line 72
def delete_ref_to_temp_file(pr_number)
  @client.delete_ref(@repo_name, "heads/pr_#{pr_number}")
end
files() click to toggle source

Files generated from migration

# File lib/pr_migrator/create.rb, line 38
def files
  @exported_pr_dir ? Dir.glob(@exported_pr_dir) : []
end
start() click to toggle source

Start the process of exporting The exported files will be json files

# File lib/pr_migrator/create.rb, line 19
def start
  #Parallel.each_with_index(files.take(3), in_processes: 10) do |file_name, index|
  files.each_with_index do |file_name, index|
    begin
      content = JSON.parse(File.read(file_name), symbolize_names: true)
      pr_number_to_be_migrated       = content[:number]
      response_temp_file_creation    = create_temp_file(file_name, pr_number_to_be_migrated)
      create_ref_to_temp_file(pr_number_to_be_migrated, response_temp_file_creation[:commit][:sha])
      response_pull_request_creation = create_pull_request(pr_number_to_be_migrated)
      update_pull_request(response_pull_request_creation[:number], content)
      delete_ref_to_temp_file(pr_number_to_be_migrated)
      puts '*'
    rescue Exception => e
      puts "Error: #{e.message}"
    end
  end
end
update_pull_request(new_pr_number, content) click to toggle source
# File lib/pr_migrator/create.rb, line 62
def update_pull_request(new_pr_number, content)
  str = content.to_s
  @client.update_pull_request(@repo_name,
                              new_pr_number,
                              { title: content[:title],
                                body: "#{content[:body]}\r\n\n <h5>The JSON format of the PR can be copied from below</h5> \r\n ```ruby\r\n#{str}\r\n```",
                                state: 'closed' }
                              )
end

Private Instance Methods

initialize_api_endpoint() click to toggle source

Initialize the Github Endpoint if Set

# File lib/pr_migrator/create.rb, line 81
def initialize_api_endpoint
  if PrMigrator.configuration.github_enterprise_endpoint
    Octokit.configure do |c|
      c.api_endpoint = PrMigrator.configuration.github_enterprise_endpoint
    end
  end
end