From 7414c0cad1bffcd65589e546addbc359bb41f3f2 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Wed, 27 Dec 2023 22:48:19 +0800 Subject: [PATCH] fix: full import.sh --- resource/runner/scripts/import.sh | 87 ++++++++++++++++++------------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/resource/runner/scripts/import.sh b/resource/runner/scripts/import.sh index 55e287c..bf256e4 100755 --- a/resource/runner/scripts/import.sh +++ b/resource/runner/scripts/import.sh @@ -3,20 +3,17 @@ 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 +read -p "Enter HTTP API Endpoint: " -r endpoint +if [ -z "$endpoint" ]; then + log_error "[-] HTTP API Endpoint cannot be empty" + exit 1 +fi -endpoint="http://localhost:8000" -token="eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwicm9sZSI6MzAsInZlcnNpb24iOjEsImV4cCI6MTcwNDAwMzg4MCwibmJmIjoxNzAzMzk5MDgwLCJpYXQiOjE3MDMzOTkwODAsImp0aSI6ImVoOU92dVVNRktaZnV3ZngifQ.5F92odFZ-KZolHNIAc5f93exyugTBt5_4PpjYIdZxcHBfQRq_xAUOnJEdCfOBQtNy0-mfdvylkD7oR288Zew2w" +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 @@ -42,6 +39,7 @@ for problem in "$WORKSPACE/problem/"*; do title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs) description=$(cat "$problem/description.md") + # TODO: extract tags log_info "[*] Title: $title" zip_file=$(mktemp -u --suffix .zip) @@ -53,23 +51,23 @@ for problem in "$WORKSPACE/problem/"*; do --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") + 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 + 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" + id=$(echo "$response" | jq -r '.body.meta.ID') + log_info "[*] Problem statement created with id: $id" log_info "[*] Uploading problem package" response=$(curl -s \ @@ -87,17 +85,36 @@ for problem in "$WORKSPACE/problem/"*; do 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" + # 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 \ - --arg pid "$title" \ - --arg storage_key "$storage_key" \ - '{ pid: $pid, storage_key: $storage_key }') - + --argjson pid "$id" \ + --arg 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 done