Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 47 additions & 54 deletions .github/workflows/diff-rendered-charts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 '<details><summary>Show Output</summary>' >> "$GITHUB_STEP_SUMMARY"
echo '' >> "$GITHUB_STEP_SUMMARY"
echo '```diff' >> "$GITHUB_STEP_SUMMARY"
cat diff.log >> "$GITHUB_STEP_SUMMARY"
echo '```' >> "$GITHUB_STEP_SUMMARY"
echo '</details>' >> "$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</details>" + "\n<br>\n\n**Warning**: Output length greater than max comment size. Continued in next comment.";
var sepStart = "Continued from previous comment.\n<details><summary>Show Output</summary>\n\n" + "```diff\n";
var comStart = "Changes found in Helm charts.\n<details><summary>Show Output</summary>\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"
Loading