#3551 Allow deleting branches in forks regardless of ALLOW_DELETE_BRANCH
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

@@ -76,7 +76,10 @@ 

                                      <i class="fa fa-fw fa-list-alt"></i>

                                      </a>

                                  </div>

-                                 {% if g.repo_committer and branch != head and config.get('ALLOW_DELETE_BRANCH', True) %}

+                                 {% if g.repo_committer and branch != head

+                                       and (

+                                         config.get('ALLOW_DELETE_BRANCH', True)

+                                         or repo.is_fork) %}

                                      <form id="delete_branch_form-{{

                                          branch | replace('/', '__') | replace('+', '___')

                                          }}" action="{{

file modified
+2 -1
@@ -2272,7 +2272,8 @@ 

  def delete_branch(repo, branchname, username=None, namespace=None):

      """ Delete the branch of a project.

      """

-     if not pagure_config.get('ALLOW_DELETE_BRANCH', True):

+     if not flask.g.repo.is_fork and \

+             not pagure_config.get('ALLOW_DELETE_BRANCH', True):

          flask.abort(

              404, 'This pagure instance does not allow branch deletion')

  

@@ -5248,9 +5248,64 @@ 

  

          user = tests.FakeUser(username = 'pingou')

          with tests.user_set(self.app.application, user):

+             # Check if the delete branch button does not show

+             output = self.app.get('/test/branches')

+             self.assertEqual(output.status_code, 200)

+             self.assertNotIn(

+                 'title="Remove branch foo"',

+                 output.get_data(as_text=True))

+ 

              # Delete the branch

              output = self.app.post('/test/b/foo/delete', follow_redirects=True)

              self.assertEqual(output.status_code, 404)

+             self.assertIn(

+                 'This pagure instance does not allow branch deletion',

+                 output.get_data(as_text=True))

+ 

+     @patch.dict('pagure.config.config', {'ALLOW_DELETE_BRANCH': False})

+     def test_delete_branch_disabled_fork(self):

+         """ Test the delete_branch endpoint when it's disabled in the entire

+         instance. """

+         item = pagure.lib.model.Project(

+             user_id=2,  # foo

+             name='test',

+             description='test project #1',

+             hook_token='aaabbb',

+             is_fork=True,

+             parent_id=1,

+         )

+         self.session.add(item)

+         self.session.commit()

+         tests.create_projects_git(

+             os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)

+ 

+         # Add a branch that we can delete

+         path = os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git')

+         tests.add_content_git_repo(path)

+         repo = pygit2.Repository(path)

+         repo.create_branch('foo', repo.head.get_object())

+ 

+         user = tests.FakeUser(username = 'foo')

+         with tests.user_set(self.app.application, user):

+             # Check if the delete branch button shows

+             output = self.app.get('/fork/foo/test/branches')

+             self.assertEqual(output.status_code, 200)

+             self.assertIn(

+                 'title="Remove branch foo"',

+                 output.get_data(as_text=True))

+ 

+             # Delete the branch

+             output = self.app.post(

+                 '/fork/foo/test/b/foo/delete',

+                 follow_redirects=True)

+             self.assertEqual(output.status_code, 200)

+ 

+             # Check if the delete branch button no longer appears

+             output = self.app.get('/fork/foo/test/branches')

+             self.assertEqual(output.status_code, 200)

+             self.assertNotIn(

+                 'title="Remove branch foo"',

+                 output.get_data(as_text=True))

  

      def test_view_docs(self):

          """ Test the view_docs endpoint. """

Basically, even if main project are not allowed to delete branches
forks should be.

Fixes https://pagure.io/pagure/issue/3547

Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr

rebased onto 51d9c05ebccb7c8f89cf830f1acb6ca8d8aa120a

5 years ago

:thumbsdown:
I have

ALLOW_DELETE_BRANCH = False

in pagure.cfg, deleting branches is disabled, the buttons don't show up. But they don't show in the fork either where they should. It is questionable if deleting branches that are in the main repo should be allowed to be deleted in the fork, but this also doesn't work for branches that are only in the fork.

ah, I missed the template... Thanks @karsten

rebased onto 4497e25

5 years ago

Code and tests adjusted for this :)

Thanks for the reviews! :)

Pull-Request has been merged by pingou

5 years ago