From e4e275087a23d5aa4657f7e548804034f3e6c105 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 22 2019 15:02:34 +0000 Subject: Make it possible to configure the default branch shown in the UI Fixes https://pagure.io/pagure/issue/4038 Signed-off-by: Pierre-Yves Chibon --- diff --git a/doc/configuration.rst b/doc/configuration.rst index 5544444..1d06fcb 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1659,6 +1659,21 @@ the highlighting language/category to use as values. Defaults to: ``{".spec": "specfile", ".patch": "diff"}`` +DEFAULT_UI_BRANCH +~~~~~~~~~~~~~~~~~ + +This configuration key allows to change the branch shown by default in the +UI when accessing the overview page of the project. +When accessing the overview page of the project, we check if there is a +branch with the same name as the one specified in this configuration key +and if so, this is the branch shown. All links from there will be to that +branch, letting the user change back to another branch using either the +page listing all branches or the drop-down menu at the top of the page +listing the commits. + +Defaults to: ``None``. + + RepoSpanner Options ------------------- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 15afad5..442d8cc 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -117,34 +117,37 @@ def view_repo(repo, username=None, namespace=None): repo_db = flask.g.repo repo_obj = flask.g.repo_obj - if not repo_obj.is_empty and not repo_obj.head_is_unborn: - head = repo_obj.head.shorthand + default_branch = pagure_config.get('DEFAULT_UI_BRANCH') + + if default_branch in repo_obj.listall_branches(): + head = repo_obj.lookup_branch(default_branch).get_object().oid.hex + branchname = default_branch else: - head = None + if not repo_obj.is_empty and not repo_obj.head_is_unborn: + head = repo_obj.head.target + branchname = repo_obj.head.shorthand + else: + head = None + branchname = None cnt = 0 last_commits = [] tree = [] if not repo_obj.is_empty: try: - for commit in repo_obj.walk( - repo_obj.head.target, pygit2.GIT_SORT_NONE - ): + for commit in repo_obj.walk(head, pygit2.GIT_SORT_NONE): last_commits.append(commit) cnt += 1 if cnt == 3: break - tree = sorted(last_commits[0].tree, key=lambda x: x.filemode) + if last_commits: + tree = sorted(last_commits[0].tree, key=lambda x: x.filemode) except pygit2.GitError: pass readme = None safe = False - if not repo_obj.is_empty and not repo_obj.head_is_unborn: - branchname = repo_obj.head.shorthand - else: - branchname = None project = pagure.lib.query.get_authorized_project( flask.g.session, repo, user=username, namespace=namespace ) diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index be77354..79f84cf 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1822,6 +1822,75 @@ class PagureFlaskRepotests(tests.Modeltests): self.perfMaxWalks(3, 18) # Ideal: (1, 3) self.perfReset() + @patch.dict('pagure.config.config', {'DEFAULT_UI_BRANCH': 'feature'}) + def test_view_repo_default_branch_feature(self): + """ Test the view_repo endpoint when a default branch is specified. + """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + # Add some content to the git repo + tests.add_content_git_repo( + os.path.join(self.path, 'repos', 'test.git')) + tests.add_readme_git_repo( + os.path.join(self.path, 'repos', 'test.git'), branch="feature") + self.perfReset() + + output = self.app.get('/test') + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertNotIn('

This repo is brand new!

', output_text) + self.assertIn( + 'Overview - test - Pagure', output_text) + self.assertIn( + 'Add a README file', + output_text) + self.assertNotIn( + 'Add some directory and a file ' + 'for more testing', output_text) + self.assertIn( + 'href="/test/branches?branchname=feature">', output_text) + self.assertNotIn( + 'href="/test/branches?branchname=master">', output_text) + self.perfMaxWalks(3, 8) # Target: (1, 3) + self.perfReset() + + @patch.dict('pagure.config.config', {'DEFAULT_UI_BRANCH': 'foobar'}) + def test_view_repo_default_branch_invalid(self): + """ Test the view_repo endpoint when an invalid default branch is + specified (in which case it goes back to the default branch). + """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + # Add some content to the git repo + tests.add_content_git_repo( + os.path.join(self.path, 'repos', 'test.git')) + tests.add_readme_git_repo( + os.path.join(self.path, 'repos', 'test.git'), branch="feature") + self.perfReset() + + output = self.app.get('/test') + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertNotIn('

This repo is brand new!

', output_text) + self.assertIn( + 'Overview - test - Pagure', output_text) + self.assertIn( + 'Add some directory and a file ' + 'for more testing', output_text) + self.assertNotIn( + 'Add a README file', + output_text) + self.assertIn( + 'href="/test/branches?branchname=master">', output_text) + self.assertNotIn( + 'href="/test/branches?branchname=feature">', output_text) + self.perfMaxWalks(3, 8) # Target: (1, 3) + self.perfReset() + def test_view_repo_empty(self): """ Test the view_repo endpoint on a repo w/o master branch. """