Skip to content

Instantly share code, notes, and snippets.

@drblue
Created August 17, 2024 14:40
Show Gist options
  • Save drblue/f7055d348846212fecc654e0b0fb2288 to your computer and use it in GitHub Desktop.
Save drblue/f7055d348846212fecc654e0b0fb2288 to your computer and use it in GitHub Desktop.
Script for cloning all student repos (including support for pagination) from a GitHub Classroom-repo.
#!/bin/bash
# Clones all student repos from GitHub Classroom using the GitHub CLI with GitHub Classroom extension
# Accepts an assignment ID as an argument
# If no argument is provided, it will show all assignments and prompt for one
# If there are more than 30 repos, it will prompt to continue and clone the next page until all repos are cloned
# Function to round up
ceil() {
num=$1
printf "%.0f\n" $(echo "$num + 0.999" | bc)
}
# Function to divide and round up
ceil_divide() {
local num1=$1
local num2=$2
local result=$(echo "scale=10; $num1 / $num2" | bc)
local rounded_up=$(echo "$result" | awk '{print ($0 == int($0)) ? $0 : int($0)+1}')
echo $rounded_up
}
# Function to strip colors
strip_colors() {
sed -E 's/\x1B\[[0-9;]*[mK]//g'
}
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null
then
echo "GitHub CLI could not be found. Please install it from https://cli.github.com/"
exit
fi
# Check if GitHub Classroom extension is installed
if ! gh extension list | grep -q "gh-classroom"
then
echo "GitHub Classroom extension could not be found. Please install it by running 'gh extension install classroom'"
exit
fi
# Get the assignment ID
if [ -z "$1" ]
then
gh classroom assignments
echo ""
echo -n "Enter the assignment ID: "
read assignment_id
echo ""
else
assignment_id=$1
fi
echo "πŸ”„ Cloning student repos..."
echo ""
# Get count of repos
repos=$(gh classroom assignment -a $assignment_id | /usr/bin/grep --color=never "Accepted" | cut -d " " -f 2 | strip_colors)
echo "πŸ“š Total repos: $repos"
# We can only clone 30 repos at a time
# Calculate the number of pages and round up
per_page=30
echo "πŸ“ƒ Per page: $per_page"
pages=$(ceil_divide $repos $per_page)
echo "πŸ“„ Total pages: $pages"
echo ""
# Start at page 1
page=1
# Clone the repos until all pages are cloned
while [ $page -le $pages ]
do
echo "πŸ” Cloning page $page..."
gh classroom clone student-repos -a $assignment_id --page $page
page=$((page + 1))
echo ""
done
echo ""
echo "πŸŽ‰ All repos have been cloned"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment