diff --git a/.github/workflows/preview_comment.yaml b/.github/workflows/preview_comment.yaml new file mode 100644 index 0000000000..a856e21de4 --- /dev/null +++ b/.github/workflows/preview_comment.yaml @@ -0,0 +1,58 @@ +name: "Post preview links for changed files" + +on: + pull_request: ~ + workflow_call: + inputs: + project: + description: 'The project to build (dev doc, user doc)' + default: '' + required: false + type: string + +jobs: + post-preview-links: + name: Post preview links for changed files + runs-on: ubuntu-latest + permissions: + # Needed to manage the comment + pull-requests: write + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Create comment for changed files + run: | + file_limit=100 + build_url="https://ez-systems-developer-documentation--${{ github.event.pull_request.number }}.com.readthedocs.build/${{inputs.project}}en/${{ github.event.pull_request.number }}/" + + change_list=$(git diff --name-only HEAD "origin/$GITHUB_BASE_REF" | grep -E "^docs\/.*\.md$" | sed -E "s|^docs/(.*)\.md$|- [docs/\1.md](${build_url}\1/)|") + change_count=$(echo "$change_list" | wc -l) + + if [[ $change_list -eq '' ]] ; then + comment="Preview of modified Markdown: no Markdown change to preview." + elif [[ $change_count -gt $file_limit ]] ; then + comment="Preview of modified Markdown: Too many files modified in a single PR, preview link list is skipped. ($change_count files >h; $file_limit)" + else + comment="Preview of modified Markdown:\n\n$change_list" + fi + + echo -e "$comment" > comment.md + + - name: Find comment + id: find-comment + uses: peter-evans/find-comment@v3 + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: 'Preview of modified Markdown' + + - name: Create or update comment + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body-path: comment.md + edit-mode: replace