Skip to content

Commit dc9755d

Browse files
authored
Fix #30 : Push tags when first branch updated (#31)
1 parent 44ad8e6 commit dc9755d

4 files changed

Lines changed: 32 additions & 26 deletions

File tree

.github/workflows/build-dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
- os: windows-latest
2727
python-version: "3.14"
2828
steps:
29-
- uses: actions/checkout@v4
29+
- uses: actions/checkout@v6
3030
- name: Set up Python ${{ matrix.python-version }}
31-
uses: actions/setup-python@v3
31+
uses: actions/setup-python@v6
3232
with:
3333
python-version: ${{ matrix.python-version }}
3434
- name: Install dependencies

.github/workflows/sonarqube.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ jobs:
1010
name: SonarQube
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v4
13+
- uses: actions/checkout@v6
1414
with:
1515
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
1616
- name: Set up Python
17-
uses: actions/setup-python@v3
17+
uses: actions/setup-python@v6
1818
with:
1919
python-version: 3.14
2020
- name: Install dependencies
@@ -27,6 +27,6 @@ jobs:
2727
coverage combine
2828
coverage xml
2929
- name: SonarQube Scan
30-
uses: SonarSource/sonarqube-scan-action@v7.0.0
30+
uses: SonarSource/sonarqube-scan-action@a31c9398be7ace6bbfaf30c0bd5d415f843d45e9
3131
env:
3232
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

git_platforms_synchro.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def set_git_credentials(username: str, password: str):
4141

4242

