remove references to gitlab

This commit is contained in:
mcbloch 2024-03-27 18:07:34 +01:00
parent e32aa8b464
commit 610c7e95b5
7 changed files with 6 additions and 141 deletions

View File

@ -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

View File

@ -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"

View File

@ -6,5 +6,4 @@ mattermostdriver
hug
gitpython
python-gitlab
giteapy

View File

@ -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.

View File

@ -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,

View File

@ -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

View File

@ -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()