Skip to content

Commit

Permalink
Merge pull request #49 from treasure-data/fix_wrong_credential
Browse files Browse the repository at this point in the history
Add a logic to check credentials before searching issues
  • Loading branch information
thangnc committed Nov 14, 2017
2 parents d0e028a + f4af064 commit 5b6d35e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/embulk/input/jira.rb
Expand Up @@ -62,6 +62,9 @@ def self.guess(config)
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|
Expand Down Expand Up @@ -93,6 +96,8 @@ def init

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
Expand Down Expand Up @@ -136,6 +141,8 @@ def logger
private

def preview
@jira.check_user_credential(task[:username])

logger.debug "For preview mode, JIRA input plugin fetches records at most #{PREVIEW_RECORDS_COUNT}"
@jira.search_issues(@jql, max_results: PREVIEW_RECORDS_COUNT).each do |issue|
values = @attributes.map do |(attribute_name, type)|
Expand Down
16 changes: 14 additions & 2 deletions lib/embulk/input/jira_api/client.rb
Expand Up @@ -40,6 +40,19 @@ def total_count(jql)
search(jql, max_results: 1).num_results
end

def check_user_credential(username)
Jiralicious::Issue.find(username)
rescue Jiralicious::JqlError, Jiralicious::AuthenticationError, Jiralicious::NotLoggedIn, Jiralicious::InvalidLogin => e
raise Embulk::ConfigError.new(e.message)
rescue ::SocketError => e
# wrong `uri` option given
raise Embulk::ConfigError.new(e.message)
rescue MultiJson::ParseError => e
html = e.message
title = html[%r|<title>(.*?)</title>|, 1] #=> e.g. "Unauthorized (401)"
raise ConfigError.new("Can not authorize with your credential.") if title == 'Unauthorized (401)'
end

private

def timeout_and_retry(wait, retry_times = DEFAULT_SEARCH_RETRY_TIMES, &block)
Expand All @@ -61,13 +74,12 @@ def timeout_and_retry(wait, retry_times = DEFAULT_SEARCH_RETRY_TIMES, &block)
title = html[%r|<title>(.*?)</title>|, 1] #=> e.g. "Unauthorized (401)"
if title
# (a)
Embulk.logger.warn "JIRA returns HTML: #{html}"
case title
when "Atlassian Cloud Notifications - Page Unavailable"
# a.k.a. HTTP 503
raise title
when "Unauthorized (401)"
Embulk.logger.warn "JIRA returns error: #{title}"
Embulk.logger.warn "JIRA returns error: #{title}. Will go to retry"
count += 1
retry
end
Expand Down
5 changes: 5 additions & 0 deletions spec/embulk/input/jira_spec.rb
Expand Up @@ -131,6 +131,7 @@
end

before do
allow(jira_api).to receive(:check_user_credential).with(username).and_return(username)
allow(jira_api).to receive(:search_issues).with(jql, max_results: described_class::GUESS_RECORDS_COUNT).and_return(jira_issues)

allow(config).to receive(:param).with(:username, :string).and_return(username)
Expand Down Expand Up @@ -211,12 +212,14 @@
end

let(:commit_report) { {} }
let(:username) { "jira-user" }

before do
allow(jira_api).to receive(:preview?).and_return(false)

# TODO: create stubs without each `it` expected
allow(Embulk::Input::JiraApi::Client).to receive(:setup).and_return(jira_api)
allow(jira_api).to receive(:check_user_credential).and_return(username)

0.step(total_count, max_result) do |start_at|
issues = jira_issues[start_at..(start_at + max_result - 1)]
Expand Down Expand Up @@ -256,6 +259,7 @@
let(:page_builder) { double("page_builder") }
let(:jira_api) { Embulk::Input::JiraApi::Client.new }
let(:jira_issues) { [Embulk::Input::JiraApi::Issue.new(attributes)] }
let(:username) { "jira-user" }
let(:attributes) do
{
"id" => "100",
Expand All @@ -281,6 +285,7 @@
allow(Embulk::Input::JiraApi::Client).to receive(:setup).and_return(jira_api)
allow(plugin).to receive(:logger).and_return(::Logger.new(File::NULL))
allow(plugin).to receive(:preview?).and_return(true)
allow(jira_api).to receive(:check_user_credential).and_return(username)
allow(jira_api).to receive(:search_issues).and_return(jira_issues)
allow(page_builder).to receive(:add)
allow(page_builder).to receive(:finish)
Expand Down

0 comments on commit 5b6d35e

Please sign in to comment.