Class: Pronto::Golang

Inherits:
Runner
  • Object
show all
Includes:
Pronto::GolangSupport::FileFinder
Defined in:
lib/pronto/golang.rb

Constant Summary collapse

CONFIG_FILE =
'.golangtools.yml'
GOLANG_FILE_EXTENSIONS =
['.go'].freeze

Instance Method Summary collapse

Methods included from Pronto::GolangSupport::FileFinder

#find_file_upwards, #find_files_upwards, root_level=, root_level?

Instance Method Details

#available_toolsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pronto/golang.rb', line 137

def available_tools
  if @tools == nil
    config = dotconfig

    @tools = GolangTools.constants.sort.map do |constant|
      next if constant.to_s == 'Base'

      tool_class = Object.const_get("Pronto::GolangTools::#{constant}")

      tool = tool_class.new(config.fetch('tools').fetch(tool_class.base_command))

      if tool.available?
        tool
      else
        nil
      end
    end.compact
  end

  return @tools
end

#dotconfigObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/pronto/golang.rb', line 159

def dotconfig
  file = find_file_upwards(CONFIG_FILE, Dir.pwd, use_home: true)

  base = {
    'tools' => {}
  }

  if file
    return base.merge(YAML.load_file(file))
  end

  return base
end

#go_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/pronto/golang.rb', line 173

def go_file?(path)
  GOLANG_FILE_EXTENSIONS.include?(File.extname(path))
end

#inspect(patch, findings) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/pronto/golang.rb', line 103

def inspect(patch, findings)
  messages = []

  findings.each do |finding|
    messages << process_line(patch, finding[:tool], finding[:line])
  end

  return messages
end

#patch_file_path(patch) ⇒ Object



37
38
39
# File 'lib/pronto/golang.rb', line 37

def patch_file_path(patch)
  return Shellwords.escape(patch.new_file_full_path.to_s)
end

#process_line(patch, tool, output_line) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pronto/golang.rb', line 113

def process_line(patch, tool, output_line)
  return nil if output_line =~ /^#/

  begin
    file_path, line_number, level, message = tool.parse_line(output_line)

    patch.added_lines.each do |line|
      if line_number.to_s == line.new_lineno.to_s &&
         patch.new_file_full_path.to_s == File.expand_path(file_path)

        prefix_message = "#{tool.base_command}: #{message}"

        return Message.new(
          file_path, line, level, prefix_message, line.commit_sha, self.class
        )
      end
    end
  rescue ::Pronto::GolangSupport::UnprocessableLine
    # Do nothing if the line is not processable
  end

  return nil
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pronto/golang.rb', line 17

def run
  return [] unless @patches

  valid_patches = @patches.select { |patch| valid_patch?(patch) }
  patch_file_paths = valid_patches.map { |patch| patch_file_path(patch) }

  collected_findings = []
  collected_findings += run_tools_for_projects
  collected_findings += run_tools_for_files(patch_file_paths)

  valid_patches
    .map { |patch| inspect(patch, collected_findings) }
    .flatten
    .compact
end

#run_command(tool, command) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pronto/golang.rb', line 77

def run_command(tool, command)
  collected_findings = []

  Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
    [stdout, stderr].each do |result_text|
      while output_line = result_text.gets
        next if output_line.strip == 'exit status 1'

        collected_findings << {
          line: output_line,
          tool: tool,
        }
      end
    end

    while output_line = stderr.gets
      collected_findings << {
        line: output_line,
        tool: tool,
      }
    end
  end

  return collected_findings
end

#run_tools_for_files(filepaths) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pronto/golang.rb', line 55

def run_tools_for_files(filepaths)
  collected_findings = []

  available_tools.each do |tool|
    if tool.execution_mode != 'file'
      next
    end

    filepaths.each do |filepath|
      # Skip the patch if the filepath is blacklisted in the 'blacklisted_files' config
      # Note: this defaults to '.*' and therefore matches everything by default
      if tool.blacklisted_files_regexp.match?(filepath)
        next
      end

      collected_findings += run_command(tool, tool.command(filepath))
    end
  end

  return collected_findings
end

#run_tools_for_projectsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pronto/golang.rb', line 41

def run_tools_for_projects
  collected_findings = []

  available_tools.each do |tool|
    if tool.execution_mode != 'project'
      next
    end

    collected_findings += run_command(tool, tool.command(''))
  end

  return collected_findings
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pronto/golang.rb', line 33

def valid_patch?(patch)
  patch.additions > 0 && go_file?(patch.new_file_full_path)
end