1+ #! /bin/bash
2+
3+ # Script to check API diffs using openapi-diff
4+ # Usage: ./scripts/api-diff/test-api-diff.sh [--fail-on-breaking] [filename.yaml]
5+ # Assumes you have Docker installed and the repo is checked out with master branch available
6+
7+ set -e # Exit on error
8+ set -o pipefail # Catch errors in pipes
9+
10+ # Change to repo root
11+ cd " $( dirname " $0 " ) /../.."
12+
13+ # Configuration
14+ DOCKER_IMAGE=" ${OPENAPI_DIFF_DOCKER_IMAGE:- openapitools/ openapi-diff: latest} "
15+ BASE_BRANCH=" ${BASE_BRANCH:- origin/ master} "
16+
17+ FAIL_ON_BREAKING=false
18+ TARGET_FILE=" "
19+
20+ # Parse arguments
21+ for arg in " $@ " ; do
22+ if [ " $arg " = " --fail-on-breaking" ]; then
23+ FAIL_ON_BREAKING=true
24+ elif [[ " $arg " == * .yaml ]]; then
25+ TARGET_FILE=" $arg "
26+ fi
27+ done
28+
29+ echo " Starting API diff check..."
30+
31+ # Ensure we're in the repo root
32+ if [ ! -f " xero_accounting.yaml" ]; then
33+ echo " Error: Not in repo root or xero_accounting.yaml not found"
34+ exit 1
35+ fi
36+
37+ # Fetch master if not already done
38+ git fetch " ${BASE_BRANCH%%/* } " " ${BASE_BRANCH##*/ } " 2> /dev/null || echo " Warning: Could not fetch ${BASE_BRANCH} "
39+
40+ # Create temp directory for master branch files (outside repo to avoid overlap with /current mount)
41+ TEMP_DIR=$( mktemp -d)
42+ trap " rm -rf $TEMP_DIR " EXIT
43+
44+ # Get list of xero*.yaml files (excluding any master_*.yaml files)
45+ if [ -n " $TARGET_FILE " ]; then
46+ # Single file specified
47+ if [ ! -f " $TARGET_FILE " ]; then
48+ echo " Error: File '$TARGET_FILE ' not found"
49+ exit 1
50+ fi
51+ files=" $TARGET_FILE "
52+ echo " Running diff for single file: $TARGET_FILE "
53+ else
54+ # All xero*.yaml files
55+ files=$( ls xero* .yaml 2> /dev/null | grep -v " ^master_" )
56+ if [ -z " $files " ]; then
57+ echo " No xero*.yaml files found"
58+ exit 1
59+ fi
60+ fi
61+
62+ BREAKING_CHANGES_FOUND=false
63+ FILES_WITH_BREAKING_CHANGES=()
64+ TOTAL_FILES=0
65+ PROCESSED_FILES=0
66+
67+ echo " ========================================"
68+ echo " API Diff Summary"
69+ echo " Using Docker image: $DOCKER_IMAGE "
70+ echo " Base branch: $BASE_BRANCH "
71+ echo " ========================================"
72+
73+ for file in $files ; do
74+ TOTAL_FILES=$(( TOTAL_FILES + 1 ))
75+ echo " "
76+ echo " ========== $file =========="
77+
78+ # Get the file from master branch
79+ if ! git show " $BASE_BRANCH :$file " > " $TEMP_DIR /$file " 2> /dev/null; then
80+ echo " ℹ️ New file (does not exist in master branch)"
81+ continue
82+ fi
83+
84+ # Verify the temp file was created
85+ if [ ! -f " $TEMP_DIR /$file " ]; then
86+ echo " ❌ Failed to create temp file"
87+ continue
88+ fi
89+
90+ # Note: openapi-diff provides deterministic results for change detection.
91+ # Both error and warning counts are consistent between runs.
92+
93+ # Run openapi-diff changelog
94+ echo " --- Changelog ---"
95+ set +e
96+ CHANGELOG_OUTPUT=$( docker run --rm -v " $( pwd) " :/current -v " $TEMP_DIR " :/base " $DOCKER_IMAGE " changelog --include-path-params /base/" $file " /current/" $file " 2>&1 )
97+ CHANGELOG_EXIT=$?
98+ set -e
99+
100+ echo " $CHANGELOG_OUTPUT "
101+
102+ if [ $CHANGELOG_EXIT -eq 0 ]; then
103+ echo " ✓ Changelog generated successfully"
104+ else
105+ echo " ⚠ Could not generate changelog (exit code: $CHANGELOG_EXIT )"
106+ fi
107+
108+ # Run breaking changes check
109+ echo " "
110+ echo " --- Breaking changes check ---"
111+ set +e
112+ BREAKING_OUTPUT=$( docker run --rm -v " $( pwd) " :/current -v " $TEMP_DIR " :/base " $DOCKER_IMAGE " breaking --fail-on ERR --include-path-params /base/" $file " /current/" $file " 2>&1 )
113+ BREAKING_EXIT=$?
114+ set -e
115+
116+ echo " $BREAKING_OUTPUT "
117+
118+ if [ $BREAKING_EXIT -eq 0 ]; then
119+ echo " ✓ No breaking changes detected"
120+ else
121+ echo " ⚠ Breaking changes detected (exit code: $BREAKING_EXIT )"
122+ BREAKING_CHANGES_FOUND=true
123+ FILES_WITH_BREAKING_CHANGES+=(" $file " )
124+ fi
125+
126+ PROCESSED_FILES=$(( PROCESSED_FILES + 1 ))
127+ done
128+
129+ echo " "
130+ echo " ========================================"
131+ echo " API Diff check completed"
132+ echo " Processed: $PROCESSED_FILES /$TOTAL_FILES files"
133+ echo " ========================================"
134+
135+ # Summary
136+ if [ " $BREAKING_CHANGES_FOUND " = true ]; then
137+ echo " "
138+ echo " ❌ Breaking changes detected in the following files:"
139+ for file in " ${FILES_WITH_BREAKING_CHANGES[@]} " ; do
140+ echo " - $file "
141+ # Output GitHub Actions annotation
142+ if [ -n " $GITHUB_ACTIONS " ]; then
143+ echo " ::warning file=${file} ::Breaking changes detected in this API spec file"
144+ fi
145+ done
146+
147+ if [ " $FAIL_ON_BREAKING " = true ]; then
148+ echo " "
149+ echo " Exiting with error due to breaking changes"
150+ exit 1
151+ else
152+ echo " "
153+ echo " Note: Not failing build (use --fail-on-breaking to fail on breaking changes)"
154+ fi
155+ else
156+ echo " "
157+ echo " ✓ No breaking changes detected across all files"
158+ fi
0 commit comments