最后活跃于 1750972057

for GitHub classroom

create-classroom-assignment.sh 原始文件
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
6set -e # Exit on any error
7
8# Color codes for output
9RED='\033[0;31m'
10GREEN='\033[0;32m'
11YELLOW='\033[1;33m'
12BLUE='\033[0;34m'
13NC='\033[0m' # No Color
14
15# Function to print colored output
16print_info() {
17 echo -e "${BLUE}[INFO]${NC} $1"
18}
19
20print_success() {
21 echo -e "${GREEN}[SUCCESS]${NC} $1"
22}
23
24print_warning() {
25 echo -e "${YELLOW}[WARNING]${NC} $1"
26}
27
28print_error() {
29 echo -e "${RED}[ERROR]${NC} $1"
30}
31
32# Function to show usage
33show_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
58CLASSROOM_ID=""
59ASSIGNMENT_TITLE=""
60STARTER_REPO=""
61DEADLINE=""
62REPO_PREFIX="assignment"
63IS_PUBLIC="false"
64IS_INDIVIDUAL="false"
65MAX_TEAMS=""
66MAX_MEMBERS=""
67
68# Parse command line arguments
69while [[ $# -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
117done
118
119# Validate required parameters
120if [[ -z "$CLASSROOM_ID" ]]; then
121 print_error "Classroom ID is required. Use -c or --classroom option."
122 show_usage
123 exit 1
124fi
125
126if [[ -z "$ASSIGNMENT_TITLE" ]]; then
127 print_error "Assignment title is required. Use -t or --title option."
128 show_usage
129 exit 1
130fi
131
132# Check if GitHub CLI is installed
133if ! 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
138fi
139
140# Check if user is authenticated with GitHub CLI
141if ! gh auth status &> /dev/null; then
142 print_error "Not authenticated with GitHub CLI. Please run:"
143 echo " gh auth login"
144 exit 1
145fi
146
147# Validate starter repo format if provided
148if [[ -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
153fi
154
155# Validate deadline format if provided
156if [[ -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
161fi
162
163print_info "Creating GitHub Classroom assignment..."
164print_info "Classroom ID: $CLASSROOM_ID"
165print_info "Title: $ASSIGNMENT_TITLE"
166print_info "Starter repo: ${STARTER_REPO:-"None"}"
167print_info "Assignment type: $([ "$IS_INDIVIDUAL" = "true" ] && echo "Individual" || echo "Group")"
168
169# Build the GitHub CLI command
170GH_CMD="gh classroom assignment create"
171GH_CMD="$GH_CMD --classroom $CLASSROOM_ID"
172GH_CMD="$GH_CMD --title \"$ASSIGNMENT_TITLE\""
173GH_CMD="$GH_CMD --prefix \"$REPO_PREFIX\""
174
175if [[ -n "$STARTER_REPO" ]]; then
176 GH_CMD="$GH_CMD --starter-code-repository $STARTER_REPO"
177fi
178
179if [[ -n "$DEADLINE" ]]; then
180 GH_CMD="$GH_CMD --deadline $DEADLINE"
181fi
182
183if [[ "$IS_PUBLIC" = "true" ]]; then
184 GH_CMD="$GH_CMD --public"
185fi
186
187if [[ "$IS_INDIVIDUAL" = "true" ]]; then
188 GH_CMD="$GH_CMD --individual"
189else
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
197fi
198
199# Execute the command
200print_info "Executing: $GH_CMD"
201echo ""
202
203if 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"
208else
209 print_error "Failed to create assignment. Please check the error messages above."
210 exit 1
211fi
212
213# Additional helpful information
214echo ""
215print_info "Next steps:"
216echo "1. Share the assignment link with your students"
217echo "2. Monitor student progress in your GitHub Classroom dashboard"
218echo "3. Students will get repositories named: $REPO_PREFIX-{student-name}"
219
220if [[ -n "$STARTER_REPO" ]]; then
221 echo "4. Student repositories will be initialized with code from: $STARTER_REPO"
222fi
223