class Xfel::Timew::Jira

Class for REST interaction with Jira servers.

Attributes

worklogs[R]

Public Class Methods

new(worklog) click to toggle source
# File lib/xfel/timew/jira.rb, line 15
def initialize(worklog)
  @start = worklog[:start]
  @duration = worklog[:duration]
  @key = worklog[:key]
  return unless ENV['XFEL_JIRA_SYNC']

  if ENV['JIRA_HOST'] && ENV['JIRA_USER'] && ENV['JIRA_PASS']
    @uri = "#{ENV['JIRA_HOST']}/rest/api/2/issue/#{@key}/worklog"
    sync
  else
    log 'Missing required env vars: JIRA_HOST, JIRA_USER, JIRA_PASS'
  end
end

Public Instance Methods

duplicated?(worklog) click to toggle source
# File lib/xfel/timew/jira.rb, line 62
def duplicated?(worklog)
  @start == worklog['started']
end
execute(req, uri) click to toggle source
# File lib/xfel/timew/jira.rb, line 66
def execute(req, uri)
  req.basic_auth ENV['JIRA_USER'], ENV['JIRA_PASS']
  Net::HTTP.start(uri.hostname, uri.port, { use_ssl: true }) { |http| http.request(req) }
end
fetch_worklogs() click to toggle source
# File lib/xfel/timew/jira.rb, line 71
def fetch_worklogs
  uri = URI(@uri)
  res = execute(Net::HTTP::Get.new(uri), uri)
  unless req_success?(res)
    log "Error getting worklogs. #{res.code}: #{res.msg}"
    exit
  end
  self.class.worklogs[@key] = JSON.parse(res.body)['worklogs']
end
log(msg) click to toggle source
# File lib/xfel/timew/jira.rb, line 29
def log(msg)
  puts "#{@key} | #{msg}"
end
req_for_sync() click to toggle source
# File lib/xfel/timew/jira.rb, line 52
def req_for_sync
  uri = URI("#{@uri}?#{vars}")
  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/json'
  req.body = {
    comment: '', started: @start, timeSpentSeconds: @duration
  }.to_json
  execute(req, uri)
end
req_success?(response) click to toggle source
# File lib/xfel/timew/jira.rb, line 47
def req_success?(response)
  code_int = response.code.to_i
  code_int > 199 && code_int < 300
end
sync() click to toggle source
# File lib/xfel/timew/jira.rb, line 37
def sync
  log "#{@start} sync..."
  if worklogs.any? { |w| duplicated?(w) }
    log "#{@start} already present, skipping it."
  else
    res = req_for_sync
    log "#{@start} error: #{res.code}. #{res.body}" unless req_success?(res)
  end
end
vars() click to toggle source
# File lib/xfel/timew/jira.rb, line 33
def vars
  'notifyUsers=false&adjustEstimate=leave&overrideEditableFlag=true'
end
worklogs() click to toggle source
# File lib/xfel/timew/jira.rb, line 81
def worklogs
  fetch_worklogs unless self.class.worklogs[@key]
  self.class.worklogs[@key]
end