mirror of
https://github.com/Xe138/windmill-git-sync.git
synced 2026-04-02 17:47:23 -04:00
Compare commits
1 Commits
v0.1.0-alp
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ad72fe986 |
42
app/sync.py
42
app/sync.py
@@ -3,6 +3,7 @@
|
|||||||
Core sync logic for pulling Windmill workspace and pushing to Git.
|
Core sync logic for pulling Windmill workspace and pushing to Git.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -223,15 +224,18 @@ def init_or_update_git_repo(config: Dict[str, Any]) -> Repo:
|
|||||||
|
|
||||||
return repo
|
return repo
|
||||||
else:
|
else:
|
||||||
# Workspace has files but no git repo - initialize new repo
|
# Workspace has files but no git repo - delete contents and clone
|
||||||
logger.info("Initializing new Git repository")
|
logger.info("Workspace has files but no git repository, cleaning and cloning from remote")
|
||||||
repo = Repo.init(WORKSPACE_DIR)
|
|
||||||
|
|
||||||
# Configure user
|
# Delete all contents in workspace
|
||||||
repo.config_writer().set_value("user", "name", git_user_name).release()
|
for item in WORKSPACE_DIR.iterdir():
|
||||||
repo.config_writer().set_value("user", "email", git_user_email).release()
|
if item.is_dir():
|
||||||
|
shutil.rmtree(item)
|
||||||
|
else:
|
||||||
|
item.unlink()
|
||||||
|
|
||||||
return repo
|
logger.info("Workspace cleaned, attempting to clone from remote")
|
||||||
|
return clone_remote_repository(config)
|
||||||
|
|
||||||
|
|
||||||
def commit_and_push_changes(repo: Repo, config: Dict[str, Any]) -> bool:
|
def commit_and_push_changes(repo: Repo, config: Dict[str, Any]) -> bool:
|
||||||
@@ -279,9 +283,21 @@ def commit_and_push_changes(repo: Repo, config: Dict[str, Any]) -> bool:
|
|||||||
|
|
||||||
# Push to remote
|
# Push to remote
|
||||||
logger.info(f"Pushing to {git_remote_url} (branch: {git_branch})")
|
logger.info(f"Pushing to {git_remote_url} (branch: {git_branch})")
|
||||||
origin.push(refspec=f'HEAD:{git_branch}', force=False)
|
push_info = origin.push(refspec=f'HEAD:{git_branch}', force=False)
|
||||||
logger.info("Push completed successfully")
|
|
||||||
|
|
||||||
|
# Check push results for errors
|
||||||
|
if push_info:
|
||||||
|
for info in push_info:
|
||||||
|
if info.flags & info.ERROR:
|
||||||
|
error_msg = f"Push failed: {info.summary}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
raise RuntimeError(error_msg)
|
||||||
|
elif info.flags & info.REJECTED:
|
||||||
|
error_msg = f"Push rejected (non-fast-forward): {info.summary}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
raise RuntimeError(error_msg)
|
||||||
|
|
||||||
|
logger.info("Push completed successfully")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except GitCommandError as e:
|
except GitCommandError as e:
|
||||||
@@ -312,12 +328,12 @@ def sync_windmill_to_git(config: Dict[str, Any]) -> Dict[str, Any]:
|
|||||||
|
|
||||||
workspace = config.get('workspace', 'admins')
|
workspace = config.get('workspace', 'admins')
|
||||||
|
|
||||||
# Pull from Windmill
|
# Initialize/update Git repo (must happen BEFORE wmill sync to clone if needed)
|
||||||
run_wmill_sync(config)
|
|
||||||
|
|
||||||
# Initialize/update Git repo
|
|
||||||
repo = init_or_update_git_repo(config)
|
repo = init_or_update_git_repo(config)
|
||||||
|
|
||||||
|
# Pull from Windmill (overwrites files with Windmill workspace content)
|
||||||
|
run_wmill_sync(config)
|
||||||
|
|
||||||
# Commit and push changes
|
# Commit and push changes
|
||||||
has_changes = commit_and_push_changes(repo, config)
|
has_changes = commit_and_push_changes(repo, config)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user