class Copyist::Job

Constants

IssueTicket

Attributes

global_labels[RW]
label_identifire[RW]
skip_identifires[RW]
template_file_path[RW]
title_identifire[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/copyist/job.rb, line 9
def initialize(argv)
  @source_md_file_path = argv

  env_path = ENV["ENVFILE_PATH"]
  Dotenv.load(env_path) if env_path && !env_path.empty?

  if ENV["GITHUB_USER_NAME"].empty? || ENV["GITHUB_REPO_NAME"].empty?
    raise "set GITHUB_USER_NAME and GITHUB_REPO_NAME to .env file"
  end
  raise "set TITLE_IDENTIFIRE to .env file" if ENV["TITLE_IDENTIFIRE"].empty?

  @title_identifire = "#{ENV["TITLE_IDENTIFIRE"]} "
  @skip_identifires = ENV["SKIP_IDENTIFIRES"]&.size&.nonzero? ? Regexp.new("^#{ENV["SKIP_IDENTIFIRES"].split(",").join(" |")}") : nil
  @label_identifire = ENV["LABEL_IDENTIFIRE"]&.size&.nonzero? ? "#{ENV["LABEL_IDENTIFIRE"]} " : nil

  @global_labels      = ENV["GLOBAL_LABELS"]&.size&.nonzero? ? ENV["GLOBAL_LABELS"] : nil
  @template_file_path = ENV["TEMPLATE_FILE_PATH"]&.size&.nonzero? ? ENV["TEMPLATE_FILE_PATH"] : nil
end

Public Instance Methods

run() click to toggle source
# File lib/copyist/job.rb, line 28
def run
  puts "make tickets to Github from markdown"

  tickets_from_markdown.each do |ticket|
    response = request_to_github(ticket)
    puts response.message
  end
  puts "process finished"
rescue StandardError => e
  puts ["fatal error.", "-------", e.backtrace, "------"].flatten.join("\n")
end
tickets_from_markdown() click to toggle source
# File lib/copyist/job.rb, line 40
def tickets_from_markdown
  tickets = []
  get_markdown.each do |line|
    next if skip_identifires && line.match?(skip_identifires)

    if line.match?(/^#{title_identifire}/)
      tickets << IssueTicket.new(line.gsub(/#{title_identifire}|\*|\*\*|`/, ""), [], [])

    elsif label_identifire && line.match?(/^#{label_identifire}/)
      (tickets&.last&.labels || []) << line.gsub(label_identifire, "").chomp.split(",").map(&:strip)

    else
      (tickets&.last&.description || []) << line
    end
  end

  tickets.each { |i| i.description = make_description(i.description) }
  tickets
end

Private Instance Methods

get_markdown() click to toggle source
# File lib/copyist/job.rb, line 96
def get_markdown
  File.new(@source_md_file_path).readlines
end
get_uri() click to toggle source
# File lib/copyist/job.rb, line 92
def get_uri
  URI.parse("https://api.github.com/repos/#{ENV["GITHUB_USER_NAME"]}/#{ENV["GITHUB_REPO_NAME"]}/issues")
end
make_description(description_text_array) click to toggle source
# File lib/copyist/job.rb, line 62
def make_description(description_text_array)
  description = description_text_array.join

  if template_file_path
    template = File.open(template_file_path, "r", &:read)
    description = template.gsub("{ticket_description_block}", description)
  end

  description
end
make_request_body(ticket) click to toggle source
# File lib/copyist/job.rb, line 84
def make_request_body(ticket)
  {
    title: ticket.title,
    body: ticket.description,
    labels: (global_labels&.split(",")&.map(&:strip) + ticket.labels).flatten.uniq
  }
end
request_to_github(ticket) click to toggle source
# File lib/copyist/job.rb, line 73
def request_to_github(ticket)
  uri = get_uri
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === "https"

  headers = { Authorization: "token #{ENV["GITHUB_PERSONAL_TOKEN"]}" }
  body = make_request_body(ticket)

  http.post(uri.path, body.to_json, headers)
end