Module: Jironimo

Defined in:
lib/jironimo.rb

Defined Under Namespace

Classes: IssuesTableRow

Constant Summary collapse

MAX_ISSUES =
100_000
STATUS_TYPES =

Issue status types that can be passed as an argument DEPRECATED, this is project specific

['Open', 'In Progress', 'Resolved', 'All', 'All Open']
EVENT_TYPES =

JQL events

%w(created resolved updated)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#clientObject

JIRA client instance



13
14
15
# File 'lib/jironimo.rb', line 13

def client
  @client
end

#current_projectObject

Current project selected



10
11
12
# File 'lib/jironimo.rb', line 10

def current_project
  @current_project
end

#issue_typesObject

All issues types found in the client instance



19
20
21
# File 'lib/jironimo.rb', line 19

def issue_types
  @issue_types
end

#issuesObject

Issues for the currently selected project



22
23
24
# File 'lib/jironimo.rb', line 22

def issues
  @issues
end

#projectsObject

Hash of all projects



7
8
9
# File 'lib/jironimo.rb', line 7

def projects
  @projects
end

#siteObject

JIRA site



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

def site
  @site
end

Class Method Details

.clientObject

JIRA client



50
51
52
# File 'lib/jironimo.rb', line 50

def self.client
  @client
end

.current_projectObject

Returns the curren project instance



173
174
175
# File 'lib/jironimo.rb', line 173

def self.current_project
  @current_project
end

.current_project=(id, options = {}) ⇒ Object

Set the current project, enables project specific accessors like @issues



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/jironimo.rb', line 149

def self.current_project=(id, options = {})
  query_options = {
    fields:      [],
    start_at:    0,
    max_results: MAX_ISSUES
  }.update(options)
  project_search = project(id)
  if project_search.is_a? JIRA::Resource::Project
    @current_project = project_search
    # Find all of the issues for this project
    @issues = {} unless @issues.empty?
    @client.Issue.jql("project = #{id}", query_options).each do |issue|
      @issues[issue.key] = issue
    end
    # This code only returns 50 results max
    # @current_project.issues.each do |issue|
    #  @issues[issue.key] = issue
    # end
  else
    @current_project = nil
  end
end

