Module: AbideDevUtils::Jira

Defined in:
lib/abide_dev_utils/jira.rb

Defined Under Namespace

Classes: Dummy

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

.add_issue_label(iss, label, dry_run: false) ⇒ Object



55
56
57
58
59
60
# File 'lib/abide_dev_utils/jira.rb', line 55

def self.add_issue_label(iss, label, dry_run: false)
  return if dry_run || iss.labels.include?(label)

  iss.labels << profile_summary
  iss.save
end

.all_project_issues_attrs(project) ⇒ Object



50
51
52
53
# File 'lib/abide_dev_utils/jira.rb', line 50

def self.all_project_issues_attrs(project)
  raw_issues = project.issues
  raw_issues.collect(&:attrs)
end

.bulk_new_issue(client, project, summaries, dry_run: false) ⇒ Object

This should probably be threaded in the future



101
102
103
# File 'lib/abide_dev_utils/jira.rb', line 101

def self.bulk_new_issue(client, project, summaries, dry_run: false)
  summaries.each { |s| new_issue(client, project, s, dry_run: dry_run) }
end

.bulk_new_subtask(client, issue, summaries, dry_run: false) ⇒ Object



123
124
125
126
127
# File 'lib/abide_dev_utils/jira.rb', line 123

def self.bulk_new_subtask(client, issue, summaries, dry_run: false)
  summaries.each do |s|
    new_subtask(client, issue, s, dry_run: dry_run)
  end
end

.client(options: {}) ⇒ Object



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

def self.client(options: {})
  opts = merge_options(options)
  return client_from_prompts if opts.empty?

  opts[:username] = AbideDevUtils::Prompt.username if opts[:username].nil?
  opts[:password] = AbideDevUtils::Prompt.password if opts[:password].nil?
  opts[:site] = AbideDevUtils::Prompt.single_line('Jira URL') if opts[:site].nil?
  opts[:context_path] = '' if opts[:context_path].nil?
  opts[:auth_type] = :basic if opts[:auth_type].nil?
  JIRA::Client.new(opts)
end

