Class: Pronto::Flow

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/flow.rb

Constant Summary collapse

CONFIG_FILE =
'.pronto_flow.yml'.freeze
CONFIG_KEYS =
%w(flow_executable).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flow_executableObject



11
12
13
# File 'lib/pronto/flow.rb', line 11

def flow_executable
  @flow_executable || 'flow'.freeze
end

Instance Method Details

#description_for_error(data, first_line_error_in_patch) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pronto/flow.rb', line 64

def description_for_error(data, first_line_error_in_patch)
  description = data.map do |item|
    item[:descr]
  end.join(" ")

  see_file_paths = data.map do |item|
    next if item[:path].nil? || item[:path].empty?

    file_path = item[:path].sub(repo_path.to_s + File::SEPARATOR, "")

    next if file_path == first_line_error_in_patch.patch.delta.new_file[:path]

    "\nSee: #{file_path}:#{item[:line]}"
  end

  description = description + see_file_paths.join("")
end

#filesObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/pronto/flow.rb', line 15

def files
  return [] if @patches.nil?

  @files ||= begin
   @patches
     .select { |patch| patch.additions > 0 }
     .map(&:new_file_full_path)
     .compact
 end
end

#messages(json_output) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pronto/flow.rb', line 82

def messages(json_output)
  json_output["errors"].map do |error|
    first_patch_with_error = nil
    files_associated_with_error = []

    data = error["message"].map do |context|
      data = { descr: context["descr"], path: context["path"], line: context["line"] }
    end

    first_line_error_in_patch = data.map do |item|
      patch_line_for_offence(item[:path], item[:line])
    end.compact.first

    next if first_line_error_in_patch.nil?

    description = description_for_error(data, first_line_error_in_patch)

    path = first_line_error_in_patch.patch.delta.new_file[:path]

    level = error["level"].to_sym

    Message.new(path, first_line_error_in_patch, level, description, nil, self.class)
  end
end

#patch_line_for_offence(path, lineno) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pronto/flow.rb', line 26

def patch_line_for_offence(path, lineno)
  patch_node = @patches.find do |patch|
    patch.new_file_full_path.to_s == path
  end

  return if patch_node.nil?

  patch_node.added_lines.find do |patch_line|
    patch_line.new_lineno == lineno
  end
end

#read_configObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/pronto/flow.rb', line 38

def read_config
  config_file = File.join(repo_path, CONFIG_FILE)
  return unless File.exist?(config_file)
  config = YAML.load_file(config_file)

  CONFIG_KEYS.each do |config_key|
    next unless config[config_key]
    send("#{config_key}=", config[config_key])
  end
end

#runObject



49
50
51
52
53
54
55
56
# File 'lib/pronto/flow.rb', line 49

def run
  if files.any?
    read_config
    messages(run_flow)
  else
    []
  end
end

#run_flowObject



58
59
60
61
62
# File 'lib/pronto/flow.rb', line 58

def run_flow
  Dir.chdir(repo_path) do
    return JSON.parse(`#{flow_executable} --json`)
  end
end