Class: Jira

Inherits:
RCTClient
  • Object
show all
Defined in:
lib/rct_jira.rb

Constant Summary collapse

BASE_PATH =
'/rest/api/2'
ServerInfo =

Retrieve server info (really only useful to test API/connection). No authentication required.

docs.atlassian.com/jira/REST/latest/#idp1713744

Required: none Optional: none Saves to state: nothing

{
  'description' => "Retrieve server info (mainly for testing connection)",
  'required' => { },
  'optional' => { }
}
NotWatching =

Retrieve a list of issues which I (user authenticating) am not watching.

Note this may not be a full list of unwatched issues if the list is larger than the limit, which defaults to 100. The server may also impose a limit which might be smaller than the requested limit.

Required:

username : Authenticate as this user.
password : Password of username.
project : JIRA project name to search.

Optional:

limit : Return up to this many results (default: 100)

Saves to state:

not_watching_result : Hash of key => description of all issues found
{
  'description' => "Retrieve list of issues I am not watching",
  'required' => {
    'username' => [ '-u', '--user', 'User name' ],
    'password' => [ '-P', '--password', 'Password' ],
    'project' => [ '-c' , '--project', 'Project name (category)' ],
  },
  'optional' => {
    'limit' => [ '-l', '--limit', 'Limit result set size to this number'],
  }
}
Mine =

Retrieve a list of issues which I (user authenticating) own.

Note this may not be a full list of if the list is larger than the limit, which defaults to 100. The server may also impose a limit which might be smaller than the requested limit.

Required:

username : Authenticate as this user.
password : Password of username.

Optional:

limit   : Return up to this many results (default: 100)
project : Limit results to this project.

Saves to state:

my_bugs : Hash of key => description of all issues found
{
  'description' => "Retrieve list of issues I own",
  'required' => {
    'username' => [ '-u', '--user', 'User name' ],
    'password' => [ '-P', '--password', 'Password' ],
  },
  'optional' => {
    'limit' => [ '-l', '--limit', 'Limit result set size to this number'],
    'project' => [ '-c' , '--project', 'Project name (category)' ],
  }
}
AddMyWatch =

Add myself as a watcher to one issue.

docs.atlassian.com/jira/REST/latest/#idp1831280

Required:

username : Authenticate as this user.
password : Password of username.
project : JIRA project name to search.
key : JIRA issue key to update.

Optional:

none

Saves to state:

nothing
{
  'description' => "Add myself as a watcher to one issue",
  'required' => {
    'username' => [ '-u', '--user', 'User name' ],
    'password' => [ '-P', '--password', 'Password' ],
    'project' => [ '-c' , '--project', 'Project name (category)' ],
    'key' => [ '-k', '--issuekey', 'Issue to add myself as watcher'],
  },
  'optional' => { }
}
WatchCategory =

Add myself as a watcher to all unwatched issues in a given category.

This is a wrapper function which combines NotWatching and AddMyWatch for convenience.

Note this may not be able to add me to all unwatched issues if there are more than the server list limit. In that case one may need to run this multiple times.

Required:

username : Authenticate as this user.
password : Password of username.
project : JIRA project name to watch.

Optional:

none

Saves to state:

nothing
{
  'description' => "Add myself as a watcher to all issues in category",
  'required' => {
    'username' => [ '-u', '--user', 'User name' ],
    'password' => [ '-P', '--password', 'Password' ],
    'project' => [ '-c' , '--project', 'Project name (category)' ],
  },
  'optional' => { }
}

Instance Method Summary collapse

Instance Method Details

#add_my_watchObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rct_jira.rb', line 274

def add_my_watch
  user = sget('username')
  password = sget('password')
  project = sget('project')
  key = sget('key')

  ssettmp(SERVER_PROTOCOL, 'https')
  ssettmp(REQ_METHOD, 'POST')
  ssettmp(REQ_AUTH_TYPE, REQ_AUTH_TYPE_BASIC)
  ssettmp(REQ_AUTH_NAME, user)
  ssettmp(REQ_AUTH_PWD, password)
  ssettmp(REQ_PATH, "#{BASE_PATH}/issue/#{key}/watchers")
  ssettmp(REQ_BODY, "\"#{user}\"")

  headers = Hash.new()
  headers['Content-type'] = 'application/json'
  ssettmp(REQ_HEADERS, headers)

  result = yield

  if (result.status == 204)
    if (is_cli)
      sset(CLI_OUTPUT, "Added #{user} as a watcher to #{key}")
    end
  else
    result.add_error("Unable to add #{user} to #{key}")
  end

  return result
end

#cliObject


CLI definition. Used by the rct framework to determine what CLI commands are available here. This maps the operation name to a Hash of info about that operation.

Note that this needs to list only those operations which can be invoked in CLI mode. Not all operations supported by an rct client module are necessarily CLI-compatible so this may be a subset of the operations available here.



57
58
59
60
61
62
63
64
65
# File 'lib/rct_jira.rb', line 57

