|
| 1 | +name: Preview README changes |
| 2 | +run-name: Preview README changes for ${{ github.ref_name }} by @${{ github.actor }} |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - charts/s3proxy/Chart.yaml |
| 7 | + - charts/s3proxy/values.yaml |
| 8 | + - README.md.gotmpl |
| 9 | + - README.md |
| 10 | + |
| 11 | +jobs: |
| 12 | + preview-readme: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + timeout-minutes: 5 |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: ${{ github.event.pull_request.head.ref }} |
| 19 | + |
| 20 | + - name: Save HEAD SHA before helm-docs |
| 21 | + id: save-sha |
| 22 | + run: | |
| 23 | + echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |
| 24 | +
|
| 25 | + - name: Render helm docs inside the README.md |
| 26 | + uses: losisin/helm-docs-github-action@v1 |
| 27 | + with: |
| 28 | + git-push: false |
| 29 | + output-file: ../../README.md |
| 30 | + template-files: README.md.gotmpl |
| 31 | + |
| 32 | + - name: Check for README changes |
| 33 | + id: check-changes |
| 34 | + run: | |
| 35 | + if git diff --quiet ${{ steps.save-sha.outputs.sha }} -- README.md |
| 36 | + then |
| 37 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 38 | + else |
| 39 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Generate diff |
| 43 | + id: diff |
| 44 | + if: steps.check-changes.outputs.has_changes == 'true' |
| 45 | + run: | |
| 46 | + echo 'diff<<EOF' >> $GITHUB_OUTPUT |
| 47 | + git diff ${{ steps.save-sha.outputs.sha }} -- README.md >> $GITHUB_OUTPUT |
| 48 | + echo 'EOF' >> $GITHUB_OUTPUT |
| 49 | +
|
| 50 | + - name: Find and hide outdated comments |
| 51 | + if: steps.check-changes.outputs.has_changes == 'true' |
| 52 | + uses: actions/github-script@v7 |
| 53 | + with: |
| 54 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + script: | |
| 56 | + const comments = await github.rest.issues.listComments({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + issue_number: context.issue.number |
| 60 | + }); |
| 61 | +
|
| 62 | + const botComments = comments.data.filter(comment => |
| 63 | + comment.user.type === 'Bot' && |
| 64 | + comment.body.includes('<!-- helm-docs-preview -->') |
| 65 | + ); |
| 66 | +
|
| 67 | + for (const comment of botComments) { |
| 68 | + await github.graphql(` |
| 69 | + mutation { |
| 70 | + minimizeComment(input: { |
| 71 | + subjectId: "${comment.node_id}", |
| 72 | + classifier: OUTDATED |
| 73 | + }) { |
| 74 | + clientMutationId |
| 75 | + } |
| 76 | + } |
| 77 | + `); |
| 78 | + } |
| 79 | +
|
| 80 | + - name: Post README changes as comment |
| 81 | + if: steps.check-changes.outputs.has_changes == 'true' |
| 82 | + uses: actions/github-script@v7 |
| 83 | + env: |
| 84 | + DIFF_CONTENT: ${{ steps.diff.outputs.diff }} |
| 85 | + with: |
| 86 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + script: | |
| 88 | + // Get the diff from the environment variable to avoid JavaScript syntax issues |
| 89 | + const diff = process.env.DIFF_CONTENT; |
| 90 | + |
| 91 | + // Use 4 backticks for the outer fence to safely contain any triple backticks in the diff |
| 92 | + const body = `<!-- helm-docs-preview --> |
| 93 | + ## 📝 README.md Preview |
| 94 | +
|
| 95 | + The following changes to \`README.md\` will be applied when this PR is merged: |
| 96 | +
|
| 97 | + <details> |
| 98 | + <summary>Click to expand diff</summary> |
| 99 | +
|
| 100 | + \`\`\`\`diff |
| 101 | + ${diff} |
| 102 | + \`\`\`\` |
| 103 | +
|
| 104 | + </details> |
| 105 | +
|
| 106 | + > **Note**: This is an automated preview generated by \`helm-docs\`. The changes will be automatically applied upon merge.`; |
| 107 | +
|
| 108 | + await github.rest.issues.createComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + issue_number: context.issue.number, |
| 112 | + body: body |
| 113 | + }); |
| 114 | +
|
| 115 | + - name: Hide outdated comments when no changes |
| 116 | + if: steps.check-changes.outputs.has_changes == 'false' |
| 117 | + uses: actions/github-script@v7 |
| 118 | + with: |
| 119 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 120 | + script: | |
| 121 | + const comments = await github.rest.issues.listComments({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + issue_number: context.issue.number |
| 125 | + }); |
| 126 | +
|
| 127 | + const botComments = comments.data.filter(comment => |
| 128 | + comment.user.type === 'Bot' && |
| 129 | + comment.body.includes('<!-- helm-docs-preview -->') |
| 130 | + ); |
| 131 | +
|
| 132 | + for (const comment of botComments) { |
| 133 | + await github.graphql(` |
| 134 | + mutation { |
| 135 | + minimizeComment(input: { |
| 136 | + subjectId: "${comment.node_id}", |
| 137 | + classifier: OUTDATED |
| 138 | + }) { |
| 139 | + clientMutationId |
| 140 | + } |
| 141 | + } |
| 142 | + `); |
| 143 | + } |
0 commit comments