-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tool to generate release notes docs from yaml files of release …
…notes. Signed-off-by: lemonlinger <[email protected]>
- Loading branch information
1 parent
52af65e
commit 22a3d46
Showing
7 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/../.. | ||
|
||
venvPath="${SCRIPT_ROOT}/tools/bin/release-notes-docs.d/venv" | ||
|
||
source ${venvPath}/bin/activate | ||
python ${SCRIPT_ROOT}/tools/src/release-notes-docs/yml2md.py "$@" | ||
deactivate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyyaml==6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import yaml | ||
import os | ||
from datetime import datetime | ||
|
||
def change_to_markdown(change): | ||
return '\n'.join("- {}".format(line.strip()) for line in change.strip().split('\n')) | ||
|
||
def format_date(date_str): | ||
date_formats = ["%b %d, %Y", "%B %d, %Y"] | ||
for date_format in date_formats: | ||
try: | ||
return datetime.strptime(date_str, date_format).date() | ||
except ValueError: | ||
pass # If the format doesn't match, move to the next one | ||
|
||
raise ValueError(f"Date string '{date_str}' does not match any supported format.") | ||
|
||
def capitalize(name): | ||
fixed_mapping = { | ||
'ir': 'IR', | ||
'api': 'API', | ||
'xds': 'xDS', | ||
'ci-tooling-testing': 'CI Tooling Testing', | ||
} | ||
if name in fixed_mapping: | ||
return fixed_mapping[name] | ||
return name.capitalize() | ||
|
||
def convert_yaml_to_markdown(input_yaml_file, output_markdown_path): | ||
# Extract the title from the input file name | ||
title = os.path.basename(input_yaml_file).split('.yaml')[0] | ||
# Generate the filename of output markdown file. | ||
output_markdown_file=os.path.join(output_markdown_path,title + '.md') | ||
|
||
with open(input_yaml_file, 'r') as file: | ||
data = yaml.safe_load(file) | ||
|
||
with open(output_markdown_file, 'w') as file: | ||
file.write('---\n') | ||
file.write('title: "{}"\n'.format(title)) | ||
file.write("publishdate: {}\n".format(format_date(data['date']))) | ||
file.write('---\n\n') | ||
|
||
file.write("Date: {}\n\n".format(data['date'])) | ||
|
||
for area in data['changes']: | ||
file.write("## {}\n".format(capitalize(area['area']))) | ||
if 'change' in area: | ||
file.write(change_to_markdown(area['change']) + '\n\n') | ||
|
||
if 'breaking-change' in area: | ||
file.write("### Breaking Changes\n") | ||
file.write(change_to_markdown(area['breaking-change']) + '\n\n') | ||
|
||
print("Markdown file '{}' has been generated.".format(output_markdown_file)) | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 3: | ||
print("Usage: python yml2md.py <input_yaml_file> <output_markdown_path>") | ||
sys.exit(1) | ||
|
||
input_yaml_file = sys.argv[1] | ||
output_markdown_path = sys.argv[2] | ||
convert_yaml_to_markdown(input_yaml_file, output_markdown_path) |