#4924 Add a new API endpoint to retrieve a commit's metadata/info
Merged 3 years ago by pingou. Opened 3 years ago by pingou.

file modified
+59
@@ -1928,6 +1928,65 @@ 

      return flask.jsonify({"total_flags": len(flags), "flags": flags})

  

  

+ @API.route("/<repo>/c/<commit_hash>/info")

+ @API.route("/<namespace>/<repo>/c/<commit_hash>/info")

+ @API.route("/fork/<username>/<repo>/c/<commit_hash>/info")

+ @API.route("/fork/<username>/<namespace>/<repo>/c/<commit_hash>/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/<repo>/c/<commit_hash>/info

+         GET /api/0/<namespace>/<repo>/c/<commit_hash>/info

+ 

+     ::

+ 

+         GET /api/0/fork/<username>/<repo>/c/<commit_hash>/info

+         GET /api/0/fork/<username>/<namespace>/<repo>/c/<commit_hash>/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("/<repo>/c/<commit_hash>/flag", methods=["POST"])

  @API.route("/<namespace>/<repo>/c/<commit_hash>/flag", methods=["POST"])

  @API.route("/fork/<username>/<repo>/c/<commit_hash>/flag", methods=["POST"])

@@ -4512,5 +4512,66 @@ 

          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)

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 pingou@pingoured.fr

Style check failed :stuck_out_tongue_winking_eye:

2 new commits added

  • Add a new API endpoint to retrieve a commit's metadata/info
  • Add a new API endpoint allowing to delete a project
3 years ago

rebased onto 1498ebf

3 years ago

Pull-Request has been merged by pingou

3 years ago