Class: Cocov::PluginKit::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/cocov/plugin_kit/run.rb

Overview

Public: Run implements helpers and prepares the environment for a Plugin runtime. All Cocov Ruby plugins must either inherit this class or directly use it by using the block alternative of Cocov::PluginKit#run.

The class provides the following accessors:

  • workdir: The path to the root of the repository being checked. The

    plugin is always executed with its pwd set to this path.
    
  • repo_name: The name of the repository being cheked.

  • commit_sha: The SHA of the commit being checked.

Constant Summary collapse

ALLOWED_KINDS =

:nodoc:

%i[style performance security bug complexity duplication convention quality].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_file) ⇒ Run

Returns a new instance of Run.



18
19
20
21
22
23
24
# File 'lib/cocov/plugin_kit/run.rb', line 18

def initialize(output_file)
  @workdir = Pathname.new(ENV.fetch("COCOV_WORKDIR"))
  @repo_name = ENV.fetch("COCOV_REPO_NAME")
  @commit_sha = ENV.fetch("COCOV_COMMIT_SHA")

  @output_file = output_file
end

Instance Attribute Details

#commit_shaObject (readonly)

Returns the value of attribute commit_sha.



16
17
18
# File 'lib/cocov/plugin_kit/run.rb', line 16

def commit_sha
  @commit_sha
end

#repo_nameObject (readonly)

Returns the value of attribute repo_name.



16
17
18
# File 'lib/cocov/plugin_kit/run.rb', line 16

def repo_name
  @repo_name
end

#workdirObject (readonly)

Returns the value of attribute workdir.



16
17
18
# File 'lib/cocov/plugin_kit/run.rb', line 16

def workdir
  @workdir
end

Instance Method Details

#emit_issue(kind:, file:, line_start:, line_end:, message:, uid: nil) ⇒ Object

Public: Emits a new issue with the provided arguments.

kind: - A symbol identifying kind of the issue being emitted. file: - The path, relative to the repository root, of the file

where the issue was found.

line_start: - The first line where the issue was found. Must be greater

than zero.

line_end: - The last line where the issue was found. Must be equal or

greater than the value of line_start.

message: - A message describing the issue. Must not be empty. uid: - An uniquely identifier representing this issue among any

other possible issue in any other file in this repository.
This identifier must be the result of a pure function;
i.e. the same issue in the same file, spanning the same
lines must have the same uid no matter how many times it
is reported. If omitted, an UID is automatically
calculated. If provided, must be a non-empty string.

Returns nothing. Raises ArgumentError in case invalid data is provided (see documentation above.)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cocov/plugin_kit/run.rb', line 94

def emit_issue(kind:, file:, line_start:, line_end:, message:, uid: nil)
  validate_issue_args! kind: kind, file: file, line_start: line_start,
                       line_end: line_end, message: message, uid: uid
  data = {
    kind: kind,
    file: file,
    line_start: line_start,
    line_end: line_end,
    message: message
  }

  uid = sha1(data.map { |k, v| [k, v].join }.join) if uid.nil?
  data[:uid] = uid

  @output_file.write("#{data.to_json}\x00")
  true
end

#exec(cmd, **options) ⇒ Object

Public: Alias to Exec.exec. For more information, see the documentation for that method.



38
39
40
# File 'lib/cocov/plugin_kit/run.rb', line 38

def exec(cmd, **options)
  Exec.exec(cmd, **options)
end

#exec2(cmd, **options) ⇒ Object

Public: Alias to Exec.exec2. For more information, see the documentation for that method.



32
33
34
# File 'lib/cocov/plugin_kit/run.rb', line 32

def exec2(cmd, **options)
  Exec.exec2(cmd, **options)
end

#runObject

Public: When inheriting this class, the plugin entrypoint must be implemented on this method’s override.



28
# File 'lib/cocov/plugin_kit/run.rb', line 28

def run; end

#sha1(data) ⇒ Object

Public: Returns the SHA1 digest of the provided data



43
44
45
# File 'lib/cocov/plugin_kit/run.rb', line 43

def sha1(data)
  Digest::SHA1.hexdigest(data)
end

#validate_issue_args!(kind:, file:, line_start:, line_end:, message:, uid:) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cocov/plugin_kit/run.rb', line 53

def validate_issue_args!(kind:, file:, line_start:, line_end:, message:, uid:)
  unless ALLOWED_KINDS.include? kind
    raise ArgumentError, "Invalid kind `#{kind}'. Valid options are #{ALLOWED_KINDS.join(", ")}"
  end

  raise ArgumentError, "file must be a non-empty string" if !file.is_a?(String) || file.strip == ""

  if !line_start.is_a?(Integer) || line_start < 1
    raise ArgumentError, "line_start must be an integer greater than zero"
  end

  raise ArgumentError, "line_end must be an integer greater than zero" if !line_end.is_a?(Integer) || line_end < 1

  raise ArgumentError, "message must be a non-empty string" if !message.is_a?(String) || message.strip == ""

  return unless !uid.nil? && (!uid.is_a?(String) || uid.strip == "")

  raise ArgumentError, "uid must be a non-empty string when provided"
end