Skip to content

Commit

Permalink
Refactor new-virtual → create-initial-repo
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerbxyz committed Nov 10, 2021
1 parent 52e8c33 commit 08209a1
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 231 deletions.
5 changes: 5 additions & 0 deletions practice-repos/caption-this/issue_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Comment below to be added as a collaborator

We will all be using this repository today. Even though it's public right now, you will not be able to make changes until you're given the correct permissions. We've automated this process via the GitHub API. Once you comment, we will add you as a collaborator.

You'll start to receive a lot of emails. (❗) You can slow these down immediately by clicking the **Unatch** button at the top of the page and selecting either the **Participating and @mentions** or **Ignore** option.
2 changes: 1 addition & 1 deletion script/create-files
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ create_files() {
fi
# Put the file in the repository
path="_slides/$new_file"
message="Activity file for $collaborator"
message="Create activity file for $collaborator"
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"$path\", \"message\": \"$message\", \"content\": \"$content\" }" \
-X PUT "$repo_endpoint/contents/$path" >>log.out 2>&1
# Remove the local file
Expand Down
210 changes: 210 additions & 0 deletions script/create-initial-repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#!/usr/bin/env bash
#
# Create initial repo

# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"

# shellcheck source=script/shared_functions
source ./shared_functions

template_name="caption-this"
org_url="https://$ROOT_URL/$CLASS_ORG"
org_repos_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG"
template_url="https://github.com/githubtraining/$template_name"
template_url_ghes="https://$ROOT_URL/$CLASS_ORG/$template_name"

get_initial_repo_name() {
# Get the repo name
read -r -p "What would you like to name the repo? " initial_repo_name

repo_url="$org_url/$initial_repo_name"

echo "Checking if that repo name is available..."
if repo_is_reachable "$repo_url"; then
echo "A repo with that name already exists. Please choose another name."
get_initial_repo_name
fi
}

create_initial_repo() {
local repo_name="$initial_repo_name"

# Set the homepage based on the root URL
case "$ROOT_URL" in
github.com) : "https://$CLASS_ORG.github.io/$initial_repo_name/" ;;
*) : "https://$ROOT_URL/pages/$CLASS_ORG/$initial_repo_name/" ;;
esac
local homepage="$_"

# Set the template URL based on the root URL
case "$ROOT_URL" in
github.com) : "$template_url" ;;
*) : "$template_url_ghes" ;;
esac
local template_repo_url="$_"

# Create a new empty repo
create_repo \
--name "$repo_name" \
--description "Let's learn about Git and GitHub" \
--homepage "$homepage" \
--private true \
--has_wiki false

# Create a temporary directory for the template repo
temp_repo_dir=$(mktemp -d)

# Delete the temporary directory on script exit
trap 'rm -rf "$temp_repo_dir"' EXIT

# Attempt to clone template repo into the temporary directory
git clone --bare "$template_repo_url" "$temp_repo_dir" >>log.out 2>&1 || {
print_error "Failed to clone template repository."
exit 1
}

# Switch to the temporary directory and push it on the stack
pushd "$temp_repo_dir" >>log.out 2>&1 || return

# Push the template repo to the new empty repo
git push --mirror "https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$repo_name" \
>>log.out 2>&1

# Switch back to original directory
popd >>log.out 2>&1 || return

# Add the token owner as a collaborator
add_repo_collaborator --repo "$repo_name" --user "$TOKEN_OWNER" --permission "admin"

# Find the default branch and store its value
default_branch=$(http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
--json GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$repo_name" |
jq '.default_branch' | sed -e 's/^"//' -e 's/"$//')
}

clone_initial_repo() {
local initial_repo_url="https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$initial_repo_name"

# Create a temporary directory
temp_initial_repo_dir=$(mktemp -d)

# Delete the temporary directory on script exit
trap 'rm -rf "$temp_initial_repo_dir"' EXIT

# Attempt to clone template repo into the temporary directory
git clone "$initial_repo_url" "$temp_initial_repo_dir" >>log.out 2>&1 || {
print_error "Failed to clone template repository."
exit 1
}
}

