From 1498ebf4ba73f0f9cac0c67e1e903bcdeb9087c4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 22 2020 08:21:11 +0000 Subject: Add a new API endpoint to retrieve a commit's metadata/info This commit adds a new API endpoint allowing to retrieve the meta-data about a commit (such as its author, committer, commit date/time). Fixes https://pagure.io/pagure/issue/4922 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 333b76a..3c50ee5 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -1928,6 +1928,65 @@ def api_commit_flags(repo, commit_hash, username=None, namespace=None): return flask.jsonify({"total_flags": len(flags), "flags": flags}) +@API.route("//c//info") +@API.route("///c//info") +@API.route("/fork///c//info") +@API.route("/fork////c//info") +@api_method +def api_commit_info(repo, commit_hash, username=None, namespace=None): + """ + Get commit information + ---------------------- + Return the metadata we could retrieve about the specified commit + + :: + + GET /api/0//c//info + GET /api/0///c//info + + :: + + GET /api/0/fork///c//info + GET /api/0/fork////c//info + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "commit": { + + } + } + + """ + repo = _get_repo(repo, username, namespace) + + reponame = pagure.utils.get_repo_path(repo) + repo_obj = pygit2.Repository(reponame) + commit_obj = None + try: + commit_obj = repo_obj.get(commit_hash) + except ValueError: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOCOMMIT) + if not isinstance(commit_obj, pygit2.Commit): + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOCOMMIT) + + info = { + "author": commit_obj.author.name, + "committer": commit_obj.committer.name, + "commit_time": commit_obj.commit_time, + "commit_time_offset": commit_obj.commit_time_offset, + "hash": commit_obj.hex, + "message": commit_obj.message, + "parent_ids": [h.hex for h in commit_obj.parent_ids], + "tree_id": commit_obj.tree_id.hex, + } + + return flask.jsonify(info) + + @API.route("//c//flag", methods=["POST"]) @API.route("///c//flag", methods=["POST"]) @API.route("/fork///c//flag", methods=["POST"]) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index febbcb6..3cf2d01 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -4512,5 +4512,66 @@ class PagureFlaskApiProjectWebhookTokenTests(tests.Modeltests): self.assertEqual(output.status_code, 401) +class PagureFlaskApiProjectCommitInfotests(tests.Modeltests): + """ Tests for the flask API of pagure for commit info + """ + + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(PagureFlaskApiProjectCommitInfotests, self).setUp() + + tests.create_projects(self.session) + repo_path = os.path.join(self.path, "repos") + self.git_path = os.path.join(repo_path, "test.git") + tests.create_projects_git(repo_path, bare=True) + tests.add_content_git_repo(self.git_path) + + repo_obj = pygit2.Repository(self.git_path) + self.commit = repo_obj.revparse_single("HEAD") + + def test_api_commit_info(self): + """ Test flagging a commit with missing precentage. """ + + output = self.app.get("/api/0/test/c/%s/info" % self.commit.oid.hex) + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + expected_output = { + "author": "Alice Author", + "commit_time": self.commit.commit_time, + "commit_time_offset": self.commit.commit_time_offset, + "committer": "Cecil Committer", + "hash": self.commit.oid.hex, + "message": "Add some directory and a file for more testing", + "parent_ids": [self.commit.parent_ids[0].hex], + "tree_id": self.commit.tree_id.hex, + } + + self.assertEqual(data, expected_output) + + def test_api_commit_info_invalid_commit(self): + """ Test flagging a commit with missing username. """ + output = self.app.get("/api/0/test/c/invalid_commit_hash/info") + + self.assertEqual(output.status_code, 404) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual( + pagure.api.APIERROR.ENOCOMMIT.name, data["error_code"] + ) + self.assertEqual(pagure.api.APIERROR.ENOCOMMIT.value, data["error"]) + + def test_api_commit_info_hash_tree(self): + """ Test flagging a commit with missing username. """ + output = self.app.get( + "/api/0/test/c/%s/info" % self.commit.tree_id.hex + ) + + self.assertEqual(output.status_code, 404) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual( + pagure.api.APIERROR.ENOCOMMIT.name, data["error_code"] + ) + self.assertEqual(pagure.api.APIERROR.ENOCOMMIT.value, data["error"]) + + if __name__ == "__main__": unittest.main(verbosity=2)