#!/usr/bin/env bash WORKSPACE=$(cd "$(dirname "$0")"/.. && pwd) . "$WORKSPACE/scripts/common.sh" 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" fi 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" fi 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" 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 ####################### # Get Problem Details # ####################### title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs) description=$(cat "$problem/description.md") tags=$(sed -n '/^## Tags/,/^##/p' "$problem/description.md" | grep -Ev '^##|^$' | sed -e 's/^- //' | xargs | jq -Rc 'split(" ")') log_info "[*] Title: $title" 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 .. ############################ # Create Problem Statement # ############################ log_info "[*] Creating problem statement" payload=$(jq -nc \ --arg title "$title" \ --arg description "$description" \ --argjson tags "$tags" \ '{ title: $title, statement: $description, tags: $tags }') 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" ################### # 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') ################## # Upload Problem # ################## log_info "[*] Uploading Problem Package" curl -s -X PUT -T "$zip_file" "$upload_url" # 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 ########################## # Create Problem Version # ########################## log_info "[*] Creating problem version" payload=$(jq -nc \ --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") 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