diff --git a/requirements.txt b/requirements.txt index b8573c9..69b6604 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ tabulate colored mattermostdriver -hug \ No newline at end of file +hug +gitpython +python-gitlab \ No newline at end of file diff --git a/src/sync.py b/src/sync.py index 0ceb4aa..9ff38b7 100755 --- a/src/sync.py +++ b/src/sync.py @@ -46,23 +46,29 @@ def checkout_branch(repo, branch_name): 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}" - print(f"Starting sync of {note}") + print(f"Starting sync of {path}") clear_repo(repo) + print(f" Checking out onto branch: {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( 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, @@ -71,6 +77,7 @@ def sync_file(drive, repo, note, sync_to): } ) else: + print(" Creating a new merge request to add the document to gitlab.") drive.mergerequests.create( { "source_branch": branch_name, @@ -78,13 +85,13 @@ def sync_file(drive, repo, note, sync_to): "title": f"[CodiMD sync] Add document {sync_to}", } ) + else: + print(" Merge request was already open.") + else: + print(" Note has no changes.") - -if __name__ == "__main__": +def init_sync(): 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"]) + return repo, drive \ No newline at end of file diff --git a/src/web.py b/src/web.py index d6b53a6..4cd593b 100644 --- a/src/web.py +++ b/src/web.py @@ -11,6 +11,8 @@ import db import dir_utils import mattermost_client import mattermost_communication +import sync +import gitlab from utils import id_to_url, url_to_id @@ -89,6 +91,13 @@ def validate_downloaded_files(): 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") def sync_mattermost(): @@ -105,5 +114,9 @@ def sync_mattermost(): print("== Finding valid files in the downloaded ones ==") validate_downloaded_files() print() + print("================================================") + print("== Syncing files to gitlab ==") + sync_files_to_gitlab() + print() return db._load_db()