Skip to content

Commit

Permalink
feat: update git hooks with validation and bundling
Browse files Browse the repository at this point in the history
Now checksums are computed (pre-commit) and checked (pre-push) for both OpenAPI spec and JSON schema and their minified versions.

Refs: #2
  • Loading branch information
jenkin committed Nov 6, 2023
1 parent 8247052 commit dd9a895
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 28 deletions.
66 changes: 53 additions & 13 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
#!/bin/bash

VERSION=512
CMD=shasum
SHA="sha$VERSION"
OAS_FILE=docs/nominatim.openapi.json
OAS_MIN_FILE=${OAS_FILE/.json/.min.json}
SCHEMA_FILE=docs/geocodejson.schema.json
SCHEMA_MIN_FILE=${SCHEMA_FILE/.json/.min.json}

INPUT=docs/nominatim.openapi.json
OUTPUT="$INPUT".checksum
SHA_CMD=shasum
SHA_VERSION=512
SHA_PREFIX="sha$SHA_VERSION"

if [ ! -f $INPUT ]
NPX_CMD=npx
VALIDATE_CMD="$NPX_CMD -y @apidevtools/swagger-cli validate"
BUNDLE_OAS_CMD="$NPX_CMD -y @apidevtools/swagger-cli bundle"
BUNDLE_SCHEMA_CMD="$NPX_CMD -y json-dereference-cli"
MINIFY_CMD="$NPX_CMD -y pretty-mini-json"

# OpenAPI spec and JSON schema files must exist
if [ ! -f $OAS_FILE ] || [ ! -f $SCHEMA_FILE ]
then
echo "$OAS_FILE or $OAS_FILE files are missing, abort"
exit 1
fi

# npx must be installed
if ! command -v $NPX_CMD &> /dev/null
then
echo "$INPUT file is missing, abort"
echo "$NPX_CMD could not be found, please install it"
exit 1
fi

if ! command -v $CMD &> /dev/null
# OpenAPI spec must be valid
if ! $VALIDATE_CMD $OAS_FILE &> /dev/null
then
echo "$CMD could not be found, please install it"
echo "$OAS_FILE file is not a valid OpenAPI spec, please check"
exit 1
fi

CHECKSUM=`$CMD -a $VERSION $INPUT | awk '{print $1}'`
# Create bundles
$BUNDLE_OAS_CMD $OAS_FILE 2> /dev/null | $MINIFY_CMD -o $OAS_MIN_FILE 2> /dev/null
$BUNDLE_SCHEMA_CMD -s $SCHEMA_FILE -o $SCHEMA_MIN_FILE 2> /dev/null

# shasum must be installed
if ! command -v $SHA_CMD &> /dev/null
then
echo "$SHA_CMD could not be found, please install it"
exit 1
fi

# Compute checksums
SHA_OAS_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $OAS_FILE | cut -d" " -f 1`
SHA_SCHEMA_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $SCHEMA_FILE | cut -d" " -f 1`
SHA_OAS_MIN_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $OAS_MIN_FILE | cut -d" " -f 1`
SHA_SCHEMA_MIN_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $SCHEMA_MIN_FILE | cut -d" " -f 1`

echo "Checksum ($OAS_FILE.checksum): $SHA_PREFIX-$SHA_OAS_CHECKSUM"
echo -n "$SHA_PREFIX-$SHA_OAS_CHECKSUM" > $OAS_FILE.checksum
echo "Checksum ($SCHEMA_FILE.checksum): $SHA_PREFIX-$SHA_SCHEMA_CHECKSUM"
echo -n "$SHA_PREFIX-$SHA_SCHEMA_CHECKSUM" > $SCHEMA_FILE.checksum
echo "Checksum ($OAS_MIN_FILE.checksum): $SHA_PREFIX-$SHA_OAS_MIN_CHECKSUM"
echo -n "$SHA_PREFIX-$SHA_OAS_MIN_CHECKSUM" > $OAS_MIN_FILE.checksum
echo "Checksum ($SCHEMA_MIN_FILE.checksum): $SHA_PREFIX-$SHA_SCHEMA_MIN_CHECKSUM"
echo -n "$SHA_PREFIX-$SHA_SCHEMA_MIN_CHECKSUM" > $SCHEMA_MIN_FILE.checksum

echo "Checksum ($OUTPUT): $SHA-$CHECKSUM"
echo -n "$SHA-$CHECKSUM" > $OUTPUT
git add $OUTPUT
# Stage new checksums
git add $OAS_FILE.checksum $SCHEMA_FILE.checksum $OAS_MIN_FILE.checksum $SCHEMA_MIN_FILE.checksum
exit 0
64 changes: 49 additions & 15 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,37 +1,71 @@
#!/bin/bash

