feat: import.sh: also parse tags

This commit is contained in:
Paul Pan 2023-12-31 16:07:02 +08:00
parent 9b8e032f12
commit e2881514d4
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4

View File

@ -9,13 +9,19 @@ if [ -z "$endpoint" ]; then
exit 1 exit 1
fi fi
read -p "Enter token: " -r token read -p "Enter Token: " -r token
if [ -z "$token" ]; then if [ -z "$token" ]; then
log_error "[-] Token cannot be empty" log_error "[-] Token cannot be empty"
exit 1 exit 1
fi 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 if [ -d "$problem" ]; then
dir_name=$(basename "$problem") dir_name=$(basename "$problem")
log_info "[+] Importing problem $dir_name" log_info "[+] Importing problem $dir_name"
@ -37,21 +43,37 @@ for problem in "$WORKSPACE/problem/"*; do
continue continue
fi fi
#######################
# Get Problem Details #
#######################
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 tags=$(sed -n '/^## Tags/,/^##/p' "$problem/description.md" | grep -Ev '^##|^$' | sed -e 's/^- //' | xargs | jq -Rc 'split(" ")')
log_info "[*] Title: $title" log_info "[*] Title: $title"
log_info "[*] Tags: $tags"
####################
# Compress Problem #
####################
zip_file=$(mktemp -u --suffix .zip) zip_file=$(mktemp -u --suffix .zip)
log_info "[*] Compressing $problem into $zip_file" >/dev/null log_info "[*] Compressing $problem into $zip_file" >/dev/null
cd "$problem" && zip -9rq "$zip_file" . -x ".mark.prebuild" && cd .. cd "$problem" && zip -9rq "$zip_file" . -x ".mark.prebuild" && cd ..
############################
# Create Problem Statement #
############################
log_info "[*] Creating problem statement"
payload=$(jq -nc \ payload=$(jq -nc \
--arg title "$title" \ --arg title "$title" \
--arg description "$description" \ --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 \ response=$(curl -s \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: $token" \ -H "Authorization: $token" \
@ -69,7 +91,12 @@ for problem in "$WORKSPACE/problem/"*; do
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" ###################
# Get Storage URL #
###################
log_info "[*] Getting Upload URL"
response=$(curl -s \ response=$(curl -s \
-H "Authorization: $token" \ -H "Authorization: $token" \
-X POST \ -X POST \
@ -84,6 +111,13 @@ 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')
##################
# Upload Problem #
##################
log_info "[*] Uploading Problem Package"
curl -s -X PUT -T "$zip_file" "$upload_url" curl -s -X PUT -T "$zip_file" "$upload_url"
# shellcheck disable=SC2181 # shellcheck disable=SC2181
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -92,12 +126,17 @@ for problem in "$WORKSPACE/problem/"*; do
continue continue
fi fi
##########################
# Create Problem Version #
##########################
log_info "[*] Creating problem version"
payload=$(jq -nc \ payload=$(jq -nc \
--argjson pid "$id" \ --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 }')
log_info "[*] Creating problem version"
response=$(curl -s \ response=$(curl -s \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: $token" \ -H "Authorization: $token" \