Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created October 21, 2018 00:39
Show Gist options
  • Save hpneo/da124fac396b6246d6003485d41fbe93 to your computer and use it in GitHub Desktop.
Save hpneo/da124fac396b6246d6003485d41fbe93 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'time_difference'
require 'awesome_print'
JIRA_EMAIL = '[email protected]'
TOKEN = '1234567890'
authorization_token = Base64.urlsafe_encode64("#{JIRA_EMAIL}:#{TOKEN}")
headers = {
"Authorization" => "Basic #{authorization_token}",
"Accept" => "application/json",
"Content-Type" => "application/json"
}
body = {
jql: 'sprint = "Sprint 17" AND status CHANGED TO "QA" ON "2018/10/19" AND status = "QA"',
# fields: [ 'id', 'name' ]
# fields: [ '*navigable', 'status' ],
# expand: [ 'transitions', 'changelog' ]
}.to_json
response = HTTParty.post("https://runahr.atlassian.net/rest/api/2/search", body: body, headers: headers)
results = JSON.parse(response.body)
issue = results['issues'].last
response = HTTParty.get("#{issue['self']}/changelog", headers: headers)
results = JSON.parse(response.body)
all_changes = results['values']
status_changes = all_changes.select { |change| change['items'].any? { |item| item['field'] == 'status' } }.map { |change|
change['items'].select { |item|
item['field'] == 'status'
}.map do |item|
{
author: change['author']['displayName'],
from_id: item['from'],
from: item['fromString'],
to_id: item['to'],
to: item['toString'],
created_at: Time.parse(change['created']),
}
end
}.flatten
status_changes = status_changes.map.with_index do |change, index|
if index == 0
time_spent = TimeDifference.between(Time.parse(issue['fields']['created']), change[:created_at])
change.merge({ time_spent: time_spent.humanize.downcase, time_spent_in_seconds: time_spent.in_seconds })
else
time_spent = TimeDifference.between(status_changes[index - 1][:created_at], change[:created_at])
change.merge({ time_spent: time_spent.humanize.downcase, time_spent_in_seconds: time_spent.in_seconds })
end
end
total_time_spent = TimeDifference.between(Time.parse(issue['fields']['created']), status_changes.last[:created_at])
puts "Card: #{issue['key']} - #{issue['fields']['summary']}"
status_changes.each do |change|
puts "#{change[:author]} moved from #{change[:from]} to #{change[:to]} in #{change[:time_spent]}"
end
puts "Runa spent #{total_time_spent.humanize.downcase} in this card"
# response = HTTParty.get("https://runahr.atlassian.net/rest/api/2/workflow", headers: headers)
# ap JSON.parse(response.body).select { |workflow| workflow['name'] == "Runa's new workflow" }
# response = HTTParty.get("https://runahr.atlassian.net/rest/api/2/status", headers: headers)
# ap JSON.parse(response.body)
# response = HTTParty.get("https://runahr.atlassian.net/rest/api/2/project/", headers: headers)
# projects = JSON.parse(response.body)
# statuses_by_project = projects.inject({}) do |hash, project|
# response = HTTParty.get("#{project['self']}/statuses", headers: headers)
# hash[project['key']] = JSON.parse(response.body).map { |issue_type|
# issue_type['statuses']
# }.flatten.map { |status|
# {
# id: status['id'],
# name: status['name']
# }
# }.uniq
# hash
# end
# ap statuses_by_project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment