Module: AbideDevUtils::Jira

Defined in:
lib/abide_dev_utils/jira.rb,
lib/abide_dev_utils/jira/client.rb,
lib/abide_dev_utils/jira/finder.rb,
lib/abide_dev_utils/jira/helper.rb,
lib/abide_dev_utils/jira/dry_run.rb,
lib/abide_dev_utils/jira/issue_builder.rb,
lib/abide_dev_utils/jira/client_builder.rb

Defined Under Namespace

Modules: DryRun Classes: Client, ClientBuilder, Dummy, Finder, Helper, IssueBuilder, ToCreateData

Constant Summary collapse

ERRORS =
AbideDevUtils::Errors::Jira
COV_PARENT_SUMMARY_PREFIX =
'::BENCHMARK:: '
COV_CHILD_SUMMARY_PREFIX =
'::CONTROL:: '
UPD_EPIC_SUMMARY_PREFIX =
'::BENCHMARK UPDATE::'
PROGRESS_BAR_FORMAT =
'%a %e %P% Created: %c of %C'

Class Method Summary collapse

Class Method Details

.client(memo: true, dry_run: false, **options) ⇒ Object



18
19
20
21
22
# File 'lib/abide_dev_utils/jira.rb', line 18

def self.client(memo: true, dry_run: false, **options)
  return AbideDevUtils::Jira::Client.new(dry_run: dry_run, **options) unless memo

  @client ||= AbideDevUtils::Jira::Client.new(dry_run: dry_run, **options)
end

.configObject



273
274
275
# File 'lib/abide_dev_utils/jira.rb', line 273

def self.config
  AbideDevUtils::Config.config_section(:jira)
end

.dr_prefix(dry_run) ⇒ Object



315
316
317
# File 'lib/abide_dev_utils/jira.rb', line 315

def self.dr_prefix(dry_run)
  dry_run ? 'DRY RUN: ' : ''
end

.explain_this(to_create) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/abide_dev_utils/jira.rb', line 158

def self.explain_this(to_create)
  should_create = to_create.select(&:should_create)
  should_not_create = to_create.reject(&:should_create)
  AbideDevUtils::Output.simple(AbideDevUtils::Output.simple_section_separator('EXPLAIN'))
  AbideDevUtils::Output.simple("Will create #{should_create.count} issues")
  AbideDevUtils::Output.simple("Will not create #{should_not_create.count} issues")
  AbideDevUtils::Output.simple(AbideDevUtils::Output.simple_section_separator('WILL CREATE'))
  should_create.each do |tc|
    AbideDevUtils::Output.simple("\"#{tc.summary}\"; labels: #{tc.labels}; metadata: #{tc.}")
  end
  AbideDevUtils::Output.simple(AbideDevUtils::Output.simple_section_separator('WILL NOT CREATE'))
  should_not_create.each do |tc|
    AbideDevUtils::Output.simple("\"#{tc.summary}\"; labels: #{tc.labels}; metadata: #{tc.}")
  end
  exit(0)
end

.filter_already_exists(to_create, project, epic) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/abide_dev_utils/jira.rb', line 124

def self.filter_already_exists(to_create, project, epic)
  AbideDevUtils::Output.simple('Checking if issues already exist...')
  project = client.find(:project, project)
  epic = client.find(:issue, epic)
  issues = client.find(:issues_by_jql, "project = \"#{project.key}\" AND 'Epic Link' = \"#{epic.key}\"")
  to_create.reject do |tc|
    if issues.any? { |i| i.summary == tc.summary }
      tc.[:already_exists] = true
      tc.should_create = false
      true
    else
      tc.[:already_exists] = false
      false
    end
  end
end

.filter_label_include(to_create, label_include) ⇒ Object

If we have a label_include, we need to filter out any controls that don’t have that label



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/abide_dev_utils/jira.rb', line 142

def self.filter_label_include(to_create, label_include)
  return to_create if label_include.nil?

  AbideDevUtils::Output.simple("Filtering out controls that don't match label include: #{label_include}")
  to_create.select do |tc|
    if tc.labels.any? { |l| l.match?(label_include) }
      tc.[:label_include] = true
      true
    else
      tc.[:label_include] = false
      tc.should_create = false
      false
    end
  end
end

.filter_to_create(to_create, label_include, project, epic) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/abide_dev_utils/jira.rb', line 112

def self.filter_to_create(to_create, label_include, project, epic)
  not_already_exists = filter_already_exists(to_create, project, epic)
  AbideDevUtils::Output.simple(
    "Filtered out #{to_create.count - not_already_exists.count} issues that already existed",
  )
  only_label_include = filter_label_include(not_already_exists, label_include)
  AbideDevUtils::Output.simple(
    "Filtered out #{not_already_exists.count - only_label_include.count} issues that didn't include the label #{label_include}",
  )
  only_label_include
end

.merge_options(options) ⇒ Object



269
270
271
# File 'lib/abide_dev_utils/jira.rb', line 269

def self.merge_options(options)
  config.merge(options)
