From 454e47456475dcb48c0b93c2ad55ba0fa0af6a42 Mon Sep 17 00:00:00 2001 From: Brian Read Date: Sun, 26 May 2024 20:18:34 +0100 Subject: [PATCH] Add github-make-push-mirror.sh --- README.md | 8 ++ github-make-push-mirror.sh | 194 +++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100755 github-make-push-mirror.sh diff --git a/README.md b/README.md index c57ba10..b984570 100644 --- a/README.md +++ b/README.md @@ -195,3 +195,11 @@ Return the newest tag for the repo * \ name of the repo (package) * \ can be any organisation or user on the remote or local GITEA instance +## github-make-push-mirror.sh + +Create a repo on github.com and add a push mirror entry to the equivalent gitea repo. + +* \ Name of the repo +* \ Name of the organisation in GITEA +* \ Name of the organisation in GITHUB + diff --git a/github-make-push-mirror.sh b/github-make-push-mirror.sh new file mode 100755 index 0000000..2b4c0b3 --- /dev/null +++ b/github-make-push-mirror.sh @@ -0,0 +1,194 @@ +#!/bin/bash +# +# $1 = Module name e.g. smeserver-ddclient (assumes github repo is to be called the same) +# $2 = gitea Organisation (smeserver or smecontrib) +# $3 = github Organisation (Koozali-SME-Server or Koozali-SME-Server-contribs or smeserver) +# + +# Make this null to get curl to check ssl cert +checkSSL="-k" +# and -v for curl verbose mode +SILENT4CURL="-s" + +if [ "$#" -ne 3 ]; then + echo "Error: github-make-push-mirror.sh " + exit 0 +fi + +# Check that jq is installed +if command -v jq -V > /dev/null; then + if [ $DEBUG ] ; then echo "Jq is installed" ; fi +else + echo "Error: jq is not installed (try EPEL)" + exit 1 +fi + +#check gh is loaded +if command -v gh --version > /dev/null; then + if [ $DEBUG ] ; then echo "gh is installed" ; fi +else + echo "Error: gh is not installed **************" + echo "try: curl -fsSL https://cli.github.com/packages/rpm/gh-cli.repo | sudo tee /etc/yum.repos.d/github-cli.repo" + echo "then:sudo dnf -y install gh" + exit 1 +fi + +# +# Pull in parameters from a config file ~/.smegit/config +# +inifilename=$(echo ~)"/.smegit/config" +if [ ! -e $inifilename ] ; then + # Not here, look at system default + if [ ! -e /etc/smegit.ini ] ; then + echo "No ini file found $inifiename or /etc/smegit.ini" + exit 1 + else + initfilename="/etc/smegit.ini" + fi +fi + +while read -r line || [[ -n "$line" ]]; do + if [[ $line =~ ^\[.*\]$ ]] + then + section=${line#*[} + section=${section%]*} + else + if [[ $line =~ ^[^#]*= ]] + then + key=${line%=*} + value=${line#*=} + declare "${section}_${key}=$value" + fi + fi +done < "$inifilename" + +GITEAUser=${remote_USER} +GITEAACCESSTOKEN=${remote_GITEAACCESSTOKEN} +GITEAHOST=${remote_GITEAHOST} + +#First check repo name exists in gitea +REPO_NAME=$1 +RESPONSE=$(curl $checkSSL $SILENT4CURL -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$2/$REPO_NAME") +if [ "$RESPONSE" == "200" ]; then + echo "$2/$1 exists in GITEA" +else + echo "Error: $2/$1 does not exists in GITEA" + exit 1 +fi + +# and that the github token has been installed in the .smegit file +# Note that this personal access token is generated by github against a particular login +GITHUBLOGIN=${remote_GITHUBLOGIN} +if [ -z "${remote_GITHUBTOKEN}" ]; then + echo "Error: The GITHUBTOKEN is not defined in .smegit/config ." +fi + +#and check that github is logged in +if ! gh auth status > /dev/null 2>&1; then + echo "You must log in with GitHub CLI first." + echo "Run 'gh auth login' and follow the instructions." + exit 1 +fi + +# first create repo on github or skip over if already created +REPO_NAME=$3/$1 +DESCRIPTION="Repo-$1 - push mirror from Koozali gitea repo $1" + +# Create the repository + +# Check if the repository exists +repo_exists=$(gh repo view "$REPO_NAME" > /dev/null 2>&1) + +if [ $? -eq 0 ]; then + echo "The repository '$REPO_NAME' already exists." +else + echo "Creating GitHub repository '$REPO_NAME'..." + gh repo create "$REPO_NAME" --description "$DESCRIPTION" --public + if [ $? -eq 0 ]; then + echo "GitHub Repository '$REPO_NAME' created successfully." + else + echo "Failed to create repository '$REPO_NAME'." + exit 1 + fi +fi + +# Now update the equivalent gitea repo to push to the github one. +GITHUB_REPO_URL="https://github.com/$3/$1.git" + +# API endpoint to create a push mirror +API_ENDPOINT="${remote_GITEAHOST}/api/v1/repos/${2}/${1}/push_mirrors" + +# See if the push mirror exists already +# Fetch repository mirror settings using the Gitea API +response=$(curl $checkSSL $SILENT4CURL -s -H "Authorization: token ${remote_GITEAACCESSTOKEN}" \ + "$API_ENDPOINT") + +if [ -z "$response" ]; then + echo "Failed to fetch mirror settings from Gitea." + exit 1 +fi + +# Check if there are any push mirrors +push_mirrors=$(echo "$response" | jq -r '.[] | select(.sync_on_commit == true)') + +if [ -z "$push_mirrors" ]; then + echo "No push mirror configurations found for the repository '$2/$1'." +else + echo "Push mirror configurations found for the repository '$2/$1:" + #echo "$push_mirrors" | jq +fi + +# check if one of them is the one to github +github_mirror=$(echo "$response" | jq -r --arg address "${GITHUB_REPO_URL}" '.[] | select(.remote_address == $address)') +if [ -z "$github_mirror" ]; then + echo "No push mirror configuration with remote address '${GITHUB_REPO_URL}' found for the repository '$REPO_NAME'." +else + echo "Push mirror configuration with remote address '${GITHUB_REPO_URL}' found for the repository '$REPO_NAME':" + echo "$github_mirror" | jq + remote_name=$(echo "$github_mirror" | jq -r '.remote_name') + echo "Deleting this push mirror entry with remote_name: $remote_name" + # Delete the mirror using the mirror ID + delete_response=$(curl $checkSSL $SILENT4CURL -s -X DELETE -H "Authorization: token ${remote_GITEAACCESSTOKEN}" \ + "$API_ENDPOINT/$remote_name") + if [ -z "$delete_response" ]; then + echo "Successfully deleted the mirror with remote name '$remote_name'." + else + echo "Failed to delete the mirror with remote name '$remote_name'. Response: $delete_response" + fi +fi + +# JSON payload to be sent to the Gitea API to create the push mirrir +MIRROR_PAYLOAD=$(cat <