Add sync script

This commit is contained in:
Charlotte Van Petegem 2022-10-06 23:13:49 +02:00
parent bbfa219008
commit 87304f6a1e
No known key found for this signature in database
GPG key ID: 019E764B7184435A

67
src/sync.py Executable file
View file

@ -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"])