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