Skip to content

Commit

Permalink
fix: Extended github script to split large diffs into multiple commen…
Browse files Browse the repository at this point in the history
…ts, using summary comments instead of full blocks, excluding empty files from diff comments
  • Loading branch information
dlactin committed Mar 6, 2024
1 parent 52c1e4d commit 381d4a5
Showing 1 changed file with 63 additions and 23 deletions.
86 changes: 63 additions & 23 deletions render-and-diff-helm/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
name: Matrix render and diff helm charts
description: Renders BASE and REF helm charts then posts the diff of the rendered charts as a comment on the pull request


on:
workflow_call:
inputs:
Expand Down Expand Up @@ -35,7 +36,7 @@ jobs:
git fetch origin ${{ github.base_ref }}:${{ github.base_ref }}
echo matrix_changed_charts=$(git diff --name-only ${{ github.base_ref }}...HEAD -- '**/k8s/**/*.yaml' '**/k8s/**/*.yml' '**/k8s/**/*.tpl' '**/k8s/**/*.tmpl' | cut -d'/' -f1,2,3 | uniq | jq -R 'split("\n")' | jq -s 'flatten(1)') >> $GITHUB_OUTPUT
echo changed_charts=$(git diff --name-only ${{ github.base_ref }}...HEAD -- '**/k8s/**/*.yaml' '**/k8s/**/*.yml' '**/k8s/**/*.tpl' '**/k8s/**/*.tmpl' | cut -d'/' -f1,2,3 | uniq) >> $GITHUB_OUTPUT
render_head_ref_charts:
runs-on: ubuntu-latest
needs: get_changed_helm_charts
Expand All @@ -54,13 +55,16 @@ jobs:
run: |
mkdir -p shared/head-charts
git fetch origin ${{ github.head_ref }}
git checkout ${{ github.head_ref }}
values_files="${{ matrix.chart }}"/values-*
for values_file in $(basename -a $values_files); do
helm template "${{ matrix.chart }}" -f "${{ matrix.chart }}/values.yaml" -f "${{ matrix.chart }}/${values_file}" --output-dir "shared/head-charts/${{ matrix.chart }}/${values_file}"
done
git checkout ${{ github.head_ref }} --
if [ -d "${{ matrix.chart }}" ]; then
helm dependency build "${{ matrix.chart }}"
values_files="${{ matrix.chart }}"/values-*
for values_file in $(basename -a $values_files); do
helm template "${{ matrix.chart }}" -f "${{ matrix.chart }}/values.yaml" -f "${{ matrix.chart }}/${values_file}" --output-dir "shared/head-charts/${{ matrix.chart }}/${values_file}"
done
fi
echo sanitized_name=$(echo "${{ matrix.chart }}" | sed 's/\//-/g') >> $GITHUB_OUTPUT
echo "${{ matrix.chart }} does not exist in ${{ github.base_ref }} ref"
- name: upload artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -85,13 +89,16 @@ jobs:
run: |
mkdir -p shared/base-charts
git fetch origin ${{ github.base_ref }}
git checkout ${{ github.base_ref }}
values_files="${{ matrix.chart }}"/values-*
for values_file in $(basename -a $values_files); do
helm template "${{ matrix.chart }}" -f "${{ matrix.chart }}/values.yaml" -f "${{ matrix.chart }}/${values_file}" --output-dir "shared/base-charts/${{ matrix.chart }}/${values_file}"
done
git checkout ${{ github.base_ref }} --
if [ -d "${{ matrix.chart }}" ]; then
helm dependency build "${{ matrix.chart }}"
values_files="${{ matrix.chart }}"/values-*
for values_file in $(basename -a $values_files); do
helm template "${{ matrix.chart }}" -f "${{ matrix.chart }}/values.yaml" -f "${{ matrix.chart }}/${values_file}" --output-dir "shared/base-charts/${{ matrix.chart }}/${values_file}"
done
fi
echo sanitized_name=$(echo "${{ matrix.chart }}" | sed 's/\//-/g') >> $GITHUB_OUTPUT
echo "${{ matrix.chart }} does not exist in ${{ github.base_ref }} ref"
- name: upload artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -100,7 +107,7 @@ jobs:

diff_helm_charts:
runs-on: ubuntu-latest
needs:
needs:
- get_changed_helm_charts
- render_base_ref_charts
- render_head_ref_charts
Expand All @@ -121,21 +128,54 @@ jobs:
for chart in ${{ needs.get_changed_helm_charts.outputs.charts }}; do
chart_diff_output=$(diff -r "shared/base-charts/${chart}" "shared/head-charts/${chart}" || true)
if [ -n "$chart_diff_output" ]; then
echo -e "Changes found in chart: ${chart}\n$(diff -ruN shared/base-charts/${chart} shared/head-charts/${chart})\n" >> diff.log
echo -e "Changes found in chart: ${chart}\n$(diff -ru shared/base-charts/${chart} shared/head-charts/${chart})\n" >> diff.log
fi
done
- name: post diff as comment on pull request
if: needs.get_changed_helm_charts.outputs.charts != ''
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const comment_char_limit = 65536; // GitHub comment character limit
const diff = fs.readFileSync('diff.log', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '```diff\n' + diff + '\n```'
})
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
})
}

0 comments on commit 381d4a5

Please sign in to comment.