-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.rb
77 lines (62 loc) · 1.81 KB
/
import.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'net/http'
require 'active_support'
require 'csv'
csvFile = "data.csv"
host = '10.55.10.13'
port = '3000'
key = 'e3dad26b1b87228ca344506a87fa936b2444e569' # redmine api key
class RedmineImport
def initialize(host,port, key)
@host = host
@port = port
@key = key
end
def readCsv(filename)
@csvData = [];
CSV.foreach(filename, {
:col_sep => ';',
:headers => true
}) do |row|
@csvData.push(row)
end
end
def importIssues()
@csvData.each{ |row|
sendRequest('/issues.json', buildRequest(row))
}
end
def buildRequest(row)
body = ActiveSupport::JSON.encode({
:key => @key,
:issue => {
:project_id => 7,
:tracker_id => 6,
:status_id => 1,
#:priority_id => 0,
:subject => row['Subject'],
:description => '',
#:category_id => 0,
:assigned_to_id => 0,
:custom_fields => [
{ :id => 3, :value => row['E-Mail']}, # E-Mail
{ :id => 4, :value => row['Website']}, # Website
{ :id => 5, :value => row['Contact Person']}, # Contact Person
{ :id => 6, :value => row['Country']}, # Country
{ :id => 7, :value => 'Some contract information'}, # Contract Information
{ :id => 8, :value => row['Address']}, # Address
{ :id => 9, :value => row['Phone']} # Phone
]
}
})
return body
end
def sendRequest(path, data)
request = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
request.body = data
response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }
puts "Response #{response.code} #{response.message}: #{response.body}"
end
end
ri = RedmineImport.new(host,port,key)
ri.readCsv(csvFile)
ri.importIssues()