Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ distroless container (`ghcr.io/nullean/assembly-differ`) and a GitHub Action (`a
| `benchmarks/Nullean.PublicApi.Benchmarks` | BenchmarkDotNet: reading, writing, parsing and comparing System.Collections.Immutable and CoreLib, with allocations. `dotnet run -c Release --project benchmarks/Nullean.PublicApi.Benchmarks -- --filter '*' --inProcess` (in process: BenchmarkDotNet 0.15 does not know net11.0 yet) |
| `build/scripts` | the F# build ([Runna.Fs](https://www.nuget.org/packages/Runna.Fs), net11.0) the `mise` tasks and CI call |
| `build/keys` | the strong-name key the tool's and the libraries' assemblies are signed with |
| `action.yml` | the GitHub Action: installs the tool, compares the libraries with their latest release, optionally checks the committed `api/` files (`api-files`), and reports both in one pull request comment through `pull-request`; the deprecated `first`/`second` inputs still run the `edge` container image |
| `action.yml` | the GitHub Action: installs the tool, compares the libraries with their latest release, optionally checks the committed `api/` files (`api-files`), compares a pull request's committed API with its base branch's (`api-diff`, also for each pull request of a stack below it) so the check follows what the pull request itself changes, and reports all of it in one comment through `pull-request`; the deprecated `first`/`second` inputs still run the `edge` container image |

## Commands

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ bundles does not count as your API; `--all-assemblies` compares every assembly t
| `release <path>` | a new build, as a `.nupkg`, a `.dll` or a directory, with the release before it; pass `--package` and `--version` unless `<path>` is a `.nupkg` |
| `files <old> <new>` | two local builds, each a `.dll`, a directory of dlls or a `.nupkg` |
| `api [path]` | nothing: writes the public API as `.api` files, and `--verify` checks committed ones against the build (see [Public API files](#public-api-files)) |
| `api-diff <old> <new>` | two directories of committed `.api` files, such as the base branch's `api/` and a pull request's: what changed, by path, with no build, and the same change in every target framework once |
| `upgrades` | in a solution, every package it references directly that has a newer release, and what updating each breaks in your code |
| `nuget <id> [<version>] [<new-version>]` | in a solution, the version your projects resolve with the latest release, or with `<version>`: what updating would change; anywhere, two published versions |
| `projects` | every library project in the directory against its latest release; what runs without a command |
Expand Down Expand Up @@ -231,6 +232,16 @@ jobs:
The comment lists every assembly it compared, the ones without changes too, so a run that finds nothing says so: "All good: the
public API is unchanged." A shallow checkout builds the libraries without their tags, and a version derived from tags (MinVer, Nerdbank.GitVersioning) then differs from the release in its own version and in the version of the assemblies it references: check out with `fetch-depth: 0`.

**What the check follows.** With `api-files`, the check, the release label and the comment follow what the pull request itself
changes: its committed API against the committed API of its base branch, target framework by target framework, so a
pull request is never held back by a breaking change `main` already has and has not released, or by the pull requests
below it in a stack. The comparison with the latest release is still in the comment, collapsed and marked as
information, and for a stacked pull request it says which of the pull requests below introduced the changes. A change
that is the same in every target framework is listed once, and one that only some frameworks have, such as code under
`#if NET8_0`, is listed apart with its frameworks. Without `api-files` there is no committed API to compare with, and the
check follows the comparison with the latest release. The action needs the `gh` CLI, as the hosted runners have, and
reads the base branch and the open pull requests whose branches the stack is made of.

Without `package`, the action compares every library project of the solution with the latest release of its package,
the way running the tool without a command does. Give `package: build/output/MyLib.1.3.0.nupkg` to compare a packed
package with the release before it instead. It installs the tool with `dotnet tool`, so it needs the .NET 10 SDK or
Expand All @@ -243,6 +254,7 @@ later on the runner, the one your build uses.
| `target` | | only these assemblies, one per line |
| `framework` | | the target framework to compare |
| `api-files` | | the directory of committed `.api` files, such as `api`: they are checked against the build too, in the same comment, and the action fails when they differ; see [Public API files](#public-api-files) |
| `base-comparison` | `true` | on a pull request with `api-files`, follow what the pull request itself changes against its base branch; `false` follows the comparison with the latest release |
| `api-split` | `none` | how the committed files were split: `none`, `namespace` or `top-level-namespace` |
| `fail-on` | `major` | fail when the suggested change is at least this, unless the pull request has `allow-label`; `none` never fails |
| `allow-label` | `breaking-allowed` | the label that lets a change at `fail-on` through |
Expand Down
88 changes: 84 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ inputs:
description: 'The directory of committed .api files, as assembly-differ api -o wrote them. When set, they are checked against the build too, in the same comment, and the action fails when they differ. Leave empty to skip.'
required: false
default: ''
base-comparison:
description: 'On a pull request with api-files, compare its committed API with its base branch: the check, the labels and the comment then follow what this pull request itself changes, and the comparison with the latest release is shown for information. Set to false to follow the comparison with the latest release.'
required: false
default: 'true'
api-split:
description: 'How the committed .api files were split: none, namespace or top-level-namespace.'
required: false
Expand Down Expand Up @@ -84,7 +88,7 @@ inputs:

outputs:
suggested-change:
description: 'The suggested version change: None, Patch, Minor or Major.'
description: 'The suggested version change: None, Patch, Minor or Major. What the pull request itself changes against its base branch when api-files is set, otherwise the comparison with the latest release.'
value: ${{ steps.report.outputs.suggested-change }}
breaking-changes:
description: 'How many breaking changes were found.'
Expand Down Expand Up @@ -177,6 +181,76 @@ runs:
echo "code=$code" >> "$GITHUB_OUTPUT"
if [ "$code" = "0" ]; then echo "matches=true" >> "$GITHUB_OUTPUT"; else echo "matches=false" >> "$GITHUB_OUTPUT"; fi

- name: Compare with the base branch
id: base
if: inputs.first == '' && inputs.api-files != '' && inputs.base-comparison == 'true' && github.event_name == 'pull_request'
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
GH_TOKEN: ${{ inputs.token }}
REPOSITORY: ${{ github.repository }}
BASE_REF: ${{ github.event.pull_request.base.ref }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
API_FILES: ${{ inputs.api-files }}
run: |
# What this pull request changes in the committed API, against its base branch, and which pull requests below it in
# a stack changed the API before it. Nothing is built: the committed .api files of each commit are compared.
tool="$RUNNER_TEMP/assembly-differ-tool/assembly-differ"
work="$RUNNER_TEMP/assembly-differ-base"
rm -rf "$work" && mkdir -p "$work"
prefix="$(git rev-parse --show-prefix)${API_FILES#./}"
prefix="${prefix%/}"
header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_TOKEN" | base64 | tr -d '\n')"

skip() { echo "::warning title=assembly-differ::$1; the check follows the comparison with the latest release"; exit 0; }

# the committed API of a commit, in a directory of its own; nothing when the commit has none
export_api() {
mkdir -p "$2"
git -c "http.https://github.com/.extraheader=$header" fetch --quiet --no-tags --depth=1 origin "$1" || return 1
git archive "$1" -- "$prefix" 2>/dev/null | tar -x -C "$2" || true
}
tip() { gh api "repos/$REPOSITORY/git/ref/heads/$1" --jq .object.sha; }

base_sha=$(tip "$BASE_REF") || skip "could not read the base branch $BASE_REF"
export_api "$base_sha" "$work/base" || skip "could not fetch the base branch $BASE_REF"
"$tool" api-diff "$work/base/$prefix" "$API_FILES" --format json --output "$work/delta.json" || skip "could not compare with the base branch"

# the pull requests below this one: the open ones whose branch is the base of the one above
numbers=(); titles=(); urls=(); shas=(); bases=()
cur="$BASE_REF"
owner="${REPOSITORY%%/*}"
for _ in $(seq 1 30); do
[ "$cur" = "$DEFAULT_BRANCH" ] && break
found=$(gh api "repos/$REPOSITORY/pulls?head=$owner:$cur&state=open&per_page=1" --jq '.[0] // empty | [.number, .title, .html_url, .head.sha, .base.ref] | @tsv') || break
[ -z "$found" ] && break
IFS=$'\t' read -r n title url sha base <<< "$found"
numbers+=("$n"); titles+=("$title"); urls+=("$url"); shas+=("$sha"); bases+=("$base")
cur="$base"
done

base_name="\`$BASE_REF\`"
[ "${#numbers[@]}" -gt 0 ] && base_name="$base_name (#${numbers[0]})"

# each of them against what it was based on, from the bottom of the stack up
echo '[]' > "$work/stack.json"
count=${#numbers[@]}
for ((i = count - 1; i >= 0; i--)); do
if [ "$i" -eq $((count - 1)) ]; then below_sha=$(tip "${bases[$i]}") || continue; else below_sha="${shas[$((i + 1))]}"; fi
export_api "$below_sha" "$work/below-$i" || continue
export_api "${shas[$i]}" "$work/at-$i" || continue
"$tool" api-diff "$work/below-$i/$prefix" "$work/at-$i/$prefix" --format json --output "$work/entry-$i.json" > /dev/null || continue
jq --argjson number "${numbers[$i]}" --arg title "${titles[$i]}" --arg url "${urls[$i]}" \
'{number: $number, title: $title, url: $url, suggestedVersionChange: .suggestedVersionChange, breakingChanges: (.distinctBreakingChanges // .breakingChanges)}' \
"$work/entry-$i.json" > "$work/entry-$i.line.json"
jq --slurpfile entry "$work/entry-$i.line.json" '. + $entry' "$work/stack.json" > "$work/stack.tmp" && mv "$work/stack.tmp" "$work/stack.json"
done

echo "report=$work/delta.json" >> "$GITHUB_OUTPUT"
echo "stack=$work/stack.json" >> "$GITHUB_OUTPUT"
echo "name=$base_name" >> "$GITHUB_OUTPUT"
echo "suggested-change=$(jq -r .suggestedVersionChange "$work/delta.json")" >> "$GITHUB_OUTPUT"

- name: Report on the pull request
id: report
if: inputs.first == ''
Expand All @@ -193,25 +267,31 @@ runs:
LABELS: ${{ inputs.labels }}
API_CODE: ${{ steps.api.outputs.code }}
API_REPORT: ${{ steps.api.outputs.report }}
BASE_REPORT: ${{ steps.base.outputs.report }}
BASE_NAME: ${{ steps.base.outputs.name }}
BASE_STACK: ${{ steps.base.outputs.stack }}
run: |
report="$RUNNER_TEMP/assembly-differ/api-changes.json"
if [ ! -f "$report" ]; then
# nothing was compared, such as a solution whose libraries have no release yet
echo "suggested-change=None" >> "$GITHUB_OUTPUT"
echo "breaking-changes=0" >> "$GITHUB_OUTPUT"
[ -z "$API_CODE" ] && exit 0
[ -z "$API_CODE" ] && [ -z "$BASE_REPORT" ] && exit 0
# the committed API files are still reported, and still fail the check
mkdir -p "$RUNNER_TEMP/assembly-differ"
echo '{"firstRelease":true,"suggestedVersionChange":"None","breakingChanges":0,"assemblies":[]}' > "$report"
fi
echo "report=$report" >> "$GITHUB_OUTPUT"
echo "suggested-change=$(jq -r .suggestedVersionChange "$report")" >> "$GITHUB_OUTPUT"
echo "breaking-changes=$(jq -r .breakingChanges "$report")" >> "$GITHUB_OUTPUT"
# on a pull request with a base branch to compare with, the outputs are what the pull request itself changes
deciding="${BASE_REPORT:-$report}"
echo "suggested-change=$(jq -r .suggestedVersionChange "$deciding")" >> "$GITHUB_OUTPUT"
echo "breaking-changes=$(jq -r '.distinctBreakingChanges // .breakingChanges' "$deciding")" >> "$GITHUB_OUTPUT"
args=(pull-request "$report" --fail-on "$FAIL_ON" --allow-label "$ALLOW_LABEL"
--major-label "$MAJOR_LABEL" --minor-label "$MINOR_LABEL" --patch-label "$PATCH_LABEL")
[ "$COMMENT" != "true" ] && args+=(--no-comment)
[ "$LABELS" != "true" ] && args+=(--no-labels)
[ -n "$API_CODE" ] && args+=(--api-code "$API_CODE" --api-report "$API_REPORT")
[ -n "$BASE_REPORT" ] && args+=(--base-report "$BASE_REPORT" --base-name "$BASE_NAME" --stack "$BASE_STACK")
set +e
"$RUNNER_TEMP/assembly-differ-tool/assembly-differ" "${args[@]}"
code=$?
Expand Down
2 changes: 2 additions & 0 deletions src/assembly-differ.skills/skills/assembly-differ/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ assemblies, and `--fail-on <none|patch|minor|major>` to exit with code 4 at or a
`--include-unpackable` or `--project <path>` change that.
- **`release` in a solution compares only the assemblies the solution builds**, not the dependencies a package bundles;
`--all-assemblies` compares every assembly, `-t` names them.
- **`api-diff <old> <new>` compares two directories of committed `.api` files** with no build, such as a base branch's
`api/` and a pull request's: what a pull request itself changes, whatever the last release says.
- **A package with no earlier release is a first release**: nothing to compare, exit code 0 (`--require-baseline`
fails instead).

Expand Down
Loading