Skip to content

Commit 567b50a

Browse files
committed
Merge rust-bitcoin#4883: Introduce and enforce import statement policy
c0f5db2 CI: Enforce policy (Tobin C. Harding) 1f46b8a docs: Add clause to import policy section (Tobin C. Harding) Pull request description: Add and enforce policy around import statements using `crate::Foo` instead of `units::Foo`. ACKs for top commit: apoelstra: ACK c0f5db2; successfully ran local tests; let's give this a shot Tree-SHA512: c85e47aae7e3992340e71c1cd81a3057fe7eea152c56d91da0a67a557c3724223c2f5dcd7ca572d1b50102eeda442336993752e815043b389e546e91fe5abca7
2 parents 72d642c + c0f5db2 commit 567b50a

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

.github/workflows/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Run from rust.yml unless stated otherwise. Unfortunately we are now exceeding th
2929
15. `WASM`
3030
16. `Kani`
3131
17. `API`
32-
18. `release` - run by `release.yml`
33-
19. `labeler` - run by `manage-pr.yml`
34-
20. `Shellcheck` - run by `shellcheck.yml`
32+
18. `Policy` - enforce repository coding policy.
33+
19. `release` - run by `release.yml`
34+
20. `labeler` - run by `manage-pr.yml`
35+
21. `Shellcheck` - run by `shellcheck.yml`

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,18 @@ jobs:
322322
run: cargo install --locked cargo-public-api --version 0.49.0
323323
- name: "Run API checker script"
324324
run: ./contrib/check-for-api-changes.sh
325+
326+
Policy:
327+
name: Enforce repo policy - stable toolchain
328+
runs-on: ubuntu-24.04
329+
strategy:
330+
fail-fast: false
331+
steps:
332+
- name: "Checkout repo"
333+
uses: actions/checkout@v4
334+
- name: "Checkout maintainer tools"
335+
uses: actions/checkout@v4
336+
- name: "Select toolchain"
337+
uses: dtolnay/rust-toolchain@stable
338+
- name: "Run policy script"
339+
run: ./contrib/check-for-policy-violations.sh
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
#
3+
# Check if PR abides by our policy.
4+
5+
set -euo pipefail
6+
7+
# When running script locally the name used for the `github.com/rust-bitcoin/rust-bitcoin` remote.
8+
REMOTE="upstream"
9+
10+
main() {
11+
check_required_commands
12+
13+
if low_level_import_usage; then
14+
err "Please do not import directly from low level crates, import using 'use crate::' instead"
15+
fi
16+
}
17+
18+
# Enforces import policy.
19+
#
20+
# See `./policy.md` section: `### On re-exports`.
21+
# Greps patch for imports that violate the policy, returns true if an
22+
# violations are found.
23+
low_level_import_usage() {
24+
local crates=("units" "primitives")
25+
local found_violation=false
26+
local violations;
27+
28+
# Determine the base branch - common CI environment variables.
29+
local base_branch="$REMOTE/master"
30+
if [[ -n "${GITHUB_BASE_REF:-}" ]]; then
31+
base_branch="$GITHUB_BASE_REF"
32+
elif [[ -n "${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-}" ]]; then
33+
base_branch="$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
34+
fi
35+
36+
for crate in "${crates[@]}"; do
37+
violations=$(git diff "$base_branch"...HEAD -- '*.rs' | grep "^+" | grep -E "use ${crate}::" | grep -v "pub use ${crate}::" || true)
38+
if [[ -n "$violations" ]]; then
39+
say_err "invalid import statement: '${violations:1}'"
40+
found_violation=true
41+
fi
42+
done
43+
44+
$found_violation
45+
}
46+
47+
# Check all the commands we use are present in the current environment.
48+
check_required_commands() {
49+
need_cmd grep
50+
need_cmd git
51+
}
52+
53+
say() {
54+
echo "policy: $1"
55+
}
56+
57+
say_err() {
58+
say "$1" >&2
59+
}
60+
61+
err() {
62+
echo "$1" >&2
63+
exit 1
64+
}
65+
66+
need_cmd() {
67+
if ! command -v "$1" > /dev/null 2>&1
68+
then err "need '$1' (command not found)"
69+
fi
70+
}
71+
72+
#
73+
# Main script
74+
#
75+
main "$@"
76+
exit 0

docs/policy.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ pub use units::{foo, FooBar};
117117
pub use units::foo::SomeError;
118118
```
119119

120+
### Usage of re-exports
121+
122+
As part of the attempt to mirror the `units` and `primitives`, and `bitcoin` APIs, in this codebase,
123+
policy is to use types from the highest crate available i.e `use crate::Foo` not `units::Foo`.
124+
125+
This is enforced by CI.
126+
120127
## Return `Self`
121128

122129
Use `Self` as the return type instead of naming the type. When constructing the return value use

0 commit comments

Comments
 (0)