Class: Copyist::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/copyist/job.rb

Defined Under Namespace

Classes: IssueTicket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Job

Returns a new instance of Job.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#global_labelsObject

Returns the value of attribute global_labels.



7
8
9
# File 'lib/copyist/job.rb', line 7

def global_labels
  @global_labels
end

#label_identifireObject

Returns the value of attribute label_identifire.



7
8
9
# File 'lib/copyist/job.rb', line 7

def label_identifire
  @label_identifire
end

#skip_identifiresObject

Returns the value of attribute skip_identifires.



7
8
9
# File 'lib/copyist/job.rb', line 7

def skip_identifires
  @skip_identifires
end

#template_file_pathObject

Returns the value of attribute template_file_path.



7
8
9
# File 'lib/copyist/job.rb', line 7

def template_file_path
  @template_file_path
end

#title_identifireObject

Returns the value of attribute title_identifire.



7
8
9
# File 'lib/copyist/job.rb', line 7

def title_identifire
  @title_identifire
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
# 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_markdownObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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