woj-server/resource/runner/scripts/import.sh

121 lines
3.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd)
. "$WORKSPACE"/scripts/common.sh
2023-12-27 22:48:19 +08:00
read -p "Enter HTTP API Endpoint: " -r endpoint
if [ -z "$endpoint" ]; then
log_error "[-] HTTP API Endpoint cannot be empty"
exit 1
fi
read -p "Enter token: " -r token
if [ -z "$token" ]; then
log_error "[-] Token cannot be empty"
exit 1
fi
for problem in "$WORKSPACE/problem/"*; do
if [ -d "$problem" ]; then
dir_name=$(basename "$problem")
log_info "[+] Importing problem $dir_name"
if [ ! -f "$problem/config.json" ]; then
log_warn "[-] Skipping: $dir_name/config.json not found"
continue
fi
if [ ! -f "$problem/description.md" ]; then
log_warn "[-] Skipping: $dir_name/description.md not found"
continue
fi
read -p "Are you sure you want to import $dir_name? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_warn "[-] Skipping: user cancelled"
continue
fi
title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs)
description=$(cat "$problem/description.md")
2023-12-27 22:48:19 +08:00
# TODO: extract tags
log_info "[*] Title: $title"
zip_file=$(mktemp -u --suffix .zip)
log_info "[*] Compressing $problem into $zip_file" >/dev/null
cd "$problem" && zip -9rq "$zip_file" . -x ".mark.prebuild" && cd ..
payload=$(jq -nc \
--arg title "$title" \
--arg description "$description" \
'{ pid: 0, title: $title, statement: $description, is_enable: false }')
2023-12-27 22:48:19 +08:00
log_info "[*] Creating problem statement"
response=$(curl -s \
-H "Content-Type: application/json" \
-H "Authorization: $token" \
-X POST \
-d "$payload" \
"$endpoint/api/v1/problem/update")
2023-12-27 22:48:19 +08:00
code=$(echo "$response" | jq -r '.code')
if [ "$code" != "0" ]; then
log_error "[-] Failed to create problem statement"
log_error "[-] Response: $response"
continue
fi
2023-12-27 22:48:19 +08:00
id=$(echo "$response" | jq -r '.body.meta.ID')
log_info "[*] Problem statement created with id: $id"
log_info "[*] Uploading problem package"
response=$(curl -s \
-H "Authorization: $token" \
-X POST \
"$endpoint/api/v1/problem/upload")
code=$(echo "$response" | jq -r '.code')
if [ "$code" != "0" ]; then
log_error "[-] Failed to get upload url"
log_error "[-] Response: $response"
continue
fi
upload_url=$(echo "$response" | jq -r '.body.url')
storage_key=$(echo "$response" | jq -r '.body.key')
curl -s -X PUT -T "$zip_file" "$upload_url"
2023-12-27 22:48:19 +08:00
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
log_error "[-] Failed to upload problem package"
echo curl -s -X PUT -T "$zip_file" "$upload_url"
continue
fi
payload=$(jq -nc \
2023-12-27 22:48:19 +08:00
--argjson pid "$id" \
--arg storage_key "$storage_key" \
'{ pid: $pid, storage_key: $storage_key }')
log_info "[*] Creating problem version"
response=$(curl -s \
-H "Content-Type: application/json" \
-H "Authorization: $token" \
-X POST \
-d "$payload" \
"$endpoint/api/v1/problem/create_version")
2023-12-27 22:48:19 +08:00
code=$(echo "$response" | jq -r '.code')
if [ "$code" != "0" ]; then
log_error "[-] Failed to create problem version"
log_error "[-] Payload: $payload"
log_error "[-] Response: $response"
continue
fi
2023-12-27 22:48:19 +08:00
log_info "[*] Problem version created"
log_info "[+] Problem $dir_name imported successfully"
fi
done