From 3812cb5f5645a150bb1dd5053ec71b658d230062 Mon Sep 17 00:00:00 2001 From: Meng Date: Sat, 6 Jan 2024 01:22:39 +0800 Subject: [PATCH] feat(tools): Add a tool to generate release notes docs from yaml files of release notes (#2281) --- .../{v0.2.0-rc.1.md => v0.2.0-rc1.md} | 0 .../{v0.2.0-rc.2.md => v0.2.0-rc2.md} | 0 tools/make/docs.mk | 7 +- tools/make/tools.mk | 9 ++- .../release-notes-docs/release-notes-docs.sh | 13 ++++ tools/src/release-notes-docs/requirements.txt | 1 + tools/src/release-notes-docs/yml2md.py | 67 +++++++++++++++++++ 7 files changed, 94 insertions(+), 3 deletions(-) rename site/content/en/latest/releases/{v0.2.0-rc.1.md => v0.2.0-rc1.md} (100%) rename site/content/en/latest/releases/{v0.2.0-rc.2.md => v0.2.0-rc2.md} (100%) create mode 100755 tools/src/release-notes-docs/release-notes-docs.sh create mode 100644 tools/src/release-notes-docs/requirements.txt create mode 100755 tools/src/release-notes-docs/yml2md.py diff --git a/site/content/en/latest/releases/v0.2.0-rc.1.md b/site/content/en/latest/releases/v0.2.0-rc1.md similarity index 100% rename from site/content/en/latest/releases/v0.2.0-rc.1.md rename to site/content/en/latest/releases/v0.2.0-rc1.md diff --git a/site/content/en/latest/releases/v0.2.0-rc.2.md b/site/content/en/latest/releases/v0.2.0-rc2.md similarity index 100% rename from site/content/en/latest/releases/v0.2.0-rc.2.md rename to site/content/en/latest/releases/v0.2.0-rc2.md diff --git a/tools/make/docs.mk b/tools/make/docs.mk index 9a78e3032e3..fc61912f86a 100644 --- a/tools/make/docs.mk +++ b/tools/make/docs.mk @@ -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 @@ -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/; \ diff --git a/tools/make/tools.mk b/tools/make/tools.mk index edecb3b58f9..dda0fa26a41 100644 --- a/tools/make/tools.mk +++ b/tools/make/tools.mk @@ -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 diff --git a/tools/src/release-notes-docs/release-notes-docs.sh b/tools/src/release-notes-docs/release-notes-docs.sh new file mode 100755 index 00000000000..d7102f49da1 --- /dev/null +++ b/tools/src/release-notes-docs/release-notes-docs.sh @@ -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 diff --git a/tools/src/release-notes-docs/requirements.txt b/tools/src/release-notes-docs/requirements.txt new file mode 100644 index 00000000000..cf39afa6b4c --- /dev/null +++ b/tools/src/release-notes-docs/requirements.txt @@ -0,0 +1 @@ +pyyaml==6.0.1 diff --git a/tools/src/release-notes-docs/yml2md.py b/tools/src/release-notes-docs/yml2md.py new file mode 100755 index 00000000000..7b334a5cd54 --- /dev/null +++ b/tools/src/release-notes-docs/yml2md.py @@ -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 ") + 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)