Class: JiraGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/jirametrics/jira_gateway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_system:) ⇒ JiraGateway

Returns a new instance of JiraGateway.



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

def initialize file_system:
  @file_system = file_system
end

Instance Attribute Details

#ignore_ssl_errorsObject

Returns the value of attribute ignore_ssl_errors.



8
9
10
# File 'lib/jirametrics/jira_gateway.rb', line 8

def ignore_ssl_errors
  @ignore_ssl_errors
end

#jira_urlObject

Returns the value of attribute jira_url.



8
9
10
# File 'lib/jirametrics/jira_gateway.rb', line 8

def jira_url
  @jira_url
end

Instance Method Details

#call_command(command) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/jirametrics/jira_gateway.rb', line 28

def call_command command
  @file_system.log "  #{command.gsub(/\s+/, ' ')}"
  result = `#{command}`
  @file_system.log result unless $CHILD_STATUS.success?
  return result if $CHILD_STATUS.success?

  @file_system.log "Failed call with exit status #{$CHILD_STATUS.exitstatus}."
  raise "Failed call with exit status #{$CHILD_STATUS.exitstatus}. " \
    "See #{@file_system.logfile_name} for details"
end

#call_url(relative_url:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jirametrics/jira_gateway.rb', line 14

def call_url relative_url:
  command = make_curl_command url: "#{@jira_url}#{relative_url}"
  result = call_command command
  begin
    json = JSON.parse(result)
  rescue # rubocop:disable Style/RescueStandardError
    raise "Error when parsing result: #{result.inspect}"
  end

  raise "Download failed with: #{JSON.pretty_generate(json)}" unless json_successful?(json)

  json
end

#json_successful?(json) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/jirametrics/jira_gateway.rb', line 71

def json_successful? json
  return false if json.is_a?(Hash) && (json['error'] || json['errorMessages'] || json['errorMessage'])
  return false if json.is_a?(Array) && json.first == 'errorMessage'

  true
end

#load_jira_config(jira_config) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jirametrics/jira_gateway.rb', line 39

def load_jira_config jira_config
  @jira_url = jira_config['url']
  raise 'Must specify URL in config' if @jira_url.nil?

  @jira_email = jira_config['email']
  @jira_api_token = jira_config['api_token']
  @jira_personal_access_token = jira_config['personal_access_token']

  raise 'When specifying an api-token, you must also specify email' if @jira_api_token && !@jira_email

  if @jira_api_token && @jira_personal_access_token
    raise "You can't specify both an api-token and a personal-access-token. They don't work together."
  end

  @cookies = (jira_config['cookies'] || []).collect { |key, value| "#{key}=#{value}" }.join(';')
end

#make_curl_command(url:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jirametrics/jira_gateway.rb', line 56

def make_curl_command url:
  command = +''
  command << 'curl'
  command << ' -L' # follow redirects
  command << ' -s' # silent
  command << ' -k' if @ignore_ssl_errors # insecure
  command << " --cookie #{@cookies.inspect}" unless @cookies.empty?
  command << " --user #{@jira_email}:#{@jira_api_token}" if @jira_api_token
  command << " -H \"Authorization: Bearer #{@jira_personal_access_token}\"" if @jira_personal_access_token
  command << ' --request GET'
  command << ' --header "Accept: application/json"'
  command << " --url \"#{url}\""
  command
end