Class: Embulk::Input::Jira

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/jira.rb

Constant Summary collapse

PER_PAGE =
50
GUESS_RECORDS_COUNT =
10
PREVIEW_RECORDS_COUNT =
15

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guess(config) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/embulk/input/jira.rb', line 44

def self.guess(config)
  username = config.param(:username, :string)
  password = config.param(:password, :string)
  uri = config.param(:uri, :string)
  jql = config.param(:jql, :string)

  jira = JiraApi::Client.setup do |jira_config|
    # TODO: api_version should be 2 (the latest version)
    # auth_type should be specified from config. (The future task)

    jira_config.username = username
    jira_config.password = password
    jira_config.uri = uri
    jira_config.api_version = "latest"
    jira_config.auth_type = "basic"
  end

  retry_limit = config.param(:retry_limit, :integer, default: 5)
  retry_initial_wait_sec = config.param(:retry_initial_wait_sec, :integer, default: 1)
  retryer = retryer(retry_limit, retry_initial_wait_sec)

  # Get credential before going to search issue
  jira.check_user_credential(username)

  # TODO: we use 0..10 issues to guess config?
  records = retryer.with_retry do
    jira.search_issues(jql, max_results: GUESS_RECORDS_COUNT).map do |issue|
      issue.to_record
    end
  end

  columns = JiraInputPluginUtils.guess_columns(records)

  guessed_config = {
    "columns" => columns,
  }

  return guessed_config
end

.loggerObject



123
124
125
# File 'lib/embulk/input/jira.rb', line 123

def self.logger
  Embulk.logger
end

.resume(task, columns, count, &control) ⇒ Object



37
38
39
40
41
42
# File 'lib/embulk/input/jira.rb', line 37

def self.resume(task, columns, count, &control)
  task_reports = yield(task, columns, count)

  next_config_diff = {}
  return next_config_diff
end

.retryer(limit, initial_wait) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/embulk/input/jira.rb', line 127

def self.retryer(limit, initial_wait)
  PerfectRetry.new do |config|
    config.limit = limit
    config.sleep = proc{|n| initial_wait + (2 ** n)}
    config.dont_rescues = [Embulk::ConfigError, Embulk::DataError]
    config.logger = Embulk.logger
    config.log_level = nil
  end
end

.transaction(config, &control) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/embulk/input/jira.rb', line 14

def self.transaction(config, &control)
  task = {
    username: config.param(:username, :string),
    password: config.param(:password, :string),
    uri: config.param(:uri, :string),
    jql: config.param(:jql, :string),
  }

  attributes = {}
  columns = config.param(:columns, :array).map do |column|
    name = column["name"]
    type = column["type"].to_sym
    attributes[name] = type
    Column.new(nil, name, type, column["format"])
  end

  task[:attributes] = attributes
  task[:retry_limit] = config.param(:retry_limit, :integer, default: 5)
  task[:retry_initial_wait_sec] = config.param(:retry_initial_wait_sec, :integer, default: 1)

  resume(task, columns, 1, &control)
end

Instance Method Details

#initObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/embulk/input/jira.rb', line 84

def init
  @attributes = task[:attributes]
  @jira = JiraApi::Client.setup do |config|
    config.username = task[:username]
    config.password = task[:password]
    config.uri = task[:uri]
    config.api_version = "latest"
    config.auth_type = "basic"
  end
  @jql = task[:jql]
  @retryer = self.class.retryer(task[:retry_limit], task[:retry_initial_wait_sec])
end

#loggerObject



137
138
139
# File 'lib/embulk/input/jira.rb', line 137

def logger
  self.class.logger
end

#runObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/embulk/input/jira.rb', line 97

def run
  return preview if preview?

  @jira.check_user_credential(task[:username])
  options = {}
  total_count = @jira.total_count(@jql)
  last_page = (total_count.to_f / PER_PAGE).ceil

  0.step(total_count, PER_PAGE).with_index(1) do |start_at, page|
    logger.debug "Fetching #{page} / #{last_page} page"
    @retryer.with_retry do
      @jira.search_issues(@jql, options.merge(start_at: start_at)).each do |issue|
        values = @attributes.map do |(attribute_name, type)|
          JiraInputPluginUtils.cast(issue[attribute_name], type)
        end
        page_builder.add(values)
      end
    end
  end

  page_builder.finish

  task_report = {}
  return task_report
end