-
Notifications
You must be signed in to change notification settings - Fork 282
/
reset-game
executable file
·140 lines (110 loc) · 4.47 KB
/
reset-game
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
#
# Reset Game
# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"
#################################################################
# 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) #
#################################################################
# cd into the script directory so relative
# paths can be used in the functions above
cd "${BASH_SOURCE%/*}/" || exit
# shellcheck source=script/ask
source ./ask
function ResetActivity() {
# Pull in the vars
REPO_NAME=$1
DEFAULT_BRANCH=$2
# Make sure no one is teaching class
if ask "Is anyone teaching a class right now using $TOKEN_OWNER?"; then
# Try again later
echo "This is not a good time to run this script. Wait until class is over and try again."
exit 0
else
# Delete the repo
DeleteDisposableRepo "$REPO_NAME"
# Sleep to allow to finish
echo "Resting 5 seconds to all deletions to resolve"
sleep 5
# Duplicate the repo
echo "Creating a fresh repo for $CLASS_ORG"
DuplicateRepo "$REPO_NAME" "$DEFAULT_BRANCH"
fi
}
function DeleteDisposableRepo() {
# Read in the repo name
REPO_NAME=$1
# Delete the repo
echo "Delete $TOKEN_OWNER $REPO_NAME repo..."
DELETE_CMD=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X DELETE "https://$INSTANCE_URL/repos/$TOKEN_OWNER/$1" >>log.out 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed to delete the repo!"
echo "ERROR:[$DELETE_CMD]"
exit 1
fi
# Delete class repo
echo "Delete $CLASS_ORG $REPO_NAME repo..."
DELETE_CLASS_CMD=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X DELETE "https://$INSTANCE_URL/repos/$CLASS_ORG/$1" >>log.out 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed to delete the class repo!"
echo "ERROR:[$DELETE_CLASS_CMD]"
exit 1
fi
}
function DuplicateRepo() {
# Read in the vars
REPO_NAME=$1
D_BRANCH=$2
# Create duplicate repo
echo "Create server-side location for fresh $CLASS_ORG/$REPO_NAME repo..."
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"name\": \"$REPO_NAME\", \"description\": \"A fun way to learn about Git and GitHub\", \"homepage\": \"https://$CLASS_ORG.github.io/github-games\", \"private\": false, \"has_issues\": true, \"has_wiki\": true, \"has_downloads\": true}" -X POST "https://$INSTANCE_URL/orgs/$CLASS_ORG/repos" >>log.out 2>&1
# Wait to finish
echo "Resting 5 more seconds to allow repo creation to resolve"
sleep 5
# Remove local repo
echo "Clone $REPO_NAME template from githubtraining org..."
rm -rf "$REPO_NAME"
# Clone the repo
echo "Trying to clone a template repo from github.com. If you're behind a firewall, this command may time out and we'll try to clone from your instance, instead."
echo "The timeout may take some time..."
git clone --bare "https://github.com/githubtraining/$REPO_NAME" "$REPO_NAME" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "!!! Wasn't able to clone the template repo from github.com. Trying to clone from your own instance."
# Clone the repo
git clone --bare "https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$REPO_NAME" "$REPO_NAME" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "!!! Couldn't clone template repo at all. Please grab a copy from https://github.com/githubtraining/$REPO_NAME and upload it to your GHE instance."
exit 1
fi
fi
# Push the repo
echo "Push the fresh $REPO_NAME back to $CLASS_ORG's repo..."
pushd "$REPO_NAME" || return
git push --mirror "https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$REPO_NAME" >>log.out 2>&1
# Wait to finish
echo "Resting 5 more seconds to allow push to resolve"
sleep 5
# Set default branch
echo "Setting default branch for repo"
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{\"name\": \"$REPO_NAME\", \"default_branch\": \"$D_BRANCH\"}" "https://$INSTANCE_URL/repos/$CLASS_ORG/$REPO_NAME" >>log.out 2>&1
popd || return
# Remove local repo
echo "Remove the local (temporary) clone"
rm -rf "$REPO_NAME"
}
# usage list the repository name and default branch
ResetActivity "github-games" "main"