-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlaunchpad.save
More file actions
334 lines (286 loc) · 10.5 KB
/
launchpad.save
File metadata and controls
334 lines (286 loc) · 10.5 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require 'xmlsimple'
require 'rdiscount'
require 'rest-client'
require 'json'
require 'pathname'
class Launchpad
@my_server = 'developer-uat.motorolasolutions.com/api/core/v3'
@my_user = ENV['user']
@my_pass = ENV['password']
@current_env = ENV['server'].nil? ? "uat" : ENV['server']
#parent places are per folder
@servers = {
"uat" =>{
"server" => "developer-uat.motorolasolutions.com/api/core/v3",
"parent_places" => {
"api" => "https://developer-uat.motorolasolutions.com/api/core/v3/places/18127",
"guide" => "https://developer-uat.motorolasolutions.com/api/core/v3/places/18127"
"guide/tutorial/" => "https://developer.motorolasolutions.com/api/core/v3/places/38904",
}
},
"prod" =>{
"server" => "developer.motorolasolutions.com/api/core/v3",
"parent_places" => {
"api" => "https://developer.motorolasolutions.com/api/core/v3/places/38904",
"guide" => "https://developer.motorolasolutions.com/api/core/v3/places/38904"
"guide/tutorial" => "https://developer.motorolasolutions.com/api/core/v3/places/38904",
}
}
}
@my_server = @servers[@current_env]["server"]
def self.generate_html(topic,parent_source)
#open Markdown content
md = File.read(topic)
#pre-pend LP specific code
html = "<div class='tableofcontents'> </div>"
# Add Markdown converted to HTML
html += RDiscount.new(md, :smart).to_html
# Reformat code blocks
# html = html.gsub(/<pre><code>(.*?)<\/code><\/pre>/m) do |m|
# m.gsub("<pre><code>","<pre class=\"jive_text_macro jive_macro_code\" jivemacro=\"code\" ___default_attr=\"javascript\" _jivemacro_uid=\"_1398870592774641\">").gsub("</code></pre>","</pre>")
# end
html.gsub!(/<pre><code>:::(\w+)\s*(\n|
)/, '<pre name="code" class="\1">')
html.gsub!('</code></pre>','</pre>')
# reformat image links
# write html file
basename = topic.gsub(parent_source,'')
parent_output = Pathname(parent_source).each_filename.to_a.last
outputfile = "#{AppConfig['launchpad_eb']}#{parent_output}/#{basename.gsub!('.md','.html')}"
File.open("#{outputfile}", 'w') {|f|
f.write(html)
puts outputfile
}
end
def self.replace_url (topic,url_map,env)
# find all a href="" and decide if it should be changed
# read file contents
html = File.read(topic)
matched = false
html_mod = html.gsub(/<a href="(.*?)"/m) do |m|
match = $1
index_key = $1
# if starts with ../ then use the string minus the ../ for the index
if index_key.start_with?('../')
index_key.gsub!('../','')
end
# otherwise use the match for the lookup
if url_map[index_key].nil?
# do nothing, myable external url or one we do not know
m
else
# if exisrts in mapping but is blank then we need to create it
if url_map[index_key]["url"][env] == ""
puts "\nERROR: #{index_key} missing url"
m
else
# get the lookup for the real LP url
# then replace the a href tag with the lookup
matched = true
newurl = url_map[index_key]["url"][env]
m.gsub(match,url_map[index_key]["url"][env])
end
end
end
if matched
puts "\nReposting #{topic}"
publish_html(topic,html_mod,url_map,env)
end
end
def self.publish_html(topic,html,url_map,env)
# read file contents
warnings = [] #missing titles or unable to post
newfiles = [] #file is a new post to Launchpad
missingfiles = [] #file is missed from mapping
# index_key = parentfolder/filename with no extension
index_key = topic.gsub(AppConfig['launchpad_eb'],'').gsub('.html','')
if url_map[index_key].nil?
#need to create doc in LP and also update mapping
create_doc = true
else
create_doc = false
# if exisrts in mapping but is blank then we need to create it
if url_map[index_key]["id"][env] == ""
create_doc = true
end
end
if html.match(/<h1>(.*)<\/h1>/).nil?
puts ("WARNING: No H1 Title #{topic}")
title = File.basename(topic).gsub('.html','')
else
title = html.match(/<h1>(.*)<\/h1>/)[1]
#remove H1 as Launchpad auto puts in H1 for Doc title
html.gsub!(/<h1>(.*)<\/h1>/,'')
end
# create REST JSON Body
jdata = {
:visibility => 'place',
:subject => title,
:content => {
:type => 'text/html',
:text => html
},
:type => 'document',
:parent => @servers[@current_env]["parent_places"][Pathname(index_key).each_filename.to_a.first]
}.to_json
begin
if create_doc
# puts "Creating"
rest_method = "post"
# Jive V3 Endpoint
endpoint = "https://#{@my_user}:#{@my_pass}@#{@my_server}/contents"
else
# puts "Updating"
rest_method = "put"
# Jive V3 Endpoint
endpoint = url_map[index_key]["id"][env].gsub("https://","https://#{@my_user}:#{@my_pass}@")
end
# Create Doc
response = RestClient::Request.execute :method=> rest_method, :url => endpoint,
:headers => {'Content-Type' => 'application/json'},
:payload => jdata
#response.code = 201 is success, else parsed.message = error message, parsed.code
parsed = JSON.parse(response)
#Jive REST API for Document Update
url_map[index_key]["id"][env] = parsed["resources"]["self"]["ref"]
#Jive URL to document
url_map[index_key]["url"][env] = parsed["resources"]["html"]["ref"]
# puts url_map[index_key]
outputfile = "#{AppConfig['launchpad_eb']}#{AppConfig['launchpad_eb_mapping']}"
File.open("#{outputfile}", 'w') {|f|
f.write(url_map.to_s)
}
# puts "Success: ID => #{@documentId}"
rescue => e
puts "\nERROR: #{topic}:#{e} - check #{topic}.error for JSON - File may have malformed HTML - check for Amperstand escaping"
File.open(topic+'.error','w'){|f|
f.write(jdata)
}
end
end
def self.delete_doc(topic,url_map,env)
# read file contents
html = File.read(topic)
warnings = [] #missing titles or unable to post
newfiles = [] #file is a new post to Launchpad
missingfiles = [] #file is missed from mapping
index_key = topic.gsub(AppConfig['launchpad_eb'],'').gsub('.html','')
if url_map[index_key].nil?
#need to create doc in LP and also update mapping
new_doc = true
else
new_doc = false
# if exisrts in mapping but is blank then we need to create it
if url_map[index_key]["id"][env] == ""
new_doc = true
end
end
begin
if new_doc
#just ignore it
else
# puts "Updating"
rest_method = "delete"
# Jive V3 Endpoint
endpoint = url_map[index_key]["id"][env].gsub("https://","https://#{@my_user}:#{@my_pass}@")
# Delete Doc
response = RestClient::Request.execute :method=> rest_method, :url => endpoint,
:headers => {'Content-Type' => 'application/json'}
#response.code = 201 is success, else parsed.message = error message, parsed.code
# if response.code == 201
#Jive REST API for Document Update
url_map[index_key]["id"][env] = ""
#Jive URL to document
url_map[index_key]["url"][env] = ""
# puts url_map[index_key]
outputfile = "#{AppConfig['launchpad_eb']}#{AppConfig['launchpad_eb_mapping']}"
File.open("#{outputfile}", 'w') {|f|
f.write(url_map.to_s)
}
# else
# puts "ERROR: #{topic}:#{response.code}#{response.message}"
# end
end
rescue => e
puts "\nERROR: #{topic}:#{e.response}"
response = JSON.parse(e.response.to_s)
if response["error"]["status"] == 404
# then doc does not exist on LP so clear mapping anyway
url_map[index_key]["id"][env] = ""
#Jive URL to document
url_map[index_key]["url"][env] = ""
# puts url_map[index_key]
outputfile = "#{AppConfig['launchpad_eb']}#{AppConfig['launchpad_eb_mapping']}"
File.open("#{outputfile}", 'w') {|f|
f.write(url_map.to_s)
}
end
end
end
# Used to created baseline hash object stored in file
# to be used to track mapping of files created on launchpad
def self.generate_mapping_index
warning = ""
index_hash = {}
puts "Getting MD in #{AppConfig['api_eb']}"
puts "Generating JSON Index: #{AppConfig['launchpad_eb']}#{AppConfig['launchpad_eb_mapping']}"
apiMD = File.join(AppConfig['api_eb'],"**","*.md")
apiFiles = Dir.glob(apiMD)
apiFiles.each do |fileName|
# use parent folder/name as index key
basename = fileName.gsub(AppConfig['api_eb'],'')
parent = Pathname(fileName.gsub(basename,'')).each_filename.to_a.last
puts "Parent: #{parent}"
index_key = parent + "/" + fileName.gsub(AppConfig['api_eb'],'').gsub('.md','')
md = File.read(fileName)
puts "Processing API: #{index_key}"
hash_object = {
"id" =>
{
"uat" => "",
"prod" => ""
},
"url" =>
{
"uat" => "",
"prod" => ""
}
}
index_hash[index_key] = hash_object
end
guidesMD = File.join(AppConfig['guides_eb'],"**","*.md")
guidesFiles = Dir.glob(guidesMD)
guidesFiles.each do |fileName|
# use parent folder/name as index key
basename = fileName.gsub(AppConfig['guides_eb'],'')
parent = Pathname(fileName.gsub(basename,'')).each_filename.to_a.last
index_key = parent + "/" + fileName.gsub(AppConfig['guides_eb'],'').gsub('.md','')
md = File.read(fileName)
puts "Processing Guide: #{index_key}"
hash_object = {
"id" =>
{
"uat" => "",
"prod" => ""
},
"url" =>
{
"uat" => "",
"prod" => ""
}
}
index_hash[index_key] = hash_object
end
outputfile = "#{AppConfig['launchpad_eb']}#{AppConfig['launchpad_eb_mapping']}"
if File.file?(outputfile)
puts "**** WARNING: previous mapping existed - last one saved in #{AppConfig['launchpad_eb_mapping']}.backup"
#save copy
outputfile_backup = outputfile + '.backup'
File.rename(outputfile,outputfile_backup)
end
File.open("#{outputfile}", 'w') {|f|
f.write(index_hash.to_s)
}
puts "Finished"
puts warning
end
end