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:
|
||||
python -m hug -f src/web.py
|
||||
sync:
|
||||
curl localhost:8000/sync-mattermost
|
||||
run:
|
||||
python src/run.py
|
||||
|
|
|
@ -9,16 +9,12 @@ selected_user = "my_username"
|
|||
|
||||
[mattermost.users.my_username]
|
||||
|
||||
name = "my_username"
|
||||
password = "..."
|
||||
token = "..."
|
||||
|
||||
|
||||
[codimd]
|
||||
|
||||
server_url = "https://codimd.zeus.gent"
|
||||
email = ""
|
||||
password = ""
|
||||
|
||||
|
||||
[gitea]
|
||||
|
||||
|
@ -30,4 +26,4 @@ remote_org = "ZeusWPI"
|
|||
remote_repo = "drive"
|
||||
|
||||
commit_user_email = "..."
|
||||
commit_user_name = "..."
|
||||
commit_user_name = "..."
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
tabulate
|
||||
humanize
|
||||
colored
|
||||
|
||||
mattermostdriver
|
||||
hug
|
||||
|
||||
gitpython
|
||||
giteapy
|
||||
giteapy
|
||||
|
|
|
@ -2,18 +2,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import pprint as pp
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from time import sleep
|
||||
from typing import Dict, List
|
||||
from typing import Dict
|
||||
|
||||
from colored import Style
|
||||
from mattermostdriver import Driver
|
||||
from tabulate import tabulate
|
||||
|
||||
from config import config
|
||||
from mattermost_objects import MMChannelPosts, MMPost, MMUser
|
||||
from utils import humanize_date_difference, timer
|
||||
from mattermost_objects import MMChannelPosts
|
||||
from utils import timer
|
||||
|
||||
pp = pp.PrettyPrinter(indent=2)
|
||||
|
||||
|
@ -28,19 +25,6 @@ class User(ABC):
|
|||
def credentials_dict(self) -> dict:
|
||||
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):
|
||||
def __init__(self, token):
|
||||
self.token = token
|
||||
|
@ -60,8 +44,6 @@ def loadusers():
|
|||
for name, data in config["mattermost"]["users"].items():
|
||||
if "token" in data:
|
||||
usr = TokenUser(token=data["token"])
|
||||
elif "name" in data and "password" in data:
|
||||
usr = NormalUser(login_id=data["name"], password=data["password"])
|
||||
else:
|
||||
print("Invalid user '{}' in toml file".format(name))
|
||||
exit(1)
|
||||
|
@ -76,7 +58,7 @@ def merge_dict(a: dict, b: dict) -> dict:
|
|||
|
||||
|
||||
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}")
|
||||
Driver.__init__(
|
||||
self,
|
||||
|
@ -92,7 +74,7 @@ class MMApi(Driver):
|
|||
self.login()
|
||||
self.user_id = self.users.get_user(user_id="me")["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" = - Team: {self.team_id}")
|
||||
|
||||
|
@ -154,7 +136,7 @@ class MMApi(Driver):
|
|||
class ChannelApi(MMApi):
|
||||
def __init__(self, channel_name=None, channel_id=None, user=None):
|
||||
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:
|
||||
self.channel_id = self.get_channel_id(channel_name)
|
||||
|
|
|
@ -5,7 +5,7 @@ from config import config
|
|||
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"]
|
||||
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']}"
|
||||
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:
|
||||
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.
|
||||
|
@ -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>
|
||||
:::
|
||||
```"""
|
||||
send_message(file_id, file_info, message)
|
||||
send_message(file_info, message)
|
||||
|
|
|
@ -5,7 +5,6 @@ import time
|
|||
import traceback
|
||||
from pprint import pprint
|
||||
|
||||
import hug
|
||||
import requests
|
||||
|
||||
import db
|
||||
|
@ -79,13 +78,11 @@ def validate_downloaded_files(post_mattermost_hint=True):
|
|||
if metadata is not None:
|
||||
is_new_file, new_file_info = db.mark_file_valid(file_id, metadata)
|
||||
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:
|
||||
changed, new_file_info = db.mark_file_invalid(file_id)
|
||||
if changed and post_mattermost_hint:
|
||||
mattermost_communication.report_newly_found_but_invalid_file(
|
||||
file_id, new_file_info
|
||||
)
|
||||
mattermost_communication.report_newly_found_but_invalid_file(new_file_info)
|
||||
|
||||
return db._load_db()
|
||||
|
||||
|
@ -94,17 +91,14 @@ def sync_files_to_gitea():
|
|||
repo, api_handler = 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"]
|
||||
try:
|
||||
sync.sync_file(repo, api_handler, local_file_path, sync_to)
|
||||
sync.sync_file(repo, api_handler, file_info)
|
||||
except Exception as e:
|
||||
print("Critical error: Failed to sync file to Gitea")
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
@hug.get("/sync-mattermost")
|
||||
def sync_mattermost():
|
||||
if __name__ == "__main__":
|
||||
print()
|
||||
print("=======================================")
|
||||
print("== Finding urls posted on mattermost ==")
|
|
@ -2,15 +2,12 @@ from __future__ import print_function
|
|||
|
||||
import os
|
||||
import pathlib
|
||||
import time
|
||||
from pprint import pprint
|
||||
|
||||
import git
|
||||
import giteapy
|
||||
from giteapy.rest import ApiException
|
||||
|
||||
import db
|
||||
from config import config
|
||||
import mattermost_communication
|
||||
|
||||
# https://docs.gitea.com/api/1.20/
|
||||
|
||||
|
@ -71,7 +68,10 @@ def checkout_branch(repo, branch_name):
|
|||
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}"
|
||||
print(f"Starting sync of {path}")
|
||||
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:
|
||||
w.write(r.read())
|
||||
print(repo.index.diff())
|
||||
print(repo.untracked_files)
|
||||
if repo.index.diff() or repo.untracked_files:
|
||||
if repo.git.diff() or repo.untracked_files:
|
||||
print(" Note has changes. Making a commit.")
|
||||
repo.index.add([sync_to])
|
||||
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}",
|
||||
),
|
||||
)
|
||||
mattermost_communication.report_newly_found_file(file_info)
|
||||
else:
|
||||
print(" Merge request was already open.")
|
||||
else:
|
||||
|
|
Loading…
Reference in a new issue