From 1f0a38b21c4c52b84a3e6e1f32627564a2a1a6f9 Mon Sep 17 00:00:00 2001 From: mcbloch Date: Mon, 18 Apr 2022 23:08:18 +0200 Subject: [PATCH] This is not the commit message you are looking for --- .gitignore | 4 ++ README.md | 17 ++++++ fetch_notes.sh | 22 ++++++++ sync_notes.sh | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 fetch_notes.sh create mode 100755 sync_notes.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c195d28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +data/ +drive-temp/ +cookiefile +.env diff --git a/README.md b/README.md new file mode 100644 index 0000000..e307d78 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ + +## Setup + +Install the glab tool using a package manager of your choice. + +Set environment variable in your shell profile: + +```bash +export GITLAB_TOKEN=xxx +export GITLAB_HOST=https://git.zeus.gent +``` + +## TODO + +- Add a way to discover new markdown files. + - Scraping Mattermost + - ? Providing an api endpoint to post an url to ? \ No newline at end of file diff --git a/fetch_notes.sh b/fetch_notes.sh new file mode 100755 index 0000000..54f623d --- /dev/null +++ b/fetch_notes.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +mkdir -p data + +echo "Login to CodiMD" +curl -c cookiefile "$CMD_SERVER_URL/login" -X POST -H "Referer: $CMD_SERVER_URL/" --data-raw "email=$CMD_EMAIL&password=$CMD_PASSWORD" >/dev/null + +echo +echo + +curl -b cookiefile 'https://codimd.zeus.gent/me' | jq +echo +notes_history=$(curl -b cookiefile 'https://codimd.zeus.gent/history') + +# echo $notes_history | jq +# note_id=$(echo "$notes_history" | jq -r '.history[1].id') +ids=$(echo "$notes_history" | jq -r '.history | map(.id) | .[]') + +while IFS= read -r line; do + echo "... Reading note with ID: $line ..." + curl -b cookiefile "https://codimd.zeus.gent/$line/download" >"data/note-$line.md" +done <<<"$ids" diff --git a/sync_notes.sh b/sync_notes.sh new file mode 100755 index 0000000..76032e1 --- /dev/null +++ b/sync_notes.sh @@ -0,0 +1,148 @@ +#!/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