Skip to content

Commit 5034776

Browse files
authored
build: Add badge update to readme script (#1773)
Signed-off-by: Charlie Egan <charlie_egan@apple.com>
1 parent 5c9e6f0 commit 5034776

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

build/update-readme.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
README_SECTIONS_DIR="./docs/readme-sections"
44
README_PATH="./README.md"
5+
BADGES_PATH="$README_SECTIONS_DIR/badges.md"
56
MANIFEST="$README_SECTIONS_DIR/github-manifest"
67

78
if [[ $# -ne 1 ]]; then
@@ -11,6 +12,46 @@ fi
1112

1213
MODE="$1"
1314

15+
# Function to update badges with current OPA version
16+
update_badges() {
17+
# Extract OPA version using go command and jq
18+
OPA_VERSION=$(go list -m -json github.com/open-policy-agent/opa | jq -r '.Version')
19+
20+
if [[ -z "$OPA_VERSION" ]]; then
21+
echo "Error: Could not find OPA version using go list command" >&2
22+
exit 1
23+
fi
24+
25+
# Create a temporary file for badges
26+
badges_tmpfile=$(mktemp)
27+
28+
# Read the badges file and update the OPA badge line
29+
while IFS= read -r line; do
30+
if [[ $line =~ ^\!\[OPA\ v[0-9]+\.[0-9]+\.[0-9]+\] ]]; then
31+
echo "![OPA $OPA_VERSION](https://www.openpolicyagent.org/badge/$OPA_VERSION)" >> "$badges_tmpfile"
32+
else
33+
echo "$line" >> "$badges_tmpfile"
34+
fi
35+
done < "$BADGES_PATH"
36+
37+
# Check if badges need updating
38+
if ! cmp -s "$badges_tmpfile" "$BADGES_PATH"; then
39+
if [[ "$MODE" == "write" ]]; then
40+
mv "$badges_tmpfile" "$BADGES_PATH"
41+
echo "Updated badges.md with OPA version $OPA_VERSION."
42+
else
43+
rm "$badges_tmpfile"
44+
echo "README.md is out of date (badges need OPA version update to $OPA_VERSION). Please run '$0 write' to update it."
45+
exit 1
46+
fi
47+
else
48+
rm "$badges_tmpfile"
49+
fi
50+
}
51+
52+
# Update badges first
53+
update_badges
54+
1455
# Create a temporary file to hold the new content
1556
tmpfile=$(mktemp)
1657

0 commit comments

Comments
 (0)