22set -euo pipefail
33
44# script/apidiff.sh
5- # Compare API of only modified Go packages between PR HEAD and default branch using apidiff.
5+ # Compare API of only modified Go packages between PR HEAD and default branch using apidiff, formatted for GitHub Actions .
66# Usage: ./script/apidiff.sh
77
88# Ensure apidiff is installed
99if ! command -v apidiff & > /dev/null; then
10- echo " apidiff not found, installing..."
10+ echo " ::warning:: apidiff not found, installing..."
1111 go install golang.org/x/exp/apidiff@latest
1212fi
1313
@@ -39,7 +39,9 @@ readarray -t changed_dirs < <(
3939 sort -u
4040)
4141
42- echo " Modified directories: ${changed_dirs[*]} "
42+ echo " ::group::Modified directories"
43+ echo " ${changed_dirs[@]} "
44+ echo " ::endgroup::"
4345
4446# Create temporary workspace
4547tmp_dir=$( mktemp -d)
5961readarray -t pkgs < <( printf ' %s
6062' " ${pkgs[@]} " | sort -u)
6163
62- echo " Packages to compare: ${pkgs[*]} "
64+ echo " ::group::Packages to compare"
65+ echo " ${pkgs[@]} "
66+ echo " ::endgroup::"
6367
6468# Prepare export dirs
6569exports_dir=" $tmp_dir /exports"
@@ -73,7 +77,6 @@ generate_pkg_exports() {
7377 pushd " $tree " > /dev/null
7478 for pkg in " ${pkgs[@]} " ; do
7579 local file=${pkg// \/ / _} .export
76- echo " Exporting $pkg -> $dest /$file "
7780 apidiff -w " $dest /$file " " $pkg "
7881 done
7982 popd > /dev/null
@@ -83,24 +86,36 @@ generate_pkg_exports "$tmp_dir/base" "$base_exports"
8386generate_pkg_exports " $tmp_dir /head" " $head_exports "
8487
8588# Compare exports for breaking changes
86- echo -e " \nComparing API for breaking changes... "
89+ echo " ::group::Comparing API for breaking changes"
8790broken=false
8891for pkg in " ${pkgs[@]} " ; do
8992 file=${pkg// \/ / _} .export
90- echo -e " \nChecking $pkg "
91- if ! apidiff " $base_exports /$file " " $head_exports /$file " ; then
93+ echo " ::group::Checking $pkg "
94+ output=$( apidiff " $base_exports /$file " " $head_exports /$file " 2>&1 )
95+ echo " ${output} "
96+ if echo " $output " | grep -q " Incompatible changes" ; then
9297 broken=true
98+ echo " ::error title=API break detected::$pkg has incompatible changes"
99+ # annotate full diff
100+ printf ' %s
101+ ' " $output " | while IFS= read -r line; do
102+ echo " ::error::$pkg : $line "
103+ done
104+ else
105+ echo " ::endgroup::"
93106 fi
94107done
95108
109+ # Close main group
110+ echo " ::endgroup::"
111+
96112# Clean up worktrees
97113git worktree remove " $tmp_dir /base" --force
98114git worktree remove " $tmp_dir /head" --force
99115
100116# Final status
101117if [[ " $broken " == true ]]; then
102- echo -e " \nBreaking API changes detected."
103118 exit 1
104119else
105- echo -e " \nNo breaking API changes detected."
120+ echo " ::notice::No breaking API changes detected."
106121fi
0 commit comments