Skip to content

Commit

Permalink
.........
Browse files Browse the repository at this point in the history
  • Loading branch information
elimelt committed Nov 19, 2024
0 parents commit fe92005
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Setup

```sh
# Install dependencies
brew install gh
brew install jq

# Make executable
chmod +x *.sh

# Create a GitHub token
gh auth login
```

or just run setup.sh

```sh
chmod +x setup.sh
./setup.sh
```
76 changes: 76 additions & 0 deletions create-hcp-team-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# Function to convert string to slug format
slugify() {
echo "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
}

# Check if gh CLI is installed
if ! command -v gh &> /dev/null; then
echo "GitHub CLI (gh) is not installed. Please install it first."
exit 1
fi

# Check if authenticated
if ! gh auth status &> /dev/null; then
echo "Please authenticate with GitHub first using 'gh auth login'"
exit 1
fi

# Get organization name
read -p "Enter your GitHub organization name: " org_name

# Get team name
read -p "Enter team name: " team_name
team_slug=$(slugify "$team_name")

# Get GitHub usernames (comma-separated)
read -p "Enter GitHub usernames (comma-separated): " github_users
IFS=',' read -ra github_array <<< "$github_users"

# Get template repository (optional)
read -p "Enter template repository name (press Enter to skip): " template_repo

# Create team
echo "Creating team: $team_slug"
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/orgs/$org_name/teams" \
-f name="$team_name" \
-f permission="push" \
-f privacy="closed"

# Create repository
echo "Creating repository: $team_slug"
if [ -n "$template_repo" ]; then
gh repo create "$org_name/$team_slug" \
--private \
--team "$team_slug" \
--template "$template_repo"
else
gh repo create "$org_name/$team_slug" \
--private \
--team "$team_slug"
fi

# Add team members
for username in "${github_array[@]}"; do
username=$(echo "$username" | tr -d ' ')
echo "Adding $username to team $team_slug"
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
"/orgs/$org_name/teams/$team_slug/memberships/$username" \
-f role="member"
done

# Set team permissions for repository
echo "Setting team permissions for repository"
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
"/orgs/$org_name/teams/$team_slug/repos/$org_name/$team_slug" \
-f permission="push"

echo "Setup completed successfully!"
31 changes: 31 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Install dependencies
if ! command -v brew &> /dev/null
then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

if ! command -v gh &> /dev/null
then
brew install gh
fi

if ! command -v jq &> /dev/null
then
brew install jq
fi

if ! command -v yq &> /dev/null
then
brew install yq
fi

# Make executable
chmod +x *.sh

# Create a GitHub token if not already authenticated
if [ -z "$(gh auth status)" ]; then
echo "Please authenticate with GitHub"
gh auth login
fi


0 comments on commit fe92005

Please sign in to comment.