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:: '
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

[View source]

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

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

[View source]

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

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

[View source]

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

[View source]

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

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

[View source]

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

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

[View source]

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

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

[View source]

234
235
236
# File 'lib/abide_dev_utils/jira.rb', line 234

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

.dr_prefix(dry_run) ⇒ Object

[View source]

276
277
278
# File 'lib/abide_dev_utils/jira.rb', line 276

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

.issue(client, issue) ⇒ Object

[View source]

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

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

[View source]

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

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

[View source]

230
231
232
# File 'lib/abide_dev_utils/jira.rb', line 230

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

.myself(client) ⇒ Object

[View source]

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

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

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

[View source]

81
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 81

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),
    'reporter' => myself(client),
    '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, labels: ['abide_dev_utils'], epic: nil, dry_run: false) ⇒ Object

[View source]

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

def self.new_issue(client, project, summary, 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['reporter'] = myself(client)
  fields['issuetype'] = issuetype(client, 'Task')
  fields['priority'] = priority(client, '6')
  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

[View source]

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

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

[View source]

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

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_subtask(client, issue, summary, dry_run: false) ⇒ Object

[View source]

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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['reporter'] = myself(client)
  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

[View source]

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

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

[View source]

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

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

.project_from_prompts(http_debug: false) ⇒ Object

[View source]

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

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

[View source]

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/abide_dev_utils/jira.rb', line 245

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

[View source]

262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/abide_dev_utils/jira.rb', line 262

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)
[View source]

238
239
240
241
242
243
# File 'lib/abide_dev_utils/jira.rb', line 238

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