update_readme() {
# Ask for class dates
echo "What are the class dates? (e.g. May 4 – 7, 2021)"
read -r CLASS_DATE
echo "Who is teaching? (Without the @)"
read -r TEACHER

# Get raw contents of README.md and _config.yml
readme_contents=$(get_repo_file_contents --repo "$initial_repo_name" --path "README.md")
config_contents=$(get_repo_file_contents --repo "$initial_repo_name" --path "_config.yml")

# Replace the placeholder text with the actual values
readme_contents="${readme_contents//THIS-DATE/$CLASS_DATE}"
readme_contents="${readme_contents//TEACHER-HANDLE/$TEACHER}"
config_contents="${config_contents//caption-this/$initial_repo_name}"

# Update README.md and _config.yml in the repo
update_file_contents --repo "$initial_repo_name" \
--path "README.md" \
--content "$readme_contents" \
--message "Update README.md"

update_file_contents --repo "$initial_repo_name" \
--path "_config.yml" \
--content "$config_contents" \
--message "Update _config.yml"
}

create_initial_project() {
# Create project and save the project ID so we can use it to create project columns and cards
create_repo_project --repo "$initial_repo_name" \
--name "Caption Contest" \
--body "Let's see who can create the best captions for our collection of memes."
local project_id="$REPO_PROJECT_ID"

# Create project columns and save the ID of the first colum so we can add a card to it
create_project_column --project_id "$project_id" --name "To do" && local column_one="$COLUMN_ID"
create_project_column --project_id "$project_id" --name "In progress"
create_project_column --project_id "$project_id" --name "Done"

# Add a note to column one
create_project_card --project_id "$project_id" --column_id "$column_one" \
--note "Each student will update their file in the _slides directory with an image and a caption."
}

update_branch_protections() {
# Update branch protection rules for the default branch
if http --check-status --ignore-stdin --quiet --auth "$TOKEN_OWNER:$TEACHER_PAT" --raw '{
"required_status_checks": null,
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1
},
"restrictions": {
"users": ["'"$TEACHER"'", "'"$TOKEN_OWNER"'"],
"teams": []
}
}' PUT "$org_repos_endpoint/$initial_repo_name/branches/$default_branch/protection"; then
echo "Branch protection rules updated."
else
print_error "Failed to update branch protection rules."
fi
}

create_initial_issue() {
# Navigate to the practice-repos directory
cd "$(dirname "$0")/../practice-repos" || exit

# Set $practice_repos_dir to the "physical" path of the working directory
practice_repos_dir=$(pwd -P)

# Switch back to the previous directory
cd - >/dev/null || exit

# Create the issue
create_issue --repo "$initial_repo_name" --title "Add collaborators to this repo" \
--body "$(<"$practice_repos_dir/caption-this/issue_1.md")"
}

enable_github_pages() {
if http --check-status --ignore-stdin --quiet --auth "$TOKEN_OWNER:$TEACHER_PAT" --raw '{
"source": {
"branch": "'"$default_branch"'"
}
}' POST "$org_repos_endpoint/$initial_repo_name/pages"; then
echo "GitHub Pages enabled."
else
print_error "Failed to enable GitHub Pages."
fi
}

# Get the repo name
get_initial_repo_name

# Create the initial repo
create_initial_repo

# Clone initial repo
clone_initial_repo

# Prepare initial repo
update_readme
update_branch_protections
create_initial_issue
create_initial_project
enable_github_pages

print_done "Here's a link to the repo: $repo_url"
print_success "Good luck with class!"
35 changes: 14 additions & 21 deletions script/create-practice-repos
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,12 @@ collab_repo=$1
template_name=$2
practice_repo_name=$3
org_url="https://$ROOT_URL/$CLASS_ORG"
org_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG"
org_repos_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG"
template_url="https://github.com/githubtraining/$template_name"
template_url_ghes="https://$ROOT_URL/$CLASS_ORG/$template_name"
template_pages_url="https://$CLASS_ORG.github.io/$template_name"
template_pages_url_ghes="https://$ROOT_URL/pages/$CLASS_ORG/$template_name"

