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

104 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd)
. "$WORKSPACE"/scripts/common.sh
# 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
endpoint="http://localhost:8000"
token="eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwicm9sZSI6MzAsInZlcnNpb24iOjEsImV4cCI6MTcwNDAwMzg4MCwibmJmIjoxNzAzMzk5MDgwLCJpYXQiOjE3MDMzOTkwODAsImp0aSI6ImVoOU92dVVNRktaZnV3ZngifQ.5F92odFZ-KZolHNIAc5f93exyugTBt5_4PpjYIdZxcHBfQRq_xAUOnJEdCfOBQtNy0-mfdvylkD7oR288Zew2w"
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")
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 }')
# 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")
# code=$(echo "$response" | jq -r '.code')
# if [ "$code" != "0" ]; then
# log_error "[-] Failed to create problem statement"
# log_error "[-] Response: $response"
# continue
# fi
# 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"
log_info "[*] Creating problem version"
payload=$(jq -nc \
--arg pid "$title" \
--arg storage_key "$storage_key" \
'{ pid: $pid, storage_key: $storage_key }')
echo "$response"
fi
done