#4582 Add a set-default-branch action to pagure-admin
Merged 4 years ago by pingou. Opened 4 years ago by jlanda.

file modified
+72 -20
@@ -17,6 +17,7 @@ 

  import requests

  from string import Template

  import sys

+ import pygit2

  

  import arrow

  from six.moves import input
@@ -34,6 +35,7 @@ 

  import pagure.lib.query  # noqa: E402

  import pagure.lib.tasks_utils  # noqa: E402

  from pagure.flask_app import generate_user_key_files  # noqa: E402

+ from pagure.utils import get_repo_path  # noqa: E402

  

  

  _config = pagure.config.reload_config()
@@ -462,6 +464,30 @@ 

      local_parser.set_defaults(func=do_create_branch)

  

  

+ def _parser_set_default_branch(subparser):

+     """ Set up the CLI argument parser for the set-default-branch action.

+ 

+     :arg subparser: an argparse subparser allowing to have action's specific

+         arguments

+ 

+      """

+     local_parser = subparser.add_parser(

+         "set-default-branch", help="Set the specified branch as default"

+     )

+     local_parser.add_argument(

+         "project",

+         help="Project to update (as namespace/project if there "

+         "is a namespace)",

+     )

+     local_parser.add_argument(

+         "--user", help="User of the project (to use only on forks)"

+     )

+     local_parser.add_argument(

+         "branch", help="Name of the branch to be set as default"

+     )

+     local_parser.set_defaults(func=do_set_default_branch)

+ 

+ 

  def parse_arguments(args=None):

      """ Set-up the argument parsing. """

      parser = argparse.ArgumentParser(
@@ -523,6 +549,9 @@ 

      # create-branch

      _parser_create_branch(subparser)

  

+     # set-default-branch

+     _parser_set_default_branch(subparser)

+ 

      return parser.parse_args(args)

  

  
@@ -556,6 +585,16 @@ 

      )

  

  

+ 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.

  
@@ -806,10 +845,7 @@ 

      # 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!"
@@ -842,10 +878,7 @@ 

      # 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(
@@ -899,10 +932,7 @@ 

      # 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"
@@ -932,10 +962,7 @@ 

      # 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"]:
@@ -1195,10 +1222,7 @@ 

      # 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(
@@ -1219,6 +1243,34 @@ 

      print("Branch created")

  

  

+ def do_set_default_branch(args):

+     """ Sets the specified git branch as default

+ 

+     Args:

+         args (argparse.Namespace): Parsed arguments

+     """

+     _log.debug("project:         %s", args.project)

+     _log.debug("user:            %s", args.user)

+     _log.debug("branch:          %s", args.branch)

+ 

+     # Get the project

+     project = _get_project(args.project, user=args.user)

+ 

+     _check_project(project, project=args.project, user=args.user)

+ 

+     repo_path = get_repo_path(project)

+     repo_obj = pygit2.Repository(repo_path)

+ 

+     if args.branch not in repo_obj.listall_branches():

+         raise pagure.exceptions.PagureException(

+             "No %s branch found on project: %s" % (args.branch, args.project)

+         )

+ 

+     pagure.lib.git.git_set_ref_head(project, args.branch)

+ 

+     print("Branch %s set as default" % (args.branch))

+ 

+ 

  def main():

      """ Start of the application. """

  

file modified
+12
@@ -2512,6 +2512,18 @@ 

          tempclone.push(username, branch, branch)

  

  

+ def git_set_ref_head(project, branch):

+     """ Set the HEAD reference of the project

+     :arg project: The project instance to set the HEAD reference

+     :arg branch: The branch to be set as HEAD reference

+     """

+     repo_path = pagure.utils.get_repo_path(project)

+     repo_obj = PagureRepo(repo_path)

+ 

+     reference = repo_obj.lookup_reference("refs/heads/%s" % branch).resolve()

+     repo_obj.set_head(reference.name)

+ 

+ 

  def delete_project_repos(project):

      """ Deletes the actual git repositories on disk or repoSpanner

  

file modified
+1 -4
@@ -1631,10 +1631,7 @@ 

      if form.validate_on_submit():

          branchname = form.branches.data

          try:

-             reference = repo_obj.lookup_reference(

-                 "refs/heads/%s" % branchname

-             ).resolve()

-             repo_obj.set_head(reference.name)

+             pagure.lib.git.git_set_ref_head(project=repo, branch=branchname)

              flask.flash("Default branch updated to %s" % branchname)

          except Exception as err:  # pragma: no cover

              _log.exception(err)

file modified
+1 -1
@@ -1,2 +1,2 @@ 

- PAGURE_CI_SERVICES = ['jenkins']

+ PAGURE_CI_SERVICES = ["jenkins"]

  ALLOW_PROJECT_DOWAIT = True

file modified
+109 -5
@@ -771,7 +771,9 @@ 

          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 @@ 

          )

          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 @@ 

          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 @@ 

          )

          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 @@ 

          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):
@@ -1974,5 +1984,99 @@ 

          )

  

  

+ class PagureSetDefaultBranchTests(tests.Modeltests):

+     """ Tests for pagure-admin set-default-branch """

+ 

+     populate_db = True

+ 

+     def setUp(self):

+         """ Set up the environment, run before every tests. """

+         super(PagureSetDefaultBranchTests, self).setUp()

+ 

+         # Create a couple of projects

+         tests.create_projects(self.session)

+         # Create their git repo

+         tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)

+ 

+         # Make the imported pagure use the correct db session

+         pagure.cli.admin.session = self.session

+ 

+     def test_set_default_branch_unknown_project(self):

+         """ Test the set-default-branch function of pagure-admin on an unknown

