-
Notifications
You must be signed in to change notification settings - Fork 2
114 lines (89 loc) · 4.37 KB
/
create-sync.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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