Preview README changes for 29/merge by @CRThaze #43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Preview README changes | |
| run-name: Preview README changes for ${{ github.ref_name }} by @${{ github.actor }} | |
| on: | |
| pull_request: | |
| paths: | |
| - charts/s3proxy/Chart.yaml | |
| - charts/s3proxy/values.yaml | |
| - README.md.gotmpl | |
| - README.md | |
| jobs: | |
| preview-readme: | |
| # This job checks out the head branch by name and posts the rendered README | |
| # diff back as a PR comment. Both of those require the head branch to live in | |
| # this repo and a write-scoped GITHUB_TOKEN. Neither holds for fork PRs: the | |
| # branch exists only in the fork (so `actions/checkout` with a bare `ref:` | |
| # fetches refs/heads/<branch> from this repo and fails), and the | |
| # pull_request event grants forks a read-only token (so createComment 403s). | |
| # Skip forks; internal branches still get the preview. | |
| if: github.event.pull_request.head.repo.full_name == github.repository | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 5 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.pull_request.head.ref }} | |
| - name: Save HEAD SHA before helm-docs | |
| id: save-sha | |
| run: | | |
| echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | |
| - name: Render helm docs inside the README.md | |
| uses: losisin/helm-docs-github-action@v1 | |
| with: | |
| git-push: false | |
| output-file: ../../README.md | |
| template-files: README.md.gotmpl | |
| - name: Check for README changes | |
| id: check-changes | |
| run: | | |
| if git diff --quiet ${{ steps.save-sha.outputs.sha }} -- README.md | |
| then | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Generate diff | |
| id: diff | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| run: | | |
| echo 'diff<<EOF' >> $GITHUB_OUTPUT | |
| git diff ${{ steps.save-sha.outputs.sha }} -- README.md >> $GITHUB_OUTPUT | |
| echo 'EOF' >> $GITHUB_OUTPUT | |
| - name: Find and hide outdated comments | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number | |
| }); | |
| const botComments = comments.data.filter(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('<!-- helm-docs-preview -->') | |
| ); | |
| for (const comment of botComments) { | |
| await github.graphql(` | |
| mutation { | |
| minimizeComment(input: { | |
| subjectId: "${comment.node_id}", | |
| classifier: OUTDATED | |
| }) { | |
| clientMutationId | |
| } | |
| } | |
| `); | |
| } | |
| - name: Post README changes as comment | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| uses: actions/github-script@v7 | |
| env: | |
| DIFF_CONTENT: ${{ steps.diff.outputs.diff }} | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| // Get the diff from the environment variable to avoid JavaScript syntax issues | |
| const diff = process.env.DIFF_CONTENT; | |
| // Use 4 backticks for the outer fence to safely contain any triple backticks in the diff | |
| const body = `<!-- helm-docs-preview --> | |
| ## 📝 README.md Preview | |
| The following changes to \`README.md\` will be applied when this PR is merged: | |
| <details> | |
| <summary>Click to expand diff</summary> | |
| \`\`\`\`diff | |
| ${diff} | |
| \`\`\`\` | |
| </details> | |
| > **Note**: This is an automated preview generated by \`helm-docs\`. The changes will be automatically applied upon merge.`; | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: body | |
| }); | |
| - name: Hide outdated comments when no changes | |
| if: steps.check-changes.outputs.has_changes == 'false' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number | |
| }); | |
| const botComments = comments.data.filter(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('<!-- helm-docs-preview -->') | |
| ); | |
| for (const comment of botComments) { | |
| await github.graphql(` | |
| mutation { | |
| minimizeComment(input: { | |
| subjectId: "${comment.node_id}", | |
| classifier: OUTDATED | |
| }) { | |
| clientMutationId | |
| } | |
| } | |
| `); | |
| } |