codimd-git-sync/sync_notes.sh

148 lines
3.8 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
REPO_FOLDER="drive-temp"
function clone_repo() {
mkdir -p "$REPO_FOLDER"
cd "$REPO_FOLDER"
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null || true)"
if [ ! "$inside_git_repo" ]; then
git init
git remote add origin "https://$GITLAB_ACCESS_TOKEN_NAME:$GITLAB_ACCESS_TOKEN@git.zeus.gent/bestuur/drive.git"
git config user.email "codimd.zeus.gent@mcbloch.dev"
git config user.name "CodiMD sync bot"
git pull origin master
else
echo "> Git repo already initialized, skipping"
fi
git fetch -a
cd ..
}
function clear_repo() {
git restore .
git checkout -- .
}
function checkout_branch() {
branch_name=$1
# Start from master
git checkout master
# Now go to the correct branch name
if ! git checkout -b "$branch_name" >/dev/null; then
echo "> Checkout existing branch"
git checkout "$branch_name" >/dev/null
else
echo "> Created new branch"
fi
if git branch --set-upstream-to="origin/$branch_name" "$branch_name"; then # >/dev/null
git pull
fi
}
function sync_file() {
note_name=$1
branch_name="codimd-sync_$sync_to"
echo "> Starting sync of $note_name"
clear_repo
checkout_branch "$branch_name"
echo "> Copy the note to $sync_to"
cp "../data/$note_name" "$sync_to"
git add "$sync_to"
if ! git commit -m "[bot] Update Gitlab with latest CodiMD file version"; then
#echo "> No changes in our file."
:
else
#echo "> Changes in our file, committing."
:
fi
git push -u origin "$branch_name"
#MR_NAME="[CodiMD Sync] $note_name"
#echo "> Checking if pr with name '$MR_NAME' already exists"
# mrs=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" "https://git.zeus.gent/api/v4/projects/$GITLAB_PROJECT_ID/merge_requests?labels=codimd-sync" | jq -e 'select(type == "array" and length == 0)' )
# echo $mrs | jq -e 'select(type == "array" and length == 0)'
# Check if a MR is already made (open or merged)
echo "> Checking if the branch differs from master"
echo "> If so a new pr should be created to push our work"
echo "> If an open pr already exists, pass"
echo
diff_lines=$(git diff "origin/master..$branch_name" | wc -l)
if [ "$diff_lines" == "0" ]; then
echo "> Branch has no changes compared to master."
else
echo "> Branch has changes"
if (glab mr list --all --source-branch="$branch_name" | grep "No merge requests match your search"); then
echo "> No matching Merge Request found at all"
glab mr create --label codimd-sync -t "[CodiMD sync] Add document $sync_to" --fill --yes
cd ..
rm -rf drive-temp
else
echo "> Matching Merge Request found"
echo "> Making sure it is an open one"
if (glab mr list --source-branch="$branch_name" | grep "No open merge requests match your search"); then
echo "No open merge request found"
glab mr create --label codimd-sync -t "[CodiMD sync] Update document $sync_to" --fill --yes
else
echo "Open merge request found."
fi
fi
fi
}
function sync_files() {
cd data
for note_name in *.md; do
echo
echo "> ======================"
echo "> Analyzing $note_name"
# Extract the sync-to path
sync_to=$(sed -n -e '/:::spoiler Gitlab sync/,/:::/ p' "$note_name" | grep "sync-to" | cut -d":" -f2 | xargs || true)
if [ "$sync_to" == "" ]; then
# echo "> No metadata found, skip"
:
else
echo "> Found a requested sync to: $sync_to"
cd ../$REPO_FOLDER
sync_file "$note_name"
cd ../data
fi
done
}
glab auth login --hostname git.zeus.gent --token "$GITLAB_TOKEN"
# A one time operation to clone the repo.
clone_repo
# Loop over the files in the data folder and sync them to Gitlab via Merge Requests
sync_files
exit 0
# https://git.zeus.gent/bestuur/drive
# GITLAB_PROJECT_ID=2