From 8d2731d55538e29ce42a5c378c0879c04c09a114 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 27 Jun 2025 19:06:35 +0900 Subject: [PATCH] Basic script check in This is not completed and under development --- ReadMe.md | 10 ++++++++++ bin/.shellcheckrc | 2 ++ bin/git_pull.sh | 33 +++++++++++++++++++++++++++++++ bin/init.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++ bin/new_clone.sh | 23 ++++++++++++++++++++++ config/.gitignore | 2 ++ 6 files changed, 120 insertions(+) create mode 100644 ReadMe.md create mode 100644 bin/.shellcheckrc create mode 100755 bin/git_pull.sh create mode 100644 bin/init.sh create mode 100755 bin/new_clone.sh create mode 100644 config/.gitignore diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..9985711 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,10 @@ +# Github webhook scripts + +These are scripts to setup the basic webhook folder for one campaign and a simple crontab script to pull data from the repository + +This will not do the basic setup, this only sets up a new folder with a basic clone run + +## Scripts + +- git_pull.sh +- init_new_clone.sh diff --git a/bin/.shellcheckrc b/bin/.shellcheckrc new file mode 100644 index 0000000..b14745b --- /dev/null +++ b/bin/.shellcheckrc @@ -0,0 +1,2 @@ +shell=bash +external-sources=true diff --git a/bin/git_pull.sh b/bin/git_pull.sh new file mode 100755 index 0000000..543f858 --- /dev/null +++ b/bin/git_pull.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# fetch and merge from a remote repositry + +REPOSITORY="$1"; +BRANCH="$2"; +REMOTE="$3"; +if [ -n "${REMOTE}" ]; then + REMOTE="origin" +fi; +# BASE_FOLDER="${HOME}/tmp/git-clone/"; +BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; +# shellcheck source=init.sh +. "${BASE_FOLDER}init.sh"; + +# check that repository path exists +REPOSITORY_PATH="${GIT_REPOSITORY_FOLDER}${CLONE_BASE}${REPOSITORY}"; +if [ ! -d "${REPOSITORY_PATH}$" ]; then + echo "${REPOSITORY} not found in clone folder"; + exit; +fi; +LOG_FILE="${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}${REPOSITORY}.log"; + +# fetch to null +${GIT_COMMAND} -C "${REPOSITORY_PATH}" fetch -q "${REMOTE}" "${BRANCH}"; +changes=$(${GIT_COMMAND} -C "${REPOSITORY_PATH}" diff --stat HEAD "${REMOTE}"/"${BRANCH}"); +if [ -n "${changes}" ]; then + echo "[$(date +"%Y-%m-%d %H:%M:%S")] Changes" &>> "$LOG_FILE"; + ${GIT_COMMAND} -C "/${REPOSITORY_PATH}" merge "${REMOTE}"/"${BRANCH}" + echo "=[END]===>" &>> "$LOG_FILE"; +fi; + +# __END__ diff --git a/bin/init.sh b/bin/init.sh new file mode 100644 index 0000000..ef59e5c --- /dev/null +++ b/bin/init.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +CONFIG_BASE="${BASE_FOLDER}../config/"; +echo "CONFIG: ${CONFIG_BASE} "; +if [ -f "${CONFIG_BASE}webhook.cfg" ]; then + # shellcheck source=../config/webhook.cfg" + # shellcheck disable=SC1091 + source <(grep "=" "${CONFIG_BASE}webhook.cfg" | sed 's/ *= */=/g') +fi; +if [ "${USE_SUDO}" != "0" ] && ! id "${SUDO_USER}" &>/dev/null; then + echo "sudo user ${SUDO_USER} does not exist"; + exit; +fi; +# check that user exist +# check that git exists +if [ -z "$(command -v git)" ]; then + echo "git is not installed"; + exit; +fi; +GIT_COMMAND="git"; +if [ -n "${USE_SUDO}" ]; then + GIT_COMMAND="sudo -u ${SUDO_USER} git"; +fi; + +# branch name not set +if [ -n "${BRANCH}" ]; then + echo "No branch name given"; + exit; +fi; + +# base folder does not exist +if [ ! -d "${GIT_REPOSITORY_FOLDER}" ]; then + echo "Base folder: ${GIT_REPOSITORY_FOLDER} not found"; + exit; +fi; + +# check that log folder exists +if [ ! -d "${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}" ]; then + echo "Log folder does not exist: ${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}"; + exit; +fi; + +# check that the base clone folder exists +if [ ! -d "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}" ]; then + echo "Clone base folder does not exist: ${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"; + exit; +fi; + +export GIT_COMMAND; + diff --git a/bin/new_clone.sh b/bin/new_clone.sh new file mode 100755 index 0000000..b30d2fa --- /dev/null +++ b/bin/new_clone.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +REPOSITORY="$1"; +BRANCH="$2"; +REMOTE_HOST="$3"; +REMOTE="$4"; +if [ -n "${REMOTE}" ]; then + REMOTE="origin" +fi; +BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; +# shellcheck source=init.sh +. "${BASE_FOLDER}init.sh"; + +if [ -z "${REMOTE_HOST}" ]; then + echo "Must set a remote host for the repository"; + exit; +fi; + +echo "New clone"; + +echo ${GIT_COMMAND} clone -b "${BRANCH}" --single-branch --depth 1 --origin "${REMOTE}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"; + +# __END__ diff --git a/config/.gitignore b/config/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/config/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore