Class: NdrDevSupport::RakeCI::BrakemanHelper

Inherits:
Object
  • Object
show all
Includes:
CommitMetadataPersistable
Defined in:
lib/ndr_dev_support/rake_ci/brakeman_helper.rb

Overview

Brakeman helper

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#new_fingerprintsObject (readonly)

Returns the value of attribute new_fingerprints.



11
12
13
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 11

def new_fingerprints
  @new_fingerprints
end

#old_fingerprintsObject (readonly)

Returns the value of attribute old_fingerprints.



11
12
13
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 11

def old_fingerprints
  @old_fingerprints
end

#trackerObject (readonly)

Returns the value of attribute tracker.



11
12
13
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 11

def tracker
  @tracker
end

Instance Method Details

#attachmentsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 89

def attachments
  attachments = []

  if @strict && current_fingerprints.any?
    # all warnings found
    attachment = {
      color: 'danger',
      title: "#{current_fingerprints.size} Brakeman warning(s) :rotating_light:",
      text: '_Brakeman_ warning fingerprint(s):' \
      "```#{current_fingerprints.to_a.join("\n")}```",
      footer: 'bundle exec rake ci:brakeman:fingerprint_details FINGERPRINTS=...',
      mrkdwn_in: ['text']
    }
    attachments << attachment
    puts attachment.inspect
  elsif new_fingerprints.any?
    # new warnings found
    attachment = {
      color: 'danger',
      title: "#{new_fingerprints.size} new Brakeman warning(s) :rotating_light:",
      text: '_Brakeman_ warning fingerprint(s):' \
      "```#{new_fingerprints.to_a.join("\n")}```",
      footer: 'bundle exec rake ci:brakeman:fingerprint_details FINGERPRINTS=...',
      mrkdwn_in: ['text']
    }
    attachments << attachment
    puts attachment.inspect
  end

  unless old_fingerprints.empty?
    # old warnings missing
    attachment = {
      color: 'good',
      title: "#{old_fingerprints.size} Brakeman warning(s) resolved :+1:",
      footer: 'bundle exec rake ci:brakeman'
    }
    attachments << attachment
    puts attachment.inspect
  end

  attachments
end

#current_fingerprintsObject



57
58
59
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 57

def current_fingerprints
  @current_fingerprints ||= filtered_warnings.map(&:fingerprint).to_set
end

#filtered_warning_counts_by_confidenceObject



47
48
49
50
51
52
53
54
55
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 47

def filtered_warning_counts_by_confidence
  return @filtered_warning_counts_by_confidence if @filtered_warning_counts_by_confidence

  @filtered_warning_counts_by_confidence = {}
  filtered_warnings.group_by(&:confidence).each do |confidence, grouped_warnings|
    @filtered_warning_counts_by_confidence[confidence] = grouped_warnings.count
  end
  @filtered_warning_counts_by_confidence
end

#filtered_warningsObject

Only the warnings we haven’t flagged as false positives (i.e. the outstanding ones)



33
34
35
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 33

def filtered_warnings
  @tracker.filtered_warnings
end

#metricsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 65

def metrics
  metrics = []

  ::Brakeman::Warning::TEXT_CONFIDENCE.each do |confidence, text|
    overall_metric = {
      name: 'brakeman_warnings',
      type: :gauge,
      label_set: { confidence: text },
      value: warning_counts_by_confidence[confidence] || 0
    }
    filtered_metric = {
      name: 'brakeman_filtered_warnings',
      type: :gauge,
      label_set: { confidence: text },
      value: filtered_warning_counts_by_confidence[confidence] || 0
    }
    metrics << overall_metric << filtered_metric
    puts overall_metric.inspect
    puts filtered_metric.inspect
  end

  metrics
end

#run(strict:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 13

def run(strict:)
  @strict = strict

  @tracker = ::Brakeman.run(app_path: '.')

  last_commit_fingerprints = load_last_commit_data
  if last_commit_fingerprints
    @new_fingerprints = current_fingerprints - last_commit_fingerprints
    @old_fingerprints = last_commit_fingerprints - current_fingerprints
  else
    @new_fingerprints = @old_fingerprints = Set.new
  end
end

#save_current_fingerprintsObject



61
62
63
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 61

def save_current_fingerprints
  save_current_commit_data(current_fingerprints)
end

#warning_counts_by_confidenceObject



37
38
39
40
41
42
43
44
45
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 37

def warning_counts_by_confidence
  return @warning_counts_by_confidence if @warning_counts_by_confidence

  @warning_counts_by_confidence = {}
  warnings.group_by(&:confidence).each do |confidence, grouped_warnings|
    @warning_counts_by_confidence[confidence] = grouped_warnings.count
  end
  @warning_counts_by_confidence
end

#warningsObject

All warnings (including those we’ve flagged as false positives)



28
29
30
# File 'lib/ndr_dev_support/rake_ci/brakeman_helper.rb', line 28

def warnings
  @tracker.warnings
end