Skip to content

Instantly share code, notes, and snippets.

@vividtone
Last active March 8, 2018 06:02
Show Gist options
  • Save vividtone/712ff4a8d5ece4f606b1 to your computer and use it in GitHub Desktop.
Save vividtone/712ff4a8d5ece4f606b1 to your computer and use it in GitHub Desktop.
RedmineからREST APIを使ってユーザーの一覧を取得しCSV形式で出力する
#!/usr/bin/env ruby
# encoding: utf-8
#
# RedmineからREST APIを使ってユーザーの一覧を取得しCSV形式で出力する
#
require 'open-uri'
require 'json'
require 'csv'
REDMINE_USER = 'admin' # システム管理者でなければならない
REDMINE_PASSWORD = 'admin'
REDMINE_URL = 'http://example.com/redmine'
REDMINE_QUERY_SIZE = 100
offset = 0
while true
io = open("#{REDMINE_URL}/users.json?limit=#{REDMINE_QUERY_SIZE}&offset=#{offset}", {:http_basic_authentication => [REDMINE_USER, REDMINE_PASSWORD]})
users_json = JSON.load(io.read)
users = users_json["users"]
break if users.size == 0
csv_string = CSV.generate do |csv|
csv << users[0].keys if offset == 0
users.each do |u|
csv << u.values
end
end
puts csv_string
offset += REDMINE_QUERY_SIZE
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment