Skip to content

Commit

Permalink
feat(tools): Add a tool to generate release notes docs from yaml file…
Browse files Browse the repository at this point in the history
…s of release notes (#2281)
  • Loading branch information
lemonlinger authored Jan 5, 2024
1 parent caf2ddb commit 3812cb5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
7 changes: 6 additions & 1 deletion tools/make/docs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docs: docs.clean helm-readme-gen docs-api docs-api-headings ## Generate Envoy Ga
cp tools/hack/get-egctl.sh $(DOCS_OUTPUT_DIR)

.PHONY: docs-release
docs-release: docs-release-prepare docs-release-gen docs ## Generate Envoy Gateway Release Docs
docs-release: docs-release-prepare release-notes-docs docs-release-gen docs ## Generate Envoy Gateway Release Docs

.PHONY: docs-serve
docs-serve: ## Start Envoy Gateway Site Locally
Expand Down Expand Up @@ -79,3 +79,8 @@ docs-release-gen:
@echo '[[params.versions]]' >> site/hugo.toml
@echo ' version = "$(TAG)"' >> site/hugo.toml
@echo ' url = "/$(TAG)"' >> site/hugo.toml

.PHONY: release-notes-docs
release-notes-docs: $(tools/release-notes-docs)
@$(LOG_TARGET)
$(tools/release-notes-docs) release-notes/$(TAG).yaml site/content/en/latest/releases/; \
9 changes: 7 additions & 2 deletions tools/make/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ $(tools.bindir)/%: $(tools.srcdir)/%/pin.go $(tools.srcdir)/%/go.mod
tools/codespell = $(tools.bindir)/codespell
tools/yamllint = $(tools.bindir)/yamllint
tools/sphinx-build = $(tools.bindir)/sphinx-build
tools/release-notes-docs = $(tools.bindir)/release-notes-docs
$(tools.bindir)/%.d/venv: $(tools.srcdir)/%/requirements.txt
mkdir -p $(@D)
python3 -m venv $@
$@/bin/pip3 install -r $< || (rm -rf $@; exit 1)
$(tools.bindir)/%: $(tools.bindir)/%.d/venv
ln -sf $*.d/venv/bin/$* $@
$(tools.bindir)/%: $(tools.bindir)/%.d/venv
@if [ -e $(tools.srcdir)/$*/$*.sh ]; then \
ln -sf ../../$(tools.srcdir)/$*/$*.sh $@; \
else \
ln -sf $*.d/venv/bin/$* $@; \
fi

ifneq ($(GOOS),windows)
# Shellcheck
Expand Down
13 changes: 13 additions & 0 deletions tools/src/release-notes-docs/release-notes-docs.sh
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
1 change: 1 addition & 0 deletions tools/src/release-notes-docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyyaml==6.0.1
67 changes: 67 additions & 0 deletions tools/src/release-notes-docs/yml2md.py
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)

0 comments on commit 3812cb5

Please sign in to comment.