4343
def git_clone(url: str, mirror: bool = False, disable_ssl_verify: bool = False, proxy: str = None) -> Repo:
44-
if os.path.exists(TMP_REPO_GIT_DIRECTORY):
44+
if os.path.exists(TMP_REPO_GIT_DIRECTORY) and len(os.listdir(TMP_REPO_GIT_DIRECTORY)):
4545
repo_cloned = Repo(TMP_REPO_GIT_DIRECTORY)
4646
origin_url = repo_cloned.remote('origin').url
4747
if repo_cloned.bare == mirror and origin_url == url:
@@ -74,7 +74,7 @@ def configure_remote_to(repo: Repo, clone_url_to: str, proxy: str = '', ssl_veri
7474
def repo_mirror(create_repo: bool, dry_run: bool, clone_url_from: str, login_from: str, password_from: str, proxy_from: str, disable_ssl_verify_from: bool,
7575
git_to: GitClient, proxy_to: str, disable_ssl_verify_to: bool, org_to: str, repo: str, description: str = ''):
7676
if dry_run:
77-
logger.info(' Dry-run mode, skipping repository creation and mirroring.')
77+
logger.info(' Dry-run mode, skipping repository creation and mirroring.')
7878
return
7979
if create_repo:
8080
git_to.create_repo(org_to, repo, description)
@@ -88,15 +88,15 @@ def repo_mirror(create_repo: bool, dry_run: bool, clone_url_from: str, login_fro
8888

8989
def repo_tags_sync(args, clone_url_from: str, git_from: GitClient, git_to: GitClient, repo: str, branches_updated: int) -> bool:
9090
if args.dry_run:
91-
logger.info(' Dry-run mode, skipping tags synchronization.')
91+
logger.info(' Dry-run mode, skipping tags synchronization.')
9292
return False
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)
9696
if branches_updated > 0 or set(tags_commits_from.keys()).issubset(set(tags_commits_to.keys())):
9797
return False
9898

99-
logger.info(' All branches already synchronized, do tags only...')
99+
logger.info(' All branches already synchronized, do tags only...')
100100
set_git_credentials(git_from.get_login_or_token(), git_from.get_password())
101101
repo_from_cloned = git_clone(url=clone_url_from, disable_ssl_verify=args.from_disable_ssl_verify, proxy=args.from_proxy)
102102
clone_url_to = git_to.get_repo_clone_url(args.to_org, repo)
@@ -107,17 +107,17 @@ def repo_tags_sync(args, clone_url_from: str, git_from: GitClient, git_to: GitCl
107107

108108

109109
def repo_branch_sync(dry_run: bool, clone_url_from: str, login_from: str, password_from: str, proxy_from: str, disable_ssl_verify_from: bool,
110-
git_to: GitClient, proxy_to: str, disable_ssl_verify_to: bool, org_to: str, repo: str, branch: str):
110+
git_to: GitClient, proxy_to: str, disable_ssl_verify_to: bool, org_to: str, repo: str, branch: str, with_tags: bool):
111111
if dry_run:
112-
logger.info(' Dry-run mode, skipping branch synchronization.')
112+
logger.info(' Dry-run mode, skipping branch synchronization.')
113113
return
114114
set_git_credentials(login_from, password_from)
115115
repo_from_cloned = git_clone(url=clone_url_from, disable_ssl_verify=disable_ssl_verify_from, proxy=proxy_from)
116116
repo_from_cloned.git.checkout(branch)
117117
clone_url_to = git_to.get_repo_clone_url(org_to, repo)
118118
configure_remote_to(repo_from_cloned, clone_url_to, proxy_to, not disable_ssl_verify_to)
119119
set_git_credentials(git_to.get_login_or_token(), git_to.get_password())
120-
repo_from_cloned.remote(GIT_REMOTE_TO).push().raise_if_error()
120+
repo_from_cloned.remote(GIT_REMOTE_TO).push(refspec=branch + ':' + branch, tags=with_tags).raise_if_error()
121121

122122

123123
def repo_branches_sync(args, branches_commits_from: dict, branches_commits_to: dict,
@@ -132,18 +132,23 @@ def repo_branches_sync(args, branches_commits_from: dict, branches_commits_to: d
132132
branches_scanned = branches_updated = 0
133133
for branch in input_parser.reduce(branches_commits_from.keys(), args.branches_include, args.branches_exclude):
134134
branches_scanned += 1
135-
logger.info(' Branch: %s', branch)
135+
logger.info(' Branch: %s', branch)
136136
commit_from = branches_commits_from.get(branch, None)
137-
logger.info(' Commit From: %s', commit_from)
137+
logger.info(' Commit From: %s', commit_from)
138138
commit_to = branches_commits_to.get(branch, None)
139-
logger.info(' Commit To : %s', commit_to)
139+
logger.info(' Commit To : %s', commit_to)
140140
if commit_from == commit_to:
141-
logger.info(' Already synchronized.')
141+
logger.info(' Already synchronized.')
142142
continue
143-
logger.info(' Synchronize branch...')
143+
with_tags = False
144+
with_tags_str = ''
145+
if branches_updated == 0:
146+
with_tags = True
147+
with_tags_str = ' (with tags)'
148+
logger.info(' Synchronize branch' + with_tags_str + '...')
144149
branches_updated += 1
145150
repo_branch_sync(args.dry_run, clone_url_from, args.from_login, args.from_password, args.from_proxy, args.from_disable_ssl_verify,
146-
git_to, args.to_proxy, args.to_disable_ssl_verify, args.to_org, repo, branch)
151+
git_to, args.to_proxy, args.to_disable_ssl_verify, args.to_org, repo, branch, with_tags)
147152
return branches_scanned, branches_updated
148153

149154

@@ -175,7 +180,7 @@ def main() -> int:
175180

176181
# New repo to create and mirror
177182
if not git_to.has_repo(args.to_org, repo):
178-
logger.info(' Repository does not exist on "to" plaform, create as mirror...')
183+
logger.info(' Repository does not exist on "to" plaform, create as mirror...')
179184
total_repos_updated += 1
180185
description = git_from.get_repo_description(args.from_org, repo)
181186
repo_mirror(True, args.dry_run, clone_url_from, args.from_login, args.from_password, args.from_proxy, args.from_disable_ssl_verify,
@@ -185,13 +190,13 @@ def main() -> int:
185190
# Branches on "from", skip if no commits
186191
branches_commits_from = git_from.get_branches(args.from_org, repo)
187192
if len(branches_commits_from) == 0:
188-
logger.info(' Repository has no branches on "from" platform, skipping.')
193+
logger.info(' Repository has no branches on "from" platform, skipping.')
189194
continue
190195

191196
# Branches on "to", mirror repo if empty
192197
branches_commits_to = git_to.get_branches(args.to_org, repo)
193198
if len(branches_commits_to) == 0:
194-
logger.info(' Repository has no branches on "to" platform, synchronize as mirror...')
199+
logger.info(' Repository has no branches on "to" platform, synchronize as mirror...')
195200
total_repos_updated += 1
196201
repo_mirror(
197202
False,

tests/test_git_platforms_synchro.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ def test_from_github_to_gitea_sync_real(httpserver: HTTPServer, caplog: LogCaptu
289289
git_platforms_synchro.main()
290290

291291
assert 'Reusing existing cloned repo ' + get_url_root(httpserver) + '/spring-projects/spring-petclinic.git' in caplog.text
292-
assert 'Synchronize branch...' in caplog.text
292+
assert 'Synchronize branch (with tags)...' in caplog.text
293293
assert 'The requested URL returned error: 542' in caplog.text
294+
assert "['git', 'push', '--porcelain', '--tags', '--', 'sync-to', 'main:main'" in caplog.text
294295

295296

296297
def test_from_github_to_gitea_sync_dry_run(httpserver: HTTPServer, caplog: LogCaptureFixture):
@@ -303,8 +304,8 @@ def test_from_github_to_gitea_sync_dry_run(httpserver: HTTPServer, caplog: LogCa
303304
with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver) + ['--dry-run']):
304305
git_platforms_synchro.main()
305306

306-
assert 'Synchronize branch...' in caplog.text
307-
assert ' Dry-run mode, skipping branch synchronization.' in caplog.text
307+
assert 'Synchronize branch (with tags)...' in caplog.text
308+
assert 'Dry-run mode, skipping branch synchronization.' in caplog.text
308309

309310

310311
def test_from_github_to_gitea_tags_only_real(httpserver: HTTPServer, caplog: LogCaptureFixture):
@@ -359,7 +360,7 @@ def test_from_github_to_gitea_all_already_sync(httpserver: HTTPServer, caplog: L
359360
git_platforms_synchro.main()
360361

361362
assert 'Already synchronized.' in caplog.text
362-
assert 'Synchronize branch...' not in caplog.text
363+
assert 'Synchronize branch' not in caplog.text
363364
assert 'All branches already synchronized, do tags only...' not in caplog.text
364365
assert 'Git Platforms Synchronization finished sucessfully. Repos updated: 0/1. Branches updated: 0/2' in caplog.text
365366

@@ -383,5 +384,5 @@ def test_from_github_to_gitea_tags_diff_sync(httpserver: HTTPServer, caplog: Log
383384
git_platforms_synchro.main()
384385

385386
assert 'Already synchronized.' in caplog.text
386-
assert 'Synchronize branch...' not in caplog.text
387+
assert 'Synchronize branch' not in caplog.text
387388
assert 'All branches already synchronized, do tags only...' in caplog.text

0 commit comments

Comments
 (0)