.client_from_prompts(http_debug: false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/abide_dev_utils/jira.rb', line 141

def self.client_from_prompts(http_debug: false)
  options = {}
  options[:username] = AbideDevUtils::Prompt.username
  options[:password] = AbideDevUtils::Prompt.password
  options[:site] = AbideDevUtils::Prompt.single_line('Jira URL')
  options[:context_path] = ''
  options[:auth_type] = :basic
  options[:http_debug] = http_debug
  JIRA::Client.new(options)
end

.configObject



299
300
301
# File 'lib/abide_dev_utils/jira.rb', line 299

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

.dr_prefix(dry_run) ⇒ Object



341
342
343
# File 'lib/abide_dev_utils/jira.rb', line 341

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

.issue(client, issue) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/abide_dev_utils/jira.rb', line 21

def self.issue(client, issue)
  client.Issue.find(issue)
rescue URI::InvalidURIError
  iss = client.Issue.all.find { |i| i.summary == issue }
  raise ERRORS::FindIssueError, issue unless iss

  iss
end

.issuetype(client, id) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/abide_dev_utils/jira.rb', line 34

def self.issuetype(client, id)
  if id.match?(%r{^\d+$})
    client.Issuetype.find(id)
  else
    client.Issuetype.all.find { |i| i.name == id }
  end
end

.merge_options(options) ⇒ Object



295
296
297
# File 'lib/abide_dev_utils/jira.rb', line 295

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

.myself(client) ⇒ Object



30
31
32
# File 'lib/abide_dev_utils/jira.rb', line 30

def self.myself(client)
  client.User.myself
end

.new_epic(client, project, summary, dry_run: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/abide_dev_utils/jira.rb', line 82

def self.new_epic(client, project, summary, dry_run: false)
  AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Creating epic '#{summary}'")
  if dry_run
    sleep(0.2)
    return Dummy.new(summary)
  end
  fields = {
    'summary' => summary,
    'project' => project(client, project),
    'issuetype' => issuetype(client, 'Epic'),
    'customfield_10007' => summary, # Epic Name
  }
  iss = client.Issue.build
  raise ERRORS::CreateEpicError, iss.attrs unless iss.save({ 'fields' => fields })

  iss
end

.new_issue(client, project, summary, description: nil, labels: ['abide_dev_utils'], epic: nil, dry_run: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/abide_dev_utils/jira.rb', line 62

def self.new_issue(client, project, summary, description: nil, labels: ['abide_dev_utils'], epic: nil, dry_run: false)
  if dry_run
    sleep(0.2)
    return Dummy.new(summary)
  end
  fields = {}
  fields['summary'] = summary
  fields['project'] = project(client, project)
  fields['issuetype'] = issuetype(client, 'Task')
  fields['priority'] = priority(client, '3')
  fields['description'] = description if description
  fields['labels'] = labels
  epic = issue(client, epic) if epic && !epic.is_a?(JIRA::Resource::Issue)
  fields['customfield_10006'] = epic.key if epic # Epic_Link
  iss = client.Issue.build
  raise ERRORS::CreateIssueError, iss.attrs unless iss.save({ 'fields' => fields })

  iss
end

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



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

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

    parent = new_issue(client, project.attrs['key'], k.to_s, dry_run: dry_run)
    AbideDevUtils::Output.simple("#{dr_prefix}Created parent issue #{k}")
    parent_issue = issue(client, parent.attrs['key']) unless parent.respond_to?(:dummy)
    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 summary_exist?(s, i_attrs)

      progress.title = "#{dr_prefix}#{s}"
      new_subtask(client, parent_issue, s, dry_run: dry_run)
      progress.increment
    end
  end
end

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



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
# File 'lib/abide_dev_utils/jira.rb', line 180

def self.new_issues_from_xccdf(client, project, xccdf_path, epic: nil, dry_run: false)
  i_attrs = 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 summary_exist?(new_epic_summary, i_attrs)
             issue(client, 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
             new_epic(client, project.key, new_epic_summary, dry_run: dry_run)
           end
         else
           issue(client, 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.reject { |s| summary_exist?(s, i_attrs) }.each do |control_summary|
      if to_create.key?(control_summary)
        to_create[control_summary] << profile_summary.split.join('_').downcase
      else
        to_create[control_summary] = [profile_summary.split.join('_').downcase]
      end
    end
  end

  unless AbideDevUtils::Prompt.yes_no("#{dr_prefix(dry_run)}Create #{to_create.keys.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: to_create.keys.count,
                                            format: PROGRESS_BAR_FORMAT)
  to_create.each do |control_summary, labels|
    abrev = control_summary.length > 40 ? control_summary[0..60] : control_summary
    progress.log("#{dr_prefix(dry_run)}Creating #{abrev}...")
    new_issue(client, project.key, control_summary, labels: labels, epic: epic, dry_run: dry_run)
    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(client, project, xccdf1_path, xccdf2_path, epic: nil, dry_run: false, auto_approve: false, diff_opts: {}) ⇒ Object



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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/abide_dev_utils/jira.rb', line 229

def self.new_issues_from_xccdf_diff(client, project, xccdf1_path, xccdf2_path, epic: nil, dry_run: false, auto_approve: false, diff_opts: {})
  require 'abide_dev_utils/xccdf/diff'
  diff = AbideDevUtils::XCCDF::Diff::BenchmarkDiff.new(xccdf1_path, xccdf2_path, diff_opts)
  i_attrs = 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 = "#{UPD_EPIC_SUMMARY_PREFIX}#{diff.this.title}: v#{diff.this.version} -> #{diff.other.version}"
           if summary_exist?(new_epic_summary, i_attrs)
             issue(client, 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
             new_epic(client, project.key, new_epic_summary, dry_run: dry_run)
           end
         else
           issue(client, 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 with #{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
          Rule #{v[:number]} - #{v[:title]} is removed from #{diff.this.title} #{diff.this.version}
        DESC
      else
        sum = "Update rule \"#{v[:from]}\""
        sum = "#{sum[0..60]}..." if sum.length > 60
        to_create[sum] = <<~DESC
          Rule #{v[:from]} is updated in #{diff.other.title} #{diff.other.version}:
          #{v[:changes].collect { |k, v| "#{k}: #{v}" }.join("\n")}
        DESC
      end
    end
  end
  approved_create = {}
  to_create.each do |summary, description|
    if AbideDevUtils::Prompt.yes_no("#{dr_prefix(dry_run)}Create new issue '#{summary}' with description:\n#{description}", auto_approve: auto_approve)
      approved_create[summary] = description
    end
  end
  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}...")
    new_issue(client, project.key, summary, description: description, labels: [], epic: epic, dry_run: dry_run)
    progress.increment
  end
  progress.finish
  AbideDevUtils::Output.simple("#{dr_prefix(dry_run)}Done creating tasks in Epic '#{epic.summary}'")
end

.new_subtask(client, issue, summary, dry_run: false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/abide_dev_utils/jira.rb', line 105

def self.new_subtask(client, issue, summary, dry_run: false)
  if dry_run
    sleep(0.2)
    return Dummy.new
  end
  issue_fields = issue.attrs['fields']
  fields = {}
  fields['parent'] = issue
  fields['summary'] = summary
  fields['project'] = issue_fields['project']
  fields['issuetype'] = issuetype(client, '5')
  fields['priority'] = issue_fields['priority']
  subtask = client.Issue.build
  raise ERRORS::CreateSubtaskError, subtask.attrs unless subtask.save({ 'fields' => fields })

  subtask
end

.priority(client, id) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/abide_dev_utils/jira.rb', line 42

def self.priority(client, id)
  if id.match?(%r{^\d+$})
    client.Priority.find(id)
  else
    client.Priority.all.find { |i| i.name == id }
  end
end

.project(client, project) ⇒ Object



17
18
19
# File 'lib/abide_dev_utils/jira.rb', line 17

def self.project(client, project)
  client.Project.find(project)
end

.project_from_prompts(http_debug: false) ⇒ Object



152
153
154
155
156
# File 'lib/abide_dev_utils/jira.rb', line 152

def self.project_from_prompts(http_debug: false)
  client = client_from_prompts(http_debug)
  project = AbideDevUtils::Prompt.single_line('Project').upcase
  client.Project.find(project)
end

.summaries_from_coverage_report(report) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/abide_dev_utils/jira.rb', line 310

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



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/abide_dev_utils/jira.rb', line 327

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)


303
304
305
306
307
308
# File 'lib/abide_dev_utils/jira.rb', line 303

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