Created
March 27, 2015 02:56
-
-
Save vividtone/15ed9b221d4cbd54d744 to your computer and use it in GitHub Desktop.
CSVファイルを読み込んでRedmineにREST API経由でユーザーを登録する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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