From 610c7e95b5fd616ce8beb98369027f40622c0aae Mon Sep 17 00:00:00 2001 From: mcbloch Date: Wed, 27 Mar 2024 18:07:34 +0100 Subject: [PATCH] remove references to gitlab --- README.md | 11 +--- config.example.toml | 10 ---- requirements.txt | 1 - src/mattermost_communication.py | 2 +- src/sync_gitea.py | 4 +- src/sync_gitlab.py | 103 -------------------------------- src/web.py | 16 +---- 7 files changed, 6 insertions(+), 141 deletions(-) delete mode 100755 src/sync_gitlab.py diff --git a/README.md b/README.md index 754b478..bd881ba 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,12 @@ A python webserver that automatically hooks into a mattermost instance, listens **TODO** - Hook into mattermost to automatically react to new files -- Change gitlab integration to gitea **Done** - Webserver that starts a sync when an endpoint is called - Automatic detection of codimd file links in mattermost message - Download of codimd files -- Creation of a gitlab pull request for any new files +- Creation of a gitea pull request for any new files - Creation of new commits for any new changes to the files. Hereby updating the existing pull request or creating a new one. @@ -41,13 +40,7 @@ You need to put the following block somewhere in you CodiMD file. Copy the `config.example.toml` file to `config.toml` and complete the missing values. -## Used tech - -- [GitLab CLI Tool](https://glab.readthedocs.io/en/latest/index.html) -- bash -- git -- curl -- python +Install the requirements listed in the `requirements.txt`. ### References diff --git a/config.example.toml b/config.example.toml index 0663870..1877e9b 100644 --- a/config.example.toml +++ b/config.example.toml @@ -20,16 +20,6 @@ email = "" password = "" -# Outdated -[gitlab] - -host = "https://git.zeus.gent" -# access_token_name = "..." -# access_token = "..." - -local_repo_folder = "drive" - - [gitea] server_url = "https://git.zeus.gent" diff --git a/requirements.txt b/requirements.txt index b7fba76..090dfd3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +6,4 @@ mattermostdriver hug gitpython -python-gitlab giteapy \ No newline at end of file diff --git a/src/mattermost_communication.py b/src/mattermost_communication.py index 9f8d716..19a5e01 100644 --- a/src/mattermost_communication.py +++ b/src/mattermost_communication.py @@ -43,7 +43,7 @@ def report_newly_found_file(file_id, file_info): def report_newly_found_but_invalid_file(file_id, file_info): message = """Hi there! :wave: I'm your friendly neighbourhood document sync bot. -I could synchronize this CodiMD file automatically to our Gitlab DRIVE for safekeeping, but the necessary metadata block is not present. +I could synchronize this CodiMD file automatically to our Git DRIVE for safekeeping, but the necessary metadata block is not present. You can easily add the correct info and I will do the rest of the work for you! Just add the following lines to your file, the location in your file is not important but at the top would be my recommendation. diff --git a/src/sync_gitea.py b/src/sync_gitea.py index 1fc9b7d..0cc94d7 100644 --- a/src/sync_gitea.py +++ b/src/sync_gitea.py @@ -100,7 +100,7 @@ def sync_file(repo, api_instance, path, sync_to): branch_requests = [r for r in resp if r.head.ref == branch_name] if len(branch_requests) > 0: print( - " Creating a new merge request to update the gitlab document with the new version from CodiMD." + " Creating a new merge request to update the git document with the new version from CodiMD." ) api_instance.repo_create_pull_request( GIT_ORG, @@ -112,7 +112,7 @@ def sync_file(repo, api_instance, path, sync_to): ), ) else: - print(" Creating a new merge request to add the document to gitlab.") + print(" Creating a new merge request to add the document to git.") api_instance.repo_create_pull_request( GIT_ORG, GIT_REPO, diff --git a/src/sync_gitlab.py b/src/sync_gitlab.py deleted file mode 100755 index f6f8030..0000000 --- a/src/sync_gitlab.py +++ /dev/null @@ -1,103 +0,0 @@ -#!/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 - -from config import config - -TOKEN_NAME = os.environ["GITLAB_ACCESS_TOKEN_NAME"] -TOKEN = os.environ["GITLAB_ACCESS_TOKEN"] - -REPO_FOLDER = config["gitlab"]["local_repo_folder"] - - -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 diff --git a/src/web.py b/src/web.py index 779f9dc..77d0628 100644 --- a/src/web.py +++ b/src/web.py @@ -5,7 +5,6 @@ import time import traceback from pprint import pprint -import gitlab import hug import requests @@ -14,7 +13,6 @@ import dir_utils import mattermost_client import mattermost_communication import sync_gitea as sync -import sync_gitlab from config import config from utils import id_to_url, url_to_id @@ -92,18 +90,6 @@ def validate_downloaded_files(post_mattermost_hint=True): return db._load_db() -def sync_files_to_gitlab(): - repo, drive = sync_gitlab.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"] - try: - sync_gitlab.sync_file(drive, repo, local_file_path, sync_to) - except Exception as e: - print("Critical error: Failed to sync file to Gitlab") - - def sync_files_to_gitea(): repo, api_handler = sync.init_sync() for file_id, file_info in db.get_files().items(): @@ -133,7 +119,7 @@ def sync_mattermost(): validate_downloaded_files(post_mattermost_hint=True) print() print("================================================") - print("== Syncing files to gitlab ==") + print("== Syncing files to git ==") sync_files_to_gitea() print()