This is not the commit message you are looking for
This commit is contained in:
commit
1f0a38b21c
4 changed files with 191 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
data/
|
||||
drive-temp/
|
||||
cookiefile
|
||||
.env
|
17
README.md
Normal file
17
README.md
Normal file
|
@ -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 ?
|
22
fetch_notes.sh
Executable file
22
fetch_notes.sh
Executable file
|
@ -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"
|
148
sync_notes.sh
Executable file
148
sync_notes.sh
Executable file
|
@ -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
|
Loading…
Reference in a new issue