Skip to content

Commit

Permalink
Making class cleanup work again
Browse files Browse the repository at this point in the history
  • Loading branch information
amyschoen committed Apr 5, 2022
1 parent 8ac62b5 commit 03b2637
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions script/clean-up-class
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,40 @@
# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"

# shellcheck source=script/shared_functions
source ./shared_functions

#################################################################
# NOTE: You must have a personal access token (PAT) #
# saved to your environment variables to use this script. #
# We recommend a dedicated service account (e.g. githubteacher) #
#################################################################

# GLOBALS
COLLAB_REPO=$1

function GetNames() {
# Get the collabs
IFS=" " read -ra COLLABS <<<"$(
curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/collaborators?affiliation=direct&per_page=100" | jq -r 'map(.login) | unique | @sh' | tr -d \'
)" >>log.out 2>&1
# shell variables
collab_repo=$1
org_url="https://$ROOT_URL/$CLASS_ORG"
org_repos_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG"

# Clean up conflict repos based on collaborators in the inital class repo
for USER_NAME in "${COLLABS[@]}"; do
function delete_repos() {
for username in "${collaborators[@]:?}"; do
# Clean up conflict and games repos for the user
CleanUpRepo "conflict-practice-$USER_NAME"
CleanUpRepo "github-games-$USER_NAME"
delete_repo "conflict-practice" $username
delete_repo "github-games" $username
done
}

function CleanUpRepo() {
# Get the Repo
REPO=$1
function delete_repo() {
practice_repo_name=$1
student=$2

# Get repo status
REPO_STATUS=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$REPO" | grep "^Status:") >>log.out 2>&1

# Check the return
if
echo "$REPO_STATUS" | grep -iq "200"
then
# Remove the repo
./remove-repo "$REPO"
if repo_is_reachable "$org_url/$practice_repo_name-$student"; then
echo "Deleting $practice_repo_name-$student... "
# delete the existing practice repo
http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
DELETE "$org_repos_endpoint/$practice_repo_name-$student" >>log.out 2>&1
fi
}

# Get User names
GetNames
get_collaborators "$collab_repo"

delete_repos

0 comments on commit 03b2637

Please sign in to comment.