end

.new_issues_from_coverage(client, project, report, dry_run: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/abide_dev_utils/jira.rb', line 24

def self.new_issues_from_coverage(client, project, report, dry_run: false)
  dr_prefix = dry_run ? 'DRY RUN: ' : ''
  client(dry_run: dry_run) # Initializes the client if needed
  i_attrs = client.helper.all_project_issues_attrs(project)
  rep_sums = summaries_from_coverage_report(report)
  rep_sums.each do |k, v|
    next if client.helper.summary_exist?(k, i_attrs)

    AbideDevUtils::Output.simple("#{dr_prefix}Creating parent issue #{k}...")
    parent = client.create(:issue, project: project, summary: k.to_s)
    AbideDevUtils::Output.simple("#{dr_prefix}Creating subtasks, this can take a while...")
    progress = AbideDevUtils::Output.progress(title: "#{dr_prefix}Creating Subtasks", total: nil)
    v.each do |s|
      next if client.helper.summary_exist?(s, i_attrs)

      progress.title = "#{dr_prefix}#{s}"
      client.create(:subtask, parent: parent, summary: s)
      progress.increment
    end
  end
end

.new_issues_from_xccdf(project, xccdf_path, epic: nil, dry_run: false, explain: false, label_include: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
102
103
104
105
106
107
108
109
110
# File 'lib/abide_dev_utils/jira.rb', line 48

def self.new_issues_from_xccdf(project, xccdf_path, epic: nil, dry_run: false, explain: false, label_include: nil)
  client(dry_run: dry_run) # Initializes the client if needed
  i_attrs = client.helper.all_project_issues_attrs(project)
  xccdf = AbideDevUtils::XCCDF::Benchmark.new(xccdf_path)
  # We need to get the actual epic Issue object, or create it if it doesn't exist
  epic = if epic.nil?
           new_epic_summary = "#{COV_PARENT_SUMMARY_PREFIX}#{xccdf.title}"
           if client.helper.summary_exist?(new_epic_summary, i_attrs)
             client.find(:issue, new_epic_summary)
           else
             unless AbideDevUtils::Prompt.yes_no("#{dr_prefix(dry_run)}Create new epic '#{new_epic_summary}'?")
               AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Aborting")
               exit(0)
             end
             client.create(:issue, project: project, summary: new_epic_summary, issuetype: 'Epic', epic_name: new_epic_summary)
           end
         else
           client.find(:issue, epic)
         end
  # Now we need to find out which issues we need to create for the benchmark
  # The profiles that the control belongs to will be added as an issue label
  to_create = []
  summaries_from_xccdf(xccdf).each do |profile_summary, control_summaries|
    control_summaries.each do |control_summary|
      existing_to_create = to_create.find { |tc| tc.summary == control_summary }
      if existing_to_create
        existing_to_create.labels << profile_summary.split.join('_').downcase
      else
        new_to_create = ToCreateData.new(
          summary: control_summary,
          labels: [profile_summary.split.join('_').downcase],
          should_create: true,
          metadata: {
            epic_key: epic.key,
            project: project,
          },
        )
        to_create << new_to_create
      end
    end
  end

  final_to_create = filter_to_create(to_create, label_include, project, epic)

  return explain_this(to_create) if explain

  unless AbideDevUtils::Prompt.yes_no("#{dr_prefix(dry_run)}Create #{final_to_create.count} new Jira issues?")
    AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Aborting")
    exit(0)
  end

  progress = AbideDevUtils::Output.progress(title: "#{dr_prefix(dry_run)}Creating issues",
                                            total: final_to_create.count,
                                            format: PROGRESS_BAR_FORMAT)
  final_to_create.each do |tc|
    abrev = tc.summary.length > 40 ? tc.summary[0..60] : tc.summary
    progress.log("#{dr_prefix(dry_run)}Creating #{abrev}...")
    client.create(:issue, project: project, summary: tc.summary, labels: tc.labels, epic_link: epic)
    progress.increment
  end
  progress.finish
  AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Done creating tasks in Epic '#{epic.summary}'")
end

.new_issues_from_xccdf_diff(project, xccdf1_path, xccdf2_path, epic: nil, dry_run: false, print_only: false, auto_approve: false, diff_opts: {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/abide_dev_utils/jira.rb', line 175

def self.new_issues_from_xccdf_diff(project, xccdf1_path, xccdf2_path, epic: nil, dry_run: false, print_only: false, auto_approve: false, diff_opts: {})
  require 'abide_dev_utils/xccdf/diff'
  diff = AbideDevUtils::XCCDF::Diff::BenchmarkDiff.new(xccdf1_path, xccdf2_path, diff_opts)
  client(dry_run: dry_run) # Initializes the client if needed
  i_attrs = client.helper.all_project_issues_attrs(project)
  # We need to get the actual epic Issue object, or create it if it doesn't exist
  epic = if epic.nil?
           new_epic_summary = "#{COV_PARENT_SUMMARY_PREFIX}#{diff.this.title}: #{diff.this.version} -> #{diff.other.version}"
           if client.helper.summary_exist?(new_epic_summary, i_attrs)
             client.find(:issue, new_epic_summary)
           else
             unless AbideDevUtils::Prompt.yes_no("#{dr_prefix(dry_run)}Create new epic '#{new_epic_summary}'?", auto_approve: auto_approve)
               AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Aborting")
               exit(0)
             end
             client.create(:issue, project: project, summary: new_epic_summary, issuetype: 'Epic', epic_name: new_epic_summary)
           end
         else
           client.find(:issue, epic)
         end
  to_create = {}
  diff.diff[:rules].each do |key, val|
    next if val.empty?

    val.each do |v|
      case key
      when :added
        sum = "Add rule #{v[:number]} - #{v[:title]}"
        sum = "#{sum[0..60]}..." if sum.length > 60
        to_create[sum] = <<~DESC
          Rule #{v[:number]} - #{v[:title]} is added

          * From:
            * Benchmark: #{diff.this.title} #{diff.this.version}
          * To:
            * Benchmark: #{diff.other.title} #{diff.other.version}
        DESC
      when :removed
        sum = "Remove rule #{v[:number]} - #{v[:title]}"
        sum = "#{sum[0..60]}..." if sum.length > 60
        to_create[sum] = <<~DESC
          Remove rule #{v[:number]} - #{v[:title]}

          * From:
            * Benchmark: #{diff.this.title} #{diff.this.version}
          * To:
            * Benchmark: #{diff.other.title} #{diff.other.version}
        DESC
      else
        sum = "Changed rule \"#{v[:from]}\""
        sum = "#{sum[0..60]}..." if sum.length > 60
        to_create[sum] = <<~DESC
          #{v[:changes].collect { |ck, cv| "Property \"#{ck}\" changed: \"#{cv.last}\" changed to \"#{cv.first}\"" }.join("\n  ")}

          * From:
            * Rule: #{v[:from]}
            * Benchmark: #{diff.this.title} #{diff.this.version}
          * To:
            * Rule: #{v[:to]}
            * Benchmark: #{diff.other.title} #{diff.other.version}
        DESC
      end
    end
  end
  approved_create = {}
  to_create.each do |summary, description|
    section_header = "#{dr_prefix(dry_run)}NEW ISSUE"
    prompt_msg = <<~PROMPT
      #{AbideDevUtils::Output.simple_section_separator(section_header, width: 90)}
      Title: '#{summary}'
      Description:
      #{description}
    PROMPT
    if print_only
      AbideDevUtils::Output.simple(prompt_msg)
    elsif AbideDevUtils::Prompt.yes_no("#{prompt_msg.strip}\nCreate?", auto_approve: auto_approve)
      approved_create[summary] = description
    end
  end
  return if approved_create.empty?

  AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Creating #{approved_create.keys.count} new Jira issues")
  progress = AbideDevUtils::Output.progress(title: "#{dr_prefix(dry_run)}Creating issues",
                                            total: approved_create.keys.count,
                                            format: PROGRESS_BAR_FORMAT)
  approved_create.each do |summary, description|
    progress.log("#{dr_prefix(dry_run)}Creating #{summary}...")
    client.create(:issue, project: project, summary: summary, description: description, labels: [], epic_link: epic)
    progress.increment
  end
  progress.finish
  AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Done creating tasks in Epic '#{epic.summary}'")
end

.summaries_from_coverage_report(report) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/abide_dev_utils/jira.rb', line 284

def self.summaries_from_coverage_report(report) # rubocop:disable Metrics/CyclomaticComplexity
  summaries = {}
  benchmark = nil
  report.each do |k, v|
    benchmark = v if k == 'benchmark'
    next unless k.match?(/^profile_/)

    parent_sum = k
    v.each do |sk, sv|
      next unless sk == 'uncovered'

      summaries[parent_sum] = sv.collect { |s| "#{COV_CHILD_SUMMARY_PREFIX}#{s}" }
    end
  end
  summaries.transform_keys { |k| "#{COV_PARENT_SUMMARY_PREFIX}#{benchmark}-#{k}"}
end

.summaries_from_xccdf(xccdf) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/abide_dev_utils/jira.rb', line 301

def self.summaries_from_xccdf(xccdf)
  summaries = {}
  xccdf.profiles.each do |profile|
    sum_key = "#{profile.level}_#{profile.title}".split.join('_').downcase
    summaries[sum_key] = profile.controls.collect do |control|
      control_id = control.respond_to?(:vulnid) ? control.vulnid : control.number
      summary = "#{control_id} - #{control.title}"
      summary = "#{summary[0..251]}..." if summary.length > 255
      summary
    end
  end
  summaries
end

.summary_exist?(summary, issue_attrs) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
# File 'lib/abide_dev_utils/jira.rb', line 277

def self.summary_exist?(summary, issue_attrs)
  issue_attrs.each do |i|
    return true if i['fields']['summary'] == summary
  end
  false
end