From 9a4fa5baf1f2a637a4a41a7b50179824f426ba21 Mon Sep 17 00:00:00 2001 From: Alix Lourme Date: Wed, 25 Feb 2026 23:27:47 +0100 Subject: [PATCH] Improve tags sync even if same number but different --- git_platforms_synchro.py | 4 +-- .../MyOrg/spring-petclinic/tags-changed.json | 14 +++++++++++ .../spring-petclinic/tags-changed.json | 12 +++++++++ tests/test_git_platforms_synchro.py | 25 ++++++++++++++++++- tests/test_utils.py | 7 ++++-- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json create mode 100644 tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json diff --git a/git_platforms_synchro.py b/git_platforms_synchro.py index 39944f6..a5e329e 100644 --- a/git_platforms_synchro.py +++ b/git_platforms_synchro.py @@ -93,7 +93,7 @@ def repo_tags_sync(args, clone_url_from: str, git_from: GitClient, git_to: GitCl tags_commits_from = git_from.get_tags(args.from_org, repo) tags_commits_to = git_to.get_tags(args.to_org, repo) - if branches_updated > 0 or len(tags_commits_from) == len(tags_commits_to): + if branches_updated > 0 or set(tags_commits_from.keys()).issubset(set(tags_commits_to.keys())): return False logger.info(' All branches already synchronized, do tags only...') @@ -212,7 +212,7 @@ def main() -> int: branches_scanned, branches_updated = repo_branches_sync(args, branches_commits_from, branches_commits_to, clone_url_from, repo, git_to) total_branches_scanned += branches_scanned - # Sync tags if no branches updated and needed (nbr tags diff between "from" and "to") + # Sync tags if no branches updated and needed (tags different between "from" and "to") tag_updated = repo_tags_sync(args, clone_url_from, git_from, git_to, repo, branches_updated) # Items updated calculation diff --git a/tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json b/tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json new file mode 100644 index 0000000..3705e90 --- /dev/null +++ b/tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json @@ -0,0 +1,14 @@ +[ + { + "name": "1.5.2", + "message": "Use leading / in app URL\n\nFixes gh-267", + "id": "c36452a2c34443ae26b4ecbba4f149906af14717", + "commit": { + "url": "http://localhost:3000/api/v1/repos/MyOrg/spring-petclinic/git/commits/c36452a2c34443ae26b4ecbba4f149906af14717", + "sha": "c36452a2c34443ae26b4ecbba4f149906af14717", + "created": "2017-11-03T14:17:56Z" + }, + "zipball_url": "http://localhost:3000/MyOrg/spring-petclinic/archive/1.5.2.zip", + "tarball_url": "http://localhost:3000/MyOrg/spring-petclinic/archive/1.5.2.tar.gz" + } +] \ No newline at end of file diff --git a/tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json b/tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json new file mode 100644 index 0000000..dceb14e --- /dev/null +++ b/tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json @@ -0,0 +1,12 @@ +[ + { + "name": "1.5.1", + "zipball_url": "https://api.github.com/repos/spring-projects/spring-petclinic/zipball/refs/tags/1.5.1", + "tarball_url": "https://api.github.com/repos/spring-projects/spring-petclinic/tarball/refs/tags/1.5.1", + "commit": { + "sha": "c36452a2c34443ae26b4ecbba4f149906af14717", + "url": "https://api.github.com/repos/spring-projects/spring-petclinic/commits/c36452a2c34443ae26b4ecbba4f149906af14717" + }, + "node_id": "MDM6UmVmNzUxNzkxODpyZWZzL3RhZ3MvMS41Lng=" + } +] \ No newline at end of file diff --git a/tests/test_git_platforms_synchro.py b/tests/test_git_platforms_synchro.py index cbdaf00..27fe60b 100644 --- a/tests/test_git_platforms_synchro.py +++ b/tests/test_git_platforms_synchro.py @@ -5,7 +5,7 @@ from unittest.mock import patch from pytest_httpserver import HTTPServer from pytest import LogCaptureFixture, raises -from tests.test_utils import get_url_root, expect_request, mock_cloned_repo +from tests.test_utils import get_url_root, expect_request, mock_cloned_repo, load_json def get_test_args_github_to_gitea(httpserver: HTTPServer): @@ -327,3 +327,26 @@ def test_from_github_to_gitea_all_already_sync(httpserver: HTTPServer, caplog: L assert 'Synchronize branch...' not in caplog.text assert 'All branches already synchronized, do tags only...' not in caplog.text assert 'Git Platforms Synchronization finished sucessfully. Repos updated: 0/1. Branches updated: 0/2' in caplog.text + + +def test_from_github_to_gitea_tags_diff_sync(httpserver: HTTPServer, caplog: LogCaptureFixture): + # GitHub with spring-projects + prepare_github_with_spring_projects(httpserver, prepare_tags=False) + httpserver.expect_request('/repos/spring-projects/spring-petclinic/tags').respond_with_json( + load_json('tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json')) + + # Gitea with same repo + prepare_gitea_with_spring_projects(httpserver, prepare_tags=False) + httpserver.expect_request( + '/api/v1/repos/MyOrg/spring-petclinic/tags', + query_string='page=1').respond_with_json(load_json('tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json')) + httpserver.expect_request('/api/v1/repos/MyOrg/spring-petclinic/tags', query_string='page=2').respond_with_json([]) + + # Clone will be engaged for tags sync + with raises(GitCommandError): + with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver)): + git_platforms_synchro.main() + + assert 'Already synchronized.' in caplog.text + assert 'Synchronize branch...' not in caplog.text + assert 'All branches already synchronized, do tags only...' in caplog.text diff --git a/tests/test_utils.py b/tests/test_utils.py index 96b274a..7760f50 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -13,9 +13,12 @@ def get_url_root(httpserver: HTTPServer) -> str: return httpserver.url_for('/').rstrip('/') -def load_json(file: str, url_to_mock: str, url_replacement: str): +def load_json(file: str, url_to_mock: str = None, url_replacement: str = None): with open(file) as f: - return json.loads(f.read().replace(url_to_mock, url_replacement)) + content = f.read() + if url_to_mock and url_replacement: + content = content.replace(url_to_mock, url_replacement) + return json.loads(content) def mock_cloned_repo(httpserver: HTTPServer, bare: bool = False):