From b2e22050dfa95a6fe6d76442da226962f7463aee Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 11 2017 14:38:16 +0000 Subject: Fix deleting a project when some of the folder are not used In some setup some of the git repositories are not generated or used so trying to delete them will end up with a 500 error. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 0e24552..8dbd4ec 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -1432,16 +1432,18 @@ def delete_repo(repo, username=None, namespace=None): _log.exception(err) flask.flash('Could not delete the project', 'error') - repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path) - docpath = os.path.join(APP.config['DOCS_FOLDER'], repo.path) - ticketpath = os.path.join(APP.config['TICKETS_FOLDER'], repo.path) - requestpath = os.path.join(APP.config['REQUESTS_FOLDER'], repo.path) + paths = [] + for key in [ + 'GIT_FOLDER', 'DOCS_FOLDER', + 'TICKETS_FOLDER', 'REQUESTS_FOLDER']: + if APP.config[key]: + path = os.path.join(APP.config[key], repo.path) + if os.path.exists(path): + paths.append(path) try: - shutil.rmtree(repopath) - shutil.rmtree(docpath) - shutil.rmtree(ticketpath) - shutil.rmtree(requestpath) + for path in paths: + shutil.rmtree(path) except (OSError, IOError) as err: _log.exception(err) flask.flash( diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 691fdef..d2f82a4 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2853,9 +2853,6 @@ index 0000000..fb7093d output = self.app.post('/test/delete', follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( - '\n Could not delete all the ' - 'repos from the system', output.data) - self.assertIn( '
\n Projects 2', output.data) self.assertIn( @@ -2875,9 +2872,6 @@ index 0000000..fb7093d output = self.app.post('/test/delete', follow_redirects=True) self.assertEqual(output.status_code, 200) - self.assertTrue( - '\n Could not delete all the ' - 'repos from the system' in output.data) self.assertIn( '
\n Projects 2', output.data) @@ -2898,9 +2892,12 @@ index 0000000..fb7093d tests.create_projects_git(os.path.join(self.path, 'docs')) output = self.app.post('/test/delete', follow_redirects=True) self.assertEqual(output.status_code, 200) - self.assertTrue( - '\n Could not delete all the ' - 'repos from the system' in output.data) + self.assertIn( + '
\n Projects 2', output.data) + self.assertIn( + 'Forks 0', + output.data) # All repo there item = pagure.lib.model.Project( @@ -3075,6 +3072,40 @@ index 0000000..fb7093d 'Forks 0', output.data) + @patch.dict('pagure.APP.config', {'TICKETS_FOLDER': None}) + @patch('pagure.lib.notify.send_email', MagicMock(return_value=True)) + @patch('pagure.ui.repo.admin_session_timedout', MagicMock(return_value=False)) + def test_delete_repo_no_ticket(self): + """ Test the delete_repo endpoint when tickets aren't enabled in + this pagure instance. """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos')) + + user = tests.FakeUser(username='pingou') + with tests.user_set(pagure.APP, user): + # Check before deleting the project + output = self.app.get('/') + self.assertEqual(output.status_code, 200) + self.assertIn( + '
\n My Projects 3', output.data) + self.assertIn( + 'Forks 0', + output.data) + + # Delete the project + output = self.app.post('/test/delete', follow_redirects=True) + self.assertEqual(output.status_code, 200) + + # Check deletion worked + self.assertIn( + '
\n Projects 2', output.data) + self.assertIn( + 'Forks 0', + output.data) + @patch('pagure.lib.notify.send_email') @patch('pagure.ui.repo.admin_session_timedout') def test_delete_repo_with_users(self, ast, send_email):