-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_controller.rb
More file actions
99 lines (90 loc) · 3.78 KB
/
migration_controller.rb
File metadata and controls
99 lines (90 loc) · 3.78 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require_relative "page"
require "json"
require "rest-client"
require "cgi"
require "redcarpet"
map_of_id = {}
missing_links = Hash.new
main_ancestor_id = 86832612
space_key = ENV['SPACE_KEY']
username = ENV['CONFLUENCE_USERNAME']
password = ENV['CONFLUENCE_PASSWORD']
confluence_base_url = ENV['CONFLUENCE_BASE_URL']
testrail_base_url = ENV['TESTRAIL_BASE_URL']
data = JSON.parse(File.read("section_names.json"))
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
data.each do |section|
begin
id = section["id"]
title = section["name"]
ancestor = section["parent_id"]? map_of_id[section["parent_id"].to_s] : main_ancestor_id
description = markdown.render(CGI::escape_html(section["description"])) if section["description"]
page = Page.new
content = page.create_section(title, ancestor, description, space_key)
response = RestClient.post("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content/", content.to_json, :content_type => :json, :accept => :json)
parsed = JSON.parse(response)
map_of_id[id.to_s] = parsed["id"]
rescue RestClient::Exception => ex
if ex.http_code == 400
title = CGI::escape(title)
puts "#{title}: #{ex.http_body}"
response = RestClient.get("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content?spaceKey=#{space_key}&title=#{title}&expand=space,body.view,version,container")
parsed = JSON.parse(response.body)
map_of_id[id.to_s] = parsed["results"][0]["id"]
next
end
puts "#{title}: #{ex.message}"
next
end
end
file = File.read("test_cases.json")
data = JSON.parse(file)
data.each do |test_case|
begin
id = test_case["id"]
title = test_case["title"]
ancestor = test_case["section_id"]? map_of_id[test_case["section_id"].to_s] : main_ancestor_id
page = Page.new
content = page.create_section(title, ancestor, "", space_key)
response = RestClient.post("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content/", content.to_json, :content_type => :json, :accept => :json)
parsed = JSON.parse(response)
map_of_id[id.to_s] = parsed["id"]
rescue RestClient::Exception => ex
if ex.http_code == 400
title = CGI::escape(title)
response = RestClient.get("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content?spaceKey=#{space_key}&title=#{title}&expand=space,body.view,version,container")
parsed = JSON.parse(response.body)
map_of_id[id.to_s] = parsed["results"][0]["id"]
next
end
puts title
puts ex.http_body
next
end
end
puts map_of_id
puts "Switching to updating"
data.each do |test_case|
begin
id = map_of_id[test_case["id"].to_s]
title = test_case["title"]
objective = test_case["custom_testcase_objective"]
preconds = test_case["custom_preconds"]
case_steps = test_case["custom_steps_separated"]
response = RestClient.get("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content?spaceKey=#{space_key}&title=#{CGI::escape(title)}&expand=space,body.view,version,container")
parsed = JSON.parse(response.body)
version = parsed["results"][0]["version"]["number"] + 1
page = Page.new
content = page.create_case(id, title, version, objective, preconds, case_steps, missing_links, map_of_id, space_key, username, password, confluence_base_url, testrail_base_url)
response = RestClient.put("https://#{username}:#{password}@#{confluence_base_url}/wiki/rest/api/content/#{id}", content.to_json, :content_type => :json, :accept => :json)
parsed = JSON.parse(response)
map_of_id[id.to_s] = parsed["id"]
rescue RestClient::Exception => ex
puts "#{title} in rescue"
puts ex.http_body
if ex.http_code == 400
next
end
end
end
puts "These are the dead links: #{missing_links}"