Skip to content

Instantly share code, notes, and snippets.

@vividtone
Created March 27, 2015 02:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vividtone/15ed9b221d4cbd54d744 to your computer and use it in GitHub Desktop.
Save vividtone/15ed9b221d4cbd54d744 to your computer and use it in GitHub Desktop.
CSVファイルを読み込んでRedmineにREST API経由でユーザーを登録する
#!/usr/bin/env ruby
# encoding: utf-8
#
# CSVファイルを読み込んで、REST API経由でRedmineのユーザーを作成する
#
# CSVファイルの形式:
# ログインID,パスワード,名,姓,メールアドレス
require 'rest-client' # gem install rest-client
require 'open-uri'
require 'json'
require 'csv'
require 'kconv'
REDMINE_USER = 'admin' # システム管理者でなければならない
REDMINE_PASSWORD = 'admin'
REDMINE_URL = 'http://redmine-trunk.dev/'
REDMINE_QUERY_SIZE = 100
csvdata = Kconv::kconv(ARGF.read, Kconv::UTF8)
line_no = 0
CSV.parse(csvdata) do |row|
line_no += 1
userdata = {
:login => row[0],
:password => row[1],
:firstname => row[2],
:lastname => row[3],
:mail => row[4],
}
begin
RestClient::Request.execute(
:method => :post,
:url => "#{REDMINE_URL}/users.json",
:user => REDMINE_USER,
:password => REDMINE_PASSWORD,
:payload => {:user => userdata}.to_json,
:headers => {
:content_type => 'application/json'
}
)
STDERR.puts "#{ARGF.filename}:#{line_no}: success"
rescue => e
STDERR.puts "#{ARGF.filename}:#{line_no}: error: #{e.message}: #{userdata.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment