From 6827c3f27831c0c2bb4eb8125893fadb6dd8f174 Mon Sep 17 00:00:00 2001 From: Micha Hobert Date: Wed, 6 Dec 2023 15:51:51 +0100 Subject: [PATCH] Test watcher --- .github/scripts/watch.sh | 57 +++++++++++++++++++ .github/workflows/watcher.yml | 41 +++++++++++++ guides/hosting/infrastructure/filesystem.md | 2 + .../hosting/infrastructure/message-queue.md | 1 + snippets/config/html_sanitizer.yaml | 1 + snippets/config/html_sanitizer_disabled.yaml | 1 + snippets/config/stock_disabled.yaml | 1 + snippets/guide/app_database_setup.md | 1 + watch.sh | 55 ++++++++++++++++++ watcher-format.json | 5 ++ 10 files changed, 165 insertions(+) create mode 100755 .github/scripts/watch.sh create mode 100644 .github/workflows/watcher.yml create mode 100644 watch.sh create mode 100644 watcher-format.json diff --git a/.github/scripts/watch.sh b/.github/scripts/watch.sh new file mode 100755 index 0000000000..e248a7c377 --- /dev/null +++ b/.github/scripts/watch.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Set the path to the snippets folder +WATCHER_PATH=$1 +ERROR_FOUND=0 + +# Find all files in the snippets folder +find $WATCHER_PATH -type f -print0 | while IFS= read -r -d '' file; do + + echo "Checking $file" + # Check if the file contains the string "WATCHER_URL" and is therefore a watcher file + cat "$file" | grep 'WATCHER_URL' >> /dev/null || continue + + + # Get the JSON from the first line of the file + HEAD=$(cat "$file" | grep 'WATCHER_URL') + JSON=$(sed "s/# //g" <<< "$HEAD") + + # Get the values from the JSON + WATCHER_URL=$(echo $JSON | jq -r '.WATCHER_URL') + WATCHER_HASH=$(echo $JSON | jq -r '.WATCHER_HASH') + WATCHER_CONTAINS=$(echo $JSON | jq -r '.WATCHER_CONTAINS') + + # Get the hash of the URL + EVAL_HASH="curl -sl $WATCHER_URL | md5sum | cut -d ' ' -f 1" + CALLED_HASH="$(eval $EVAL_HASH)" + + # Check if the hashes are equal + if [ "$WATCHER_HASH" == "$CALLED_HASH" ]; then + echo "Both strings are equal." + else + echo "Strings are not equal." + + FILE_CONTENT=$(curl -sl $WATCHER_URL) + + # Check if $WATCHER_CONTAINS contains the string "null" and is therefore empty or not set + if [[ $WATCHER_CONTAINS == "null" ]]; then + echo "No contain set" + ERROR_FOUND=1 + fi + # Check if $WATCHER_CONTAINS contains the string $WATCHER_CONTAINS and if not call the Slack webhook + if [[ $FILE_CONTENT == *"$WATCHER_CONTAINS"* ]]; then + echo "String found!" + else + echo "String not found! Please check $WATCHER_URL" + ERROR_FOUND=1 + fi + + fi + +done + +if [ "$ERROR_FOUND" -eq 1 ] + then + echo "Error found, exiting..." + exit 1 + fi diff --git a/.github/workflows/watcher.yml b/.github/workflows/watcher.yml new file mode 100644 index 0000000000..ca6b26da64 --- /dev/null +++ b/.github/workflows/watcher.yml @@ -0,0 +1,41 @@ +name: Check files for changes +on: + pull_request: + workflow_dispatch: + inputs: + path: + description: "Path to watch" + required: true + type: string + default: "snippets guides" + +jobs: + check-files: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + ./.github/scripts/watch.sh 'snippets guides' + id: watcher + continue-on-error: true + - name: Send custom JSON data to Slack workflow + if: steps.watcher.outcome != 'success' + id: slack + uses: slackapi/slack-github-action@v1.24.0 + with: + payload: | + { + "text": "Text: GitHub Action build result: ${{ job.status }}", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ":ladybug: Failed file watcher with status ${{ steps.watcher.outcome }}: Please check ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" + } + } + ] + } + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }} + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK diff --git a/guides/hosting/infrastructure/filesystem.md b/guides/hosting/infrastructure/filesystem.md index 820046ce8a..ecec144498 100644 --- a/guides/hosting/infrastructure/filesystem.md +++ b/guides/hosting/infrastructure/filesystem.md @@ -58,6 +58,8 @@ shopware: sitemap: url: "{url-to-your-sitemap-files}" # The Adapter Configuration + +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940fA"} ``` If you want to regulate the uploaded file types, then you could add the keys `allowed_extensions`for the public filesystem or `private_local_download_strategy` for the private filesystem. diff --git a/guides/hosting/infrastructure/message-queue.md b/guides/hosting/infrastructure/message-queue.md index bc9a710046..e85f2a09f6 100644 --- a/guides/hosting/infrastructure/message-queue.md +++ b/guides/hosting/infrastructure/message-queue.md @@ -64,6 +64,7 @@ If you have configured the cli-worker, you should turn off the admin worker in t shopware: admin_worker: enable_admin_worker: false +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940f","WATCHER_CONTAINS":"enable_admin_worker"} ``` ::: warning diff --git a/snippets/config/html_sanitizer.yaml b/snippets/config/html_sanitizer.yaml index d23d6e3e65..0f31b963ac 100644 --- a/snippets/config/html_sanitizer.yaml +++ b/snippets/config/html_sanitizer.yaml @@ -1,3 +1,4 @@ +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940f","WATCHER_CONTAINS":"html_sanitizer"} shopware: html_sanitizer: sets: diff --git a/snippets/config/html_sanitizer_disabled.yaml b/snippets/config/html_sanitizer_disabled.yaml index d3acd43938..4cff2f22b0 100644 --- a/snippets/config/html_sanitizer_disabled.yaml +++ b/snippets/config/html_sanitizer_disabled.yaml @@ -1,3 +1,4 @@ shopware: html_sanitizer: enabled: false +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940f","WATCHER_CONTAINS":"html_sanitizer"} diff --git a/snippets/config/stock_disabled.yaml b/snippets/config/stock_disabled.yaml index e8f5b81be2..032a808b2f 100644 --- a/snippets/config/stock_disabled.yaml +++ b/snippets/config/stock_disabled.yaml @@ -1,3 +1,4 @@ +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940f","WATCHER_CONTAINS":"enable_stock_management"} # /config/packages/shopware.yaml shopware: stock: diff --git a/snippets/guide/app_database_setup.md b/snippets/guide/app_database_setup.md index 60f974b082..8345deb363 100644 --- a/snippets/guide/app_database_setup.md +++ b/snippets/guide/app_database_setup.md @@ -6,6 +6,7 @@ You can also use SQLite by setting the `DATABASE_URL` to `sqlite:///%kernel.proj After choosing your database engine, you need to require two extra composer packages. ```shell +# {"WATCHER_URL":"https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml","WATCHER_HASH":"183f85ba8f15e8e7d0006b70be20940f","WATCHER_CONTAINS":"html_sanitizer"} composer req symfony/maker-bundle migrations ``` diff --git a/watch.sh b/watch.sh new file mode 100644 index 0000000000..1c445d2bda --- /dev/null +++ b/watch.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# Set the path to the snippets folder +WATCHER_PATH='snippets' + +# Set the Slack webhook +SLACK_WEBHOOK='https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX' + +file_contains(){ + # Get the content of the URL + FILE_CONTENT=$(curl -sl $WATCHER_URL) + + # Check if $WATCHER_CONTAINS contains the string "null" and is therefore empty or not set + if [[ $WATCHER_CONTAINS == "null" ]]; then + exit 0 + fi + # Check if $WATCHER_CONTAINS contains the string $WATCHER_CONTAINS and if not call the Slack webhook + if [[ $FILE_CONTENT == *"$WATCHER_CONTAINS"* ]]; then + echo "String found - nice!" + else + echo "String not found - not nice!" + curl -X POST -H 'Content-type: application/json' --data '{"text":"String not found - not nice!"}' $SLACK_WEBHOOK + fi + } + +# Find all files in the snippets folder +find $WATCHER_PATH -type f -print0 | while IFS= read -r -d '' file; do + + # Check if the file contains the string "WATCHER_URL" and is therefore a watcher file + cat "$file" | grep 'WATCHER_URL' >> /dev/null || continue + + + # Get the JSON from the first line of the file + HEAD=$(cat "$file" | grep 'WATCHER_URL') + JSON=$(sed "s/# //g" <<< "$HEAD") + + # Get the values from the JSON + WATCHER_URL=$(echo $JSON | jq -r '.WATCHER_URL') + WATCHER_HASH=$(echo $JSON | jq -r '.WATCHER_HASH') + WATCHER_CONTAINS=$(echo $JSON | jq -r '.WATCHER_CONTAINS') + + # Get the hash of the URL + EVAL_HASH="curl -sl $WATCHER_URL | md5sum | cut -d ' ' -f 1" + CALLED_HASH="$(eval $EVAL_HASH)" + + # Check if the hashes are equal + if [ "$WATCHER_HASH" == "$CALLED_HASH" ]; then + echo "Both strings are equal." + else + echo "Strings are not equal." + file_contains + fi + + +done diff --git a/watcher-format.json b/watcher-format.json new file mode 100644 index 0000000000..78b52006be --- /dev/null +++ b/watcher-format.json @@ -0,0 +1,5 @@ +{ + "WATCHER_URL": 'https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/Resources/config/packages/shopware.yaml', + "WATCHER_HASH": "007d0a0b2d9ed8db6f484980b9a29459", + "WATCHER_CONTAINS": "html_sanitizer" +}