#!/bin/bash
# Set Gitea server IP and API key
GITEA_IP="your_gitea_server_ip"
API_KEY="your_gitea_api_key"
# Directory containing configuration and CSV file
CONFIG_DIR="/path/to/config"
# Function to create a user in Gitea
create_user() {
local email=$1
local api_key=$2
# Check if user exists
if curl -s -o /dev/null -w "%{http_code}" -X GET \
-H "Authorization: token $api_key" \
"http://$GITEA_IP/api/v1/users/$email" | grep -q 404; then
# User does not exist, create user
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $api_key" \
-d "{\"email\": \"$email\"}" \
"http://$GITEA_IP/api/v1/admin/users"
# Send email invitation to collaborate (implement as needed)
echo "Invitation email sent to $email"
else
echo "User $email already exists"
fi
}
# Function to create a repository in Gitea
create_repository() {
local name=$1
local uuid=$2
local api_key=$3
# Check if repository exists
if curl -s -o /dev/null -w "%{http_code}" -X GET \
-H "Authorization: token $api_key" \
"http://$GITEA_IP/api/v1/repos/$name" | grep -q 404; then
# Repository does not exist, create repository
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $api_key" \
-d "{\"name\": \"$name\", \"uuid\": \"$uuid\"}" \
"http://$GITEA_IP/api/v1/user/repos"
echo "Repository $name created"
else
echo "Repository $name already exists"
fi
}
# Function to add user as a collaborator to a repository in Gitea
add_collaborator() {
local name=$1
local username=$2
local api_key=$3
# Add user as collaborator
curl -s -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: token $api_key" \
-d "{\"permission\": \"write\"}" \
"http://$GITEA_IP/api/v1/repos/$name/collaborators/$username"
echo "User $username added as a collaborator to repository $name"
}
# Function to create branches in a repository in Gitea
create_branches() {
local name=$1
local api_key=$2
# Create branches
branches=("Production" "Staging" "Development")
for branch in "${branches[@]}"; do
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $api_key" \
-d "{\"branch_name\": \"$branch\"}" \
"http://$GITEA_IP/api/v1/repos/$name/branches"
echo "Branch $branch created in repository $name"
done
}
# Function to add webhook to a repository in Gitea
add_webhook() {
local name=$1
local uuid=$2
local ip_port=$3
local api_key=$4
# Add webhook
ip=$(grep "$ip_port" "$CONFIG_DIR/hosts" | awk '{print $2}' | cut -d: -f1)
port=$(grep "$ip_port" "$CONFIG_DIR/hosts" | awk '{print $2}' | cut -d: -f2)
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $api_key" \
-d "{\"type\": \"gitea\", \"config\": {\"content_type\": \"json\", \"url\": \"http://$ip:$port/git/$uuid\", \"secret\": \"\", \"events\": [\"push\"]}}" \
"http://$GITEA_IP/api/v1/repos/$name/hooks"
echo "Webhook added to repository $name"
}
# Read CSV file and perform actions for each line
while IFS=';' read -r uuid email location features start_date end_date days version; do
# Check user existence and create if needed
create_user "$email" "$API_KEY"
# Create repository
create_repository "$features" "$uuid" "$API_KEY"
# Add collaborator
add_collaborator "$features" "$email" "$API_KEY"
# Create branches
create_branches "$features" "$API_KEY"
# Add webhook
add_webhook "$features" "$uuid" "$location" "$API_KEY"
# Limit user to prevent creating branches (implement as needed)
# Part the CSV and rsync to clients (implement as needed)
done < "$CONFIG_DIR/csv_file.csv"