Compare commits

...

5 Commits

Author SHA1 Message Date
mcbloch 0d63fcb6b5 add extra folder to gitignore 2024-03-27 17:24:36 +01:00
mcbloch e22720fea2 add mattermost messages warning 2024-03-27 17:23:47 +01:00
mcbloch 22451000b3 update readme 2024-03-27 17:19:45 +01:00
mcbloch 66830a3087 move more into config file 2024-03-27 17:16:34 +01:00
mcbloch 8d050a241d finish gitea integration 2024-03-27 17:13:33 +01:00
6 changed files with 60 additions and 73 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
data/
data_backup/
drive/
drive-temp/
db.json
.env
@ -7,4 +8,4 @@ db.json
venv/
__pycache__/
.idea
config.toml
config.toml

View File

@ -11,6 +11,13 @@ A python webserver that automatically hooks into a mattermost instance, listens
- 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.
## Warning !
When running this in a new folder it will create a lot of mattermost messages because it will find a lot of CodiMD links that are not yet locally cached.
Afterwards all these links are registered in a local `db.json` file so the application knows they have been handled.
## How to run (currently)
# Start local webserver
@ -32,19 +39,7 @@ You need to put the following block somewhere in you CodiMD file.
## Setup
Install the glab tool using a package manager of your choice.
Set environment variable in your shell profile:
```bash
export GITLAB_HOST=https://git.zeus.gent
export GITLAB_TOKEN_NAME=the-token-name-in-gitlab
export GITLAB_TOKEN=********
export CMD_SERVER_URL=https://codimd.zeus.gent
export CMD_EMAIL=codimd.zeus.gent@mcbloch.dev
export CMD_PASSWORD=*******
```
Copy the `config.example.toml` file to `config.toml` and complete the missing values.
## Used tech

View File

@ -20,6 +20,7 @@ email = ""
password = ""
# Outdated
[gitlab]
host = "https://git.zeus.gent"
@ -31,6 +32,12 @@ local_repo_folder = "drive"
[gitea]
server_url = "https://git.zeus.gent"
# Generate at gitea.example.com/user/settings/applications
access_token = "..."
local_repo_folder = "drive"
remote_org = "ZeusWPI"
remote_repo = "drive"
commit_user_email = "..."
commit_user_name = "..."

View File

@ -14,7 +14,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_client.users["flynn"],
user=mattermost_client.users[config['mattermost']['selected_user']],
)
prefix = ""
@ -36,7 +36,8 @@ def send_message(file_id, file_info, message):
def report_newly_found_file(file_id, file_info):
message = f"I found a new CodiMD file in this post! Making work of putting it on git :)\n - Requested location in the [drive](https://git.zeus.gent/bestuur/drive): {file_info['metadata']['sync-to']}"
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)

View File

@ -1,6 +1,7 @@
from __future__ import print_function
import os
import pathlib
import time
from pprint import pprint
@ -8,18 +9,24 @@ import git
import giteapy
from giteapy.rest import ApiException
import db
from config import config
# https://docs.gitea.com/api/1.20/
REPO_FOLDER = config["gitea"]["local_repo_folder"]
TOKEN = config["gitea"]["access_token"]
GIT_ORG = config["gitea"]["remote_org"]
GIT_REPO = config["gitea"]["remote_repo"]
def init_sync():
repo = get_repo()
configuration = giteapy.Configuration()
configuration.host = "https://git.zeus.gent/api/v1"
configuration.host = f"https://{config['gitea']['server_url']}/api/v1"
configuration.api_key["token"] = config["gitea"]["access_token"]
configuration.debug = False
@ -36,11 +43,12 @@ def get_repo():
else:
print("Cloning repo")
repo = git.Repo.clone_from(
f"https://{TOKEN_NAME}:{TOKEN}@git.zeus.gent/bestuur/drive.git", REPO_FOLDER
f"https://{TOKEN}@{config['gitea']['server_url']}/{GIT_ORG}/{config['gitea']['remote_repo']}.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")
cw.set_value("user", "email", config["gitea"]["commit_user_email"])
cw.set_value("user", "name", config["gitea"]["commit_user_name"])
repo.remotes.origin.fetch()
return repo
@ -79,70 +87,42 @@ def sync_file(repo, api_instance, path, sync_to):
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)
repo.index.add([sync_to])
repo.index.commit("Updating file with codimd version")
print(f" Pushing to branch: {branch_name}")
repo.git.push("-u", "origin", branch_name)
resp = api_instance.repo_list_pull_requests("ZeusWPI", "drive", state="open")
if not drive.mergerequests.list(source_branch=branch_name, state="opened"):
if drive.mergerequests.list(source_branch=branch_name):
resp = api_instance.repo_list_pull_requests(GIT_ORG, GIT_REPO, state="open")
open_branch_requests = [
r for r in resp if r.head.ref == branch_name and r.state == "open"
]
if len(open_branch_requests) == 0:
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."
)
drive.mergerequests.create(
{
"source_branch": branch_name,
"target_branch": "master",
"title": f"[CodiMD sync] Update document {sync_to}",
}
api_instance.repo_create_pull_request(
GIT_ORG,
GIT_REPO,
body=giteapy.CreatePullRequestOption(
base="master",
head=branch_name,
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}",
}
api_instance.repo_create_pull_request(
GIT_ORG,
GIT_REPO,
body=giteapy.CreatePullRequestOption(
base="master",
head=branch_name,
title=f"[CodiMD sync] Add document {sync_to}",
),
)
else:
print(" Merge request was already open.")
else:
print(" Note has no changes.")
if __name__ == "__main__":
repo, api_handler = 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_file(repo, api_handler, local_file_path, sync_to)
except Exception as e:
print("Critical error: Failed to sync file to Gitea")
# # username = 'username_example' # str | username of the user that will own the created organization
# # organization = giteapy.CreateOrgOption() # CreateOrgOption |
# pull_request = giteapy.CreatePullRequestOption(
# head="master", base="activiteiten_23-24", title="foo"
# )
# try:
# # Create an organization
# # api_response = api_instance.admin_create_org(username, organization)
# api_response = api_instance.repo_create_pull_request(
# "ZeusWPI", "drive", async_req=False, body=pull_request
# )
# # pprint(api_response)
# except ApiException as e:
# print(
# "Exception when calling RepositoryApi->repo_create_pull_request: %s\n" % e
# )

View File

@ -17,6 +17,8 @@ import gitlab
from utils import id_to_url, url_to_id
from config import config
import traceback
def find_codimd_files_on_mattermost():
mattermost = mattermost_client.MMApi()
@ -111,6 +113,7 @@ def sync_files_to_gitea():
sync.sync_file(repo, api_handler, local_file_path, sync_to)
except Exception as e:
print("Critical error: Failed to sync file to Gitea")
traceback.print_exc()
@hug.get("/sync-mattermost")
def sync_mattermost():