Class: JiraCache::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jira_cache/client.rb

Overview

The JIRA API Client.

Constant Summary collapse

JIRA_MAX_RESULTS =
1000
EXPANDED_FIELDS =
%w(
  renderedFields
  changelog
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain: ENV["JIRA_DOMAIN"], username: ENV["JIRA_USERNAME"], password: ENV["JIRA_PASSWORD"], notifier: default_notifier, logger: default_logger) ⇒ Client

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

Parameters:

  • domain (String) (defaults to: ENV["JIRA_DOMAIN"])

    JIRA API domain (e.g. your-project.atlassian.net)

  • username (String) (defaults to: ENV["JIRA_USERNAME"])

    JIRA user“s name, if required

  • password (String) (defaults to: ENV["JIRA_PASSWORD"])

    JIRA user“s password, if required

  • logger (Logger) (defaults to: default_logger)

    used to log message (defaults to a logger to STDOUT at info level)

  • notifier (Notifier) (defaults to: default_notifier)

    a notifier instance that will be used to publish event notifications (see ‘JiraCache::Notifier` for more information)



31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#loggerObject (readonly)

Other possible fields: names, schema, operations, editmeta



18
19
20
# File 'lib/jira_cache/client.rb', line 18

def logger
  @logger
end

#notifierObject (readonly)

Other possible fields: names, schema, operations, editmeta



18
19
20
# File 'lib/jira_cache/client.rb', line 18

def notifier
  @notifier
end

Instance Method Details

#authorization_prefixObject



142
143
144
145
# 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) ⇒ Object



101
102
103
104
105
106
# 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_loggerObject



147
148
149
150
151
152
# 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_notifierObject



154
155
156
157
# 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 = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
# 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

#infoObject

Returns an hash of info on the client



160
161
162
163
164
165
# File 'lib/jira_cache/client.rb', line 160

def info
  {
    domain: @domain,
    username: @username
  }
end

#issue_data(id_or_key) ⇒ Object

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ total, issues

Returns - total: [Int] the total number of issues in the query results

  • issues: [Array] array of issues in the response (max ‘JIRA_MAX_RESULTS`).

Returns:

  • (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`)



87
88
89
90
91
92
93
94
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
# 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) ⇒ Object



113
114
115
# 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) ⇒ Object



117
118
119
# File 'lib/jira_cache/client.rb', line 117

def project_data(id)
  do_get "/project/#{id}"
end

#projects_dataObject



121
122
123
# File 'lib/jira_cache/client.rb', line 121

def projects_data
  do_get "/project"
end

#uri(path) ⇒ Object

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



138
139
140
# File 'lib/jira_cache/client.rb', line 138

def uri(path)
  "https://#{authorization_prefix}#{@domain}/rest/api/2#{path}"
end