Skip to content

Commit 5a39627

Browse files
committed
Auto merge of #12126 - weihanglo:ci-version-bump, r=epage
ci: check if any version bump needed for member crates ### What does this PR try to resolve? Check if a crate requires a version bump in CI. Part of #12033 ### How should we test and review this PR? Demo action result: <https://github.com/weihanglo/cargo/actions/runs/4941952784/jobs/8835049007> ### Additional information This doesn't devalue #12089. I love the changelog detection, saving maintainers' times a lot ( I am the one writing changelogs for each release for now). However, the amount of code there is too excessive to me. I think someday when we are going to integrate [cargo-release](https://crates.io/crates/cargo-release) and/or [ehuss/cargo-new-release](https://github.com/ehuss/cargo-new-release), we can remove this script perhaps entirely :) I tried to document the script a bit hard. Hope it won't get worse over time.
2 parents e5e68c4 + ab2b241 commit 5a39627

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ jobs:
5151
- run: rustup update stable && rustup default stable
5252
- run: cargo update -p cargo --locked
5353

54+
check-version-bump:
55+
runs-on: ubuntu-latest
56+
env:
57+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
58+
HEAD_SHA: ${{ github.sha }}
59+
steps:
60+
- uses: actions/checkout@v3
61+
with:
62+
fetch-depth: 0 # make `git diff` work
63+
- run: rustup update stable && rustup default stable
64+
- run: ci/validate-version-bump.sh
65+
5466
test:
5567
runs-on: ${{ matrix.os }}
5668
env:

ci/validate-version-bump.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
# This script checks if a crate needs a version bump.
3+
#
4+
# At the time of writing, it doesn't check what kind of bump is required.
5+
# In the future, we could take SemVer compatibliity into account, like
6+
# integrating `cargo-semver-checks` of else
7+
#
8+
# Inputs:
9+
# BASE_SHA The commit SHA of the branch where the PR wants to merge into.
10+
# HEAD_SHA The commit SHA that triggered the workflow.
11+
12+
set -euo pipefail
13+
14+
# When `BASE_SHA` is missing, we assume it is from bors merge commit,
15+
# so hope `HEAD~` to find the previous commit on master branch.
16+
base_sha=$(git rev-parse "${BASE_SHA:-HEAD~1}")
17+
head_sha=$(git rev-parse "${HEAD_SHA:-HEAD}")
18+
19+
echo "Base branch is $base_sha"
20+
echo "Current head is $head_sha"
21+
22+
# Gets crate names of members that has been changed from $bash_sha to $head_sha.
23+
changed_crates=$(
24+
git diff --name-only "$base_sha" "$head_sha" -- crates/ credential/ benches/ \
25+
| cut -d'/' -f2 \
26+
| sort -u
27+
)
28+
29+
if [ -z "$changed_crates" ]
30+
then
31+
echo "No file changed in member crates."
32+
exit 0
33+
fi
34+
35+
# Checks publish status for only crates with code changes.
36+
publish_status_table=$(
37+
echo "$changed_crates" \
38+
| xargs printf -- '--package %s\n' \
39+
| xargs cargo unpublished
40+
)
41+
42+
# "yes" -> code changed but no version difference -> need a bump
43+
# Prints 2nd column (sep by space), which is the name of the crate.
44+
crates_need_bump=$(
45+
echo "$publish_status_table" \
46+
| { grep '| yes ' || true; } \
47+
| awk '{print $2}'
48+
)
49+
50+
if [ -z "$crates_need_bump" ]
51+
then
52+
echo "No version bump needed for member crates."
53+
exit 0
54+
fi
55+
56+
echo "Detected changes in these crates but no version bump found:"
57+
echo "$crates_need_bump"
58+
echo
59+
echo "Please bump at least one patch version for each corresponding Cargo.toml:"
60+
echo 'Run "cargo unpublished" to read the publish status table for details.'
61+
exit 1

crates/xtask-unpublished/src/xtask.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! `xtask-unpublished` outputs a table with publish status --- a local version
2+
//! and a version on crates.io for comparisons.
3+
//!
4+
//! This aims to help developers check if there is any crate required a new
5+
//! publish, as well as detect if a version bump is needed in CI pipeline.
6+
17
use std::collections::HashSet;
28

39
use cargo::core::registry::PackageRegistry;

0 commit comments

Comments
 (0)