diff --git a/.github/workflows/diff-rendered-charts.yml b/.github/workflows/diff-rendered-charts.yml index 94685d3..a91289a 100644 --- a/.github/workflows/diff-rendered-charts.yml +++ b/.github/workflows/diff-rendered-charts.yml @@ -3,7 +3,7 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # Reusable workflow to render and diff helm charts between the head and base ref of a pull request -# A comment is made on the pull request containing the diff output +# The diff output is posted as a job summary name: render and diff helm charts on: @@ -123,58 +123,51 @@ jobs: done env: CHARTS: ${{ needs.get_changed_helm_charts.outputs.charts }} - - name: post diff as comment on pull request + - name: post diff as job summary + id: post_diff if: needs.get_changed_helm_charts.outputs.charts != '' - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + run: | + if [ ! -f diff.log ]; then + echo "diff.log not found" + exit 0 + fi + max_size=$((1024 * 1024)) + file_size=$(stat --format=%s diff.log) + if [ "$file_size" -gt "$max_size" ]; then + echo "diff_too_large=true" >> "$GITHUB_OUTPUT" + echo "Changes found in Helm charts." >> "$GITHUB_STEP_SUMMARY" + echo '' >> "$GITHUB_STEP_SUMMARY" + echo "Diff output exceeds 1MiB and is too large to display inline. Download the \`helm-diff\` artifact to view the full diff." >> "$GITHUB_STEP_SUMMARY" + else + echo "diff_too_large=false" >> "$GITHUB_OUTPUT" + echo "Changes found in Helm charts." >> "$GITHUB_STEP_SUMMARY" + echo '
Show Output' >> "$GITHUB_STEP_SUMMARY" + echo '' >> "$GITHUB_STEP_SUMMARY" + echo '```diff' >> "$GITHUB_STEP_SUMMARY" + cat diff.log >> "$GITHUB_STEP_SUMMARY" + echo '```' >> "$GITHUB_STEP_SUMMARY" + echo '
' >> "$GITHUB_STEP_SUMMARY" + fi + + - name: upload diff artifact + if: steps.post_diff.outputs.diff_too_large == 'true' + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f #v6.0.0 with: - script: | - const fs = require('fs'); - const comment_char_limit = 65536; // GitHub comment character limit - const diff_file = 'diff.log'; - - if (fs.existsSync(diff_file)) { - var diff = fs.readFileSync(diff_file, 'utf8'); - } else { - console.log(diff_file + " not found") - return - } - - function splitComment(comment, maxSize, sepEnd, sepStart, comStart) { - // Adapted from Atlantis SplitComment function - // https://github.com/runatlantis/atlantis/blob/main/server/events/vcs/common/common.go#L18 - if (comment.length <= (comment_char_limit - comStart.length)) { - return [comStart + diff] - } - maxWithSep = comment_char_limit - sepEnd.length - sepStart.length; - var comments = []; - var numComments = Math.ceil(comment.length / maxWithSep); - for (var i = 0; i < numComments; i++) { - var upTo = Math.min(comment.length, (i + 1) * maxWithSep); - var portion = comment.slice(i * maxWithSep, upTo); - if (i < numComments - 1) { - portion += sepEnd; - } - if (i > 0) { - portion = sepStart + portion - } else { - portion = comStart + portion - } - comments.push(portion); - } - return comments; - } - - var sepEnd = "\n```\n" + "\n
\n\n**Warning**: Output length greater than max comment size. Continued in next comment."; - var sepStart = "Continued from previous comment.\n
Show Output\n\n" + "```diff\n"; - var comStart = "Changes found in Helm charts.\n
Show Output\n\n" + "```diff\n"; - - comments = splitComment(diff, comment_char_limit, sepEnd, sepStart, comStart); - - for (const comment of comments) { - await github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: comment - }) - } + name: helm-diff + path: diff.log + + - name: comment on pull request + if: steps.post_diff.outputs.diff_too_large != '' + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + DIFF_TOO_LARGE: ${{ steps.post_diff.outputs.diff_too_large }} + PR_NUMBER: ${{ github.event.number }} + run: | + run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" + if [ "$DIFF_TOO_LARGE" = "true" ]; then + body="Helm chart changes detected. The diff is too large for the job summary. Download the \`helm-diff\` artifact from the [workflow run](${run_url})." + else + body="Helm chart changes detected. View the diff in the [job summary](${run_url})." + fi + gh pr comment "$PR_NUMBER" --body "$body"