Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rust semver-checks workflow against base branch #13

Merged
merged 6 commits into from
Oct 21, 2024
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
145 changes: 145 additions & 0 deletions .github/workflows/rs-semver-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
name: Rust semver-checks

on:
# Allow this workflow to be called by another workflow
workflow_call:
inputs:
baseline-rev:
description: "The base rev to compare against. Defaults to the PR's base branch."
type: string
required: false
apt-dependencies:
description: "A list of space-separated apt dependencies to install before running."
type: string
default: ""
required: false
secrets:
GITHUB_PAT:
description: 'The github token for the user that will post comments.'
required: true

env:
CARGO_TERM_COLOR: always
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"

jobs:
semver-checks:
name: Rust semver-checks 🦀
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_PAT }}
steps:
- uses: actions/checkout@v4
with:
path: PR_BRANCH
- name: Checkout baseline
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the most useful is to be able to test against the last tagged commit (possibly with the tag matching some pattern), is that possible?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the default behaviour, and it's actually what semver-checks-action does.
The problem is that the output is not really useful for the PR author, as once a breaking change gets merged will reappear as noise on every subsequent PR.
We'll still get the aggregated comparison against the last release on the release-plz PR, so no need to repeat it here.

s-c-action plans to add the baseline checks at some point, but it requires some changes to its definition first.
See the discussion in obi1kenobi/cargo-semver-checks-action#64
For now the alternative is to manually run semver-checks, as we do here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I agree that release-plz is handling my problem

uses: actions/checkout@v4
with:
ref: ${{ inputs.baseline-rev || github.event.pull_request.base.sha || github.event.merge_group.base.sha }}
path: BASELINE_BRANCH
- uses: mozilla-actions/[email protected]
- name: Install apt dependencies
if: ${{ inputs.apt-dependencies != '' }}
run: |
echo "Installing apt dependencies: $APT_DEPENDENCIES"
sudo apt-get install -y $APT_DEPENDENCIES
env:
APT_DEPENDENCIES: ${{ inputs.apt-dependencies }}
- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
- name: Install cargo-semver-checks
run: cargo install cargo-semver-checks

# Run cargo-semver-checks against the PR's target branch.
- name: Check for public API changes
id: check-changes
run: |
# Don't fail the workflow when semver-checks returns a non-zero exit code.
set +e

cd PR_BRANCH
cargo semver-checks --color never --baseline-root ../BASELINE_BRANCH > diagnostic.txt
if [ "$?" -ne 0 ]; then
echo "breaking=true" >> $GITHUB_OUTPUT
else
echo "breaking=false" >> $GITHUB_OUTPUT
fi

{
echo 'semver_checks_diagnostic<<EOF'
cat diagnostic.txt
echo
echo EOF
} >> $GITHUB_OUTPUT

# Check if the PR title contains a breaking change flag,
# to change the feedback message.
- name: Check for breaking change flag
if: github.event_name == 'pull_request'
id: breaking-pr
run: |
if [[ "${PR_TITLE}" =~ ^.*\!:.*$ ]]; then
echo "breaking=true" >> $GITHUB_OUTPUT
else
echo "breaking=false" >> $GITHUB_OUTPUT
fi
env:
PR_TITLE: ${{ github.event.pull_request.title }}

# Debug step
- run: |
echo "breaking: ${{ steps.check-changes.outputs.breaking }}"
echo "breaking-pr: ${{ steps.breaking-pr.outputs.breaking }}"

# Post a diagnostics comment if there are breaking changes and the PR has been marked as breaking.
- name: Post a comment about the breaking changes. PR marked as breaking.
if: ${{ github.event_name == 'pull_request' && steps.check-changes.outputs.breaking == 'true' && steps.breaking-pr.outputs.breaking == 'true' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: rs-semver-checks
message: |
This PR contains breaking changes to the public Rust API.

<details>
<summary>cargo-semver-checks summary</summary>

```
${{ steps.check-changes.outputs.semver_checks_diagnostic }}
```

</details>
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}

# Post a help comment if there are breaking changes, and the PR hasn't been marked as breaking.
- name: Post a comment about the breaking changes. PR *not* marked as breaking.
if: ${{ github.event_name == 'pull_request' && steps.check-changes.outputs.breaking == 'true' && steps.breaking-pr.outputs.breaking == 'false' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: rs-semver-checks
message: |
This PR contains breaking changes to the public Rust API.
Please deprecate the old API instead (if possible), or mark the PR with a `!` to indicate a breaking change.

<details>
<summary>cargo-semver-checks summary</summary>

```
${{ steps.check-changes.outputs.semver_checks_diagnostic }}
```

</details>
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
- name: Fail if there are undeclared breaking changes
if: ${{ steps.check-changes.outputs.breaking == 'true' && steps.breaking-pr.outputs.breaking == 'false' }}
run: exit 1

# Delete previous comments when the issues have been resolved
# This step doesn't run if any of the previous checks fails.
- name: Delete previous comments
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ github.event_name == 'pull_request' && steps.check-changes.outputs.breaking == 'false' }}
with:
header: rs-semver-checks
delete: true
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ tokens](https://github.com/settings/personal-access-tokens/new) with the

The following workflows are available:

- [`drop-cache`](#drop-cache): Drops the cache for a branch when a pull request is closed.
- [`pr-title`](#pr-title): Checks the title of pull requests to ensure they follow the conventional commits format.
- [`rs-semver-checks`](#rs-semver-checks): Runs `cargo-semver-checks` on a PR against the base branch, and reports back if there are breaking changes.
- [`add-to-project`](#add-to-project): Adds new issues to a GitHub project board when they are created.

### [`drop-cache`](https://github.com/CQCL/hugrverse-actions/blob/main/.github/workflows/drop-cache.yml)

Drops the cache for a branch when a pull request is closed. This helps to avoid
Expand Down Expand Up @@ -70,6 +75,38 @@ The fine-grained `GITHUB_PAT` secret must include the following permissions:
| --- | --- |
| Pull requests | Read and write |

### [`rs-semver-checks`](https://github.com/CQCL/hugrverse-actions/blob/main/.github/workflows/rs-semver-checks.yml)

Runs `cargo-semver-checks` on a PR against the base branch, and reports back if
there are breaking changes.
Suggests adding a breaking change flag to the PR title if necessary.

#### Usage
```yaml
name: Rust Semver Checks
on:
pull_request:
branches:
- main

jobs:
rs-semver-checks:
uses: CQCL/hugrverse-actions/.github/workflows/rs-semver-checks.yml@main
secrets:
GITHUB_PAT: ${{ secrets.GITHUB_PAT }}
```

The workflow compares against the base branch of the PR by default. Use the `baseline-rev` input to specify a different base commit.

#### Token Permissions

The fine-grained `GITHUB_PAT` secret must include the following permissions:

| Permission | Access |
| --- | --- |
| Pull requests | Read and write |


### [`add-to-project`](https://github.com/CQCL/hugrverse-actions/blob/main/.github/workflows/add-to-project.yml)

Adds new issues to a GitHub project board when they are created.
Expand Down