Skip to content

Commit 310170f

Browse files
author
Allen Bai
committed
coreos-release-note-generator: init script for generating release notes
This script generates Fedora CoreOS release note from pre-mature release note snippets. Takes two arguments: --config-dir and --output-dir. The config_dir is the directory where a 'release-notes.d' directory resides, typically a fedora-coreos-config directory, which has pre-mature release notes yaml snippets. The output_dir is the directory where a 'release-notes.yaml' resides. If there's no such file then a new file will be created. If a 'release-notes.yaml' already exists, then checks if the 'next-release' field is present in the file. It inserts the 'next-release' field to the front of the file if none is present or updates the existing 'next-release' field if one already exists. Related: coreos/fedora-coreos-tracker#194 Signed-off-by: Allen Bai <abai@redhat.com>
1 parent 96d915a commit 310170f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/python3 -u
2+
3+
import yaml
4+
import argparse
5+
import os
6+
import glob
7+
8+
# create the top-level parser
9+
parser = argparse.ArgumentParser()
10+
11+
# create the parser for the "build" command
12+
parser.add_argument(
13+
'--config-dir', help='FCOS config directory', required=True)
14+
parser.add_argument(
15+
'--output-dir', help='output directory for release-notes.yaml', required=True)
16+
args = parser.parse_args()
17+
18+
if not os.path.exists(args.config_dir):
19+
raise Exception(
20+
"config directory '{}' does not exist".format(args.config_dir))
21+
22+
if not os.path.exists(os.path.join(args.config_dir, 'release-notes.d')):
23+
raise Exception(
24+
"release-notes.d does not exist under {}".format(args.config_dir))
25+
26+
27+
if not os.path.exists(args.output_dir):
28+
raise Exception(
29+
"output directory '{}' does not exist".format(args.output_dir))
30+
31+
if not os.path.isdir(args.output_dir):
32+
raise Exception(
33+
"output path '{}' is not a directory".format(args.output_dir))
34+
35+
snippet_yaml_list = glob.glob(os.path.join(
36+
args.config_dir, 'release-notes.d/*.yaml'))
37+
if len(snippet_yaml_list) == 0:
38+
print("release-notes.d/ does not contain any yaml snippets under '{}'".format(args.config_dir))
39+
exit(0)
40+
41+
snippet_dict = dict()
42+
for snippet_yaml in snippet_yaml_list:
43+
with open(snippet_yaml, 'r') as f:
44+
basename = os.path.basename(snippet_yaml)
45+
project_name = os.path.splitext(basename)[0]
46+
snippet = yaml.load(f, Loader=yaml.FullLoader)
47+
snippet_dict[project_name] = snippet
48+
49+
for project in snippet_dict.copy():
50+
# filter out empty yaml file content
51+
if snippet_dict[project] is None:
52+
snippet_dict.pop(project, None)
53+
continue
54+
# eliminate note items with empty subject
55+
snippet_dict[project] = [
56+
note_item for note_item in snippet_dict[project] if note_item['subject']]
57+
# filter empty note items
58+
if len(snippet_dict[project]) == 0:
59+
snippet_dict.pop(project, None)
60+
61+
# write release-notes.yaml to the output directory
62+
outfile = os.path.join(args.output_dir, 'release-notes.yaml')
63+
# store list of release note dictionaries
64+
release_notes = []
65+
66+
if os.path.exists(outfile):
67+
print("{} already exists, checking 'next-release' field".format(outfile))
68+
with open(outfile, 'r') as f:
69+
release_notes = yaml.load(f, Loader=yaml.FullLoader)
70+
# checks if the release note already exists in the yaml file
71+
latest_note = release_notes[0] if len(release_notes) != 0 else dict()
72+
if snippet_dict == latest_note.get("next-release", dict()):
73+
print("'next-release' field already exists and no changes detected")
74+
exit(0)
75+
76+
with open(outfile, 'w') as w:
77+
# check if 'next-release' item already exists
78+
if len(release_notes) > 0 and "next-release" in release_notes[0]:
79+
print("'next-release' field updated for {}".format(outfile))
80+
release_notes[0]["next-release"] = snippet_dict
81+
else:
82+
# else insert the new release notes at the front of the list
83+
release_notes.insert(0, {"next-release": snippet_dict})
84+
yaml.dump(release_notes, w)
85+
print(f"Successfully wrote release note file at {outfile}")

0 commit comments

Comments
 (0)