Skip to content

Commit 1b6ce28

Browse files
author
Rafid Bin Mostofa
committed
ci: check for removed slices
We would not like for slices to be deleted randomly. The CI will check for any deleted (or renamed) slices and trigger an error if there are any.
1 parent c1fc1a8 commit 1b6ce28

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# This script takes in a git ref and checks if any slices present in the former
4+
# ref (commit, branch etc) were removed. Must be run in the chisel-releases
5+
# root directory. It exits with an error (1) if removals found.
6+
#
7+
# Note: there may be errors if the current git status is dirty, since this
8+
# script does "git checkout" to the old ref and back.
9+
#
10+
# Deps: yq (https://github.com/mikefarah/yq).
11+
12+
set -eu
13+
14+
if (( $# != 1 )); then
15+
echo -e "Usage:\n\t$(basename "$0") <old-git-ref>"
16+
exit 1
17+
fi
18+
19+
export LC_COLLATE="C"
20+
oldref="$1"
21+
curref="$(git rev-parse --short HEAD)"
22+
23+
# Revert to the current git ref on exit.
24+
revert() {
25+
git checkout -q "$curref"
26+
}
27+
trap revert EXIT
28+
29+
# Lists all the slices in the 'slices/' directory.
30+
list() {
31+
local pkg f
32+
echo "Checking slices in $(git rev-parse --short HEAD)..." >&2
33+
# Iterate over the files in a sorted order to produce deterministic order.
34+
for f in $(find "slices/" -name "*.yaml" | sort); do
35+
pkg="$(yq '.package' "$f")"
36+
# Get the slice names from the file, prefix with package name and an
37+
# underscore and finally, sort.
38+
yq '.slices | keys | .[]' "$f" \
39+
| sed "s/^/$pkg\_/" \
40+
| sort
41+
done
42+
}
43+
44+
# List of slices in current ref, in a file.
45+
cur="$(list)"
46+
47+
# List of slices in old ref, in a file.
48+
git checkout -q "$oldref"
49+
old="$(list)"
50+
51+
# Check which slices are removed.
52+
# The following 'diff' syntax only reports the deletions from the 'old' file.
53+
echo "The following slices from $oldref have been removed:"
54+
diff --changed-group-format='%<' --unchanged-group-format='' \
55+
<(echo "$old") <(echo "$cur") \
56+
&& echo "None"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "Removed slices"
2+
3+
on:
4+
push:
5+
paths:
6+
- "slices/**"
7+
pull_request:
8+
paths:
9+
- "slices/**"
10+
workflow_call:
11+
12+
jobs:
13+
check:
14+
name: "Check for removed slices"
15+
runs-on: "ubuntu-latest"
16+
env:
17+
files_main: "main-files"
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
# We do need to fetch all history for all branches.
22+
fetch-depth: 0
23+
24+
- name: "Set reference to main branch"
25+
id: set-main-ref
26+
run: |
27+
if [[ "${{ github.base_ref }}" == "main" ]]; then
28+
# For PRs to main, use the updated files.
29+
# Leaving checkout_main_ref unset will checkout the PR head_ref.
30+
echo "ref_main=" >> $GITHUB_OUTPUT
31+
else
32+
echo "ref_main=main" >> $GITHUB_OUTPUT
33+
fi
34+
35+
- name: "Checkout main branch"
36+
uses: actions/checkout@v4
37+
with:
38+
ref: ${{ steps.set-main-ref.outputs.ref_main }}
39+
path: ${{ env.files_main }}
40+
41+
- name: "Install yq"
42+
uses: mikefarah/yq@v4.45.4
43+
44+
- name: "Check for removed slices"
45+
run: |
46+
ln -s "${{ env.files_main }}/.github/scripts/removed-slices/removed-slices" .
47+
./removed-slices "${{ github.base_ref || github.event.before }}"

0 commit comments

Comments
 (0)