Skip to content

Commit

Permalink
Test watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Isengo1989 committed Dec 6, 2023
1 parent d8b2677 commit 864acf5
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/scripts/watch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# Set the path to the snippets folder
WATCHER_PATH=$1

output() {
echo "$1"
}

file_contains(){
local ERROR_FOUND=0
# 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
output "No contain set"
echo 1
fi
# Check if $WATCHER_CONTAINS contains the string $WATCHER_CONTAINS and if not call the Slack webhook
if [[ $FILE_CONTENT == *"$WATCHER_CONTAINS"* ]]; then
output "String found!"
else
output echo "String not found! Please check $WATCHER_URL"
echo 1
fi
}

error_found() {
ERROR_FOUND=$1
echo "Error found: $ERROR_FOUND"
if [ "$ERROR_FOUND" -eq 1 ]
then
echo "Error found, exiting..."
exit 1
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."
ERROR_FOUND="$(file_contains)"
fi

done

error_found $ERROR_FOUND
41 changes: 41 additions & 0 deletions .github/workflows/watcher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Check files for changes
on:
pull_request:
workflow_dispatch:
inputs:
path:
description: "Path to watch"
required: true
type: string

jobs:
check-files:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
bash .github/scripts/watch.sh snippets
bash .github/scripts/watch.sh guides
id: watcher
continue-on-error: true
- name: Send custom JSON data to Slack workflow
if: steps.watcher.outcome != 'success'
id: slack
uses: slackapi/[email protected]
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
2 changes: 2 additions & 0 deletions guides/hosting/infrastructure/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions guides/hosting/infrastructure/message-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions snippets/config/html_sanitizer.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 1 addition & 0 deletions snippets/config/html_sanitizer_disabled.yaml
Original file line number Diff line number Diff line change
@@ -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"}
1 change: 1 addition & 0 deletions snippets/config/stock_disabled.yaml
Original file line number Diff line number Diff line change
@@ -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"}
# <project root>/config/packages/shopware.yaml
shopware:
stock:
Expand Down
1 change: 1 addition & 0 deletions snippets/guide/app_database_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
55 changes: 55 additions & 0 deletions watch.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions watcher-format.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 864acf5

Please sign in to comment.