From b88d6605e28836844f73f18655ba681c3bbe8604 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 25 2020 16:25:45 +0000 Subject: Fix the view_commit endpoint when the identifier provided is a git tag Instead of crashing, redirect the user to the corresponding underlying commit. Fixes https://pagure.io/pagure/issue/4685 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index cbffdac..e3bd547 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -889,6 +889,18 @@ def view_commit(repo, commitid, username=None, namespace=None): if isinstance(commit, pygit2.Blob): flask.abort(404, description="Commit not found") + if isinstance(commit, pygit2.Tag): + commit = commit.peel(pygit2.Commit) + return flask.redirect( + flask.url_for( + "ui_ns.view_commit", + repo=repo.name, + username=username, + namespace=repo.namespace, + commitid=commit.hex, + ) + ) + if commit.parents: diff = repo_obj.diff(commit.parents[0], commit) else: diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 0f977d8..4aa9105 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2282,6 +2282,44 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn("Commits - test - Pagure", output_text) self.assertEqual(output_text.count(''), 1) + def test_view_commit_from_tag(self): + """ Test the view_commit endpoint given a tag. """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) + + # Add a README to the git repo - First commit + tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git")) + repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git")) + first_commit = repo.revparse_single("HEAD") + tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0) + repo.create_tag( + "0.0.1", + first_commit.oid.hex, + pygit2.GIT_OBJ_COMMIT, + tagger, + "Release 0.0.1", + ) + + tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git")) + repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git")) + project = pagure.lib.query.get_authorized_project(self.session, "test") + tags = pagure.lib.git.get_git_tags_objects(project) + tag_id = tags[0]["object"].oid + commit_id = tags[0]["object"].peel(pygit2.Commit).hex + + output = self.app.get("/test/c/%s" % tag_id) + self.assertEqual(output.status_code, 302) + + output = self.app.get("/test/c/%s" % tag_id, follow_redirects=True) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn(first_commit.oid.hex, output_text) + self.assertIn( + "Commit - test - %s - Pagure" % commit_id, + output_text, + ) + def test_compare_commits(self): """ Test the compare_commits endpoint. """