diff --git a/.github/workflows/build-codemirror.yml b/.github/workflows/build-codemirror.yml index 2e5b31d..dcc5e49 100644 --- a/.github/workflows/build-codemirror.yml +++ b/.github/workflows/build-codemirror.yml @@ -23,9 +23,11 @@ on: required: true type: string default: "0.25.11" - -permissions: - contents: read + produce_pr_to_bump_lib: + description: "Produce a PR to update vendor/codemirror and geometry-editor.js" + required: false + type: boolean + default: false jobs: build: @@ -33,6 +35,10 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 + permissions: + contents: write + pull-requests: write + defaults: run: working-directory: build/codemirror @@ -47,6 +53,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v6 + with: + ref: main - name: Set up Node.js uses: actions/setup-node@v7 @@ -121,3 +129,71 @@ jobs: path: build/codemirror/dist/${{ steps.js.outputs.name }}.js if-no-files-found: error retention-days: 30 + + - name: Prepare vendor bundle and update component import + if: ${{ inputs.produce_pr_to_bump_lib }} + working-directory: . + env: + BUNDLE_NAME: ${{ steps.js.outputs.name }} + run: | + set -euo pipefail + + branch="automation/bump-codemirror-${BUNDLE_NAME}" + git fetch origin main + git switch --force-create "${branch}" origin/main + + source="build/codemirror/dist/${BUNDLE_NAME}.js" + target="vendor/codemirror/${BUNDLE_NAME}.js" + + test -s "${source}" + mkdir -p vendor/codemirror + cp "${source}" "${target}" + + python3 build/codemirror/scripts/update_component_import.py "${BUNDLE_NAME}" + + + - name: Create or update CodeMirror bump pull request + if: ${{ inputs.produce_pr_to_bump_lib }} + working-directory: . + env: + GH_TOKEN: ${{ github.token }} + BUNDLE_NAME: ${{ steps.js.outputs.name }} + run: | + set -euo pipefail + + branch="automation/bump-codemirror-${BUNDLE_NAME}" + + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + + git add \ + "vendor/codemirror/${BUNDLE_NAME}.js" \ + "lib/modules/components/geometry-editor.js" + + if git diff --cached --quiet; then + echo "::notice::The selected CodeMirror bundle is already present on main; no bump PR is needed." + exit 0 + fi + + git commit -m "Bump CodeMirror bundle to ${BUNDLE_NAME}" + git push --force origin "${branch}" + + if gh pr view "${branch}" --repo "${GITHUB_REPOSITORY}" >/dev/null 2>&1; then + echo "Updated existing pull request for ${branch}." + else + cat > "${RUNNER_TEMP}/pr-body.md" <<'EOF' + ## Summary + + - Add the generated CodeMirror bundle to `vendor/codemirror/`. + - Update the bundle import in `geometry-editor.js`. + + Generated by the **Build CodeMirror bundle** workflow. This pull request is intentionally left open for manual review and merging. + EOF + + gh pr create \ + --repo "${GITHUB_REPOSITORY}" \ + --base main \ + --head "${branch}" \ + --title "Bump CodeMirror bundle (${BUNDLE_NAME})" \ + --body-file "${RUNNER_TEMP}/pr-body.md" + fi diff --git a/build/codemirror/scripts/update_component_import.py b/build/codemirror/scripts/update_component_import.py new file mode 100644 index 0000000..57c6e4f --- /dev/null +++ b/build/codemirror/scripts/update_component_import.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Update the CodeMirror bundle import in the geometry editor component.""" + +import argparse +import re +from pathlib import Path + + +def update_import(bundle_name: str, component: Path) -> None: + """Point the component's CodeMirror import at the selected bundle.""" + if not re.fullmatch(r"[a-zA-Z0-9._-]+", bundle_name): + raise ValueError(f"Invalid bundle name: {bundle_name!r}") + + source = component.read_text(encoding="utf-8") + pattern = re.compile( + r'(?P\bfrom\s*)(?P["\'])' + r'/vendor/codemirror/[^"\']+\.js(?P=quote)' + ) + matches = list(pattern.finditer(source)) + + if len(matches) != 1: + raise ValueError( + f"Expected exactly one CodeMirror bundle import in {component}; " + f"found {len(matches)}." + ) + + match = matches[0] + quote = match.group("quote") + replacement = ( + f'{match.group("prefix")}{quote}' + f'/vendor/codemirror/{bundle_name}.js{quote}' + ) + updated = source[: match.start()] + replacement + source[match.end() :] + component.write_text(updated, encoding="utf-8") + print(f"Updated CodeMirror bundle import in {component}") + + +def main() -> None: + parser = argparse.ArgumentParser( + description="Update the geometry editor's CodeMirror bundle import." + ) + parser.add_argument("bundle_name", help="Bundle name, without the .js suffix") + parser.add_argument( + "--component", + type=Path, + default=Path("lib/modules/components/geometry-editor.js"), + help="Component file to update", + ) + args = parser.parse_args() + + try: + update_import(args.bundle_name, args.component) + except (OSError, ValueError) as exc: + parser.error(str(exc)) + + +if __name__ == "__main__": + main()