def cli
  return {
    'server_info' => ServerInfo,
    'not_watching' => NotWatching,
    'add_my_watch' => AddMyWatch,
    'watch_category' => WatchCategory,
    'mine' => Mine
  }
end

#descriptionObject


Class description, for automated help.



41
42
43
44
# File 'lib/rct_jira.rb', line 41

def description
  "The RCT Jira class implements access to some of the common Jira\n" +
  "(http://en.wikipedia.org/wiki/JIRA) APIs."
end

#mineObject



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

def mine
  user = sget('username')
  password = sget('password')
  project = sget('project')

  limit = sget('limit')
  limit = '100' if (limit == nil)

  ssettmp(SERVER_PROTOCOL, 'https')
  ssettmp(REQ_METHOD, 'GET')
  ssettmp(REQ_AUTH_TYPE, REQ_AUTH_TYPE_BASIC)
  ssettmp(REQ_AUTH_NAME, user)
  ssettmp(REQ_AUTH_PWD, password)
  ssettmp(REQ_PATH, "#{BASE_PATH}/search")

  params = add_param(nil, 'maxResults', limit)
  params = add_param(params, 'fields', 'summary')

  jql = ""
  if (project != nil)
    jql = "project=#{project} AND "
  end
  jql = "#{jql}assignee = currentUser() AND (status=\"Open\" OR status=\"In Progress\")"

  params = add_param(params, 'jql', jql)
  ssettmp(REQ_PARAMS, params)

  result = yield

  if (result.ok)
    # On success, create a simple key->description hash with the results
    if (is_cli) then cli_output = "\n" end
    list = Hash.new()
    json = JSON.parse(result.body)
    issues = json['issues']
    issues.each { |h|
      key = h['key']
      summary = h['fields']['summary']
      list[key] = summary
      if (is_cli) then cli_output += "#{key} : #{summary}\n" end
    }
    sset('my_bugs', list)
    if (is_cli)
      sset(CLI_OUTPUT, cli_output)
    end
  end

  return result
end

#not_watchingObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rct_jira.rb', line 120

def not_watching
  user = sget('username')
  password = sget('password')
  project = sget('project')

  limit = sget('limit')
  limit = '100' if (limit == nil)

  ssettmp(SERVER_PROTOCOL, 'https')
  ssettmp(REQ_METHOD, 'GET')
  ssettmp(REQ_AUTH_TYPE, REQ_AUTH_TYPE_BASIC)
  ssettmp(REQ_AUTH_NAME, user)
  ssettmp(REQ_AUTH_PWD, password)
  ssettmp(REQ_PATH, "#{BASE_PATH}/search")

  params = add_param(nil, 'maxResults', limit)
  params = add_param(params, 'fields', 'summary')
  params = add_param(params, 'jql',
                     "project=#{project} and watcher != currentUser()")
  ssettmp(REQ_PARAMS, params)

  result = yield

  if (result.ok)
    # On success, create a simple key->description hash with the results
    if (is_cli) then cli_output = "\n" end
    unwatched = Hash.new()
    json = JSON.parse(result.body)

    issues = json['issues']
    if (issues != nil)
      issues.each { |h|
        key = h['key']
        summary = h['fields']['summary']
        unwatched[key] = summary
        if (is_cli) then cli_output += "#{key} : #{summary}\n" end
      }
    end

    sset('not_watching_result', unwatched)
    if (is_cli)
      sset(CLI_OUTPUT, cli_output)
    end
  end

  return result
end

#server_infoObject



84
85
86
87
88
89
# File 'lib/rct_jira.rb', line 84

def server_info
  ssettmp(SERVER_PROTOCOL, 'https')
  ssettmp(REQ_METHOD, 'GET')
  ssettmp(REQ_PATH, "#{BASE_PATH}/serverInfo")
  yield
end

#watch_categoryObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rct_jira.rb', line 335

def watch_category

  project = sget('project')

  # Pretend we're not in CLI mode even if we are to avoid duplicate output
  # from the functions we're wrapping.

  mode = RCT.sget(RCT_MODE)
  RCT.sdelete(RCT_MODE)

  result = not_watching { $HTTP.handle_request() }
  if (!result.ok)
    result.add_error("Unable to get list of unwatched issues")
    RCT.sset(RCT_MODE, mode)
    return result
  end

  issues = RCT.sget('not_watching_result')
  count = 0

  issues.each { |key, desc|
    RCT.log(RESULT, "#{key}: #{desc}")
    RCT.sset('key', key)
    result = add_my_watch { $HTTP.handle_request() }
    if (!result.ok)
      result.add_error("Unable to add watcher to #{key}")
      RCT.sset(RCT_MODE, mode)
      return result
    end
    count = count + 1
  }

  RCT.sset(RCT_MODE, mode)
  if (is_cli)
    sset(CLI_OUTPUT, "Added #{count} bugs to watch in #{project}")
  end

  return result
end