create-classroom-assignment.sh
· 6.0 KiB · Bash
Bruto
#!/bin/bash
# GitHub Classroom Assignment Creator
# This script creates a new assignment in a GitHub Classroom using the GitHub CLI
set -e # Exit on any error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Creates a GitHub Classroom assignment with specified parameters."
echo ""
echo "Options:"
echo " -c, --classroom CLASSROOM_ID GitHub Classroom ID (required)"
echo " -t, --title TITLE Assignment title (required)"
echo " -s, --starter-repo REPO Starter code repository (org/repo format)"
echo " -d, --deadline DEADLINE Assignment deadline (YYYY-MM-DD format)"
echo " -p, --prefix PREFIX Repository name prefix (default: assignment)"
echo " --public Make student repositories public (default: private)"
echo " --individual Individual assignment (default: group)"
echo " --max-teams MAX Maximum number of teams (for group assignments)"
echo " --max-members MAX Maximum members per team (for group assignments)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 -c 12345 -t \"Data Pipeline Project\" -s ZipCodeCore/CentralLibraryData"
echo " $0 -c 12345 -t \"Individual Exercise\" -s myorg/starter-repo --individual"
echo ""
echo "Note: This script requires the GitHub CLI (gh) to be installed and authenticated."
}
# Default values
CLASSROOM_ID=""
ASSIGNMENT_TITLE=""
STARTER_REPO=""
DEADLINE=""
REPO_PREFIX="assignment"
IS_PUBLIC="false"
IS_INDIVIDUAL="false"
MAX_TEAMS=""
MAX_MEMBERS=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-c|--classroom)
CLASSROOM_ID="$2"
shift 2
;;
-t|--title)
ASSIGNMENT_TITLE="$2"
shift 2
;;
-s|--starter-repo)
STARTER_REPO="$2"
shift 2
;;
-d|--deadline)
DEADLINE="$2"
shift 2
;;
-p|--prefix)
REPO_PREFIX="$2"
shift 2
;;
--public)
IS_PUBLIC="true"
shift
;;
--individual)
IS_INDIVIDUAL="true"
shift
;;
--max-teams)
MAX_TEAMS="$2"
shift 2
;;
--max-members)
MAX_MEMBERS="$2"
shift 2
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Validate required parameters
if [[ -z "$CLASSROOM_ID" ]]; then
print_error "Classroom ID is required. Use -c or --classroom option."
show_usage
exit 1
fi
if [[ -z "$ASSIGNMENT_TITLE" ]]; then
print_error "Assignment title is required. Use -t or --title option."
show_usage
exit 1
fi
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
print_error "GitHub CLI (gh) is not installed. Please install it first:"
echo " brew install gh"
echo " or visit: https://cli.github.com/"
exit 1
fi
# Check if user is authenticated with GitHub CLI
if ! gh auth status &> /dev/null; then
print_error "Not authenticated with GitHub CLI. Please run:"
echo " gh auth login"
exit 1
fi
# Validate starter repo format if provided
if [[ -n "$STARTER_REPO" ]]; then
if [[ ! "$STARTER_REPO" =~ ^[^/]+/[^/]+$ ]]; then
print_error "Starter repository must be in format 'org/repo'"
exit 1
fi
fi
# Validate deadline format if provided
if [[ -n "$DEADLINE" ]]; then
if [[ ! "$DEADLINE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
print_error "Deadline must be in YYYY-MM-DD format"
exit 1
fi
fi
print_info "Creating GitHub Classroom assignment..."
print_info "Classroom ID: $CLASSROOM_ID"
print_info "Title: $ASSIGNMENT_TITLE"
print_info "Starter repo: ${STARTER_REPO:-"None"}"
print_info "Assignment type: $([ "$IS_INDIVIDUAL" = "true" ] && echo "Individual" || echo "Group")"
# Build the GitHub CLI command
GH_CMD="gh classroom assignment create"
GH_CMD="$GH_CMD --classroom $CLASSROOM_ID"
GH_CMD="$GH_CMD --title \"$ASSIGNMENT_TITLE\""
GH_CMD="$GH_CMD --prefix \"$REPO_PREFIX\""
if [[ -n "$STARTER_REPO" ]]; then
GH_CMD="$GH_CMD --starter-code-repository $STARTER_REPO"
fi
if [[ -n "$DEADLINE" ]]; then
GH_CMD="$GH_CMD --deadline $DEADLINE"
fi
if [[ "$IS_PUBLIC" = "true" ]]; then
GH_CMD="$GH_CMD --public"
fi
if [[ "$IS_INDIVIDUAL" = "true" ]]; then
GH_CMD="$GH_CMD --individual"
else
# Group assignment options
if [[ -n "$MAX_TEAMS" ]]; then
GH_CMD="$GH_CMD --max-teams $MAX_TEAMS"
fi
if [[ -n "$MAX_MEMBERS" ]]; then
GH_CMD="$GH_CMD --max-members $MAX_MEMBERS"
fi
fi
# Execute the command
print_info "Executing: $GH_CMD"
echo ""
if eval "$GH_CMD"; then
print_success "Assignment created successfully!"
echo ""
print_info "Students can access the assignment through your GitHub Classroom."
print_info "Assignment repositories will be created with prefix: $REPO_PREFIX"
else
print_error "Failed to create assignment. Please check the error messages above."
exit 1
fi
# Additional helpful information
echo ""
print_info "Next steps:"
echo "1. Share the assignment link with your students"
echo "2. Monitor student progress in your GitHub Classroom dashboard"
echo "3. Students will get repositories named: $REPO_PREFIX-{student-name}"
if [[ -n "$STARTER_REPO" ]]; then
echo "4. Student repositories will be initialized with code from: $STARTER_REPO"
fi
1 | #!/bin/bash |
2 | |
3 | # GitHub Classroom Assignment Creator |
4 | # This script creates a new assignment in a GitHub Classroom using the GitHub CLI |
5 | |
6 | set -e # Exit on any error |
7 | |
8 | # Color codes for output |
9 | RED='\033[0;31m' |
10 | GREEN='\033[0;32m' |
11 | YELLOW='\033[1;33m' |
12 | BLUE='\033[0;34m' |
13 | NC='\033[0m' # No Color |
14 | |
15 | # Function to print colored output |
16 | print_info() { |
17 | echo -e "${BLUE}[INFO]${NC} $1" |
18 | } |
19 | |
20 | print_success() { |
21 | echo -e "${GREEN}[SUCCESS]${NC} $1" |
22 | } |
23 | |
24 | print_warning() { |
25 | echo -e "${YELLOW}[WARNING]${NC} $1" |
26 | } |
27 | |
28 | print_error() { |
29 | echo -e "${RED}[ERROR]${NC} $1" |
30 | } |
31 | |
32 | # Function to show usage |
33 | show_usage() { |
34 | echo "Usage: $0 [OPTIONS]" |
35 | echo "" |
36 | echo "Creates a GitHub Classroom assignment with specified parameters." |
37 | echo "" |
38 | echo "Options:" |
39 | echo " -c, --classroom CLASSROOM_ID GitHub Classroom ID (required)" |
40 | echo " -t, --title TITLE Assignment title (required)" |
41 | echo " -s, --starter-repo REPO Starter code repository (org/repo format)" |
42 | echo " -d, --deadline DEADLINE Assignment deadline (YYYY-MM-DD format)" |
43 | echo " -p, --prefix PREFIX Repository name prefix (default: assignment)" |
44 | echo " --public Make student repositories public (default: private)" |
45 | echo " --individual Individual assignment (default: group)" |
46 | echo " --max-teams MAX Maximum number of teams (for group assignments)" |
47 | echo " --max-members MAX Maximum members per team (for group assignments)" |
48 | echo " -h, --help Show this help message" |
49 | echo "" |
50 | echo "Examples:" |
51 | echo " $0 -c 12345 -t \"Data Pipeline Project\" -s ZipCodeCore/CentralLibraryData" |
52 | echo " $0 -c 12345 -t \"Individual Exercise\" -s myorg/starter-repo --individual" |
53 | echo "" |
54 | echo "Note: This script requires the GitHub CLI (gh) to be installed and authenticated." |
55 | } |
56 | |
57 | # Default values |
58 | CLASSROOM_ID="" |
59 | ASSIGNMENT_TITLE="" |
60 | STARTER_REPO="" |
61 | DEADLINE="" |
62 | REPO_PREFIX="assignment" |
63 | IS_PUBLIC="false" |
64 | IS_INDIVIDUAL="false" |
65 | MAX_TEAMS="" |
66 | MAX_MEMBERS="" |
67 | |
68 | # Parse command line arguments |
69 | while [[ $# -gt 0 ]]; do |
70 | case $1 in |
71 | -c|--classroom) |
72 | CLASSROOM_ID="$2" |
73 | shift 2 |
74 | ;; |
75 | -t|--title) |
76 | ASSIGNMENT_TITLE="$2" |
77 | shift 2 |
78 | ;; |
79 | -s|--starter-repo) |
80 | STARTER_REPO="$2" |
81 | shift 2 |
82 | ;; |
83 | -d|--deadline) |
84 | DEADLINE="$2" |
85 | shift 2 |
86 | ;; |
87 | -p|--prefix) |
88 | REPO_PREFIX="$2" |
89 | shift 2 |
90 | ;; |
91 | --public) |
92 | IS_PUBLIC="true" |
93 | shift |
94 | ;; |
95 | --individual) |
96 | IS_INDIVIDUAL="true" |
97 | shift |
98 | ;; |
99 | --max-teams) |
100 | MAX_TEAMS="$2" |
101 | shift 2 |
102 | ;; |
103 | --max-members) |
104 | MAX_MEMBERS="$2" |
105 | shift 2 |
106 | ;; |
107 | -h|--help) |
108 | show_usage |
109 | exit 0 |
110 | ;; |
111 | *) |
112 | print_error "Unknown option: $1" |
113 | show_usage |
114 | exit 1 |
115 | ;; |
116 | esac |
117 | done |
118 | |
119 | # Validate required parameters |
120 | if [[ -z "$CLASSROOM_ID" ]]; then |
121 | print_error "Classroom ID is required. Use -c or --classroom option." |
122 | show_usage |
123 | exit 1 |
124 | fi |
125 | |
126 | if [[ -z "$ASSIGNMENT_TITLE" ]]; then |
127 | print_error "Assignment title is required. Use -t or --title option." |
128 | show_usage |
129 | exit 1 |
130 | fi |
131 | |
132 | # Check if GitHub CLI is installed |
133 | if ! command -v gh &> /dev/null; then |
134 | print_error "GitHub CLI (gh) is not installed. Please install it first:" |
135 | echo " brew install gh" |
136 | echo " or visit: https://cli.github.com/" |
137 | exit 1 |
138 | fi |
139 | |
140 | # Check if user is authenticated with GitHub CLI |
141 | if ! gh auth status &> /dev/null; then |
142 | print_error "Not authenticated with GitHub CLI. Please run:" |
143 | echo " gh auth login" |
144 | exit 1 |
145 | fi |
146 | |
147 | # Validate starter repo format if provided |
148 | if [[ -n "$STARTER_REPO" ]]; then |
149 | if [[ ! "$STARTER_REPO" =~ ^[^/]+/[^/]+$ ]]; then |
150 | print_error "Starter repository must be in format 'org/repo'" |
151 | exit 1 |
152 | fi |
153 | fi |
154 | |
155 | # Validate deadline format if provided |
156 | if [[ -n "$DEADLINE" ]]; then |
157 | if [[ ! "$DEADLINE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then |
158 | print_error "Deadline must be in YYYY-MM-DD format" |
159 | exit 1 |
160 | fi |
161 | fi |
162 | |
163 | print_info "Creating GitHub Classroom assignment..." |
164 | print_info "Classroom ID: $CLASSROOM_ID" |
165 | print_info "Title: $ASSIGNMENT_TITLE" |
166 | print_info "Starter repo: ${STARTER_REPO:-"None"}" |
167 | print_info "Assignment type: $([ "$IS_INDIVIDUAL" = "true" ] && echo "Individual" || echo "Group")" |
168 | |
169 | # Build the GitHub CLI command |
170 | GH_CMD="gh classroom assignment create" |
171 | GH_CMD="$GH_CMD --classroom $CLASSROOM_ID" |
172 | GH_CMD="$GH_CMD --title \"$ASSIGNMENT_TITLE\"" |
173 | GH_CMD="$GH_CMD --prefix \"$REPO_PREFIX\"" |
174 | |
175 | if [[ -n "$STARTER_REPO" ]]; then |
176 | GH_CMD="$GH_CMD --starter-code-repository $STARTER_REPO" |
177 | fi |
178 | |
179 | if [[ -n "$DEADLINE" ]]; then |
180 | GH_CMD="$GH_CMD --deadline $DEADLINE" |
181 | fi |
182 | |
183 | if [[ "$IS_PUBLIC" = "true" ]]; then |
184 | GH_CMD="$GH_CMD --public" |
185 | fi |
186 | |
187 | if [[ "$IS_INDIVIDUAL" = "true" ]]; then |
188 | GH_CMD="$GH_CMD --individual" |
189 | else |
190 | # Group assignment options |
191 | if [[ -n "$MAX_TEAMS" ]]; then |
192 | GH_CMD="$GH_CMD --max-teams $MAX_TEAMS" |
193 | fi |
194 | if [[ -n "$MAX_MEMBERS" ]]; then |
195 | GH_CMD="$GH_CMD --max-members $MAX_MEMBERS" |
196 | fi |
197 | fi |
198 | |
199 | # Execute the command |
200 | print_info "Executing: $GH_CMD" |
201 | echo "" |
202 | |
203 | if eval "$GH_CMD"; then |
204 | print_success "Assignment created successfully!" |
205 | echo "" |
206 | print_info "Students can access the assignment through your GitHub Classroom." |
207 | print_info "Assignment repositories will be created with prefix: $REPO_PREFIX" |
208 | else |
209 | print_error "Failed to create assignment. Please check the error messages above." |
210 | exit 1 |
211 | fi |
212 | |
213 | # Additional helpful information |
214 | echo "" |
215 | print_info "Next steps:" |
216 | echo "1. Share the assignment link with your students" |
217 | echo "2. Monitor student progress in your GitHub Classroom dashboard" |
218 | echo "3. Students will get repositories named: $REPO_PREFIX-{student-name}" |
219 | |
220 | if [[ -n "$STARTER_REPO" ]]; then |
221 | echo "4. Student repositories will be initialized with code from: $STARTER_REPO" |
222 | fi |
223 |