Skip to content

Instantly share code, notes, and snippets.

@vividtone
Created August 22, 2015 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vividtone/271a6a7ece6cfb5f565e to your computer and use it in GitHub Desktop.
Save vividtone/271a6a7ece6cfb5f565e to your computer and use it in GitHub Desktop.
CSVファイルを読み込んで、REST API経由でRedmineにウォッチャーを追加する
#!/usr/bin/env ruby
# encoding: utf-8
#
# CSVファイルを読み込んで、REST API経由でRedmineにウォッチャーを追加する
#
# CSVファイルの形式:
# チケットID番号,ユーザーID番号
#
# 実行方法:
# ruby add_redmine_watcher.rb csv_file_name
require 'rest-client' # gem install rest-client
require 'open-uri'
require 'json'
require 'csv'
REDMINE_USER = 'manager' # 「ウォッチャーの追加」権限が必要
REDMINE_PASSWORD = 'manager'
REDMINE_URL = 'http://redmine.example.jp'
line_no = 0
CSV.parse(ARGF.read) do |row|
line_no += 1
(issue_id, user_id) = row
begin
RestClient::Request.execute(
:method => :post,
:url => "#{REDMINE_URL}/issues/#{issue_id}/watchers.json",
:user => REDMINE_USER,
:password => REDMINE_PASSWORD,
:payload => {:user_id => user_id}.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}: data = #{row.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment