From 25779e8b32d76d1566a0592467b8a2b9e39d6d7e Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 30 Oct 2024 17:07:31 +0100 Subject: [PATCH] checkupdates: --merge_request commits to branch named after appid --- fdroidserver/checkupdates.py | 32 +++++++++++++++-------- tests/checkupdates.TestCase | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/fdroidserver/checkupdates.py b/fdroidserver/checkupdates.py index caf07eda..d812d0b6 100644 --- a/fdroidserver/checkupdates.py +++ b/fdroidserver/checkupdates.py @@ -684,7 +684,15 @@ def get_last_build_from_app(app: metadata.App) -> metadata.Build: return metadata.Build() -def push_commits(remote_name='origin', verbose=False): +def get_git_repo_and_main_branch(): + git_repo = git.Repo.init('.') + main_branch = 'main' + if main_branch not in git_repo.heads: + main_branch = 'master' + return git_repo, main_branch + + +def push_commits(remote_name='origin', branch_name='checkupdates', verbose=False): """Make git branch then push commits as merge request. This uses the appid as the standard branch name so that there is @@ -701,17 +709,16 @@ def push_commits(remote_name='origin', verbose=False): * https://docs.gitlab.com/ee/user/project/push_options.html """ - git_repo = git.Repo.init('.') + git_repo, default = get_git_repo_and_main_branch() files = set() - upstream_main = 'main' if 'main' in git_repo.remotes.upstream.refs else 'master' - local_main = 'main' if 'main' in git_repo.refs else 'master' + upstream_main = default if default in git_repo.remotes.upstream.refs else 'main' + local_main = default if default in git_repo.refs else 'main' for commit in git_repo.iter_commits(f'upstream/{upstream_main}...{local_main}'): files.update(commit.stats.files.keys()) - branch_name = 'checkupdates' files = list(files) if len(files) == 1: - m = re.match(r'metadata/([^\s]+)\.yml', files[0]) + m = re.match(r'metadata/(\S+)\.yml', files[0]) if m: branch_name = m.group(1) # appid if not files: @@ -767,13 +774,10 @@ def push_commits(remote_name='origin', verbose=False): logging.debug(remote.url + ': ' + pushinfo.summary) -def prune_empty_appid_branches(git_repo=None): +def prune_empty_appid_branches(git_repo=None, main_branch='main'): """Remove empty branches from checkupdates-bot git remote.""" if git_repo is None: - git_repo = git.Repo.init('.') - main_branch = 'main' - if main_branch not in git_repo.remotes.upstream.refs: - main_branch = 'master' + git_repo, main_branch = get_git_repo_and_main_branch() upstream_main = 'upstream/' + main_branch remote = git_repo.remotes.origin @@ -856,6 +860,12 @@ def main(): logging.info(msg) try: + if options.merge_request: + logging.info(f'Creating merge request branch for {appid}') + git_repo, main_branch = get_git_repo_and_main_branch() + git_repo.create_head(appid, f"upstream/{main_branch}", force=True) + git_repo.git.checkout(appid) + checkupdates_app(app, options.auto, options.commit or options.merge_request) processed.append(appid) except Exception as e: diff --git a/tests/checkupdates.TestCase b/tests/checkupdates.TestCase index 8c8bb03f..8006dd2d 100755 --- a/tests/checkupdates.TestCase +++ b/tests/checkupdates.TestCase @@ -463,6 +463,55 @@ class CheckupdatesTest(unittest.TestCase): fdroidserver.checkupdates.main() sys_exit.assert_not_called() + def test_get_git_repo_and_main_branch(self): + os.chdir(self.testdir.name) + git_repo = git.Repo.init() + open('foo', 'w').close() + git_repo.git.add(all=True) + git_repo.index.commit("all files") + + repo, branch = fdroidserver.checkupdates.get_git_repo_and_main_branch() + self.assertTrue(branch in ('main', 'master')) + self.assertTrue(branch in repo.heads) + + @mock.patch('sys.exit') + @mock.patch('fdroidserver.common.read_app_args') + @mock.patch('fdroidserver.checkupdates.checkupdates_app') + def test_merge_requests_branch(self, checkupdates_app, read_app_args, sys_exit): + def _sys_exit(return_code=0): + assert return_code == 0 + + def _checkupdates_app(app, auto, commit): # pylint: disable=unused-argument + os.mkdir('metadata') + Path(f'metadata/{app["packageName"]}.yml').write_text('AutoName: Example') + git_repo.git.add(all=True) + git_repo.index.commit("Example") + + def _read_app_args(apps=[]): + appid = apps[0] + return {appid: {'packageName': appid}} + + appid = 'com.example' + read_app_args.side_effect = _read_app_args + checkupdates_app.side_effect = _checkupdates_app + sys_exit.side_effect = _sys_exit + + # set up clean git repo + os.chdir(self.testdir.name) + git_repo = git.Repo.init() + open('foo', 'w').close() + git_repo.git.add(all=True) + git_repo.index.commit("all files") + # --merge-request assumes remotes called 'origin' and 'upstream' + git_repo.create_remote('origin', os.getcwd()).fetch() + git_repo.create_remote('upstream', os.getcwd()).fetch() + + assert appid not in git_repo.heads + with mock.patch('sys.argv', ['fdroid checkupdates', '--merge-request', appid]): + fdroidserver.checkupdates.main() + sys_exit.assert_called_once() + assert appid in git_repo.heads + if __name__ == "__main__": import argparse