195 lines
6.0 KiB
Bash
195 lines
6.0 KiB
Bash
|
#!/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 <modulename> <organization> <github orgamisation>"
|
||
|
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 <<EOF
|
||
|
{
|
||
|
"interval" : "8h0m0s",
|
||
|
"remote_address": "${GITHUB_REPO_URL}",
|
||
|
"remote_username": "${GITHUBLOGIN}",
|
||
|
"remote_password": "${remote_GITHUBTOKEN}",
|
||
|
"sync_on_commit" : true
|
||
|
}
|
||
|
EOF
|
||
|
)
|
||
|
|
||
|
# Create the push mirror
|
||
|
response=$(curl $checkSSL $SILENT4CURL -s -w "\nHTTP_STATUS_CODE:%{http_code}" -X POST "$API_ENDPOINT" \
|
||
|
-H "Authorization: token ${remote_GITEAACCESSTOKEN}" \
|
||
|
-H "Content-Type: application/json" \
|
||
|
-d "$MIRROR_PAYLOAD")
|
||
|
|
||
|
# Extract the response and HTTP status code
|
||
|
echo "Creating the push mirror for $2/$1 to $3/$1"
|
||
|
body=$(echo "$response" | sed -n '1,/HTTP_STATUS_CODE:/p' | sed '$d')
|
||
|
status_code=$(echo "$response" | grep "HTTP_STATUS_CODE:" | awk -F ':' '{print $2}')
|
||
|
|
||
|
# Evaluate the response
|
||
|
if [[ "$status_code" -ge 200 && "$status_code" -lt 300 ]]; then
|
||
|
echo "Successfully set up push mirror from Gitea repository to GitHub for $2/$1 to $3/$1."
|
||
|
else
|
||
|
echo "Failed to set up push mirror. HTTP Status Code: $status_code"
|
||
|
echo "Response: $body"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
exit 0
|
||
|
|
||
|
|