refactor: harden the joj3-forge-convert script
All checks were successful
push / build (push) Successful in 51s

This commit is contained in:
张泊明518370910136 2025-06-25 09:04:01 -04:00
parent f128933252
commit ac4fbb83ed
GPG Key ID: CA088E6D9284F870

View File

@ -1,46 +1,67 @@
#!/usr/bin/bash
set -ex
#!/usr/bin/env bash
ORG_NAME=$(basename "$(dirname "$(pwd)")")
REPO_NAME=$(basename "$(pwd)")
set -euo pipefail
COURSE=${REPO_NAME%-*}
CONFIG_REPO_PATH="/home/tt/.cache/$COURSE-config"
GRADING_REPO_PATH="/home/tt/.cache/$COURSE-joj"
GIT_USER="bot-$COURSE"
GIT_EMAIL="bot-$COURSE@focs.ji.sjtu.edu.cn"
export PATH="/usr/bin:/bin:/usr/local/bin"
unset IFS
if ! [ -d "$CONFIG_REPO_PATH" ]; then
git clone "ssh://git@focs.ji.sjtu.edu.cn:2222/$COURSE/$COURSE-joj.git" "$CONFIG_REPO_PATH"
main() {
local org_name
local repo_name
local course
org_name=$(basename "$(dirname "$(pwd)")")
repo_name=$(basename "$(pwd)")
course=${repo_name%-*}
if ! [[ "$course" =~ ^[a-zA-Z0-9_-]+$ ]]; then
exit 1
fi
local config_repo_path="/home/tt/.cache/$course-config"
local grading_repo_path="/home/tt/.cache/$course-joj"
local git_user="bot-$course"
local git_email="bot-$course@focs.ji.sjtu.edu.cn"
local git_server="ssh://git@focs.ji.sjtu.edu.cn:2222"
if ! [ -d "$config_repo_path" ]; then
git clone "$git_server/$course/$course-config.git" "$config_repo_path"
fi
# TODO: move this into if statement
git -C "$CONFIG_REPO_PATH" config user.name "$GIT_USER"
git -C "$CONFIG_REPO_PATH" config user.email "$GIT_EMAIL"
git -C "$config_repo_path" config user.name "$git_user"
git -C "$config_repo_path" config user.email "$git_email"
if ! [ -d "$GRADING_REPO_PATH" ]; then
git clone "ssh://git@focs.ji.sjtu.edu.cn:2222/$COURSE/$COURSE-joj.git" "$GRADING_REPO_PATH"
git -C "$GRADING_REPO_PATH" switch --orphan grading
echo "# $COURSE JOJ grading" >"$GRADING_REPO_PATH/Readme.md"
echo "This branch is automatically updated by JOJ, **never edit any file in this branch!**" >>"$GRADING_REPO_PATH/Readme.md"
git -C "$GRADING_REPO_PATH" add Readme.md
git -C "$GRADING_REPO_PATH" commit -m"docs: readme"
git -C "$GRADING_REPO_PATH" push -u origin grading
if ! [ -d "$grading_repo_path" ]; then
git clone "$git_server/$course/$course-joj.git" "$grading_repo_path"
git -C "$grading_repo_path" switch --orphan grading
cat >"$grading_repo_path/Readme.md" <<EOF
# $course JOJ grading
This branch is automatically updated by JOJ, **never edit any file in this branch!**
EOF
git -C "$grading_repo_path" add Readme.md
git -C "$grading_repo_path" commit -m"docs: readme"
git -C "$grading_repo_path" push -u origin grading
fi
# TODO: move this into if statement
git -C "$GRADING_REPO_PATH" config user.name "$GIT_USER"
git -C "$GRADING_REPO_PATH" config user.email "$GIT_EMAIL"
git -C "$grading_repo_path" config user.name "$git_user"
git -C "$grading_repo_path" config user.email "$git_email"
git -C "$CONFIG_REPO_PATH" pull --rebase
rsync -r --delete "$CONFIG_REPO_PATH/home/tt/.config/" /home/tt/.config
joj3-forge convert /home/tt/.config/joj
rsync -r --delete /home/tt/.config/joj/ "$CONFIG_REPO_PATH/home/tt/.config/joj"
git -C "$CONFIG_REPO_PATH" add home/tt/.config/joj
if ! git -C "$CONFIG_REPO_PATH" diff --staged --quiet; then
git -C "$CONFIG_REPO_PATH" commit -m "chore: joj3-forge convert [skip ci]"
git -C "$CONFIG_REPO_PATH" push
git -C "$config_repo_path" pull --rebase
rsync -a --delete "$config_repo_path/home/tt/.config/" "/home/tt/.config"
joj3-forge-convert "/home/tt/.config/joj"
rsync -a --delete "/home/tt/.config/joj/" "$config_repo_path/home/tt/.config/joj"
git -C "$config_repo_path" add home/tt/.config/joj
if ! git -C "$config_repo_path" diff --staged --quiet; then
git -C "$config_repo_path" commit -m "chore: joj3-forge convert [skip ci]"
git -C "$config_repo_path" push
fi
if [ -n "$TEAPOT_GITEA_TOKEN" ]; then
mkdir -p /home/tt/.config/teapot
echo "GITEA_ORG_NAME=$ORG_NAME" >"/home/tt/.config/teapot/teapot.env"
echo "GITEA_ACCESS_TOKEN=$TEAPOT_GITEA_TOKEN" >>"/home/tt/.config/teapot/teapot.env"
if [ -n "${TEAPOT_GITEA_TOKEN:-}" ]; then
local teapot_config_dir="/home/tt/.config/teapot"
mkdir -p "$teapot_config_dir"
cat >"$teapot_config_dir/teapot.env" <<EOF
GITEA_ORG_NAME=$org_name
GITEA_ACCESS_TOKEN=$TEAPOT_GITEA_TOKEN
EOF
fi
}
main "$@"