diff --git a/.gitignore b/.gitignore index 28a1000..5e861b6 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,3 @@ venv/ __pycache__/ .idea users.toml - diff --git a/.tool-versions b/.tool-versions index 9dc2289..47179ff 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -python 3.10.4 \ No newline at end of file +python 3.10.9 diff --git a/Makefile b/Makefile index 2c8a8db..116f979 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,4 @@ web: - python -m hug -f src/web.py \ No newline at end of file + python -m hug -f src/web.py +sync: + curl localhost:8000/sync-mattermost diff --git a/README.md b/README.md index e86cc08..4cab6c5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,25 @@ -A 'simple' collection of bash scripts that syncs CodiMD files with a certain metadata block to Gitlab +A python webserver that automatically hooks into a mattermost instance, listens for any mentions of codiMD files, downloads them locally and then commits them to a git repo based on a specific metadata block in the codimd file. +**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 new commits for any new changes to the files. Hereby updating the existing pull request or creating a new one. + +## How to run (currently) + + # Start local webserver + make webserver + + # Curl the webserver to trigger the sync + make sync + +## CodiMD You need to put the following block somewhere in you CodiMD file. @@ -26,18 +46,13 @@ export CMD_EMAIL=codimd.zeus.gent@mcbloch.dev export CMD_PASSWORD=******* ``` -## TODO - -- Add a way to discover new markdown files. - - Scraping Mattermost - - ? Providing an api endpoint to post an url to ? - ## Used tech - [GitLab CLI Tool](https://glab.readthedocs.io/en/latest/index.html) - bash - git - curl +- python ### References diff --git a/src/mattermost_communication.py b/src/mattermost_communication.py index c939e1b..0ff8d99 100644 --- a/src/mattermost_communication.py +++ b/src/mattermost_communication.py @@ -1,5 +1,6 @@ import mattermostdriver.exceptions +import mattermost_client from mattermost_client import ChannelApi, MMApi @@ -11,7 +12,7 @@ def send_message(file_id, file_info, message): # channel_id = MMApi().get_channel_id("bestuur-dev") channel = ChannelApi( channel_id=channel_id, - user=mattermost.users["flynn"], + user=mattermost_client.users["flynn"], ) prefix = "" diff --git a/src/web.py b/src/web.py index 4cd593b..bd4e83d 100644 --- a/src/web.py +++ b/src/web.py @@ -97,7 +97,10 @@ def sync_files_to_gitlab(): 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) + try: + sync.sync_file(drive, repo, local_file_path, sync_to) + except Exception as e: + print("Critical error: Failed to sync file to Gitlab") @hug.get("/sync-mattermost") def sync_mattermost():