feat: import.sh: also parse tags
This commit is contained in:
parent
9b8e032f12
commit
e2881514d4
@ -9,13 +9,19 @@ if [ -z "$endpoint" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
read -p "Enter token: " -r token
|
||||
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
|
||||
read -p "Enter Directory: " -r directory
|
||||
if [ ! -d "$directory" ]; then
|
||||
log_error "[-] Not a Directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for problem in "$directory/"*; do
|
||||
if [ -d "$problem" ]; then
|
||||
dir_name=$(basename "$problem")
|
||||
log_info "[+] Importing problem $dir_name"
|
||||
@ -37,21 +43,37 @@ for problem in "$WORKSPACE/problem/"*; do
|
||||
continue
|
||||
fi
|
||||
|
||||
#######################
|
||||
# Get Problem Details #
|
||||
#######################
|
||||
|
||||
title=$(head -n 1 "$problem/description.md" | sed -e 's/^# //' | xargs)
|
||||
description=$(cat "$problem/description.md")
|
||||
# TODO: extract tags
|
||||
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" \
|
||||
'{ pid: 0, title: $title, statement: $description, is_enable: false }')
|
||||
--argjson tags "$tags" \
|
||||
'{ title: $title, statement: $description, tags: $tags }')
|
||||
|
||||
log_info "[*] Creating problem statement"
|
||||
response=$(curl -s \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: $token" \
|
||||
@ -69,7 +91,12 @@ for problem in "$WORKSPACE/problem/"*; do
|
||||
id=$(echo "$response" | jq -r '.body.meta.ID')
|
||||
log_info "[*] Problem statement created with id: $id"
|
||||
|
||||
log_info "[*] Uploading problem package"
|
||||
###################
|
||||
# Get Storage URL #
|
||||
###################
|
||||
|
||||
log_info "[*] Getting Upload URL"
|
||||
|
||||
response=$(curl -s \
|
||||
-H "Authorization: $token" \
|
||||
-X POST \
|
||||
@ -84,6 +111,13 @@ for problem in "$WORKSPACE/problem/"*; do
|
||||
|
||||
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
|
||||
@ -92,12 +126,17 @@ for problem in "$WORKSPACE/problem/"*; do
|
||||
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 }')
|
||||
|
||||
log_info "[*] Creating problem version"
|
||||
response=$(curl -s \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: $token" \
|
||||
|
Loading…
Reference in New Issue
Block a user