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

175 lines
5.1 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd)
. "$WORKSPACE"/scripts/common.sh
2024-01-05 15:03:04 +08:00
if [ -z "$WOJ_ENDPOINT" ]; then
read -p "Enter HTTP API Endpoint: " -r endpoint
if [ -z "$endpoint" ]; then
log_error "[-] HTTP API Endpoint cannot be empty"
exit 1
fi
else
endpoint="$WOJ_ENDPOINT"
log_info "[*] Using HTTP API Endpoint: $endpoint"
2023-12-27 22:48:19 +08:00
fi
2024-01-05 15:03:04 +08:00
if [ -z "$WOJ_TOKEN" ]; then
read -p "Enter Token: " -r token
if [ -z "$token" ]; then
log_error "[-] Token cannot be empty"
exit 1
fi
else
token="$WOJ_TOKEN"
log_info "[*] Using Token: $token"
2023-12-27 22:48:19 +08:00
fi
2024-01-05 15:03:04 +08:00
if [ -z "$WOJ_DIR" ]; then
read -p "Enter Directory: " -r directory
if [ ! -d "$directory" ]; then
log_error "[-] Not a Directory"
exit 1
fi
else
directory="$WOJ_DIR"
log_info "[*] Using Directory: $directory"
2023-12-31 16:07:02 +08:00
fi
for problem in "$directory/"*; 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
2023-12-31 16:07:02 +08:00
#######################
# Get Problem Details #
#######################
title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs)
description=$(cat "$problem/description.md")
2023-12-31 16:07:02 +08:00
tags=$(sed -n '/^## Tags/,/^##/p' "$problem/description.md" | grep -Ev '^##|^$' | sed -e 's/^- //' | xargs | jq -Rc 'split(" ")')
log_info "[*] Title: $title"
2023-12-31 16:07:02 +08:00
log_info "[*] Tags: $tags"
####################
# Compress Problem #
####################
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 ..
2023-12-31 16:07:02 +08:00
############################
# Create Problem Statement #
############################
log_info "[*] Creating problem statement"
payload=$(jq -nc \
--arg title "$title" \
--arg description "$description" \
2023-12-31 16:07:02 +08:00
--argjson tags "$tags" \
'{ title: $title, statement: $description, tags: $tags }')
2023-12-27 22:48:19 +08:00
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"
2023-12-31 16:07:02 +08:00
###################
# Get Storage URL #
###################
log_info "[*] Getting Upload URL"
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')
2023-12-31 16:07:02 +08:00
##################
# Upload Problem #
##################
log_info "[*] Uploading Problem Package"
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
2023-12-31 16:07:02 +08:00
##########################
# Create Problem Version #
##########################
log_info "[*] Creating problem version"
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 }')
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