From 80252c290fa71de1eff6396c6d725c90452098d5 Mon Sep 17 00:00:00 2001 From: Nikolay Shaplov Date: Sun, 7 Jun 2026 09:24:56 +0000 Subject: [PATCH] Script for cloning a new repository plus universal webhooks for updating existing repositories --- .gitignore | 1 + {nginx-conf => conf/nginx}/git4ai | 5 ++--- conf/webhook.conf | 22 ++++++++++++++++++++++ scripts/clone_repo.sh | 22 ++++++++++++++++++++++ scripts/config._sh.example | 9 +++++++++ scripts/update_repo.sh | 27 +++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 .gitignore rename {nginx-conf => conf/nginx}/git4ai (98%) create mode 100644 conf/webhook.conf create mode 100755 scripts/clone_repo.sh create mode 100644 scripts/config._sh.example create mode 100755 scripts/update_repo.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6ab79d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +scripts/config._sh diff --git a/nginx-conf/git4ai b/conf/nginx/git4ai similarity index 98% rename from nginx-conf/git4ai rename to conf/nginx/git4ai index f18bfdf..95f4610 100644 --- a/nginx-conf/git4ai +++ b/conf/nginx/git4ai @@ -226,8 +226,7 @@ location ~ "^/gitai-cat-file/([^/]+)/([0-9a-f]{4,40})(?:/[a-zA-Z0-9]+)?$" { fastcgi_param PATH_INFO $1; include fastcgi_params; } - - location /hooks { - proxy_pass http://127.0.0.1:9000/hooks; + location ~ ^/hooks/update-repo/([a-zA-Z0-9._-]+)$ { + proxy_pass http://127.0.0.1:9000/hooks/update-repo?name=$1; } } diff --git a/conf/webhook.conf b/conf/webhook.conf new file mode 100644 index 0000000..8ca870c --- /dev/null +++ b/conf/webhook.conf @@ -0,0 +1,22 @@ +[ + { + "id": "update-repo", + "execute-command": "/srv/scripts/update_repo.sh", + "pass-arguments-to-command": [ + { + "source": "url", + "name": "name" + } + ], + "trigger-rule": { + "match": { + "type": "regex", + "regex": "^[a-zA-Z0-9._-]+$", + "parameter": { + "source": "url", + "name": "name" + } + } + } + } +] diff --git a/scripts/clone_repo.sh b/scripts/clone_repo.sh new file mode 100755 index 0000000..469e281 --- /dev/null +++ b/scripts/clone_repo.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +set -e + +source "${BASH_SOURCE%/*}"/config._sh + +GIT_REPO=$1 + +if [ -z "$GIT_REPO" ]; then + echo "Specify repository name as a first argument" >&2 + exit 1 +fi + +if [ -d "$GIT_CLONE_PATH/$GIT_REPO" ]; then + echo "Repository $GIT_CLONE_PATH/$GIT_REPO already exists" >&2 + exit 1 +fi + +git clone --mirror "$GIT_REMOTE/$GIT_REPO" "$GIT_CLONE_PATH/$GIT_REPO" + +chown -R www-data: "$GIT_CLONE_PATH/$GIT_REPO" + diff --git a/scripts/config._sh.example b/scripts/config._sh.example new file mode 100644 index 0000000..33a1827 --- /dev/null +++ b/scripts/config._sh.example @@ -0,0 +1,9 @@ + +DEFAULT_GIT_REMOTE=https://gitlab.com/my-namespace + +DEFAULT_GIT_CLONE_PATH=/srv/repos + + + +export GIT_REMOTE=${GIT_REMOTE:-$DEFAULT_GIT_REMOTE} +export GIT_CLONE_PATH=${GIT_CLONE_PATH:-$DEFAULT_GIT_CLONE_PATH} diff --git a/scripts/update_repo.sh b/scripts/update_repo.sh new file mode 100755 index 0000000..bd03767 --- /dev/null +++ b/scripts/update_repo.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +set -e + +source "${BASH_SOURCE%/*}"/config._sh + +if [ -z "$GIT_REPO" ]; then + GIT_REPO=$1 +fi + +if [ -z "$GIT_REPO" ]; then + echo "Specify repository name as a first argument or in GIT_REPO env variable" >&2 + exit 1 +fi + +if [ ! -d "$GIT_CLONE_PATH/$GIT_REPO" ]; then + echo "Repository $GIT_CLONE_PATH/$GIT_REPO does not exists" >&2 + exit 1 +fi + +cd "$GIT_CLONE_PATH/$GIT_REPO" + +if [ "$(id -u)" -eq 0 ]; then + sudo -u www-data git remote update --prune +else + git remote update --prune +fi -- 2.47.3