checkupdates: reuse per-app branches when making merge requests

https://gitlab.com/fdroid/fdroidserver/-/merge_requests/1551#note_2206085258
This commit is contained in:
Hans-Christoph Steiner 2024-10-30 21:25:10 +01:00
parent 20ff302e89
commit cd8d4ef88b
2 changed files with 172 additions and 4 deletions

View file

@ -464,6 +464,127 @@ class CheckupdatesTest(unittest.TestCase):
self.assertTrue(branch in ('main', 'master'))
self.assertTrue(branch in repo.heads)
def test_checkout_appid_branch_does_not_exist(self):
appid = 'com.example'
os.chdir(self.testdir.name)
git_repo, main_branch = fdroidserver.checkupdates.get_git_repo_and_main_branch()
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()
self.assertNotIn(appid, git_repo.heads)
fdroidserver.checkupdates.checkout_appid_branch(appid)
self.assertIn(appid, git_repo.heads)
def test_checkout_appid_branch_exists(self):
appid = 'com.example'
upstream_dir = os.path.join(self.testdir.name, 'upstream_git')
os.mkdir(upstream_dir)
upstream_repo = git.Repo.init(upstream_dir)
(Path(upstream_dir) / 'README').write_text('README')
upstream_repo.git.add(all=True)
upstream_repo.index.commit("README")
upstream_repo.create_head(appid)
local_dir = os.path.join(self.testdir.name, 'local_git')
git.Repo.clone_from(upstream_dir, local_dir)
os.chdir(local_dir)
git_repo, main_branch = fdroidserver.checkupdates.get_git_repo_and_main_branch()
# --merge-request assumes remotes called 'origin' and 'upstream'
git_repo.create_remote('upstream', upstream_dir).fetch()
self.assertNotIn(appid, git_repo.heads)
fdroidserver.checkupdates.checkout_appid_branch(appid)
self.assertIn(appid, git_repo.heads)
def test_checkout_appid_branch_skip_bot_commit(self):
appid = 'com.example'
upstream_dir = os.path.join(self.testdir.name, 'upstream_git')
os.mkdir(upstream_dir)
upstream_repo = git.Repo.init(upstream_dir)
(Path(upstream_dir) / 'README').write_text('README')
upstream_repo.git.add(all=True)
upstream_repo.index.commit("README")
upstream_repo.create_head(appid)
local_dir = os.path.join(self.testdir.name, 'local_git')
git.Repo.clone_from(upstream_dir, local_dir)
os.chdir(local_dir)
git_repo, main_branch = fdroidserver.checkupdates.get_git_repo_and_main_branch()
# --merge-request assumes remotes called 'origin' and 'upstream'
git_repo.create_remote('upstream', upstream_dir).fetch()
os.mkdir('metadata')
git_repo.create_head(appid, f'origin/{appid}', force=True)
git_repo.git.checkout(appid)
# fake checkupdates-bot commit
Path(f'metadata/{appid}.yml').write_text('AutoName: Example\n')
with git_repo.config_writer() as cw:
cw.set_value('user', 'email', fdroidserver.checkupdates.BOT_EMAIL)
git_repo.git.add(all=True)
git_repo.index.commit("Example")
# set up starting from remote branch
git_repo.remotes.origin.push(appid)
git_repo.git.checkout(main_branch)
git_repo.delete_head(appid, force=True)
self.assertTrue(
fdroidserver.checkupdates.checkout_appid_branch(appid),
'This should have been true since there are only bot commits.',
)
def test_checkout_appid_branch_skip_human_edits(self):
appid = 'com.example'
upstream_dir = os.path.join(self.testdir.name, 'upstream_git')
os.mkdir(upstream_dir)
upstream_repo = git.Repo.init(upstream_dir)
(Path(upstream_dir) / 'README').write_text('README')
upstream_repo.git.add(all=True)
upstream_repo.index.commit("README")
upstream_repo.create_head(appid)
local_dir = os.path.join(self.testdir.name, 'local_git')
git.Repo.clone_from(upstream_dir, local_dir)
os.chdir(local_dir)
git_repo, main_branch = fdroidserver.checkupdates.get_git_repo_and_main_branch()
# --merge-request assumes remotes called 'origin' and 'upstream'
git_repo.create_remote('upstream', upstream_dir).fetch()
os.mkdir('metadata')
git_repo.create_head(appid, f'origin/{appid}', force=True)
git_repo.git.checkout(appid)
with git_repo.config_writer() as cw:
cw.set_value('user', 'email', fdroidserver.checkupdates.BOT_EMAIL)
# fake checkupdates-bot commit
Path(f'metadata/{appid}.yml').write_text('AutoName: Example\n')
git_repo.git.add(all=True)
git_repo.index.commit("Example")
# fake commit added on top by a human
Path(f'metadata/{appid}.yml').write_text('AutoName: Example\nName: Foo\n')
with git_repo.config_writer() as cw:
cw.set_value('user', 'email', 'human@bar.com')
git_repo.git.add(all=True)
git_repo.index.commit("Example")
# set up starting from remote branch
git_repo.remotes.origin.push(appid)
git_repo.git.reset(main_branch)
self.assertFalse(
fdroidserver.checkupdates.checkout_appid_branch(appid),
'This should have been false since there are human edits.',
)
@mock.patch('git.remote.Remote.push')
@mock.patch('sys.exit')
@mock.patch('fdroidserver.common.read_app_args')