finish gitea integration

This commit is contained in:
mcbloch 2024-03-27 17:13:33 +01:00
parent 04853ea239
commit 8d050a241d

View file

@ -1,6 +1,7 @@
from __future__ import print_function from __future__ import print_function
import os import os
import pathlib
import time import time
from pprint import pprint from pprint import pprint
@ -8,18 +9,24 @@ import git
import giteapy import giteapy
from giteapy.rest import ApiException from giteapy.rest import ApiException
import db
from config import config from config import config
# https://docs.gitea.com/api/1.20/ # https://docs.gitea.com/api/1.20/
REPO_FOLDER = config["gitea"]["local_repo_folder"] 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(): def init_sync():
repo = get_repo() repo = get_repo()
configuration = giteapy.Configuration() 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.api_key["token"] = config["gitea"]["access_token"]
configuration.debug = False configuration.debug = False
@ -36,11 +43,12 @@ def get_repo():
else: else:
print("Cloning repo") print("Cloning repo")
repo = git.Repo.clone_from( 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: with repo.config_writer() as cw:
cw.set_value("user", "email", "codimd.zeus.gent@mcbloch.dev") cw.set_value("user", "email", config["gitea"]["commit_user_email"])
cw.set_value("user", "name", "CodiMD sync bot") cw.set_value("user", "name", config["gitea"]["commit_user_name"])
repo.remotes.origin.fetch() repo.remotes.origin.fetch()
return repo return repo
@ -79,34 +87,40 @@ def sync_file(repo, api_instance, path, sync_to):
print(repo.untracked_files) print(repo.untracked_files)
if repo.index.diff() or 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.git.add(sync_to) repo.index.add([sync_to])
repo.git.commit("-m", sync_to) repo.index.commit("Updating file with codimd version")
print(f" Pushing to branch: {branch_name}") print(f" Pushing to branch: {branch_name}")
repo.git.push("-u", "origin", branch_name) repo.git.push("-u", "origin", branch_name)
resp = api_instance.repo_list_pull_requests("ZeusWPI", "drive", state="open") 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 not drive.mergerequests.list(source_branch=branch_name, state="opened"): ]
if drive.mergerequests.list(source_branch=branch_name): 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( 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 gitlab document with the new version from CodiMD."
) )
drive.mergerequests.create( api_instance.repo_create_pull_request(
{ GIT_ORG,
"source_branch": branch_name, GIT_REPO,
"target_branch": "master", body=giteapy.CreatePullRequestOption(
"title": f"[CodiMD sync] Update document {sync_to}", base="master",
} head=branch_name,
title=f"[CodiMD sync] Update document {sync_to}",
),
) )
else: else:
print(" Creating a new merge request to add the document to gitlab.") print(" Creating a new merge request to add the document to gitlab.")
drive.mergerequests.create( api_instance.repo_create_pull_request(
{ GIT_ORG,
"source_branch": branch_name, GIT_REPO,
"target_branch": "master", body=giteapy.CreatePullRequestOption(
"title": f"[CodiMD sync] Add document {sync_to}", base="master",
} head=branch_name,
title=f"[CodiMD sync] Add document {sync_to}",
),
) )
else: else:
print(" Merge request was already open.") print(" Merge request was already open.")
@ -117,16 +131,17 @@ def sync_file(repo, api_instance, path, sync_to):
if __name__ == "__main__": if __name__ == "__main__":
repo, api_handler = init_sync() repo, api_handler = init_sync()
for file_id, file_info in db.get_files().items(): for i, (file_id, file_info) in enumerate(db.get_files().items()):
if file_info["valid"]: if file_info["valid"]:
local_file_path = file_info["local_file_path"] local_file_path = file_info["local_file_path"]
sync_to = file_info["metadata"]["sync-to"] sync_to = file_info["metadata"]["sync-to"]
try: try:
sync_file(repo, api_handler, local_file_path, sync_to) sync_file(repo, api_handler, local_file_path, sync_to)
except Exception as e: except Exception as e:
import traceback
print("Critical error: Failed to sync file to Gitea") print("Critical error: Failed to sync file to Gitea")
traceback.print_exc()
# # username = 'username_example' # str | username of the user that will own the created organization # # username = 'username_example' # str | username of the user that will own the created organization
# # organization = giteapy.CreateOrgOption() # CreateOrgOption | # # organization = giteapy.CreateOrgOption() # CreateOrgOption |