97 lines
3.3 KiB
Python
Executable file
97 lines
3.3 KiB
Python
Executable file
#!/usr/bin/env -S nix shell --impure --expr "(import (builtins.getFlake \"nixpkgs\") {}).python3.withPackages (ps: [ ps.python-gitlab ps.GitPython ])" --command python
|
|
|
|
import json
|
|
import os
|
|
import pathlib
|
|
|
|
import git
|
|
import gitlab
|
|
|
|
TOKEN_NAME = os.environ["GITLAB_ACCESS_TOKEN_NAME"]
|
|
TOKEN = os.environ["GITLAB_ACCESS_TOKEN"]
|
|
REPO_FOLDER = "drive"
|
|
|
|
|
|
def get_repo():
|
|
if os.path.exists(REPO_FOLDER):
|
|
print("Repo already exists")
|
|
repo = git.Repo(REPO_FOLDER)
|
|
else:
|
|
print("Cloning repo")
|
|
repo = git.Repo.clone_from(
|
|
f"https://{TOKEN_NAME}:{TOKEN}@git.zeus.gent/bestuur/drive.git", REPO_FOLDER
|
|
)
|
|
with repo.config_writer() as cw:
|
|
cw.set_value("user", "email", "codimd.zeus.gent@mcbloch.dev")
|
|
cw.set_value("user", "name", "CodiMD sync bot")
|
|
repo.remotes.origin.fetch()
|
|
return repo
|
|
|
|
|
|
def clear_repo(repo):
|
|
repo.git.restore("--staged", "--", "*")
|
|
repo.git.restore("--", "*")
|
|
|
|
|
|
def checkout_branch(repo, branch_name):
|
|
repo.git.switch("master")
|
|
if branch_name in repo.heads:
|
|
repo.git.switch(branch_name)
|
|
else:
|
|
repo.git.switch("-c", branch_name)
|
|
if branch_name in repo.remotes.origin.refs:
|
|
repo.heads[branch_name].set_tracking_branch(
|
|
repo.remotes.origin.refs[branch_name]
|
|
)
|
|
repo.remotes.origin.pull()
|
|
|
|
|
|
def sync_file(drive, repo, path, sync_to):
|
|
branch_name = f"codimd-sync_{sync_to}"
|
|
print(f"Starting sync of {path}")
|
|
clear_repo(repo)
|
|
print(f" Checking out onto branch: {branch_name}")
|
|
checkout_branch(repo, branch_name)
|
|
with open(path) as r:
|
|
pathlib.Path(f'{REPO_FOLDER}/{sync_to[:sync_to.rfind("/")]}').mkdir(
|
|
parents=True, exist_ok=True
|
|
)
|
|
with open(f"{REPO_FOLDER}/{sync_to}", "w") as w:
|
|
w.write(r.read())
|
|
print(repo.index.diff())
|
|
print(repo.untracked_files)
|
|
if repo.index.diff() or repo.untracked_files:
|
|
print(" Note has changes. Making a commit.")
|
|
repo.git.add(sync_to)
|
|
repo.git.commit("-m", sync_to)
|
|
print(f" Pushing to branch: {branch_name}")
|
|
repo.git.push("-u", "origin", branch_name)
|
|
if not drive.mergerequests.list(source_branch=branch_name, state="opened"):
|
|
if drive.mergerequests.list(source_branch=branch_name):
|
|
print(" Creating a new merge request to update the gitlab document with the new version from CodiMD.")
|
|
drive.mergerequests.create(
|
|
{
|
|
"source_branch": branch_name,
|
|
"target_branch": "master",
|
|
"title": f"[CodiMD sync] Update document {sync_to}",
|
|
}
|
|
)
|
|
else:
|
|
print(" Creating a new merge request to add the document to gitlab.")
|
|
drive.mergerequests.create(
|
|
{
|
|
"source_branch": branch_name,
|
|
"target_branch": "master",
|
|
"title": f"[CodiMD sync] Add document {sync_to}",
|
|
}
|
|
)
|
|
else:
|
|
print(" Merge request was already open.")
|
|
else:
|
|
print(" Note has no changes.")
|
|
|
|
def init_sync():
|
|
repo = get_repo()
|
|
gl = gitlab.Gitlab("https://git.zeus.gent", private_token=TOKEN)
|
|
drive = gl.projects.get(2)
|
|
return repo, drive |