feat: generate bot and ar tokens

This commit is contained in:
bot-ece477 2026-03-17 18:56:51 +08:00
parent 963ccbe956
commit 2541c83de7

View File

@ -6,11 +6,15 @@ echoerr() { ERR=$1; shift; echo -e "Error: $@" 1>&2; exit $ERR; }
# exit if no course provided
[ -z $1 ] && echoerr 255 "Usage: $0 coursecode"
COURSE=$1
COURSECFG=$HOME/$COURSE
COURSECFG=$HOME/courses/$COURSE
GITEAAPI="https://focs.ji.sjtu.edu.cn/git/api/v1"
read -n 1 -p "Before proceeding confirm that $COURSE organization, $COURSE-joj repository, and bot-$COURSE user exist: [y/N] " READY
[ "x$READY" = "xy" ] || { echo; exit; }
# dir for course config/setup
[ -d $COURSECFG ] || mkdir $COURSECFG
@ -28,20 +32,21 @@ fs_create() {
# generate token (write user: ssh key, read org: action runner token)
# create course-joj action secret for teapot
gitea_config() {
if [ ! -e $COURSECFG/teapot.env ]; then
echo "Configuring Gitea access"
read -s -p "Input bot-$COURSE passwd: " BOTPWD
GTOKEN=$(curl -s -X POST -u "bot-$COURSE:$BOTPWD" "$GITEAAPI/users/bot-$COURSE/tokens" -H "Content-Type: application/json" -d '{"name":"teapot","scopes":["write:issue", "read:organization", "write:repository", "write:user"]}' | jq -r '.sha1')
echo -e "GITEA_ORG_NAME=$COURSE\nGITEA_ACCESS_TOKEN=$GTOKEN" > $COURSECFG/teapot.env
. $COURSECFG/teapot.env
curl -s -X PUT "$GITEAAPI/repos/$COURSE/$COURSE-joj/actions/secrets/TEAPOT_GITEA_TOKEN" -H "Content-Type: application/json" -d "{\"data\":\"$GITEA_ACCESS_TOKEN\"}" -H "Authorization: Bearer $GITEA_ACCESS_TOKEN"
else
. $COURSECFG/teapot.env
fi
. $COURSECFG/teapot.env
curl -s -X PUT "$GITEAAPI/repos/$COURSE/$COURSE-joj/actions/secrets/TEAPOT_GITEA_TOKEN" -H "Content-Type: application/json" -d "{\"data\":\"$GITEA_ACCESS_TOKEN\"}" -H "Authorization: Bearer $GITEA_ACCESS_TOKEN"
}
@ -60,11 +65,10 @@ ssh_config() {
}
ar_preconfig() {
echo "Getting an act_runner token"
curl -X GET -s "$GITEAAPI/orgs/$COURSE/actions/runners/registration-token?token=$GITEA_ACCESS_TOKEN" | jq -r '.token' > $COURSECFG/act_runner.token
if [ ! -e $COURSECFG/act_runner.token ]; then
echo "Getting an act_runner token"
curl -X GET -s "$GITEAAPI/orgs/$COURSE/actions/runners/registration-token?token=$GITEA_ACCESS_TOKEN" | jq -r '.token' > $COURSECFG/act_runner.token
fi
}