add intermediary commit while migrating to gitea

This commit is contained in:
mcbloch 2024-03-27 00:40:27 +01:00
parent 9da2481df9
commit 04853ea239
3 changed files with 149 additions and 22 deletions

View file

@ -33,3 +33,4 @@ local_repo_folder = "drive"
# Generate at gitea.example.com/user/settings/applications # Generate at gitea.example.com/user/settings/applications
access_token = "..." access_token = "..."
local_repo_folder = "drive"

View file

@ -1,34 +1,148 @@
from __future__ import print_function from __future__ import print_function
import os
import time import time
from pprint import pprint
import git
import giteapy import giteapy
from giteapy.rest import ApiException from giteapy.rest import ApiException
from pprint import pprint
from config import config from config import config
# https://docs.gitea.com/api/1.20/
REPO_FOLDER = config["gitea"]["local_repo_folder"]
def init_sync(): def init_sync():
pass repo = get_repo()
if __name__ == "__main__":
configuration = giteapy.Configuration() configuration = giteapy.Configuration()
configuration.host = "https://git.zeus.gent/api/v1" configuration.host = "https://git.zeus.gent/api/v1"
configuration.api_key['token'] = config["gitea"]["access_token"] configuration.api_key["token"] = config["gitea"]["access_token"]
configuration.debug = True configuration.debug = False
# create an instance of the API class # create an instance of the API class
api_instance = giteapy.RepositoryApi(giteapy.ApiClient(configuration)) api_instance = giteapy.RepositoryApi(giteapy.ApiClient(configuration))
# username = 'username_example' # str | username of the user that will own the created organization
# organization = giteapy.CreateOrgOption() # CreateOrgOption |
pull_request = giteapy.CreatePullRequestOption( return repo, api_instance
body="Test 123"
)
try:
# Create an organization def get_repo():
# api_response = api_instance.admin_create_org(username, organization) if os.path.exists(REPO_FOLDER):
api_response = api_instance.repo_create_pull_request("ZeusWPI", "drive", async_req=False, body=pull_request) print("Repo already exists")
pprint(api_response) repo = git.Repo(REPO_FOLDER)
except ApiException as e: else:
print("Exception when calling RepositoryApi->repo_create_pull_request: %s\n" % e) 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(repo, api_instance, 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)
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):
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.")
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

@ -12,6 +12,7 @@ import dir_utils
import mattermost_client import mattermost_client
import mattermost_communication import mattermost_communication
import sync_gitea as sync import sync_gitea as sync
import sync_gitlab
import gitlab import gitlab
from utils import id_to_url, url_to_id from utils import id_to_url, url_to_id
from config import config from config import config
@ -90,16 +91,27 @@ def validate_downloaded_files(post_mattermost_hint=True):
return db._load_db() return db._load_db()
def sync_files_to_gitlab(): def sync_files_to_gitlab():
repo, drive = sync.init_sync() repo, drive = sync_gitlab.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"] 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.sync_file(drive, repo, local_file_path, sync_to) sync_gitlab.sync_file(drive, repo, local_file_path, sync_to)
except Exception as e: except Exception as e:
print("Critical error: Failed to sync file to Gitlab") 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():
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)
except Exception as e:
print("Critical error: Failed to sync file to Gitea")
@hug.get("/sync-mattermost") @hug.get("/sync-mattermost")
def sync_mattermost(): def sync_mattermost():
print() print()
@ -117,7 +129,7 @@ def sync_mattermost():
print() print()
print("================================================") print("================================================")
print("== Syncing files to gitlab ==") print("== Syncing files to gitlab ==")
# sync_files_to_gitlab() sync_files_to_gitea()
print() print()
# return db._load_db() # return db._load_db()