VERSION=512
CMD=shasum
SHA="sha$VERSION"
OAS_FILE=docs/nominatim.openapi.json
OAS_MIN_FILE=${OAS_FILE/.json/.min.json}
SCHEMA_FILE=docs/geocodejson.schema.json
SCHEMA_MIN_FILE=${SCHEMA_FILE/.json/.min.json}

INPUT=docs/nominatim.openapi.json
OUTPUT="$INPUT".checksum
SHA_CMD=shasum
SHA_VERSION=512
SHA_PREFIX="sha$SHA_VERSION"

if [ ! -f $INPUT ]
# OpenAPI spec and JSON schema files must exist
if [ ! -f $OAS_FILE ] || [ ! -f $OAS_MIN_FILE ] || [ ! -f $SCHEMA_FILE ] || [ ! -f $SCHEMA_MIN_FILE ]
then
echo "$INPUT file is missing, abort"
echo "$OAS_FILE or $OAS_MIN_FILE or $SCHEMA_FILE or $SCHEMA_MIN_FILE files are missing, abort"
exit 1
fi

if ! command -v $CMD &> /dev/null
# shasum must be installed
if ! command -v $SHA_CMD &> /dev/null
then
echo "$CMD could not be found, please install it"
echo "$SHA_CMD could not be found, please install it"
exit 1
fi

CHECKSUM=`$CMD -a $VERSION $INPUT | awk '{print $1}'`
# Checksums must exists
if [ ! -f "$OAS_FILE.checksum" ] || [ ! -f "$OAS_MIN_FILE.checksum" ] || [ ! -f "$SCHEMA_FILE.checksum" ] || [ ! -f "$SCHEMA_MIN_FILE.checksum" ]
then
echo "Checksum files are missing, abort"
exit 1
fi

# Compute checksums
SHA_OAS_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $OAS_FILE | cut -d" " -f 1`
SHA_SCHEMA_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $SCHEMA_FILE | cut -d" " -f 1`
SHA_OAS_MIN_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $OAS_MIN_FILE | cut -d" " -f 1`
SHA_SCHEMA_MIN_CHECKSUM=`$SHA_CMD -a $SHA_VERSION $SCHEMA_MIN_FILE | cut -d" " -f 1`

if ! grep "$SHA_PREFIX-$SHA_OAS_CHECKSUM" $OAS_FILE.checksum &>/dev/null
then
echo "wrong checksum for $OAS_FILE, please verify"
exit 1
else
echo "Checksum ($OAS_FILE): $SHA_PREFIX-$SHA_OAS_CHECKSUM"
fi

if ! grep "$SHA_PREFIX-$SHA_SCHEMA_CHECKSUM" $SCHEMA_FILE.checksum &>/dev/null
then
echo "wrong checksum for $SCHEMA_FILE, please verify"
exit 1
else
echo "Checksum ($SCHEMA_FILE): $SHA_PREFIX-$SHA_SCHEMA_CHECKSUM"
fi

if [ ! -f $OUTPUT ]
if ! grep "$SHA_PREFIX-$SHA_OAS_MIN_CHECKSUM" $OAS_MIN_FILE.checksum &>/dev/null
then
echo "$OUTPUT file is missing, abort"
echo "wrong checksum for $OAS_MIN_FILE, please verify"
exit 1
else
echo "Checksum ($OAS_MIN_FILE): $SHA_PREFIX-$SHA_OAS_MIN_CHECKSUM"
fi

if ! grep "$SHA-$CHECKSUM" $OUTPUT &>/dev/null
if ! grep "$SHA_PREFIX-$SHA_SCHEMA_MIN_CHECKSUM" $SCHEMA_MIN_FILE.checksum &>/dev/null
then
echo "wrong checksum, please verify"
echo "wrong checksum for $SCHEMA_MIN_FILE, please verify"
exit 1
else
echo "Checksum ($SCHEMA_MIN_FILE): $SHA_PREFIX-$SHA_SCHEMA_MIN_CHECKSUM"
fi

echo "Checksum ($OUTPUT): $SHA-$CHECKSUM"
exit 0
1 change: 1 addition & 0 deletions docs/geocodejson.schema.json.checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512-8974b9a6663700a00a3035ce324c7e47b81e17d531015a8e62bab450413c1889f5ca99031b456d882b75600b6fe68845e9d06841e1eb57f84fc142e59b9b39f0
1 change: 1 addition & 0 deletions docs/geocodejson.schema.min.json.checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512-c2190a6be0a9e25d046b5ba8ef301b5d534a41125d434351efc837e541e67fa0d8008e733bbad0c7606d8d7b7698415b26f4c0813a676d549d3dd0edbc1ef4c8
1 change: 1 addition & 0 deletions docs/nominatim.openapi.min.json.checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512-9fcf7a7fbcf1cd54c39baba7cba2cc1a50734a324423c33bbfd0a2e482d13934369bcea3c258fe139d42bdaf6f9f85171c6024ff5c9bc05b46e514a82b7b0f69

0 comments on commit dd9a895

Please sign in to comment.