Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples validation to v3.0.1 #1111

Merged
merged 10 commits into from
Sep 10, 2024
2 changes: 1 addition & 1 deletion .github/workflows/validate_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #v4.1.7
- name: Install Python dependencies
run: |
python3 -m pip install pyshacl==0.26.0 check-jsonschema==0.29.1
python3 -m pip install pyshacl==0.26.0 check-jsonschema==0.29.2
- name: Install dependencies
run: |
sudo apt install -y gawk
Expand Down
49 changes: 24 additions & 25 deletions bin/check-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,66 @@

set -e

THIS_DIR=$(dirname $0)

for f in examples/jsonld/*.json; do
echo "Checking $f"

check-jsonschema \
-v \
--schemafile https://spdx.org/schema/3.0.0/spdx-json-schema.json \
$f

pyshacl \
-s https://spdx.org/rdf/3.0.0/spdx-model.ttl \
-e https://spdx.org/rdf/3.0.0/spdx-model.ttl \
$f
done

T=$(mktemp -d)
THIS_DIR="$(dirname "$0")"
SPDX_VERSION="3.0.1"
SCHEMA_URL="https://spdx.org/schema/${SPDX_VERSION}/spdx-json-schema.json"
RDF_URL="https://spdx.org/rdf/${SPDX_VERSION}/spdx-model.ttl"
CONTEXT_URL="https://spdx.org/rdf/${SPDX_VERSION}/spdx-context.jsonld"

check_schema() {
check-jsonschema \
-v \
--schemafile https://spdx.org/schema/3.0.0/spdx-json-schema.json \
--schemafile $SCHEMA_URL \
"$1"
}

check_model() {
pyshacl \
-s https://spdx.org/rdf/3.0.0/spdx-model.ttl \
-e https://spdx.org/rdf/3.0.0/spdx-model.ttl \
-s $RDF_URL \
-e $RDF_URL \
"$1"
}

# Check examples in JSON files in examples/jsonld/
if [ "$(ls $THIS_DIR/../examples/jsonld/*.json 2>/dev/null)" ]; then
for f in $THIS_DIR/../examples/jsonld/*.json; do
echo "Checking $f"
check_schema $f
check_model $f
done
fi

TEMP=$(mktemp -d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary directory is not cleaned up


# Check examples in inline code snippets in Markdown files in docs/annexes/
for f in $THIS_DIR/../docs/annexes/*.md; do
if ! grep -q '^```json' $f; then
continue
fi
echo "Checking $f"
DEST=$T/$(basename $f)
DEST=$TEMP/$(basename $f)
mkdir -p $DEST

# Read inline code snippets and save them in separate, numbered files.
cat $f | awk -v DEST="$DEST" 'BEGIN{flag=0} /^```json/, $0=="```" { if (/^---$/){flag++} else if ($0 !~ /^```.*/ ) print $0 > DEST "/doc-" flag ".spdx.json"}'

# Combine all JSON code snippets into a single file, with SPDX context and creation info.
echo "[" > $DEST/combined.json

for doc in $DEST/*.spdx.json; do
if ! grep -q '@context' $doc; then
mv $doc $doc.fragment
cat >> $doc <<HEREDOC
{
"@context": "https://spdx.org/rdf/3.0.0/spdx-context.jsonld",
"@context": "$CONTEXT_URL",
"@graph": [
HEREDOC
cat $doc.fragment >> $doc
cat >> $doc <<HEREDOC
{
"type": "CreationInfo",
"@id": "_:creationInfo",
"specVersion": "3.0.0",
"specVersion": "$SPDX_VERSION",
"created": "2024-04-23T00:00:00Z",
"createdBy": [
{
Expand All @@ -88,5 +89,3 @@ HEREDOC

check_model $DEST/combined.json
done