Skip to content

Commit 537a812

Browse files
ginerstas-sbi
authored andcommitted
actions: Add workflow to fetch DSO configs daily
1 parent a02e2ab commit 537a812

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Fetch DSO Configs
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *" # Every day at midnight UTC
7+
8+
permissions: {}
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
defaults:
15+
run:
16+
shell: bash --noprofile --norc -euo pipefail {0}
17+
18+
jobs:
19+
fetch-configs-and-create-pr:
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 10
22+
permissions:
23+
contents: write
24+
pull-requests: write
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Fetch DSO configs
30+
id: fetch-dso-configs
31+
run: |
32+
result=$(scripts/fetch-dso-configs.sh)
33+
34+
{
35+
echo "result<<EOF"
36+
echo "$result"
37+
echo "EOF"
38+
} >> "$GITHUB_OUTPUT"
39+
40+
- name: Git config
41+
run: |
42+
git config user.name "github-actions[bot]"
43+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
44+
45+
- name: Git commit changes and push
46+
run: |
47+
git add configs
48+
49+
if git diff --cached --quiet; then
50+
echo "No changes to commit"
51+
exit 0
52+
fi
53+
54+
git commit -m "$COMMIT_MESSAGE"
55+
git push origin
56+
env:
57+
COMMIT_MESSAGE: |
58+
Update DSO configs
59+
60+
${{ steps.fetch-dso-configs.outputs.result }}

scripts/fetch-dso-configs.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
CURL_TIMEOUT=5
6+
CURL_CMD=(curl -fsS -m "$CURL_TIMEOUT")
7+
8+
ENVS_AND_DSO_URLS=(
9+
DevNet https://docs.dev.global.canton.network.sync.global/dso
10+
TestNet https://docs.test.global.canton.network.sync.global/dso
11+
MainNet https://docs.global.canton.network.sync.global/dso
12+
)
13+
14+
SCRIPTS_DIR=$(dirname "$0")
15+
CONFIGS_DIR="$SCRIPTS_DIR/../configs"
16+
17+
IS_LINUX=$([[ "$(uname -s)" == "Linux" ]] && echo true || echo false)
18+
19+
20+
fetch_dso_data() {
21+
local dso_response; dso_response=$(
22+
# "${CURL_CMD[@]}" -w '%{header_json}' "$dso_url" # NOTE: header_json is supported by curl >= 7.83.0, below is a workaround for older versions
23+
local response; response=$("${CURL_CMD[@]}" -i "$dso_url") || exit 1
24+
local response_body; response_body=$(echo "$response" | sed '1,/^\r*$/d')
25+
local response_header; response_header=$(echo "$response" | sed '/^\r*$/,$d')
26+
local header_last_modified; header_last_modified=$(echo "$response_header" | grep '^last-modified:' | sed 's/^last-modified: //' | jq -nR '{"last-modified": [inputs | rtrimstr("\r")]}')
27+
echo "$response_body$header_last_modified"
28+
) || { echo "ERROR: Unable to fetch DSO from $dso_url" >&2; return 1; }
29+
30+
[[ $(echo "$dso_response" | jq -s length) -eq 2 ]] ||
31+
{ echo "ERROR: Unable to parse the response from $dso_url" >&2; return 1; }
32+
33+
echo "$dso_response"
34+
}
35+
36+
fetch_dso_configs() {
37+
local env=$1
38+
local dso_url=$2
39+
40+
local configs_dir_env="$CONFIGS_DIR/$env"
41+
mkdir -p "$configs_dir_env"
42+
43+
local dso_rules_file="$configs_dir_env/dso-rules.json"
44+
local amulet_rules_file="$configs_dir_env/amulet-rules.json"
45+
46+
local dso_response; dso_response=$(fetch_dso_data "$dso_url") || return 1
47+
48+
local dso_data; dso_data=$(echo "$dso_response" | jq -s '.[0]')
49+
local header_json; header_json=$(echo "$dso_response" | jq -s '.[1]')
50+
local last_modified; last_modified=$(echo "$header_json" | jq -r '."last-modified" // empty | .[]')
51+
52+
if [[ -n $last_modified ]]; then
53+
local last_modified_seconds modified_seconds_ago
54+
55+
"$IS_LINUX" &&
56+
last_modified_seconds=$(date -d "$last_modified" +%s) ||
57+
last_modified_seconds=$(date -j -f "%a, %d %b %Y %T %Z" "$last_modified" +%s)
58+
59+
modified_seconds_ago=$(( $(date +%s) - last_modified_seconds ))
60+
fi
61+
62+
echo "INFO: DSO data for $env fetched successfully. Last modified: $last_modified ($modified_seconds_ago seconds ago)"
63+
64+
echo "$dso_data" | jq -S '.dso_rules' > "$dso_rules_file"
65+
echo "$dso_data" | jq -S '.amulet_rules' > "$amulet_rules_file"
66+
}
67+
68+
main() {
69+
for ((i = 0; i < ${#ENVS_AND_DSO_URLS[@]}; i += 2)); do
70+
env=${ENVS_AND_DSO_URLS[i]}
71+
dso_url=${ENVS_AND_DSO_URLS[i + 1]}
72+
fetch_dso_configs "$env" "$dso_url"
73+
done
74+
}
75+
76+
main "$@"

0 commit comments

Comments
 (0)