update
This commit is contained in:
parent
610c7e95b5
commit
d5e18b2360
7 changed files with 27 additions and 61 deletions
6
Makefile
6
Makefile
|
@ -1,4 +1,2 @@
|
||||||
web:
|
run:
|
||||||
python -m hug -f src/web.py
|
python src/run.py
|
||||||
sync:
|
|
||||||
curl localhost:8000/sync-mattermost
|
|
||||||
|
|
|
@ -9,16 +9,12 @@ selected_user = "my_username"
|
||||||
|
|
||||||
[mattermost.users.my_username]
|
[mattermost.users.my_username]
|
||||||
|
|
||||||
name = "my_username"
|
token = "..."
|
||||||
password = "..."
|
|
||||||
|
|
||||||
|
|
||||||
[codimd]
|
[codimd]
|
||||||
|
|
||||||
server_url = "https://codimd.zeus.gent"
|
server_url = "https://codimd.zeus.gent"
|
||||||
email = ""
|
|
||||||
password = ""
|
|
||||||
|
|
||||||
|
|
||||||
[gitea]
|
[gitea]
|
||||||
|
|
||||||
|
@ -30,4 +26,4 @@ remote_org = "ZeusWPI"
|
||||||
remote_repo = "drive"
|
remote_repo = "drive"
|
||||||
|
|
||||||
commit_user_email = "..."
|
commit_user_email = "..."
|
||||||
commit_user_name = "..."
|
commit_user_name = "..."
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
tabulate
|
tabulate
|
||||||
humanize
|
humanize
|
||||||
colored
|
colored
|
||||||
|
|
||||||
mattermostdriver
|
mattermostdriver
|
||||||
hug
|
|
||||||
|
|
||||||
gitpython
|
gitpython
|
||||||
giteapy
|
giteapy
|
||||||
|
|
|
@ -2,18 +2,15 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import pprint as pp
|
import pprint as pp
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from datetime import datetime
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from time import sleep
|
from typing import Dict
|
||||||
from typing import Dict, List
|
|
||||||
|
|
||||||
from colored import Style
|
from colored import Style
|
||||||
from mattermostdriver import Driver
|
from mattermostdriver import Driver
|
||||||
from tabulate import tabulate
|
|
||||||
|
|
||||||
from config import config
|
from config import config
|
||||||
from mattermost_objects import MMChannelPosts, MMPost, MMUser
|
from mattermost_objects import MMChannelPosts
|
||||||
from utils import humanize_date_difference, timer
|
from utils import timer
|
||||||
|
|
||||||
pp = pp.PrettyPrinter(indent=2)
|
pp = pp.PrettyPrinter(indent=2)
|
||||||
|
|
||||||
|
@ -28,19 +25,6 @@ class User(ABC):
|
||||||
def credentials_dict(self) -> dict:
|
def credentials_dict(self) -> dict:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NormalUser(User):
|
|
||||||
def __init__(self, login_id, password):
|
|
||||||
self.login_id = login_id
|
|
||||||
self.password = password
|
|
||||||
|
|
||||||
def credentials_dict(self) -> dict:
|
|
||||||
return {"login_id": self.login_id, "password": self.password}
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "User<name: {}, password: ******>".format(self.login_id)
|
|
||||||
|
|
||||||
|
|
||||||
class TokenUser(User):
|
class TokenUser(User):
|
||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
self.token = token
|
self.token = token
|
||||||
|
@ -60,8 +44,6 @@ def loadusers():
|
||||||
for name, data in config["mattermost"]["users"].items():
|
for name, data in config["mattermost"]["users"].items():
|
||||||
if "token" in data:
|
if "token" in data:
|
||||||
usr = TokenUser(token=data["token"])
|
usr = TokenUser(token=data["token"])
|
||||||
elif "name" in data and "password" in data:
|
|
||||||
usr = NormalUser(login_id=data["name"], password=data["password"])
|
|
||||||
else:
|
else:
|
||||||
print("Invalid user '{}' in toml file".format(name))
|
print("Invalid user '{}' in toml file".format(name))
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -76,7 +58,7 @@ def merge_dict(a: dict, b: dict) -> dict:
|
||||||
|
|
||||||
|
|
||||||
class MMApi(Driver):
|
class MMApi(Driver):
|
||||||
def __init__(self, user: User = users["flynn"]):
|
def __init__(self, user: User = users["zeusbot9000"]):
|
||||||
print(f"Initializing MMApi client for user {user}")
|
print(f"Initializing MMApi client for user {user}")
|
||||||
Driver.__init__(
|
Driver.__init__(
|
||||||
self,
|
self,
|
||||||
|
@ -92,7 +74,7 @@ class MMApi(Driver):
|
||||||
self.login()
|
self.login()
|
||||||
self.user_id = self.users.get_user(user_id="me")["id"]
|
self.user_id = self.users.get_user(user_id="me")["id"]
|
||||||
self.team_id = self.teams.get_team_by_name("zeus")["id"]
|
self.team_id = self.teams.get_team_by_name("zeus")["id"]
|
||||||
print(f" = Creating mattermost client")
|
print(" = Creating mattermost client")
|
||||||
print(f" = - User: {self.user_id}")
|
print(f" = - User: {self.user_id}")
|
||||||
print(f" = - Team: {self.team_id}")
|
print(f" = - Team: {self.team_id}")
|
||||||
|
|
||||||
|
@ -154,7 +136,7 @@ class MMApi(Driver):
|
||||||
class ChannelApi(MMApi):
|
class ChannelApi(MMApi):
|
||||||
def __init__(self, channel_name=None, channel_id=None, user=None):
|
def __init__(self, channel_name=None, channel_id=None, user=None):
|
||||||
MMApi.__init__(self, user)
|
MMApi.__init__(self, user)
|
||||||
assert channel_name != None or channel_id != None
|
assert channel_name is not None or channel_id != None
|
||||||
|
|
||||||
if channel_name is not None:
|
if channel_name is not None:
|
||||||
self.channel_id = self.get_channel_id(channel_name)
|
self.channel_id = self.get_channel_id(channel_name)
|
||||||
|
|
|
@ -5,7 +5,7 @@ from config import config
|
||||||
from mattermost_client import ChannelApi, MMApi
|
from mattermost_client import ChannelApi, MMApi
|
||||||
|
|
||||||
|
|
||||||
def send_message(file_id, file_info, message):
|
def send_message(file_info, message):
|
||||||
channel_id = file_info["originating_mm_post_channel_id"]
|
channel_id = file_info["originating_mm_post_channel_id"]
|
||||||
post_id = file_info["originating_mm_post_id"]
|
post_id = file_info["originating_mm_post_id"]
|
||||||
|
|
||||||
|
@ -34,13 +34,13 @@ def send_message(file_id, file_info, message):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def report_newly_found_file(file_id, file_info):
|
def report_newly_found_file(file_info):
|
||||||
git_url = f"https://{config['gitea']['server_url']}/{config['gitea']['remote_org']}/{config['gitea']['remote_repo']}"
|
git_url = f"https://{config['gitea']['server_url']}/{config['gitea']['remote_org']}/{config['gitea']['remote_repo']}"
|
||||||
message = f"I found a new CodiMD file in this post! Making work of putting it on git :)\n - Requested location in the [drive]({git_url}): {file_info['metadata']['sync-to']}"
|
message = f"I found a new CodiMD file in this post! Making work of putting it on git :)\n - Requested location in the [drive]({git_url}): {file_info['metadata']['sync-to']}"
|
||||||
send_message(file_id, file_info, message)
|
send_message(file_info, message)
|
||||||
|
|
||||||
|
|
||||||
def report_newly_found_but_invalid_file(file_id, file_info):
|
def report_newly_found_but_invalid_file(file_info):
|
||||||
message = """Hi there! :wave:
|
message = """Hi there! :wave:
|
||||||
I'm your friendly neighbourhood document sync bot.
|
I'm your friendly neighbourhood document sync bot.
|
||||||
I could synchronize this CodiMD file automatically to our Git 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.
|
||||||
|
@ -53,4 +53,4 @@ Just add the following lines to your file, the location in your file is not impo
|
||||||
- sync-to: <a valid path on the DRIVE, for ex.: verslagen/21-22/2022-05-13.md>
|
- sync-to: <a valid path on the DRIVE, for ex.: verslagen/21-22/2022-05-13.md>
|
||||||
:::
|
:::
|
||||||
```"""
|
```"""
|
||||||
send_message(file_id, file_info, message)
|
send_message(file_info, message)
|
||||||
|
|
|
@ -5,7 +5,6 @@ import time
|
||||||
import traceback
|
import traceback
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
import hug
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
import db
|
import db
|
||||||
|
@ -79,13 +78,11 @@ def validate_downloaded_files(post_mattermost_hint=True):
|
||||||
if metadata is not None:
|
if metadata is not None:
|
||||||
is_new_file, new_file_info = db.mark_file_valid(file_id, metadata)
|
is_new_file, new_file_info = db.mark_file_valid(file_id, metadata)
|
||||||
if is_new_file:
|
if is_new_file:
|
||||||
mattermost_communication.report_newly_found_file(file_id, new_file_info)
|
print(f"new file found: {new_file_info["source_url"]}")
|
||||||
else:
|
else:
|
||||||
changed, new_file_info = db.mark_file_invalid(file_id)
|
changed, new_file_info = db.mark_file_invalid(file_id)
|
||||||
if changed and post_mattermost_hint:
|
if changed and post_mattermost_hint:
|
||||||
mattermost_communication.report_newly_found_but_invalid_file(
|
mattermost_communication.report_newly_found_but_invalid_file(new_file_info)
|
||||||
file_id, new_file_info
|
|
||||||
)
|
|
||||||
|
|
||||||
return db._load_db()
|
return db._load_db()
|
||||||
|
|
||||||
|
@ -94,17 +91,14 @@ def sync_files_to_gitea():
|
||||||
repo, api_handler = sync.init_sync()
|
repo, api_handler = sync.init_sync()
|
||||||
for file_id, file_info in db.get_files().items():
|
for file_id, file_info in db.get_files().items():
|
||||||
if file_info["valid"]:
|
if file_info["valid"]:
|
||||||
local_file_path = file_info["local_file_path"]
|
|
||||||
sync_to = file_info["metadata"]["sync-to"]
|
|
||||||
try:
|
try:
|
||||||
sync.sync_file(repo, api_handler, local_file_path, sync_to)
|
sync.sync_file(repo, api_handler, file_info)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Critical error: Failed to sync file to Gitea")
|
print("Critical error: Failed to sync file to Gitea")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
@hug.get("/sync-mattermost")
|
if __name__ == "__main__":
|
||||||
def sync_mattermost():
|
|
||||||
print()
|
print()
|
||||||
print("=======================================")
|
print("=======================================")
|
||||||
print("== Finding urls posted on mattermost ==")
|
print("== Finding urls posted on mattermost ==")
|
|
@ -2,15 +2,12 @@ from __future__ import print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import time
|
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
import git
|
import git
|
||||||
import giteapy
|
import giteapy
|
||||||
from giteapy.rest import ApiException
|
|
||||||
|
|
||||||
import db
|
|
||||||
from config import config
|
from config import config
|
||||||
|
import mattermost_communication
|
||||||
|
|
||||||
# https://docs.gitea.com/api/1.20/
|
# https://docs.gitea.com/api/1.20/
|
||||||
|
|
||||||
|
@ -71,7 +68,10 @@ def checkout_branch(repo, branch_name):
|
||||||
repo.remotes.origin.pull()
|
repo.remotes.origin.pull()
|
||||||
|
|
||||||
|
|
||||||
def sync_file(repo, api_instance, path, sync_to):
|
def sync_file(repo, api_instance, file_info):
|
||||||
|
path = file_info["local_file_path"]
|
||||||
|
sync_to = file_info["metadata"]["sync-to"]
|
||||||
|
|
||||||
branch_name = f"codimd-sync_{sync_to}"
|
branch_name = f"codimd-sync_{sync_to}"
|
||||||
print(f"Starting sync of {path}")
|
print(f"Starting sync of {path}")
|
||||||
clear_repo(repo)
|
clear_repo(repo)
|
||||||
|
@ -83,9 +83,7 @@ def sync_file(repo, api_instance, path, sync_to):
|
||||||
)
|
)
|
||||||
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())
|
if repo.git.diff() or repo.untracked_files:
|
||||||
print(repo.untracked_files)
|
|
||||||
if repo.index.diff() or repo.untracked_files:
|
|
||||||
print(" Note has changes. Making a commit.")
|
print(" Note has changes. Making a commit.")
|
||||||
repo.index.add([sync_to])
|
repo.index.add([sync_to])
|
||||||
repo.index.commit("Updating file with codimd version")
|
repo.index.commit("Updating file with codimd version")
|
||||||
|
@ -122,6 +120,7 @@ def sync_file(repo, api_instance, path, sync_to):
|
||||||
title=f"[CodiMD sync] Add document {sync_to}",
|
title=f"[CodiMD sync] Add document {sync_to}",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
mattermost_communication.report_newly_found_file(file_info)
|
||||||
else:
|
else:
|
||||||
print(" Merge request was already open.")
|
print(" Merge request was already open.")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue