2025-07-29 16:33:49 -04:00
# apidiff-action
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
A GitHub Action that detects breaking API changes in Go code and comments on pull requests. This action uses the official [`golang.org/x/exp/cmd/apidiff` ](https://pkg.go.dev/golang.org/x/exp/cmd/apidiff ) tool to analyze API compatibility.
2025-07-29 10:25:22 -04:00
## Features
2025-07-29 16:33:49 -04:00
- 🔍 Automatically detects breaking API changes in pull requests
- 💬 Comments on PRs with detailed change reports
- ✅ Identifies both breaking and compatible changes
- 🚫 Can fail CI builds when breaking changes are detected
- 📊 Provides summary statistics in action outputs
2025-07-29 10:25:22 -04:00
## Usage
2025-07-29 16:33:49 -04:00
Add this action to your workflow:
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
```yaml
name: API Compatibility Check
on:
pull_request:
types: [opened, synchronize]
jobs:
apidiff:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write # Required for commenting on PRs
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to access base commit
- uses: actions/setup-go@v5
with:
go-version: 'stable'
- uses: imjasonh/apidiff-action@v1
with:
fail-on-breaking: true
2025-07-29 10:25:22 -04:00
```
2025-07-29 16:33:49 -04:00
## Inputs
| Input | Description | Default |
| ------------------- | ----------------------------------------------------------- | --------------------- |
| `working-directory` | Directory to run apidiff in | `.` |
| `fail-on-breaking` | Whether to fail the action if breaking changes are detected | `true` |
| `comment-on-pr` | Whether to comment on the PR with results | `true` |
| `token` | GitHub token for commenting on PRs | `${{ github.token }}` |
## Outputs
| Output | Description |
| ---------------------- | ------------------------------------------------------- |
| `has-breaking-changes` | Whether breaking changes were detected (`true` /`false` ) |
| `breaking-count` | Number of breaking changes found |
| `compatible-count` | Number of compatible changes found |
## Example PR Comment
The action will comment on your PR with a formatted report:
> # API Compatibility Check Results
>
> ## Summary
>
> | Type | Count |
> | ------------------ | ----- |
> | Breaking changes | 1 |
> | Compatible changes | 3 |
>
> ⚠️ **This PR contains breaking API changes!**
>
> ## Details
>
> ### `github.com/example/mypackage`
>
> #### ❌ Breaking changes
>
> - (\*Client).DoSomething: changed from func(string) error to func(context.Context, string) error
>
> #### ✅ Compatible changes
>
> - NewOption: added
> - WithTimeout: added
> - DefaultTimeout: added
2025-07-29 17:41:16 -04:00
Example in action: https://github.com/imjasonh/apidiff-action/pull/3
2025-07-29 16:33:49 -04:00
## Advanced Usage
### Continue on breaking changes
To get notified about breaking changes without failing the build:
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
```yaml
- uses: imjasonh/apidiff-action@v1
with:
fail-on-breaking: false
2025-07-29 10:25:22 -04:00
```
2025-07-29 16:33:49 -04:00
### Check specific directory
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
To check a specific package or module:
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
```yaml
- uses: imjasonh/apidiff-action@v1
with:
working-directory: ./api
2025-07-29 10:25:22 -04:00
```
2025-07-29 16:33:49 -04:00
### Use outputs in subsequent steps
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
```yaml
- uses: imjasonh/apidiff-action@v1
id: apidiff
with:
fail-on-breaking: false
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
- name: Handle breaking changes
if: steps.apidiff.outputs.has-breaking-changes == 'true'
run: |
echo "Found ${{ steps.apidiff.outputs.breaking-count }} breaking changes"
# Add custom handling here
2025-07-29 10:25:22 -04:00
```
2025-07-29 16:33:49 -04:00
## How it works
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
This action:
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
1. Installs the official `apidiff` tool from `golang.org/x/exp/cmd/apidiff`
2. Runs it to compare the base and head commits of your PR
3. Parses the output to identify breaking and compatible changes
4. Creates or updates a comment on the PR with the results
5. Optionally fails the build if breaking changes are detected
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
The underlying `apidiff` tool implements the compatibility rules from the [Go 1 compatibility guarantee ](https://golang.org/doc/go1compat ), checking for changes that would cause client code to stop compiling.
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
## License
2025-07-29 10:25:22 -04:00
2025-07-29 16:33:49 -04:00
This action is available under the [Apache License 2.0 ](LICENSE ).