From 87304f6a1ed89bda84f4486a1f2938b633bb5b73 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 6 Oct 2022 23:13:49 +0200 Subject: [PATCH] Add sync script --- src/sync.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 src/sync.py diff --git a/src/sync.py b/src/sync.py new file mode 100755 index 0000000..4e4cff9 --- /dev/null +++ b/src/sync.py @@ -0,0 +1,67 @@ +#!/usr/bin/env -S nix shell --impure --expr "(import (builtins.getFlake \"nixpkgs\") {}).python3.withPackages (ps: [ ps.python-gitlab ps.GitPython ])" --command python + +import git +import gitlab +import json +import os +import pathlib + +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, note, sync_to): + branch_name = f'codimd-sync_{sync_to}' + print(f'Starting sync of {note}') + clear_repo(repo) + checkout_branch(repo, branch_name) + with open(f'data/{note}') 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()) + if repo.index.diff() or repo.untracked_files: + repo.git.add(sync_to) + repo.git.commit('-m', sync_to) + 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): + drive.mergerequests.create({"source_branch": branch_name, "target_branch": 'master', "title": f'[CodiMD sync] Update document {sync_to}'}) + else: + drive.mergerequests.create({"source_branch": branch_name, "target_branch": 'master', "title": f'[CodiMD sync] Add document {sync_to}'}) + +if __name__ == '__main__': + repo = get_repo() + gl = gitlab.Gitlab('https://git.zeus.gent', private_token=TOKEN) + drive = gl.projects.get(2) + with open('files.json') as f: + files = json.load(f)["valid_files"] + for path in files: + sync_file(drive, repo, path, files[path]["sync-to"]) +