From 76327a810d485c67df6c5732c135d768da575806 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sep 25 2019 12:55:54 +0000 Subject: pagure-admin: reusable project checking --- diff --git a/pagure/cli/admin.py b/pagure/cli/admin.py index a00cc1b..3808f3c 100644 --- a/pagure/cli/admin.py +++ b/pagure/cli/admin.py @@ -585,6 +585,16 @@ def _get_project(arg_project, user=None): ) +def _check_project(_project, **kwargs): + """ Check that the project extracted with args is a valid project """ + if _project is None: + raise pagure.exceptions.PagureException( + "No project found with: {}".format( + ", ".join(["{}={}".format(k, v) for k, v in kwargs.items()]) + ) + ) + + def do_generate_acl(args): """ Regenerate the gitolite ACL file. @@ -835,10 +845,7 @@ def do_delete_project(args): # Get the project project = _get_project(args.project, user=args.user) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s" % args.project - ) + _check_project(project, project=args.project, user=args.user) print( "Are you sure you want to delete: %s?\n This cannot be undone!" @@ -871,10 +878,7 @@ def do_get_watch_status(args): # Get the project project = _get_project(args.project) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s" % args.project - ) + _check_project(project, project=args.project) level = ( pagure.lib.query.get_watch_level_on_repo( @@ -928,10 +932,7 @@ def do_update_watch_status(args): # Get the project project = _get_project(args.project) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s" % args.project - ) + _check_project(project, project=args.project) print( "Updating watch status of %s to %s (%s) on %s" @@ -961,10 +962,7 @@ def do_read_only(args): # Get the project project = _get_project(args.project, user=args.user) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s" % args.project - ) + _check_project(project, project=args.project) # Validate ro flag if args.ro and args.ro.lower() not in ["true", "false"]: @@ -1224,10 +1222,7 @@ def do_create_branch(args): # Get the project project = _get_project(args.project, user=args.user) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s, user: %s" % (args.project, args.user) - ) + _check_project(project, project=args.project, user=args.user) try: pagure.lib.git.new_git_branch( @@ -1261,10 +1256,7 @@ def do_set_default_branch(args): # Get the project project = _get_project(args.project, user=args.user) - if project is None: - raise pagure.exceptions.PagureException( - "No project found with: %s, user: %s" % (args.project, args.user) - ) + _check_project(project, project=args.project, user=args.user) repo_path = get_repo_path(project) repo_obj = pygit2.Repository(repo_path) diff --git a/tests/test_pagure_admin.py b/tests/test_pagure_admin.py index 8ddeed2..b647679 100644 --- a/tests/test_pagure_admin.py +++ b/tests/test_pagure_admin.py @@ -771,7 +771,9 @@ class PagureAdminGetWatchTests(tests.Modeltests): args = munch.Munch({"project": "foobar", "user": "pingou"}) with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_get_watch_status(args) - self.assertEqual(cm.exception.args[0], "No project found with: foobar") + self.assertEqual( + cm.exception.args[0], "No project found with: project=foobar" + ) def test_get_watch_get_project_invalid_project(self): """ Test the get-watch function of pagure-admin with an invalid @@ -912,7 +914,9 @@ class PagureAdminUpdateWatchTests(tests.Modeltests): ) with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_update_watch_status(args) - self.assertEqual(cm.exception.args[0], "No project found with: foob") + self.assertEqual( + cm.exception.args[0], "No project found with: project=foob" + ) def test_get_watch_update_project_invalid_project(self): """ Test the update-watch function of pagure-admin on an invalid @@ -1041,7 +1045,9 @@ class PagureAdminReadOnlyTests(tests.Modeltests): args = munch.Munch({"project": "foob", "user": None, "ro": None}) with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_read_only(args) - self.assertEqual(cm.exception.args[0], "No project found with: foob") + self.assertEqual( + cm.exception.args[0], "No project found with: project=foob" + ) def test_read_only_invalid_project(self): """ Test the read-only function of pagure-admin on an invalid @@ -1725,7 +1731,10 @@ class PagureAdminDeleteProjectTests(tests.Modeltests): ) with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_delete_project(args) - self.assertEqual(cm.exception.args[0], "No project found with: foob") + self.assertEqual( + cm.exception.args[0], + "No project found with: project=foob, user=None", + ) def test_delete_project_invalid_project(self): """ Test the read-only function of pagure-admin on an invalid @@ -1851,7 +1860,8 @@ class PagureCreateBranchTests(tests.Modeltests): with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_create_branch(args) self.assertEqual( - cm.exception.args[0], "No project found with: foob, user: None" + cm.exception.args[0], + "No project found with: project=foob, user=None", ) def test_create_branch_invalid_project(self): @@ -2002,7 +2012,8 @@ class PagureSetDefaultBranchTests(tests.Modeltests): with self.assertRaises(pagure.exceptions.PagureException) as cm: pagure.cli.admin.do_set_default_branch(args) self.assertEqual( - cm.exception.args[0], "No project found with: foob, user: None" + cm.exception.args[0], + "No project found with: project=foob, user=None", ) def test_set_default_branch_invalid_project(self):