Integrate create-repo/add-collaborators/list-projs/repo-config into repo script, remove originals, update git-ctrl and SKILL.md
This commit is contained in:
@@ -1,37 +1,232 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Get the directory where this script is located
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "Usage: $0 <command> [options]"
|
||||
MYSQL_CONTAINER="git-kc-mysql"
|
||||
MYSQL_USER="root"
|
||||
MYSQL_DB="giteadb"
|
||||
MYSQL_ROOT_PASS="K0DprNKJ^vAu3Mx32hMZ%LCzWKElFRfA"
|
||||
GIT_HOST="root@vps.git"
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# create
|
||||
# ─────────────────────────────────────────────
|
||||
do_create() {
|
||||
if [[ $# -lt 1 ]]; then
|
||||
echo "Usage: $0 create <repo-name>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "${AGENT_WORKSPACE:-}" ]]; then
|
||||
echo "Error: script must be executed by pcexec"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
REPO_NAME="$1"
|
||||
|
||||
# Validate repo name
|
||||
if ! [[ "$REPO_NAME" =~ ^[a-zA-Z0-9_.-]+$ ]]; then
|
||||
echo "Error: Invalid repository name '$REPO_NAME'"
|
||||
echo "Only alphanumeric, hyphens, underscores, and dots are allowed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
REPO_DIR="${AGENT_WORKSPACE}/${REPO_NAME}"
|
||||
|
||||
mkdir -p "${REPO_DIR}"
|
||||
cd "${REPO_DIR}"
|
||||
git init
|
||||
|
||||
USERNAME="$(secret-mgr get-username --key git)"
|
||||
REMOTE_URL="https://git.hangman-lab.top/${USERNAME}/${REPO_NAME}.git"
|
||||
git remote add origin "${REMOTE_URL}"
|
||||
|
||||
do_config --repo-path "${REPO_DIR}"
|
||||
|
||||
echo "Done! Repository created at: ${REPO_DIR}"
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# add-collaborators
|
||||
# ─────────────────────────────────────────────
|
||||
do_add_collaborators() {
|
||||
local user="" repo=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--user) user="$2"; shift 2 ;;
|
||||
--repo) repo="$2"; shift 2 ;;
|
||||
*) echo "Unknown option: $1"; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$user" || -z "$repo" ]]; then
|
||||
echo "Usage: $0 add-collaborators --user <user> --repo <repo>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! secret-mgr list | grep -q "git-access-token"; then
|
||||
echo "Error: git-access-token not found. Generate one first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
owner=$(secret-mgr get-username --key git)
|
||||
token=$(secret-mgr get-secret --key git-access-token)
|
||||
|
||||
curl -s -X PUT \
|
||||
-H "Authorization: token $token" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"permission":"write"}' \
|
||||
"https://git.hangman-lab.top/api/v1/repos/$owner/$repo/collaborators/$user"
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# list-all
|
||||
# ─────────────────────────────────────────────
|
||||
do_list_all() {
|
||||
USERNAME=$(ego-mgr get default-username)
|
||||
if [[ -z "$USERNAME" ]]; then
|
||||
echo "Error: cannot get username from ego-mgr" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
QUERY="
|
||||
SELECT r.name, u.name as owner, r.is_private,
|
||||
COALESCE((r.owner_id = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
OR a.user_id = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
OR EXISTS (SELECT 1 FROM team_user tu JOIN team t ON t.id = tu.team_id
|
||||
WHERE tu.uid = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
AND (t.includes_all_repositories = 1
|
||||
OR EXISTS (SELECT 1 FROM team_repo tr WHERE tr.team_id = t.id AND tr.repo_id = r.id)))), 0) as can_write
|
||||
FROM repository r
|
||||
JOIN user u ON r.owner_id = u.id
|
||||
LEFT JOIN access a ON a.repo_id = r.id AND a.user_id = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
WHERE r.is_archived = 0
|
||||
AND (r.owner_id = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
OR r.is_private = 0
|
||||
OR a.user_id = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci
|
||||
OR EXISTS (SELECT 1 FROM team_user tu WHERE tu.uid = (SELECT id FROM user WHERE lower_name = LOWER('$USERNAME')) COLLATE utf8mb4_unicode_ci))
|
||||
ORDER BY r.name
|
||||
"
|
||||
|
||||
RESULT=$(ssh -o StrictHostKeyChecking=no "$GIT_HOST" \
|
||||
"docker exec $MYSQL_CONTAINER mysql -u $MYSQL_USER -p'$MYSQL_ROOT_PASS' -N -e \"$QUERY\" $MYSQL_DB" 2>/dev/null)
|
||||
|
||||
echo "| proj-name | owner | url | can-write |"
|
||||
echo "|------------|-------|-----|-----------|"
|
||||
|
||||
[[ -z "$RESULT" ]] && exit 0
|
||||
|
||||
echo "$RESULT" | while IFS=$'\t' read -r name owner is_private can_write; do
|
||||
can_write_val=$([[ "$can_write" == "1" ]] && echo "yes" || echo "no")
|
||||
echo "| $name | $owner | https://git.hangman-lab.top/$owner/$name.git | $can_write_val |"
|
||||
done
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# config
|
||||
# ─────────────────────────────────────────────
|
||||
do_config() {
|
||||
local REPO_PATH=""
|
||||
local RECURSIVE=false
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--repo-path) REPO_PATH="${2:-}"; shift 2 ;;
|
||||
--recursive) RECURSIVE=true; shift ;;
|
||||
*) echo "Usage: $0 config --repo-path <path> [--recursive]"; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$REPO_PATH" ]]; then
|
||||
echo "Usage: $0 config --repo-path <path> [--recursive]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
EMAIL=$(ego-mgr get email)
|
||||
if [[ -z "$EMAIL" ]]; then
|
||||
echo "Error: email not set in ego-mgr"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
is_git_repo() {
|
||||
if [[ -d "$1/.git" ]]; then return 0; fi
|
||||
if [[ -f "$1/.git" ]]; then
|
||||
gitdir=$(grep -m1 "gitdir:" "$1/.git" | cut -d' ' -f2 | tr -d ' ')
|
||||
[[ -n "$gitdir" ]]; return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
if ! is_git_repo "$REPO_PATH"; then
|
||||
echo "Not a git repo: $REPO_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
USER="$(secret-mgr get-username --key git)"
|
||||
PASS="$(secret-mgr get-secret --key git)"
|
||||
ENC_USER="$(U="$USER" python3 - <<'PY'
|
||||
import os, urllib.parse
|
||||
print(urllib.parse.quote(os.environ['U'], safe=''))
|
||||
PY
|
||||
)"
|
||||
|
||||
configure_repo() {
|
||||
local repo="$1" relative="${2:-}"
|
||||
local name="${relative:-$repo}"
|
||||
echo "Configuring: $name"
|
||||
( cd "$repo" && git config user.name "$USER" )
|
||||
( cd "$repo" && git config user.email "$EMAIL" )
|
||||
local git_dir
|
||||
git_dir="$(cd "$repo" && git rev-parse --absolute-git-dir)"
|
||||
local cred_file="${git_dir}/credentials"
|
||||
( cd "$repo" && git config credential.helper "store --file ${cred_file}" )
|
||||
( cd "$repo" && GIT_ASKPASS=true git credential-store --file "${cred_file}" store <<EOF
|
||||
protocol=https
|
||||
host=git.hangman-lab.top
|
||||
username=${ENC_USER}
|
||||
password=${PASS}
|
||||
EOF
|
||||
)
|
||||
}
|
||||
|
||||
configure_repo "$REPO_PATH"
|
||||
|
||||
if [[ "$RECURSIVE" == "true" ]]; then
|
||||
submodules=$(cd "$REPO_PATH" && git submodule status --recursive 2>/dev/null | awk '{print $2}' || true)
|
||||
for sm in $submodules; do
|
||||
sm_path="$REPO_PATH/$sm"
|
||||
if is_git_repo "$sm_path"; then
|
||||
configure_repo "$sm_path" "$sm"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo "OK"
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# Dispatch
|
||||
# ─────────────────────────────────────────────
|
||||
if [[ $# -lt 1 ]]; then
|
||||
echo "Usage: $0 <create|add-collaborators|list-all|config> [args...]"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " create <repo-name> Create a new repository"
|
||||
echo " add-collaborators <args> Add collaborator to repository"
|
||||
echo " list-all [args] List all visible repositories"
|
||||
echo " config <args> Configure repository"
|
||||
echo " create <repo-name> Create a new repository"
|
||||
echo " add-collaborators --user <u> --repo <r> Add collaborator"
|
||||
echo " list-all List all visible repositories"
|
||||
echo " config --repo-path <path> [--recursive] Configure repo credentials"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
subcommand="$1"
|
||||
shift
|
||||
subcommand="$1"; shift
|
||||
|
||||
case "$subcommand" in
|
||||
create)
|
||||
"$SCRIPT_DIR/create-repo" "$@"
|
||||
;;
|
||||
add-collaborators)
|
||||
"$SCRIPT_DIR/repo-add-collaborators" "$@"
|
||||
;;
|
||||
list-all)
|
||||
"$SCRIPT_DIR/list-projs" "$@"
|
||||
;;
|
||||
config)
|
||||
"$SCRIPT_DIR/repo-config" "$@"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown command: $subcommand"
|
||||
echo "Run '$0' for usage information"
|
||||
exit 1
|
||||
;;
|
||||
create) do_create "$@" ;;
|
||||
add-collaborators) do_add_collaborators "$@" ;;
|
||||
list-all) do_list_all "$@" ;;
|
||||
config) do_config "$@" ;;
|
||||
*) echo "Unknown command: $subcommand"; exit 1 ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user