Skip to content

Commit d2d95fb

Browse files
authored
Fix #24 : Improve tags sync even if same number but different (#25)
1 parent fd51ee2 commit d2d95fb

5 files changed

Lines changed: 57 additions & 5 deletions

File tree

git_platforms_synchro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def repo_tags_sync(args, clone_url_from: str, git_from: GitClient, git_to: GitCl
9393

9494
tags_commits_from = git_from.get_tags(args.from_org, repo)
9595
tags_commits_to = git_to.get_tags(args.to_org, repo)
96-
if branches_updated > 0 or len(tags_commits_from) == len(tags_commits_to):
96+
if branches_updated > 0 or set(tags_commits_from.keys()).issubset(set(tags_commits_to.keys())):
9797
return False
9898

9999
logger.info(' All branches already synchronized, do tags only...')
@@ -212,7 +212,7 @@ def main() -> int:
212212
branches_scanned, branches_updated = repo_branches_sync(args, branches_commits_from, branches_commits_to, clone_url_from, repo, git_to)
213213
total_branches_scanned += branches_scanned
214214

215-
# Sync tags if no branches updated and needed (nbr tags diff between "from" and "to")
215+
# Sync tags if no branches updated and needed (tags different between "from" and "to")
216216
tag_updated = repo_tags_sync(args, clone_url_from, git_from, git_to, repo, branches_updated)
217217

218218
# Items updated calculation
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"name": "1.5.2",
4+
"message": "Use leading / in app URL\n\nFixes gh-267",
5+
"id": "c36452a2c34443ae26b4ecbba4f149906af14717",
6+
"commit": {
7+
"url": "http://localhost:3000/api/v1/repos/MyOrg/spring-petclinic/git/commits/c36452a2c34443ae26b4ecbba4f149906af14717",
8+
"sha": "c36452a2c34443ae26b4ecbba4f149906af14717",
9+
"created": "2017-11-03T14:17:56Z"
10+
},
11+
"zipball_url": "http://localhost:3000/MyOrg/spring-petclinic/archive/1.5.2.zip",
12+
"tarball_url": "http://localhost:3000/MyOrg/spring-petclinic/archive/1.5.2.tar.gz"
13+
}
14+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "1.5.1",
4+
"zipball_url": "https://api.github.com/repos/spring-projects/spring-petclinic/zipball/refs/tags/1.5.1",
5+
"tarball_url": "https://api.github.com/repos/spring-projects/spring-petclinic/tarball/refs/tags/1.5.1",
6+
"commit": {
7+
"sha": "c36452a2c34443ae26b4ecbba4f149906af14717",
8+
"url": "https://api.github.com/repos/spring-projects/spring-petclinic/commits/c36452a2c34443ae26b4ecbba4f149906af14717"
9+
},
10+
"node_id": "MDM6UmVmNzUxNzkxODpyZWZzL3RhZ3MvMS41Lng="
11+
}
12+
]

tests/test_git_platforms_synchro.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from unittest.mock import patch
66
from pytest_httpserver import HTTPServer
77
from pytest import LogCaptureFixture, raises
8-
from tests.test_utils import get_url_root, expect_request, mock_cloned_repo
8+
from tests.test_utils import get_url_root, expect_request, mock_cloned_repo, load_json
99

1010

1111
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
327327
assert 'Synchronize branch...' not in caplog.text
328328
assert 'All branches already synchronized, do tags only...' not in caplog.text
329329
assert 'Git Platforms Synchronization finished sucessfully. Repos updated: 0/1. Branches updated: 0/2' in caplog.text
330+
331+
332+
def test_from_github_to_gitea_tags_diff_sync(httpserver: HTTPServer, caplog: LogCaptureFixture):
333+
# GitHub with spring-projects
334+
prepare_github_with_spring_projects(httpserver, prepare_tags=False)
335+
httpserver.expect_request('/repos/spring-projects/spring-petclinic/tags').respond_with_json(
336+
load_json('tests/http_mocks/github/repos/spring-projects/spring-petclinic/tags-changed.json'))
337+
338+
# Gitea with same repo
339+
prepare_gitea_with_spring_projects(httpserver, prepare_tags=False)
340+
httpserver.expect_request(
341+
'/api/v1/repos/MyOrg/spring-petclinic/tags',
342+
query_string='page=1').respond_with_json(load_json('tests/http_mocks/gitea/api/v1/repos/MyOrg/spring-petclinic/tags-changed.json'))
343+
httpserver.expect_request('/api/v1/repos/MyOrg/spring-petclinic/tags', query_string='page=2').respond_with_json([])
344+
345+
# Clone will be engaged for tags sync
346+
with raises(GitCommandError):
347+
with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver)):
348+
git_platforms_synchro.main()
349+
350+
assert 'Already synchronized.' in caplog.text
351+
assert 'Synchronize branch...' not in caplog.text
352+
assert 'All branches already synchronized, do tags only...' in caplog.text

tests/test_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ def get_url_root(httpserver: HTTPServer) -> str:
1313
return httpserver.url_for('/').rstrip('/')
1414

1515

16-
def load_json(file: str, url_to_mock: str, url_replacement: str):
16+
def load_json(file: str, url_to_mock: str = None, url_replacement: str = None):
1717
with open(file) as f:
18-
return json.loads(f.read().replace(url_to_mock, url_replacement))
18+
content = f.read()
19+
if url_to_mock and url_replacement:
20+
content = content.replace(url_to_mock, url_replacement)
21+
return json.loads(content)
1922

2023

2124
def mock_cloned_repo(httpserver: HTTPServer, bare: bool = False):

0 commit comments

Comments
 (0)