Skip to content

Commit 73722a0

Browse files
committed
update script to loop over file
1 parent 9c8d8fa commit 73722a0

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

scripts/2_repo_fetch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"path_to_folder_to_synchronize_from": "https://github.com/InseeFrLab/satellite-images-docs/tree/main/cards",
4-
"last_commit_sha": "",
4+
"last_commit_sha": "862f2dc2f0c12d62a38b62de48d096452a1685ca",
55
"path_to_folder_to_synchronize_to": "project/2022_satellites/cards",
66
"replacements": [
77
{
@@ -28,7 +28,7 @@
2828
},
2929
{
3030
"path_to_folder_to_synchronize_from": "https://github.com/InseeFrLab/codif-ape-prez/tree/main/cards",
31-
"last_commit_sha": "",
31+
"last_commit_sha": "0d474addc0735a610ecb7cc54bd5362114446925",
3232
"path_to_folder_to_synchronize_to": "project/2022_codif_ape/cards",
3333
"replacements": [
3434
{

scripts/2_synchronize.sh

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Install jq if not present
2-
sudo apt-get install jq
2+
# sudo apt-get install jq
33

44
# Function to extract owner, repository name, and path after main/ from a GitHub URL
55
extract_github_info() {
66
local url="$1"
77

88
# Extract owner, repository name, and path after main/
9-
local owner=$(echo "$url" | awk -F'/' '{print $4}')
10-
local repo=$(echo "$url" | awk -F'/' '{print $5}')
11-
local path=$(echo "$url" | awk -F'main/' '{print $2}')
9+
local owner=$(echo "$url" | awk -F'/' '{print $4}' | tr -d '"')
10+
local repo=$(echo "$url" | awk -F'/' '{print $5}' | tr -d '"')
11+
local path=$(echo "$url" | awk -F'main/' '{print $2}' | tr -d '"') # tr -d to remove trailing "
1212

1313
# Print the extracted information
1414
echo "$owner $repo $path"
@@ -23,7 +23,7 @@ get_last_commit() {
2323
local output=$(curl -L \
2424
-H "Accept: application/vnd.github+json" \
2525
-H "X-GitHub-Api-Version: 2022-11-28" \
26-
"https://api.github.com/repos/$owner/$repo/commits?path=$path")
26+
"https://api.github.com/repos/$owner/$repo/commits?path=$path" --silent)
2727

2828
echo "$output" | jq --raw-output '.[0].sha'
2929
}
@@ -37,7 +37,8 @@ clone_repo() {
3737

3838
mkdir temp
3939
cd temp
40-
git clone "https://github.com/$owner_git/$repo_git"
40+
echo "cloning https://github.com/$owner_git/$repo_git"
41+
git clone "https://github.com/$owner_git/$repo_git" --quiet --depth 1 --single-branch # To clone only last commit for efficiency and main branch
4142
rm -rf "../$path_ssphub"
4243
cp "$repo_git/$subfolder_git/" "../$path_ssphub" -rf
4344
cd ../
@@ -71,7 +72,7 @@ replace_in_qmd_files() {
7172
update_commit_sha() {
7273
local old_commit_sha="$1"
7374
local new_commit_sha="$2"
74-
local json_file="scripts/2_repo_fetch.json"
75+
local json_file="$3"
7576

7677
# Check if the JSON file exists
7778
if [ ! -f "$json_file" ]; then
@@ -84,7 +85,7 @@ update_commit_sha() {
8485
'.[] |= if .last_commit_sha == $old_sha then .last_commit_sha = $new_sha else . end' \
8586
"$json_file" > tmp.json && mv tmp.json "$json_file"
8687

87-
echo "Updated last_commit_sha from $old_commit_sha to $new_commit_sha in $json_file"
88+
echo "Updated last_commit_sha from ${old_commit_sha:0:6}... to ${new_commit_sha:0:6}... in $json_file"
8889
}
8990

9091

@@ -100,11 +101,16 @@ main() {
100101
fi
101102

102103
# Read the JSON file and process each entry
103-
jq -c '.[]' "$json_file" | while read -r entry; do
104-
local path_to_folder_to_synchronize_from=$(echo "$entry" | jq -r '.path_to_folder_to_synchronize_from')
105-
local last_commit_sha=$(echo "$entry" | jq -r '.last_commit_sha')
106-
local path_to_folder_to_synchronize_to=$(echo "$entry" | jq -r '.path_to_folder_to_synchronize_to')
107-
local replacements=$(echo "$entry" | jq -c '.replacements')
104+
# Initialization for number of elements to loop over the file
105+
declare -i nb_elem
106+
nb_elem=$(jq 'length' "$json_file")
107+
108+
for ((i=1; i <=nb_elem; i++)); do
109+
# Extract values of ith element of the file
110+
local path_to_folder_to_synchronize_from=$(jq --argjson index "$((i-1))" '.[$index].path_to_folder_to_synchronize_from' "$json_file")
111+
local last_commit_sha=$(jq --argjson index "$((i-1))" '.[$index].last_commit_sha' "$json_file" | tr -d '"')
112+
local path_to_folder_to_synchronize_to=$(jq --argjson index "$((i-1))" '.[$index].path_to_folder_to_synchronize_to' "$json_file" | tr -d '"')
113+
local replacements=$(jq --argjson index "$((i-1))" '.[$index].replacements' "$json_file")
108114

109115
# Extract owner, repo, and path after main/
110116
local info=$(extract_github_info "$path_to_folder_to_synchronize_from")
@@ -117,32 +123,47 @@ main() {
117123

118124
# Check if the last commit SHA is different
119125
if [ "$last_commit_sha" != "$new_commit_sha" ]; then
120-
echo "New commit found for $path_to_folder_to_synchronize_from"
126+
echo "Commit "${new_commit_sha:0:6}" found for $path_to_folder_to_synchronize_from"
127+
128+
# Debug
129+
echo "will perform the cloning with params
130+
last commit : "$last_commit_sha"
131+
new_commit_sha : "$new_commit_sha"
132+
owner: "$owner"
133+
repo: "$repo"
134+
path: "$path"
135+
path_to_folder_to_synchronize_to: "$path_to_folder_to_synchronize_to"
136+
replacement: "${replacements:0:20}""
137+
138+
# # Delete branch
139+
git branch -D origin/auto_fetch
140+
git branch -d auto_fetch
121141

122142
# Create a branch only if it hasn't been created yet
123143
if [ "$branch_created" = false ]; then
124144
git pull origin fusion_site_ssplab
125145
git checkout -b auto_fetch
126-
git push -u origin auto_fetch
146+
# git push -u origin auto_fetch
127147
branch_created=true
128-
git checkout auto_fetch
129148
fi
130149

150+
git checkout auto_fetch
151+
131152
# Clone the repository and move the subfolder
132153
clone_repo "$owner" "$repo" "$path" "$path_to_folder_to_synchronize_to"
133154

134155
# Replace patterns in .qmd files
135156
replace_in_qmd_files "$path_to_folder_to_synchronize_to" "$replacements"
136157

137158
# Update the last commit SHA in the JSON file
138-
update_commit_sha "$last_commit_sha" "$new_commit_sha"
159+
update_commit_sha "$last_commit_sha" "$new_commit_sha" "$json_file"
139160

140161
# Commit the changes
141162
git add "$path_to_folder_to_synchronize_to" "$json_file"
142163
git commit -m "Update $path_to_folder_to_synchronize_to based on commit $new_commit_sha made to $path_to_folder_to_synchronize_from"
143-
git push
164+
# # git push
144165
else
145-
echo "No new commit found for $path_to_folder_to_synchronize_from"
166+
echo "No new commit since ${last_commit_sha:0:6} found for $path_to_folder_to_synchronize_from"
146167
fi
147168
done
148169
}

0 commit comments

Comments
 (0)