Merge pull request #155 from kudos-ink/add-simode-hyperbridge #109
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Process JSON File Changes | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'data/projects/*.json' | |
jobs: | |
process_json: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 2 | |
- name: Determine file status and content differences | |
run: | | |
FILE_CHANGE=$(git diff --name-status ${{ github.event.before }} ${{ github.event.after }} | grep data/projects/.*\.json) | |
FILE_STATUS=$(echo $FILE_CHANGE | awk '{print $1}') | |
FILE_PATH=$(echo $FILE_CHANGE | awk '{print $2}') | |
if [ "$FILE_STATUS" == "A" ]; then | |
echo "Action: Create new entry for $FILE_PATH" | |
PROJECT=$(jq '.' "$FILE_PATH") | |
SLUG=$(jq '.slug' "$FILE_PATH") | |
RESPONSE=$(curl -s -X POST "${{ secrets.CREATE_URL }}" \ | |
-H "Content-Type: application/json" \ | |
-d "$PROJECT") | |
echo "Response from POST request: $RESPONSE" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/project-infos-${SLUG}" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/all-projects" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/projects" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/project-options" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
elif [ "$FILE_STATUS" == "M" ]; then | |
git show ${{ github.event.before }}:"$FILE_PATH" > old_file.json | |
git show ${{ github.event.after }}:"$FILE_PATH" > new_file.json | |
OLD_REPOS=$(jq -S '.links.repository | sort_by(.label, .url)' old_file.json) | |
NEW_REPOS=$(jq -S '.links.repository | sort_by(.label, .url)' new_file.json) | |
SYNC="false" | |
REPOS_TO_REMOVE="[]" | |
REPOS_TO_ADD="[]" | |
if [ "$OLD_REPOS" != "$NEW_REPOS" ]; then | |
SYNC="true" | |
REPOS_TO_REMOVE=$(echo "$OLD_REPOS" | jq --argjson new "$NEW_REPOS" '. - $new') | |
REPOS_TO_ADD=$(echo "$NEW_REPOS" | jq --argjson old "$OLD_REPOS" '. - $old') | |
fi | |
OLD_ATTRS=$(jq '.attributes' old_file.json | jq -S .) | |
NEW_ATTRS=$(jq '.attributes' new_file.json | jq -S .) | |
UPDATE="false" | |
if [ "$OLD_ATTRS" != "$NEW_ATTRS" ]; then | |
UPDATE="true" | |
fi | |
if [ "$SYNC" == "true" ] || [ "$UPDATE" == "true" ]; then | |
PROJECT_NAME=$(jq -r '.name' new_file.json) | |
PROJECT_SLUG=$(jq -r '.slug' new_file.json) | |
PAYLOAD=$(jq -n \ | |
--arg project_slug "$PROJECT_SLUG" \ | |
--arg project_name "$PROJECT_NAME" \ | |
--argjson repos_to_add "$REPOS_TO_ADD" \ | |
--argjson repos_to_remove "$REPOS_TO_REMOVE" \ | |
'{ | |
project_slug: $project_slug, | |
project_name: $project_name, | |
repos_to_add: $repos_to_add, | |
repos_to_remove: $repos_to_remove | |
}' | |
) | |
if [ "$UPDATE" == "true" ]; then | |
PAYLOAD=$(echo "$PAYLOAD" | jq --argjson attributes "$NEW_ATTRS" '. + {attributes: $attributes}') | |
fi | |
curl -X POST -H "Content-Type: application/json" -d "$PAYLOAD" "${{ secrets.SYNC_URL }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/project-infos-${PROJECT_SLUG}" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/all-projects" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/projects" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
curl --request POST "${{ vars.REVALIDATE_CACHE_URL }}/project-options" \ | |
--header "${{ secrets.AUTH_HEADER }}" | |
fi | |
if [ "$SYNC" == "false" ] && [ "$UPDATE" == "false" ]; then | |
echo "No significant changes detected; no action taken." | |
fi | |
else | |
echo "File deleted or other non-applicable change detected; no action taken." | |
fi | |