class JiraCache::Client

The JIRA API Client.

Constants

EXPANDED_FIELDS
JIRA_MAX_RESULTS

Attributes

logger[R]

Other possible fields: names, schema, operations, editmeta

notifier[R]

Other possible fields: names, schema, operations, editmeta

Public Class Methods

new(domain: ENV["JIRA_DOMAIN"], username: ENV["JIRA_USERNAME"], password: ENV["JIRA_PASSWORD"], notifier: default_notifier, logger: default_logger) click to toggle source

Returns a new instance of the client, configured with the specified parameters.

@param domain [String] JIRA API domain (e.g. your-project.atlassian.net) @param username [String] JIRA user“s name, if required @param password [String] JIRA user”s password, if required @param logger [Logger] used to log message (defaults to a logger to STDOUT at

info level)

@param notifier [Notifier] a notifier instance that will be used to publish

event notifications (see `JiraCache::Notifier` for more information)
# File lib/jira_cache/client.rb, line 31
def initialize(domain: ENV["JIRA_DOMAIN"],
               username: ENV["JIRA_USERNAME"],
               password: ENV["JIRA_PASSWORD"],
               notifier: default_notifier,
               logger: default_logger)
  check_domain!(domain)
  check_password!(username, password)
  @domain = domain
  @username = username
  @password = password
  @notifier = notifier
  @logger = logger
end

Public Instance Methods

authorization_prefix() click to toggle source
# File lib/jira_cache/client.rb, line 142
def authorization_prefix
  return "" if missing_credential?
  "#{CGI.escape(@username)}:#{CGI.escape(@password)}@"
end
complete_worklogs(id_or_key, issue_data) click to toggle source
# File lib/jira_cache/client.rb, line 101
def complete_worklogs(id_or_key, issue_data)
  if incomplete_worklogs?(issue_data)
    issue_data["fields"]["worklog"] = issue_worklog_content(id_or_key)
  end
  issue_data
end
default_logger() click to toggle source
# File lib/jira_cache/client.rb, line 147
def default_logger
  return @logger unless @logger.nil?
  @logger = ::Logger.new(STDOUT)
  @logger.level = ::Logger::FATAL
  @logger
end
default_notifier() click to toggle source
# File lib/jira_cache/client.rb, line 154
def default_notifier
  return @notifier unless @notifier.nil?
  @notifier = JiraCache::Notifier.new(@logger)
end
do_get(path, params = {}) click to toggle source
# File lib/jira_cache/client.rb, line 125
def do_get(path, params = {})
  logger.debug "GET #{uri(path)} #{params}"
  response = RestClient.get uri(path),
    params: params,
    content_type: "application/json"
  begin
    JSON.parse(response.body)
  rescue JSON::ParseError
    response.body
  end
end
incomplete_worklogs?(issue_data) click to toggle source
# File lib/jira_cache/client.rb, line 108
def incomplete_worklogs?(issue_data)
  worklog = issue_data["fields"]["worklog"]
  worklog["total"].to_i > worklog["maxResults"].to_i
end
info() click to toggle source

Returns an hash of info on the client

# File lib/jira_cache/client.rb, line 160
def info
  {
    domain: @domain,
    username: @username
  }
end
issue_data(id_or_key) click to toggle source

Fetches the issue represented by id_or_key from the client. If the data is already present in the cache, returns the cached version, unless if :allow_cache option is false.

# File lib/jira_cache/client.rb, line 50
def issue_data(id_or_key)
  logger.info "Fetching data for issue #{id_or_key}"
  issue_data = do_get("/issue/#{id_or_key}",
    expand: EXPANDED_FIELDS.join(",")
  ).to_hash
  return nil if issue_not_found?(issue_data)
  issue_data = complete_worklogs(id_or_key, issue_data)
  begin
    notifier.publish "fetched_issue", key: id_or_key, data: issue_data
  rescue => e
    logger.error "Notifier failed: #{e}"
    logger.error e.backtrace
  end
  issue_data
end
issue_ids_in_limits(jql_query, start_at) click to toggle source

@return [total, issues]

- total: [Int] the total number of issues in the query results
- issues: [Array] array of issues in the response
  (max `JIRA_MAX_RESULTS`)
# File lib/jira_cache/client.rb, line 87
def issue_ids_in_limits(jql_query, start_at)
  results = do_get "/search",
    jql: jql_query,
    startAt: start_at,
    fields: "id",
    maxResults: JIRA_MAX_RESULTS
  [results["total"], results["issues"]]
end
issue_keys_for_query(jql_query) click to toggle source
# File lib/jira_cache/client.rb, line 66
def issue_keys_for_query(jql_query)
  start_at = 0
  issues = []
  loop do
    total, page_issues = issue_ids_in_limits(jql_query, start_at)
    logger.info "Total number of issues: #{total}" if issues.length == 0
    issues += page_issues
    logger.info "  -- loaded #{page_issues.length} issues"
    start_at = issues.length
    break if issues.length == total
  end
  issues.collect { |issue| issue["key"] }
end
issue_not_found?(issue_data) click to toggle source
# File lib/jira_cache/client.rb, line 96
def issue_not_found?(issue_data)
  return false if issue_data["errorMessages"].nil?
  issue_data["errorMessages"].first == "Issue Does Not Exist"
end
issue_worklog_content(id_or_key) click to toggle source
# File lib/jira_cache/client.rb, line 113
def issue_worklog_content(id_or_key)
  do_get("/issue/#{id_or_key}/worklog").to_hash
end
project_data(id) click to toggle source
# File lib/jira_cache/client.rb, line 117
def project_data(id)
  do_get "/project/#{id}"
end
projects_data() click to toggle source
# File lib/jira_cache/client.rb, line 121
def projects_data
  do_get "/project"
end
uri(path) click to toggle source

Returns the JIRA API“s base URI (build using `config`)

# File lib/jira_cache/client.rb, line 138
def uri(path)
  "https://#{authorization_prefix}#{@domain}/rest/api/2#{path}"
end

Private Instance Methods

check_domain!(domain) click to toggle source
# File lib/jira_cache/client.rb, line 169
def check_domain!(domain)
  raise "Missing domain" if domain.nil? || domain.empty?
end
check_password!(username, password) click to toggle source
# File lib/jira_cache/client.rb, line 173
def check_password!(username, password)
  unless (username.nil? || username.empty?)
    raise "Missing password (mandatory if username given)" if password.nil? || password.empty?
  end
end
missing_credential?() click to toggle source
# File lib/jira_cache/client.rb, line 179
def missing_credential?
  return true if @username.nil? || @username.empty?
  return true if @password.nil? || @password.empty?
  false
end