diff --git a/.github/workflows/docs_bundle.yml b/.github/workflows/docs_bundle.yml index a04d1bb4..b2d85018 100644 --- a/.github/workflows/docs_bundle.yml +++ b/.github/workflows/docs_bundle.yml @@ -1,79 +1,90 @@ name: Process Documentation on: - push: - branches: [main] - paths: - - 'tools/**' - - 'plugin-dev-en/**' - - 'plugin-dev-ja/**' - - 'plugin-dev-zh/**' - - 'en/**' - - 'ja-jp/**' - - 'zh_hans/**' - workflow_dispatch: + pull_request: + types: [opened, synchronize] + branches: [main] + workflow_dispatch: jobs: - process-docs: - runs-on: ubuntu-latest - permissions: - contents: write + process-docs: + runs-on: ubuntu-latest + permissions: + contents: write - steps: - - name: Checkout repository - uses: actions/checkout@v3 + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.10' + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pyyaml + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pyyaml - - name: Run documentation tools - id: doc-tools - run: python tools/main_docs_bundle.py + - name: Run documentation tools + id: doc-tools + run: python tools/main_docs_bundle.py - - name: Display results - run: | - echo "Execution results:" - echo "Successful operations: ${{ steps.doc-tools.outputs.success_count }}" - echo "Failed operations: ${{ steps.doc-tools.outputs.error_count }}" - if [ "${{ steps.doc-tools.outputs.detailed_message }}" != "" ]; then - echo "Details:" - echo "${{ steps.doc-tools.outputs.detailed_message }}" - fi + - name: Display results + run: | + echo "Execution results:" + echo "Successful operations: ${{ steps.doc-tools.outputs.success_count }}" + echo "Failed operations: ${{ steps.doc-tools.outputs.error_count }}" + if [ "${{ steps.doc-tools.outputs.detailed_message }}" != "" ]; then + echo "Details:" + echo "${{ steps.doc-tools.outputs.detailed_message }}" + fi - - name: Commit changes - if: always() - run: | - if [[ -n "$(git status --porcelain)" ]]; then - FINAL_COMMIT_SUBJECT="" - if [[ -n "${{ steps.doc-tools.outputs.commit_message }}" ]]; then - FINAL_COMMIT_SUBJECT="${{ steps.doc-tools.outputs.commit_message }}" - else - FINAL_COMMIT_SUBJECT="Docs: Automated documentation updates" - fi - - FINAL_COMMIT_BODY="" - if [[ -n "${{ steps.doc-tools.outputs.detailed_message }}" ]]; then - FINAL_COMMIT_BODY="${{ steps.doc-tools.outputs.detailed_message }}" - else - FINAL_COMMIT_BODY="No detailed information provided by the script." - fi - - git config --local user.email "88554920+alterxyz@users.noreply.github.com" - git config --local user.name "alterxyz" - git add . - git commit -m "$FINAL_COMMIT_SUBJECT" -m "$FINAL_COMMIT_BODY" || echo "No changes to commit" - git push origin HEAD:main - - echo "Changes committed with subject: $FINAL_COMMIT_SUBJECT" - echo "Description:" - echo "$FINAL_COMMIT_BODY" - else - echo "No file changes, skipping commit" - fi \ No newline at end of file + - name: Commit and Push changes (if applicable) + run: | + # Push only if the PR is coming from a branch within the same repository + # where the workflow is running (i.e., not a PR from an external fork targeting this repo). + # This covers: + # 1. PRs within the main repository. + # 2. PRs within a fork of the main repository (when this workflow runs in that fork). + if [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then + if [[ -n "$(git status --porcelain)" ]]; then + FINAL_COMMIT_SUBJECT="" + if [[ -n "${{ steps.doc-tools.outputs.commit_message }}" ]]; then + FINAL_COMMIT_SUBJECT="${{ steps.doc-tools.outputs.commit_message }}" + else + FINAL_COMMIT_SUBJECT="Docs: Apply automated formatting by CI" + fi + + FINAL_COMMIT_BODY="" + if [[ -n "${{ steps.doc-tools.outputs.detailed_message }}" ]]; then + FINAL_COMMIT_BODY="${{ steps.doc-tools.outputs.detailed_message }}" + else + FINAL_COMMIT_BODY="Automated changes by CI. This commit was made to the PR branch." + fi + + git config --local user.email "88554920+alterxyz@users.noreply.github.com" + git config --local user.name "alterxyz" + git add . + git commit -m "$FINAL_COMMIT_SUBJECT" -m "$FINAL_COMMIT_BODY" + # Push to the PR's head ref (the source branch of the PR) + git push origin HEAD:${{ github.head_ref }} + + echo "Formatting changes automatically committed and pushed to PR branch: ${{ github.head_ref }}" + echo "Commit Subject: $FINAL_COMMIT_SUBJECT" + echo "Description:" + echo "$FINAL_COMMIT_BODY" + else + echo "No file changes detected by script in this PR (source repo: ${{ github.event.pull_request.head.repo.full_name }}, target repo: ${{ github.repository }}). Nothing to commit or push." + fi + else + # This case covers PRs from truly external forks targeting this repository. + echo "PR is from an external fork ('${{ github.event.pull_request.head.repo.full_name }}' to '${{ github.repository }}'). Formatting changes will not be pushed to the PR branch." + if [[ -n "$(git status --porcelain)" ]]; then + echo "Note: The script identified formatting changes. These will be handled by other processes post-merge if necessary." + else + echo "No formatting changes were identified by the script in this externally forked PR." + fi + fi \ No newline at end of file