diff --git a/.github/workflows/docs-gb-update-file-references.yaml b/.github/workflows/docs-gb-update-file-references.yaml new file mode 100644 index 0000000000..654ff991b6 --- /dev/null +++ b/.github/workflows/docs-gb-update-file-references.yaml @@ -0,0 +1,32 @@ +name: docs-gb-update-file-references + +on: + pull_request: + branches: + - 'release-**' + types: + - opened + - reopened + - edited + +jobs: + update-docs-gb-file-references: + # if: github.repository == 'SeldonIO/seldon-core' # Do not run this on forks. + runs-on: ubuntu-latest + permissions: + pull-requests: read + contents: write + steps: + - name: Setup repository + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + - run: | + make docs-gb-update-file-references + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git add . + git commit -m "Update file references in docs" + git push + env: + RELEASE_BRANCH_REF: ${{ github.base_ref }} diff --git a/hack/update_references.py b/hack/update_references.py new file mode 100644 index 0000000000..608aa4bc64 --- /dev/null +++ b/hack/update_references.py @@ -0,0 +1,78 @@ +import os +import re +import subprocess +from typing import List, Tuple + +def get_markdown_files(directory: str) -> List[str]: + """ + Recursively find all markdown files in the given directory and all sub-directories. + """ + markdown_files = [] + for root, _, files in os.walk(directory): + for file in files: + if file.endswith('.md'): + markdown_files.append(os.path.join(root, file)) + return markdown_files + +def get_current_git_tag() -> str: + return subprocess.check_output( + ['git', 'describe', '--tags', '--abbrev=0'] + ).decode('utf-8').strip() + +def get_target_git_branch() -> str: + head_name = subprocess.check_output( + ['git', 'rev-parse', '--abbrev-ref HEAD'] + ).decode('utf-8').strip() + return os.environ.get('RELEASE_BRANCH_REF', head_name) + +def update_urls(content: str, new_version: str) -> Tuple[str, int]: + """ + Update URLs in the content with the new version. + Returns the updated content and the number of replacements made. + """ + pattern = r'(https://github\.com/SeldonIO/seldon-core/blob/v)(\d+(?:\.\d+)*)(/.+)' + + def replace_version(match): + prefix = match.group(1) + old_version = match.group(2) + suffix = match.group(3) + return f"{prefix}{new_version[1:]}{suffix}" + + updated_content, count = re.subn(pattern, replace_version, content) + return updated_content, count + +def process_file(file_path: str, new_version: str) -> int: + """ + Process a single markdown file, updating URLs if necessary. + Returns the number of replacements made. + """ + with open(file_path, 'r') as file: + content = file.read() + + updated_content, count = update_urls(content, new_version) + + if count > 0: + with open(file_path, 'w') as file: + file.write(updated_content) + + return count + +def main(): + codebase_dir = './docs-gb' # GitBook directory, change if needed + new_version = get_target_git_branch() + + markdown_files = get_markdown_files(codebase_dir) + total_replacements = 0 + + for file_path in markdown_files: + replacements = process_file(file_path, new_version) + if replacements > 0: + print(f"Updated {replacements} URL(s) in {file_path}") + total_replacements += replacements + + print(f"\nTotal replacements made: {total_replacements}") + print(f"New version: {new_version}") + +if __name__ == "__main__": + main() +