+         project.

+         """

+ 

+         args = munch.Munch(

+             {"project": "foob", "user": None, "branch": "master"}

+         )

+         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: project=foob, user=None",

+         )

+ 

+     def test_set_default_branch_invalid_project(self):

+         """ Test the set-default-branch function of pagure-admin on an invalid

+         project.

+         """

+ 

+         args = munch.Munch(

+             {"project": "f/o/o/b", "user": None, "branch": "master"}

+         )

+         with self.assertRaises(pagure.exceptions.PagureException) as cm:

+             pagure.cli.admin.do_set_default_branch(args)

+         self.assertEqual(

+             cm.exception.args[0],

+             'Invalid project name, has more than one "/": f/o/o/b',

+         )

+ 

+     def test_set_default_branch_unknown_branch(self):

+         """ Test the set-default-branch function of pagure-admin on an unknown

+         branch.

+         """

+ 

+         args = munch.Munch({"project": "test", "user": None, "branch": "foob"})

+         with self.assertRaises(pagure.exceptions.PagureException) as cm:

+             pagure.cli.admin.do_set_default_branch(args)

+         self.assertEqual(

+             cm.exception.args[0], "No foob branch found on project: test"

+         )

+ 

+     def test_set_default_branch_invalid_branch(self):

+         """ Test the set-default-branch function of pagure-admin on an invalid branch.

+         """

+ 

+         args = munch.Munch(

+             {"project": "test", "user": None, "branch": "~invalid~"}

+         )

+         with self.assertRaises(pagure.exceptions.PagureException) as cm:

+             pagure.cli.admin.do_set_default_branch(args)

+         self.assertEqual(

+             cm.exception.args[0], "No ~invalid~ branch found on project: test"

+         )

+ 

+     def test_set_default_branch(self):

+         """ Test the set-default-branch funcion of pagure-admin. """

+ 

+         gitrepo_path = os.path.join(self.path, "repos", "test.git")

+         tests.add_content_git_repo(gitrepo_path)

+         tests.add_commit_git_repo(gitrepo_path, branch="dev")

+ 

+         # Check default branch before:

+         gitrepo = pygit2.Repository(gitrepo_path)

+         self.assertEqual(gitrepo.head.shorthand, "master")

+ 

+         args = munch.Munch({"project": "test", "user": None, "branch": "dev"})

+ 

+         with tests.capture_output() as output:

+             pagure.cli.admin.do_set_default_branch(args)

+         output = output.getvalue()

+         self.assertEqual("Branch dev set as default\n", output)

+ 

+         # Check default branch after:

+         self.assertEqual(gitrepo.head.shorthand, "dev")

+ 

+ 

  if __name__ == "__main__":

      unittest.main(verbosity=2)

no initial comment

rebased onto c4d86bccc1bda952e9485b2b22c6068746ecc2b5

4 years ago

rebased onto 0daaf49614bd93afe85703e3a9ace46e00ceb3ef

4 years ago

rebased onto 0daaf49614bd93afe85703e3a9ace46e00ceb3ef

4 years ago

I feel like we're repeating code here, maybe we could consolidate this in one place :)

yeah, I thought that we should move set_default_branch thing to lib and use it both on ui and pagure-admin

yeah, I thought that we should move set_default_branch thing to lib and use it both on ui and pagure-admin

+1

rebased onto bdf4194fa2b48488cb020328505ece06a11bd74a

4 years ago

rebased onto d5b7b0f02ddb2e7f15d121c8fa8c58596eb0d320

4 years ago

rebased onto d5b7b0f02ddb2e7f15d121c8fa8c58596eb0d320

4 years ago

pretty please pagure-ci rebuild

4 years ago

pretty please pagure-ci rebuild

4 years ago

Something is up on our pipeline. This was passing 11 days ago and I did not touch it and now fails

The test suite is failing due to #4611 . #4612 should temporary fix it.

1 new commit added

  • pagure-admin: reusable project checking
4 years ago

Another PR waiting to jenkins & ready to review =)

rebased onto cd89020d7547a7c7fca1d6b9122a1c4d1a842c0a

4 years ago

pretty please pagure-ci rebuild

4 years ago

rebased onto a7873b57881332206a3e4a22b2617abaf83f5eeb

4 years ago

Let's rebase this one and get it in :)

rebased onto a553cb313572d23194afe4de0e183f298144023b

4 years ago

pretty please pagure-ci rebuild

4 years ago

rebased onto 4de8f25

4 years ago

Pull-Request has been merged by pingou

4 years ago