Class: Homebrew::DevCmd::AdvisoryMatch::DirEmitter Private

Inherits:
Emitter show all
Defined in:
dev-cmd/advisory-match.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Instance Method Summary collapse

Constructor Details

#initialize(dir, verbose:) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • dir (String)
  • verbose (Boolean)


142
143
144
145
146
147
148
149
150
# File 'dev-cmd/advisory-match.rb', line 142

def initialize(dir, verbose:)
  super()
  FileUtils.mkdir_p(dir)
  @dir = dir
  @verbose = verbose
  @written = T.let(0, Integer)
  @unchanged = T.let(0, Integer)
  @skipped_generated = T.let(0, Integer)
end

Instance Method Details

#<<(record) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'dev-cmd/advisory-match.rb', line 153

def <<(record)
  path = File.join(@dir, "#{record.fetch(:id)}.json")
  # A record already emitted by `generate-vulns-advisories` (a formula
  # `resolves` patch annotation) is more authoritative than a matched
  # candidate; overwriting it would drop `fix: "patch"` for a derived
  # `fix: null`/`"bump"`.
  if File.file?(path) && existing_source(path) == "generated"
    @skipped_generated += 1
    return
  end
  merged = Homebrew::Vulns::OsvExport.merge_existing(path, record)
  if merged.nil?
    @unchanged += 1
    return
  end
  File.write(path, "#{JSON.pretty_generate(merged)}\n")
  puts "  wrote #{path}" if @verbose
  @written += 1
end

#existing_source(path) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



174
175
176
177
178
# File 'dev-cmd/advisory-match.rb', line 174

def existing_source(path)
  JSON.parse(File.read(path)).dig("database_specific", "source")
rescue JSON::ParserError
  nil
end

#finishvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



181
182
183
184
# File 'dev-cmd/advisory-match.rb', line 181

def finish
  Utils::Output.ohai "#{@written} records written to #{@dir} " \
                     "(#{@unchanged} unchanged, #{@skipped_generated} generated left as-is)"
end