.current_status_types(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jironimo.rb', line 54

def self.current_status_types(options = {})
  options = {
    project: @current_project.key
  }.update(options)
  if options[:project].nil?
    Origen.log.error('Cannot find current status types, no project argument passed!')
    fail
  else
    status_types_found = @projects[options[:project]].issues.map { |issue| issue.status.name }.uniq
  end
end

.delete_issue(issue_key) ⇒ Object

Delete a JIRA issue



346
347
348
349
# File 'lib/jironimo.rb', line 346

def self.delete_issue(issue_key)
  Origen.log.error "Could not delete issue '#{issue_key}', exiting..." unless @issues.include? issue_key
  @issues[issue_key].delete
end

.initializeObject



35
36
37
38
39
40
41
42
# File 'lib/jironimo.rb', line 35

def self.initialize
  @client ||= nil
  @site ||= nil
  @projects ||= {}
  @issue_types ||= {}
  @current_project ||= nil
  @issues ||= {}
end

.issue_type_mappingObject

Returns a hash of issue types with issue type name as key and issue type number as value



289
290
291
292
293
294
295
# File 'lib/jironimo.rb', line 289

def self.issue_type_mapping
  issue_type_names = {}
  @issue_types.each do |type_name, issue_type|
    issue_type_names[type_name] = issue_type.id
  end
  issue_type_names
end

.issue_types(filter_arg = nil) ⇒ Object

Allows filtering of @issue_types



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
# File 'lib/jironimo.rb', line 67

def self.issue_types(filter_arg = nil)
  if filter_arg.nil?
    return @issue_types
  else
    issue_types_found = case filter_arg
    when Regexp
      @issue_types.filter(filter_arg)
    when String
      if @issue_types.include? filter_arg
        @issue_types[filter_arg]
      else
        Origen.log.warn "Found no JIRA issue types using the argument '#{filter_arg}', refine your search!"
        return nil
      end
    end
    if issue_types_found.is_a? Hash
      if issue_types_found.size > 1
        issue_types_found
      else
        Origen.log.warn "Found no JIRA issue types using the argument '#{filter_arg}', refine your search!"
        return nil
      end
    else
      issue_types_found
    end
  end
end

.issues(id = nil) ⇒ Object

Allows filtering of @issues



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/jironimo.rb', line 250

def self.issues(id = nil)
  if @current_project.nil?
    return {}
  elsif @current_project.is_a? JIRA::Resource::Project
    if id.nil?
      @issues
    elsif @issues.include? id
      @issues[id]
    else
      Origen.log.error("Jironimo: Issue '#{id}' is not found in project '#{@current_project.key}'!")
      exit 0
    end
  else
    Origen.log.error "Jironimo.current_project is of incorrect type '#{@current_project.class}', should be nil or JIRA::Resource::Project, exiting.."
    exit 0
  end
end

.latest_issues(time, options = {}) ⇒ Object

Get the latest issues



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/jironimo.rb', line 224

def self.latest_issues(time, options = {})
  query_options = {
    verbose:     false,
    assignee:    User.current.id,
    fields:      [],
    start_at:    0,
    max_results: MAX_ISSUES,
    event:       'created'
  }.update(options)
  latest_issues = {}
  console_print = query_options.delete(:verbose)
  event = query_options.delete(:event)
  assignee = query_options.delete(:assignee)
  Origen.log.errpr 'Jironimo.current_project is not set, exiting...' if @current_project.nil?
  Origen.log.error 'Jironimo.client is not set, exiting...' if @client.nil?
  Origen.log.error "Event '#{event}' is not supported, choose from #{EVENT_TYPES.join(', ')}, exiting..." unless EVENT_TYPES.include? event
  Origen.log.error "Could not get latest issues with argument '#{time}', exiting..." unless jql_time_ok?(time)
  jql = "assignee = #{assignee} AND #{event} <= '#{time}'"
  @client.Issue.jql(jql).each do |issue|
    latest_issues[issue.key] = issue
  end
  latest_issues
end

.launch(options = {}) ⇒ Object

Launches the JIRA interface client and initializes



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/jironimo.rb', line 270

def self.launch(options = {})
  options = {
    username:     User.current.id,
    password:     User.current.password,
    site:         'http://jira.amd.com',
    auth_type:    :basic,
    use_ssl:      false,
    context_path: ''
  }.update(options)
  initialize
  @client = JIRA::Client.new(options)
  @site = options[:site]
  @projects = create_projects_hash(@client.Project.all)
  @issue_types = create_issue_types_hash(@client.Issuetype.all)
  @client
end

.my_issues(options = {}) ⇒ Object

Returns a hash of JIRA issues assigned to the current user Defaults to ‘Open’ or ‘In Progress’ issue status



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
# File 'lib/jironimo.rb', line 179

def self.my_issues(options = {})
  query_options = {
    verbose:     false,
    assignee:    User.current.id,
    project:     @current_project.key,
    fields:      [],
    start_at:    0,
    max_results: MAX_ISSUES,
    status:      nil
  }.update(options)
  my_issues = {}
  console_print = query_options.delete(:verbose)
  Origen.log.error 'User option must be a String, exiting...' unless query_options[:assignee].is_a? String
  assignee = query_options.delete(:assignee)
  jql_str = "assignee = #{assignee.upcase}"
  if query_options[:project].nil?
    # Cannot search every project for issues, takes too long
    Origen.log.error('No project passed, cannot fetch your issues!')
    fail
  else
    jql_str += " AND project = #{query_options[:project]}"
  end
  # Check which issue status types to retrieve
  valid_status_types = current_status_types(query_options)
  begin
    case query_options[:status]
    # This can and should be expanded over time
    when 'All', nil
      jql_str = jql_str # Don't change naything as status doesn't need to be filtered
    end
  ensure
    query_options.delete(:status)
  end
  if @client.nil?
    Origen.log.error 'No JIRA client instantiated, cannot check for your issues, exiting...'
  else
    @client.Issue.jql(jql_str, query_options).each do |issue|
      my_issues[issue.key] = issue
    end
  end
  show_issues(my_issues) if console_print
  my_issues
end

.new_issue(options = {}) ⇒ Object

Create anew JIRA issue for the current project or one passed as an argument



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/jironimo.rb', line 313

def self.new_issue(options = {})
  options = {
    project:     nil,
    type:        nil,
    summary:     nil,
    description: nil
  }.merge(options)
  project_attr = ''
  args_result, options = check_issue_args(options)
  Origen.log.jironimo 'Creating a new JIRA issue failed due to bad arguments, check previous log warnings, exiting...' unless args_result
  if options[:project].numeric?
    project_attr = 'id'
  elsif options[:project].is_a? String
    project_attr = 'key'
  else
    Origen.log.error 'options[:project] must be String (use project key) or Integer (use project ID), exiting...'
  end
  issue = @client.Issue.build
  issue.save(assemble_issue_fields(options, project_attr))
  if issue.respond_to?(:errors)
    Origen.log.error('Jironimo: Cannot create Jira issue, here is why:')
    issue.errors.each do |err|
      Origen.log.error("Jironimo: #{err}")
    end
    exit 0
  else
    # Successfully created an issue
    Origen.log.jironimo "Created issue for #{issue.key}"
  end
  issue
end

.project(id) ⇒ Object

Retrieve a project using a numeric ID or String key



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jironimo.rb', line 114

def self.project(id)
  # Try the project key first and then the ID
  if id.is_a? String
    return projects(id)
  elsif id.is_a? Integer
    # Try the ID
    projects_found = @projects.select { |key, p| p.id.to_numeric == id }
    if projects_found.size == 1
      return projects_found.values.first
    else
      Origen.log.warn "Found more than one JIRA project using the argument '#{id}', refine your search from #{projects_found.keys.join(', ')}"
      return projects_found.values
    end
  end
end

.projects(filter_arg = nil) ⇒ Object

Allows filtering of @projects



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jironimo.rb', line 96

def self.projects(filter_arg = nil)
  if filter_arg.nil?
    return @projects
  else
    projects_found = @projects.filter(filter_arg)
    if projects_found.size == 1
      return projects_found.values.first
    elsif projects_found > 1
      Origen.log.warn "Found more than one JIRA project using the argument '#{filter_arg}', refine your search from #{projects_found.keys.join(', ')}"
      return projects_found.values
    else
      Origen.log.warn "Found no JIRA issue types using the argument '#{filter_arg}', refine your search from #{projects_found.keys.join(', ')}"
      return nil
    end
  end
end

.refresh(options = {}) ⇒ Object

Re-fresh the JIRA client and releated module accessors Useful after adding, deleting, ormodifying anythign on the JIRA server



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jironimo.rb', line 132

def self.refresh(options = {})
  query_options = {
    fields:      [],
    start_at:    0,
    max_results: MAX_ISSUES
  }.update(options)
  Origen.log.errpr 'Jironimo#refresh needs Jironimo.current_project to be set' if @current_project.nil?
  Origen.log.error 'Jironimo#refresh needs to have Jironimo.client to be valid' if @client.nil?
  @projects = create_projects_hash(@client.Project.all)
  @issue_types = create_issue_types_hash(@client.Issuetype.all)
  @issues = {}
  @client.Issue.jql("project = #{@current_project.key}", query_options).each do |issue|
    @issues[issue.key] = issue
  end
end

.siteObject

JIRA sites



45
46
47
# File 'lib/jironimo.rb', line 45

def self.site
  @site
end

.update_issue(issue_key) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/jironimo.rb', line 297

def self.update_issue(issue_key)
  options = {
    type:        nil,
    summary:     nil,
    description: nil
  }.merge(options)
  Origen.log.error "Cannot update issue '#{issue_key}', please set the current project" if @issues.empty?
  project_attr = 'key'
  args_result, options = check_issue_args(options)
  Origen.log.error "Updating JIRA issue '#{issue_key}' failed due to bad arguments, check previous log warnings, exiting..." unless args_result
  issue = @issues[issue_key]
  issue.save(assemble_issue_fields(options, project_attr))
  issue
end