fix: full import.sh

This commit is contained in:
Paul Pan 2023-12-27 22:48:19 +08:00
parent cc2c42bded
commit 7414c0cad1
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4

View File

@ -3,20 +3,17 @@
WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd) WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd)
. "$WORKSPACE"/scripts/common.sh . "$WORKSPACE"/scripts/common.sh
# read -p "Enter HTTP API Endpoint: " -r endpoint read -p "Enter HTTP API Endpoint: " -r endpoint
# if [ -z "$endpoint" ]; then if [ -z "$endpoint" ]; then
# log_error "[-] HTTP API Endpoint cannot be empty" log_error "[-] HTTP API Endpoint cannot be empty"
# exit 1 exit 1
# fi fi
#
# read -p "Enter token: " -r token
# if [ -z "$token" ]; then
# log_error "[-] Token cannot be empty"
# exit 1
# fi
endpoint="http://localhost:8000" read -p "Enter token: " -r token
token="eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwicm9sZSI6MzAsInZlcnNpb24iOjEsImV4cCI6MTcwNDAwMzg4MCwibmJmIjoxNzAzMzk5MDgwLCJpYXQiOjE3MDMzOTkwODAsImp0aSI6ImVoOU92dVVNRktaZnV3ZngifQ.5F92odFZ-KZolHNIAc5f93exyugTBt5_4PpjYIdZxcHBfQRq_xAUOnJEdCfOBQtNy0-mfdvylkD7oR288Zew2w" if [ -z "$token" ]; then
log_error "[-] Token cannot be empty"
exit 1
fi
for problem in "$WORKSPACE/problem/"*; do for problem in "$WORKSPACE/problem/"*; do
if [ -d "$problem" ]; then if [ -d "$problem" ]; then
@ -42,6 +39,7 @@ for problem in "$WORKSPACE/problem/"*; do
title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs) title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs)
description=$(cat "$problem/description.md") description=$(cat "$problem/description.md")
# TODO: extract tags
log_info "[*] Title: $title" log_info "[*] Title: $title"
zip_file=$(mktemp -u --suffix .zip) zip_file=$(mktemp -u --suffix .zip)
@ -53,23 +51,23 @@ for problem in "$WORKSPACE/problem/"*; do
--arg description "$description" \ --arg description "$description" \
'{ pid: 0, title: $title, statement: $description, is_enable: false }') '{ pid: 0, title: $title, statement: $description, is_enable: false }')
# log_info "[*] Creating problem statement" log_info "[*] Creating problem statement"
# response=$(curl -s \ response=$(curl -s \
# -H "Content-Type: application/json" \ -H "Content-Type: application/json" \
# -H "Authorization: $token" \ -H "Authorization: $token" \
# -X POST \ -X POST \
# -d "$payload" \ -d "$payload" \
# "$endpoint/api/v1/problem/update") "$endpoint/api/v1/problem/update")
# code=$(echo "$response" | jq -r '.code') code=$(echo "$response" | jq -r '.code')
# if [ "$code" != "0" ]; then if [ "$code" != "0" ]; then
# log_error "[-] Failed to create problem statement" log_error "[-] Failed to create problem statement"
# log_error "[-] Response: $response" log_error "[-] Response: $response"
# continue continue
# fi fi
# id=$(echo "$response" | jq -r '.body.meta.ID') id=$(echo "$response" | jq -r '.body.meta.ID')
# log_info "[*] Problem statement created with id: $id" log_info "[*] Problem statement created with id: $id"
log_info "[*] Uploading problem package" log_info "[*] Uploading problem package"
response=$(curl -s \ response=$(curl -s \
@ -87,17 +85,36 @@ for problem in "$WORKSPACE/problem/"*; do
upload_url=$(echo "$response" | jq -r '.body.url') upload_url=$(echo "$response" | jq -r '.body.url')
storage_key=$(echo "$response" | jq -r '.body.key') storage_key=$(echo "$response" | jq -r '.body.key')
curl -s -X PUT -T "$zip_file" "$upload_url" curl -s -X PUT -T "$zip_file" "$upload_url"
# shellcheck disable=SC2181
log_info "[*] Creating problem version" 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 \ payload=$(jq -nc \
--arg pid "$title" \ --argjson pid "$id" \
--arg storage_key "$storage_key" \ --arg storage_key "$storage_key" \
'{ pid: $pid, storage_key: $storage_key }') '{ pid: $pid, storage_key: $storage_key }')
echo "$response" 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")
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
log_info "[*] Problem version created"
log_info "[+] Problem $dir_name imported successfully"
fi fi
done done