# https://superuser.com/a/1415376
# Returns exit code 0 (success) if $1 is a reachable git remote url
repo_is_reachable() {
local repo_url=$1
git ls-remote "$repo_url" CHECK_GIT_REMOTE_URL_REACHABILITY &>/dev/null
}

check_template_url() {
# if root url is not github.com
if [ "$ROOT_URL" != "github.com" ]; then
Expand Down Expand Up @@ -77,33 +70,33 @@ generate_repos() {
if repo_is_reachable "$org_url/$practice_repo_name-$username"; then
# ask if the repository should be deleted and recreated
if ask "A $practice_repo_name repo already exists for $username.\nIs it OK to delete and recreate?" N; then
echo "Deleting $CLASS_ORG/$practice_repo_name-$username..."
echo "Deleting $CLASS_ORG/$practice_repo_name-$username... "
# delete the existing practice repo
http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
DELETE "$org_endpoint/$practice_repo_name-$username" >>log.out 2>&1
DELETE "$org_repos_endpoint/$practice_repo_name-$username" >>log.out 2>&1
# create a new practice repo
create_repo "$username"
create_practice_repo "$username"
else
echo "OK. Skipping $username..."
echo "OK. Skipping $username... "
fi
else
# create a new practice repository
create_repo "$username"
create_practice_repo "$username"
fi
done
}

git_push() {
# Push to remote repository
echo "Pushing to $CLASS_ORG/$repo_name..."
echo "Pushing to $CLASS_ORG/$repo_name... "
git push --mirror "https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$repo_name" >>log.out 2>&1 || {
# if git push command failed
print_error "Failed to push commits to $CLASS_ORG/$repo_name."
exit 1
}
}

create_repo() {
create_practice_repo() {
student=$1
local repo_name="$practice_repo_name-$student"
local repo_url="https://$ROOT_URL/$CLASS_ORG/$repo_name"
Expand All @@ -119,7 +112,7 @@ create_repo() {
local repo_description="Let's resolve some conflicts."

# Create a new repo named $repo_name in $CLASS_ORG
echo "Creating $CLASS_ORG/$repo_name for $student..."
echo "Creating $CLASS_ORG/$repo_name for $student... "
http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
"https://$INSTANCE_URL/orgs/$CLASS_ORG/repos" \
name="$repo_name" \
Expand All @@ -132,7 +125,7 @@ create_repo() {
git_push

# Create PRs for each branch
echo "Creating practice pull requests for $CLASS_ORG/$repo_name..."
echo "Creating practice pull requests for $CLASS_ORG/$repo_name... "
create_pull_request --repo "$repo_name" --head 'manual' --base "$default_branch" \
--title "Updates to game manual" \
--body "$(<"$practice_repos_dir/conflict-practice/pull_request_1.md")"
Expand All @@ -148,7 +141,7 @@ create_repo() {
local repo_description="A fun way to learn about git troubleshooting."

# Create a new practice repo named $repo_name in $CLASS_ORG
echo "Creating $CLASS_ORG/$repo_name for $student..."
echo "Creating $CLASS_ORG/$repo_name for $student... "
http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
"https://$INSTANCE_URL/orgs/$CLASS_ORG/repos" \
name="$repo_name" \
Expand All @@ -162,7 +155,7 @@ create_repo() {
git_push

# Create issues for problems
echo "Creating practice issues for $CLASS_ORG/$repo_name..."
echo "Creating practice issues for $CLASS_ORG/$repo_name... "
create_issue --repo "$repo_name" \
--title "Game broken" \
--body "$(envsubst <"$practice_repos_dir/github-games/issue_1.md")"
Expand All @@ -178,9 +171,9 @@ create_repo() {
esac

# Invite student as a collaborator
echo "Inviting $student as a collaborator to $CLASS_ORG/$repo_name..."
echo "Inviting $student as a collaborator to $CLASS_ORG/$repo_name... "
http --auth "$TOKEN_OWNER:$TEACHER_PAT" \
PUT "$org_endpoint/$repo_name/collaborators/$student" \
PUT "$org_repos_endpoint/$repo_name/collaborators/$student" \
permission=admin >>log.out 2>&1

print_done "Repo URL: $repo_url"
Expand Down
Loading

0 comments on commit 08209a1

Please sign in to comment.