diff --git a/pagure/lib/git.py b/pagure/lib/git.py index e50acad..9cf9a3c 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -2153,6 +2153,20 @@ def update_pull_ref(request, repo): _log.info(" Adding remote: %s pointing to: %s", reponame, repopath) rc = RemoteCollection(repo) + + try: + # we do rc.delete(reponame) both here and in the finally block below: + # * here: it's useful for cases when worker was interrupted + # on the previous execution of this function and didn't manage + # to remove the ref + # * in the finally clause: to remove the ref so that it doesn't stay + # in the fork forever (as noted above, it might still stay there + # if the worker gets interrupted, but that's not a huge deal) + rc[reponame] + rc.delete(reponame) + except KeyError: + pass + remote = rc.create(reponame, repopath) try: _log.info( diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 88637ec..69fe990 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -3017,6 +3017,57 @@ index 0000000..60f7480 ) ) + @patch("pagure.utils.get_repo_path") + def test_update_pull_ref(self, get_repo_path): + fake_pr = MagicMock() + fake_pr.uid = "1234567" + fake_pr.branch_from = "master" + fake_pr.id = 6 + fake_pr.user = MagicMock() + fake_pr.user.user = "pingou" + fake_pr.project = "doesnt_matter_mocked_out" + projects = tests.create_projects_git( + os.path.join(self.path, "repos"), + bare=True + ) + tests.add_content_git_repo(projects[0]) + tests.add_content_git_repo(projects[1]) + orig = pygit2.Repository(projects[0]) + fork = pygit2.Repository(projects[1]) + get_repo_path.return_value = projects[0] + + # make sure that creating works the first time + pagure.lib.git.update_pull_ref(fake_pr, fork) + oldhex = fork.references["refs/heads/master"].get_object().hex + self.assertEqual( + orig.references["refs/pull/6/head"].get_object().hex, + oldhex, + ) + + # make sure that updating works correctly + tests.add_content_git_repo(projects[1], append="foobar") + newhex = fork.references["refs/heads/master"].get_object().hex + self.assertNotEqual(oldhex, newhex) + pagure.lib.git.update_pull_ref(fake_pr, fork) + self.assertEqual( + orig.references["refs/pull/6/head"].get_object().hex, + newhex, + ) + + # make sure the function works fine even if there's a leftover + # ref from previous failed run of the function + with patch("pygit2.remote.RemoteCollection.delete"): + pagure.lib.git.update_pull_ref(fake_pr, fork) + self.assertIsNotNone(fork.remotes["pingou_1234567"]) + tests.add_content_git_repo(projects[1], append="foobarbaz") + newesthex = fork.references["refs/heads/master"].get_object().hex + self.assertNotEqual(newhex, newesthex) + pagure.lib.git.update_pull_ref(fake_pr, fork) + self.assertEqual( + orig.references["refs/pull/6/head"].get_object().hex, + newesthex, + ) + class PagureLibGitCommitToPatchtests(tests.Modeltests): """ Tests for pagure.lib.git """