forked from demisto/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_notes.py
590 lines (431 loc) · 15.6 KB
/
release_notes.py
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import abc
import datetime
import json
import sys
import yaml
from Tests.test_utils import print_error
contentLibPath = "./"
limitedVersion = False
NEW_RN = "New"
MODIFIED_RN = "Improved"
LAYOUT_TYPE_TO_NAME = {
"details": "Summary",
"edit": "New/Edit",
"close": "Close",
}
INTEGRATIONS_DIR = "Integrations"
SCRIPTS_DIR = "Scripts"
PLAYBOOKS_DIR = "Playbooks"
REPORTS_DIR = "Reports"
DASHBOARDS_DIR = "Dashboards"
WIDGETS_DIR = "Widgets"
INCIDENT_FIELDS_DIR = "IncidentFields"
LAYOUTS_DIR = "Layouts"
CLASSIFIERS_DIR = "Classifiers"
REPUTATIONS_DIR = "Misc"
RELEASE_NOTES_ORDER = [INTEGRATIONS_DIR, SCRIPTS_DIR, PLAYBOOKS_DIR, REPORTS_DIR,
DASHBOARDS_DIR, WIDGETS_DIR, INCIDENT_FIELDS_DIR, LAYOUTS_DIR,
CLASSIFIERS_DIR, REPUTATIONS_DIR]
def add_dot(text):
if text.endswith('.'):
return text
return text + '.'
def release_notes_item(header, body):
return '- __' + header + '__\n' + add_dot(body) + '\n'
class Content:
__metaclass__ = abc.ABCMeta
def __init__(self):
self.modified_store = [] # holds modified file paths
self.added_store = [] # holds added file paths
self.deleted_store = [] # holds deleted file paths
self.show_secondary_header = True
def add(self, change_type, data):
if change_type == "M":
self.modified_store.append(data)
elif change_type == "A":
self.added_store.append(data)
elif change_type == "D":
self.deleted_store.append(data)
else:
print "Unknown change type " + change_type
@abc.abstractmethod
def get_header(self):
return
@abc.abstractmethod
def added_release_notes(self, data):
return
@abc.abstractmethod
def modified_release_notes(self, data):
return
@abc.abstractmethod
def load_data(self, data):
return
# create a release notes section for store (add or modified) - return None if found missing release notes
def release_notes_section(self, store, title_prefix):
res = ""
missing_rn = False
if len(store) > 0:
new_str = ""
new_count = 0
for path in store:
with open(path, 'r') as f:
print " - adding release notes (%s) for file - [%s]... " % (path, title_prefix),
raw_content = f.read()
cnt = self.load_data(raw_content)
if title_prefix == NEW_RN:
ans = self.added_release_notes(cnt)
elif title_prefix == MODIFIED_RN:
ans = self.modified_release_notes(cnt)
else:
# should never get here
print_error("Error:\n Unknown release notes type" % (title_prefix,))
return None
if ans is None:
print_error("Error:\n[%s] is missing releaseNotes entry" % (path,))
missing_rn = True
elif ans:
new_count += 1
new_str += ans
print "Success"
else:
print "Skipped"
if len(new_str) > 0:
if self.show_secondary_header:
count_str = ""
if new_count > 1:
count_str = " " + str(new_count)
res = "\n#### %s %s %s\n" % (count_str, title_prefix, self.get_header())
res += new_str
if missing_rn:
return None
return res
def generate_release_notes(self):
res = ""
if len(self.modified_store) + len(self.deleted_store) + len(self.added_store) > 0:
print "starting %s RN" % (self.get_header(),)
# Added files
add_rn = self.release_notes_section(self.added_store, NEW_RN)
# Modified files
modified_rn = self.release_notes_section(self.modified_store, MODIFIED_RN)
if add_rn is None or modified_rn is None:
return None
section_body = add_rn + modified_rn
# Deleted files
if len(self.deleted_store) > 0:
section_body += "\n##### Removed " + self.get_header() + "\n"
for name in self.deleted_store:
print " - adding release notes (Removed) for - [%s]" % (name,),
section_body += "- __" + name + "__\n"
print "Success"
if len(section_body) > 0:
res = "### " + self.get_header() + "\n"
res += section_body
return res
class ScriptContent(Content):
def load_data(self, data):
return yaml.safe_load(data)
def get_header(self):
return "Scripts"
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) > 0 and rn == "-":
return ""
return release_notes_item(cnt["name"], cnt["comment"])
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["name"], rn)
return res
Content.register(ScriptContent)
class PlaybookContent(Content):
def load_data(self, data):
return yaml.safe_load(data)
def get_header(self):
return "Playbooks"
def added_release_notes(self, cnt):
rn = cnt.get("description")
if not rn:
return None
if rn == "-":
return ""
return release_notes_item(cnt["name"], rn)
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["name"], rn)
return res
Content.register(PlaybookContent)
class ReportContent(Content):
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Reports"
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) > 0 and rn == "-":
return ""
return release_notes_item(cnt["name"], cnt["description"])
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["name"], rn)
return res
Content.register(ReportContent)
class DashboardContent(Content):
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Dashboards"
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) > 0 and rn == "-":
return ""
return release_notes_item(cnt["name"], cnt["description"])
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["name"], rn)
return res
Content.register(DashboardContent)
class WidgetContent(Content):
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Widgets"
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) > 0 and rn == "-":
return ""
return release_notes_item(cnt["name"], cnt["description"])
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["name"], rn)
return res
Content.register(WidgetContent)
class IncidentFieldContent(Content):
def __init__(self):
super(IncidentFieldContent, self).__init__()
self.show_secondary_header = False
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Incident Fields"
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
return add_dot(rn) + "\n"
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = add_dot(rn) + "\n"
return res
Content.register(IncidentFieldContent)
class LayoutContent(Content):
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Incident Layouts"
@staticmethod
def get_release_notes(cnt):
rn = cnt.get("releaseNotes", "")
layout_kind = LAYOUT_TYPE_TO_NAME.get(cnt.get("kind", ""))
if not layout_kind:
print_error("invalid layout kind %s" % (cnt.get("kind", ""),))
return None
layout_type = cnt.get("typeId")
if not layout_type:
print_error("invalid layout kind %s" % (layout_type,))
return None
return release_notes_item(layout_type + " - " + layout_kind, rn)
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
return LayoutContent.get_release_notes(cnt)
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
if rn == "-":
return ""
return LayoutContent.get_release_notes(cnt)
Content.register(LayoutContent)
class ClassifierContent(Content):
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Classification & Mapping"
@staticmethod
def get_release_notes(cnt):
rn = cnt.get("releaseNotes", "")
brand_name = cnt.get("brandName")
if not brand_name:
print_error("invalid classifier brand name %s" % (brand_name,))
return None
return release_notes_item(brand_name, rn)
def added_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
return ClassifierContent.get_release_notes(cnt)
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
if rn == "-":
return ""
return ClassifierContent.get_release_notes(cnt)
Content.register(ClassifierContent)
class ReputationContent(Content):
def __init__(self):
super(ReputationContent, self).__init__()
self.show_secondary_header = False
def load_data(self, data):
return json.loads(data)
def get_header(self):
return "Reputations"
def added_release_notes(self, cnt):
# This should never happen
return ""
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = add_dot(rn) + "\n"
return res
Content.register(ReputationContent)
class IntegrationContent(Content):
def load_data(self, data):
return yaml.safe_load(data)
def get_header(self):
return "Integrations"
def added_release_notes(self, cnt):
return release_notes_item(cnt["display"], cnt["description"])
def modified_release_notes(self, cnt):
rn = cnt.get("releaseNotes", "")
if len(rn) == 0:
return None
res = ""
if rn != '-':
res = release_notes_item(cnt["display"], rn)
return res
Content.register(IntegrationContent)
release_note_generator = {
INTEGRATIONS_DIR: IntegrationContent(),
SCRIPTS_DIR: ScriptContent(),
PLAYBOOKS_DIR: PlaybookContent(),
REPORTS_DIR: ReportContent(),
DASHBOARDS_DIR: DashboardContent(),
WIDGETS_DIR: WidgetContent(),
INCIDENT_FIELDS_DIR: IncidentFieldContent(),
LAYOUTS_DIR: LayoutContent(),
CLASSIFIERS_DIR: ClassifierContent(),
REPUTATIONS_DIR: ReputationContent()
}
def parse_change_list(file_path):
with open(file_path, 'r') as f:
data = f.read()
return data.split("\n")
def get_deleted_content(full_file_name, data):
start_index = data.find(full_file_name)
if start_index > 0:
name_index = data.find("-name:", start_index)
if name_index > 0:
return data[name_index:].split("\n")[0][len("-name:"):].strip()
return full_file_name
def handle_deleted_file(delete_file_path, full_file_name):
with open(delete_file_path, 'r') as f:
data = f.read()
if "/" in full_file_name:
file_type = full_file_name.split("/")[0]
file_type_mapping = release_note_generator.get(file_type)
deleted_content = get_deleted_content(full_file_name, data)
if file_type_mapping is not None:
file_type_mapping.add("D", deleted_content)
def create_file_release_notes(file_name, delete_file_path):
if len(file_name) > 0:
names = file_name.split("\t")
change_type = names[0]
full_file_name = names[1]
if not "/" in full_file_name:
return
file_type = full_file_name.split("/")[0]
file_type_mapping = release_note_generator.get(file_type)
if file_type_mapping is None:
print "Unsupported file type " + file_type
return
if change_type == "D":
handle_deleted_file(delete_file_path, full_file_name)
elif change_type != "R100" and change_type != "R094":
if change_type == "R093" or change_type == "R098" or change_type == "R078":
# handle the same as modified
full_file_name = names[2]
change_type = 'M'
file_type_mapping.add(change_type, contentLibPath + full_file_name)
def create_content_descriptor(version, asset_id, res):
# time format example 2017 - 06 - 11T15:25:57.0 + 00:00
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.0+00:00")
release_notes = "## Demisto Content Release Notes for version " + version + " (" + asset_id + ")\n"
release_notes += "##### Published on %s\n%s" % (datetime.datetime.now().strftime("%d %B %Y"), res)
content_descriptor = {
"installDate": "0001-01-01T00:00:00Z",
"assetId": int(asset_id),
"releaseNotes": release_notes,
"modified": date,
"ignoreGit": False,
"releaseDate": date,
"version": -1,
"release": version,
"id": ""
}
with open('content-descriptor.json', 'w') as outfile:
json.dump(content_descriptor, outfile)
with open('release-notes.txt', 'w') as outfile:
outfile.write(release_notes)
def main(argv):
if len(argv) < 4:
print "<Release version>, <File with the full list of changes>, " \
"<Complete diff file for deleted files>, <assetID>"
sys.exit(1)
files = parse_change_list(argv[1])
for file in files:
create_file_release_notes(file, argv[2])
res = []
missing_release_notes = False
for key in RELEASE_NOTES_ORDER:
value = release_note_generator[key]
ans = value.generate_release_notes()
if ans is None:
missing_release_notes = True
elif len(ans) > 0:
res.append(ans)
if missing_release_notes:
sys.exit(1)
version = argv[0]
asset_id = argv[3]
release_notes = "\n---\n".join(res)
create_content_descriptor(version, asset_id, release_notes)
if __name__ == "__main__":
main(sys.argv[1:])