redmine-to-gitea/redmine_api.rb

170 lines
3.6 KiB
Ruby

require 'rest-client'
require 'json'
require 'pry'
module Redmine
class API
attr_accessor :api_key
attr_accessor :base_url
attr_accessor :http_headers
attr_accessor :rest_client
def initialize(api_key:, base_url:, rest_client: RestClient)
@api_key = api_key
@base_url = base_url
@http_headers = {
"X-Redmine-API-Key" => api_key,
"Content-Type" => "application/json",
"Accept" => "application/json"
}
@rest_client = rest_client
end
def get_issues(project_id:, offset: 0, limit: 25)
puts "Redmine::API.get_issues(project_id: #{project_id}, offset: #{offset}, limit: #{limit})"
response = rest_client.get(
"#{base_url}/issues.json?project_id=#{project_id}&offset=#{offset}&limit=#{limit}&status_id=*",
http_headers
)
result = JSON.parse(response.body)
ResultSet.new(
results: result.fetch("issues").map { |issue| Issue.new(issue) },
total: result.fetch("total_count"),
offset: result.fetch("offset"),
limit: result.fetch("limit")
)
rescue => ex
binding.pry
end
def get_issue(id:)
puts "Redmine::API.get_issue(id: #{id})"
response = rest_client.get(
"#{base_url}/issues/#{id}.json?include=journals",
http_headers
)
result = JSON.parse(response.body)
Issue.new(result.fetch("issue"))
rescue => ex
binding.pry
end
end
class ResultSet
attr_reader :results
attr_reader :total
attr_reader :offset
attr_reader :limit
def initialize(results:, total:, offset:, limit:)
@results = results
@total = total
@offset = offset
@limit = limit
end
def remaining_results?
(offset + limit) < total
end
end
class Issue
attr_reader :issue
def initialize(issue)
@issue = issue
end
def id
issue.fetch("id")
end
def project_name
issue.fetch("project", {}).fetch("name")
end
def tracker_name
issue.fetch("tracker", {}).fetch("name")
end
def status_id
issue.fetch("status", {}).fetch("id")
end
def status_name
issue.fetch("status", {}).fetch("name")
end
def category_name
if issue.key?("category")
issue.fetch("category", {}).fetch("name")
end
end
def author_id
issue.fetch("author", {}).fetch("id")
end
def author_name
issue.fetch("author", {}).fetch("name")
end
def assigned_to
if issue.key?("assigned_to")
issue.fetch("assigned_to").fetch("id")
end
end
def start_date
issue.fetch("start_date")
end
def done_ratio
issue.fetch("done_ratio")
end
def created_on
issue.fetch("created_on")
end
def updated_on
issue.fetch("updated_on")
end
def subject
issue.fetch("subject")
end
def description
issue.fetch("description")
end
def journals
if issue.key?("journals")
issue.fetch("journals", {}).map { |journal|
Journal.new(journal)
}
else
[]
end
end
end
class Journal
attr_reader :journal
def initialize(journal)
@journal = journal
end
def user_id
journal.fetch("user", {}).fetch("id")
end
def user_name
journal.fetch("user", {}).fetch("name")
end
def created_on
journal.fetch("created_on")
end
def details
journal.fetch("details", [])
end
def notes
journal.fetch("notes", "")
end
def private_notes?
journal.fetch("private_notes", false)
end
end
end