diff --git a/pagure/api/project.py b/pagure/api/project.py index 3f3a5a5..f4d72b6 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -50,6 +50,17 @@ def api_git_tags(repo, username=None, namespace=None): GET /api/0/fork///git/tags GET /api/0/fork////git/tags + Parameters + ^^^^^^^^^^ + + +-----------------+----------+---------------+--------------------------+ + | Key | Type | Optionality | Description | + +=================+==========+===============+==========================+ + | ``with_commits``| string | Optional | | Include the commit hash| + | | | | corresponding to the | + | | | | tags found in the repo | + +-----------------+----------+---------------+--------------------------+ + Sample response ^^^^^^^^^^^^^^^ @@ -60,13 +71,27 @@ def api_git_tags(repo, username=None, namespace=None): "tags": ["0.0.1", "0.0.2"] } + + { + "total_tags": 2, + "tags": { + "0.0.1": "bb8fa2aa199da08d6085e1c9badc3d83d188d38c", + "0.0.2": "d16fe107eca31a1bdd66fb32c6a5c568e45b627e" + } + } + """ + with_commits = flask.request.values.get('with_commits', None) or False + + if str(with_commits).lower() in ['1', 'true']: + with_commits = True + repo = get_authorized_api_project( flask.g.session, repo, user=username, namespace=namespace) if repo is None: raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) - tags = pagure.lib.git.get_git_tags(repo) + tags = pagure.lib.git.get_git_tags(repo, with_commits=with_commits) jsonout = flask.jsonify({ 'total_tags': len(tags), diff --git a/pagure/lib/git.py b/pagure/lib/git.py index ca6bf59..f04ca57 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1608,18 +1608,28 @@ def update_pull_ref(request, repo): rc.delete(reponame) -def get_git_tags(project): +def get_git_tags(project, with_commits=False): """ Returns the list of tags created in the git repositorie of the specified project. """ repopath = pagure.utils.get_repo_path(project) repo_obj = PagureRepo(repopath) - tags = [ - tag.split('refs/tags/')[1] - for tag in repo_obj.listall_references() - if 'refs/tags/' in tag - ] + if with_commits: + tags = {} + for tag in repo_obj.listall_references(): + if 'refs/tags/' in tag: + ref = repo_obj.lookup_reference(tag) + if ref: + com = ref.get_object() + if com: + tags[tag.split('refs/tags/')[1]] = com.oid.hex + else: + tags = [ + tag.split('refs/tags/')[1] + for tag in repo_obj.listall_references() + if 'refs/tags/' in tag + ] return tags diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index b9713e5..742425d 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -87,6 +87,18 @@ class PagureFlaskApiProjecttests(tests.Modeltests): {'tags': ['0.0.1'], 'total_tags': 1} ) + # Check tags with commits + output = self.app.get('/api/0/test/git/tags?with_commits=True') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['tags']['0.0.1'] = 'bb8fa2aa199da08d6085e1c9badc3d83d188d38c' + self.assertDictEqual( + data, + { + u'tags': {u'0.0.1': u'bb8fa2aa199da08d6085e1c9badc3d83d188d38c'}, + u'total_tags': 1} + ) + shutil.rmtree(newpath) def test_api_git_branches(self):