initial work on integrating gl sync

This commit is contained in:
mcbloch 2022-10-07 14:03:13 +02:00
parent c84c843f30
commit d39e635d10
3 changed files with 32 additions and 10 deletions

View file

@ -1,4 +1,6 @@
tabulate tabulate
colored colored
mattermostdriver mattermostdriver
hug hug
gitpython
python-gitlab

View file

@ -46,23 +46,29 @@ def checkout_branch(repo, branch_name):
repo.remotes.origin.pull() repo.remotes.origin.pull()
def sync_file(drive, repo, note, sync_to): def sync_file(drive, repo, path, sync_to):
branch_name = f"codimd-sync_{sync_to}" branch_name = f"codimd-sync_{sync_to}"
print(f"Starting sync of {note}") print(f"Starting sync of {path}")
clear_repo(repo) clear_repo(repo)
print(f" Checking out onto branch: {branch_name}")
checkout_branch(repo, branch_name) checkout_branch(repo, branch_name)
with open(f"data/{note}") as r: with open(path) as r:
pathlib.Path(f'{REPO_FOLDER}/{sync_to[:sync_to.rfind("/")]}').mkdir( pathlib.Path(f'{REPO_FOLDER}/{sync_to[:sync_to.rfind("/")]}').mkdir(
parents=True, exist_ok=True parents=True, exist_ok=True
) )
with open(f"{REPO_FOLDER}/{sync_to}", "w") as w: with open(f"{REPO_FOLDER}/{sync_to}", "w") as w:
w.write(r.read()) w.write(r.read())
print(repo.index.diff())
print(repo.untracked_files)
if repo.index.diff() or 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.add(sync_to)
repo.git.commit("-m", sync_to) repo.git.commit("-m", sync_to)
print(f" Pushing to branch: {branch_name}")
repo.git.push("-u", "origin", branch_name) repo.git.push("-u", "origin", branch_name)
if not drive.mergerequests.list(source_branch=branch_name, state="opened"): if not drive.mergerequests.list(source_branch=branch_name, state="opened"):
if drive.mergerequests.list(source_branch=branch_name): 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( drive.mergerequests.create(
{ {
"source_branch": branch_name, "source_branch": branch_name,
@ -71,6 +77,7 @@ def sync_file(drive, repo, note, sync_to):
} }
) )
else: else:
print(" Creating a new merge request to add the document to gitlab.")
drive.mergerequests.create( drive.mergerequests.create(
{ {
"source_branch": branch_name, "source_branch": branch_name,
@ -78,13 +85,13 @@ def sync_file(drive, repo, note, sync_to):
"title": f"[CodiMD sync] Add document {sync_to}", "title": f"[CodiMD sync] Add document {sync_to}",
} }
) )
else:
print(" Merge request was already open.")
else:
print(" Note has no changes.")
def init_sync():
if __name__ == "__main__":
repo = get_repo() repo = get_repo()
gl = gitlab.Gitlab("https://git.zeus.gent", private_token=TOKEN) gl = gitlab.Gitlab("https://git.zeus.gent", private_token=TOKEN)
drive = gl.projects.get(2) drive = gl.projects.get(2)
with open("files.json") as f: return repo, drive
files = json.load(f)["valid_files"]
for path in files:
sync_file(drive, repo, path, files[path]["sync-to"])

View file

@ -11,6 +11,8 @@ import db
import dir_utils import dir_utils
import mattermost_client import mattermost_client
import mattermost_communication import mattermost_communication
import sync
import gitlab
from utils import id_to_url, url_to_id from utils import id_to_url, url_to_id
@ -89,6 +91,13 @@ def validate_downloaded_files():
return db._load_db() return db._load_db()
def sync_files_to_gitlab():
repo, drive = sync.init_sync()
for file_id, file_info in db.get_files().items():
if file_info["valid"]:
local_file_path = file_info["local_file_path"]
sync_to = file_info["metadata"]["sync-to"]
sync.sync_file(drive, repo, local_file_path, sync_to)
@hug.get("/sync-mattermost") @hug.get("/sync-mattermost")
def sync_mattermost(): def sync_mattermost():
@ -105,5 +114,9 @@ def sync_mattermost():
print("== Finding valid files in the downloaded ones ==") print("== Finding valid files in the downloaded ones ==")
validate_downloaded_files() validate_downloaded_files()
print() print()
print("================================================")
print("== Syncing files to gitlab ==")
sync_files_to_gitlab()
print()
return db._load_db() return db._load_db()