feat: add active sync configuration checks before branch checkout and repository unlinking

This commit is contained in:
Sam Chau
2025-01-09 20:10:28 +10:30
parent e970262ba0
commit c53d8681be
5 changed files with 90 additions and 5 deletions

View File

@@ -529,3 +529,35 @@ def run_import_for_config(config_row):
'total_successful': total_successful,
'sync_percentage': sync_percentage
}
def check_active_sync_configs():
"""
Check if there are any ARR configurations with non-manual sync methods.
Returns (has_active_configs, details) tuple.
"""
with get_db() as conn:
cursor = conn.execute('''
SELECT id, name, sync_method, data_to_sync
FROM arr_config
WHERE sync_method != 'manual'
''')
active_configs = cursor.fetchall()
if not active_configs:
return False, None
details = []
for config in active_configs:
data_to_sync = json.loads(
config['data_to_sync'] if config['data_to_sync'] else '{}')
if data_to_sync.get('profiles') or data_to_sync.get(
'customFormats'):
details.append({
'id': config['id'],
'name': config['name'],
'sync_method': config['sync_method'],
'data': data_to_sync
})
return bool(details), details

View File

@@ -2,12 +2,30 @@
import git
import logging
from ...arr.manager import check_active_sync_configs
logger = logging.getLogger(__name__)
def checkout_branch(repo_path, branch_name):
try:
# Check for active sync configurations first
has_active_configs, configs = check_active_sync_configs()
if has_active_configs:
error_msg = (
"Cannot checkout branch while automatic sync configurations are active.\n"
"The following configurations must be set to manual sync first:\n"
)
for config in configs:
error_msg += f"- {config['name']} (ID: {config['id']}, {config['sync_method']} sync)\n"
logger.error(error_msg)
return False, {
"error": error_msg,
"code": "ACTIVE_SYNC_CONFIGS",
"configs": configs
}
logger.debug(f"Attempting to checkout branch: {branch_name}")
repo = git.Repo(repo_path)

View File

@@ -3,12 +3,30 @@ import os
import shutil
import logging
from ...db import save_settings
from ...arr.manager import check_active_sync_configs
logger = logging.getLogger(__name__)
def unlink_repository(repo_path, remove_files=False):
try:
# Check for active sync configurations first
has_active_configs, configs = check_active_sync_configs()
if has_active_configs:
error_msg = (
"Cannot unlink repository while automatic sync configurations are active.\n"
"The following configurations must be set to manual sync first:\n"
)
for config in configs:
error_msg += f"- {config['name']} (ID: {config['id']}, {config['sync_method']} sync)\n"
logger.error(error_msg)
return False, {
"error": error_msg,
"code": "ACTIVE_SYNC_CONFIGS",
"configs": configs
}
logger.info(
f"Starting unlink_repository with repo_path: {repo_path} and remove_files: {remove_files}"
)

View File

@@ -305,7 +305,10 @@ export const unlinkRepo = async (removeFiles = false) => {
});
return response.data;
} catch (error) {
console.error('Error unlinking repository:', error);
if (error.response?.data) {
// Return the error response directly
return error.response.data;
}
throw error;
}
};

View File

@@ -140,12 +140,26 @@ const RepoContainer = ({settings, setSettings, fetchGitStatus, status}) => {
Alert.success('Repository unlinked successfully');
setShowUnlinkModal(false);
} else {
Alert.error(response.error || 'Failed to unlink repository');
// Check if the error is a structured object
if (
typeof response.error === 'object' &&
response.error.error
) {
Alert.error(response.error.error);
} else if (typeof response.error === 'string') {
Alert.error(response.error);
} else {
Alert.error('Failed to unlink repository');
}
}
} catch (error) {
Alert.error(
'An unexpected error occurred while unlinking the repository'
);
if (error.response?.data?.error) {
Alert.error(error.response.data.error);
} else {
Alert.error(
'An unexpected error occurred while unlinking the repository'
);
}
console.error('Error unlinking repository:', error);
} finally {
setLoadingAction('');