From 06f5ad85d0509a4ecdadb91c79badae047cb8b86 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 08 2018 10:32:46 +0000 Subject: [PATCH 1/3] Expand the API endpoint listing tags to include the hash if asked to Signed-off-by: Pierre-Yves Chibon --- 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): From abf47117a2d66be66a2b2fd2a6a9361d783b653a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 08 2018 10:32:46 +0000 Subject: [PATCH 2/3] List the tags of the project in the list of commits Fixes https://pagure.io/pagure/issue/3012 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/templates/commits.html b/pagure/templates/commits.html index 9a4d6a7..68cc836 100644 --- a/pagure/templates/commits.html +++ b/pagure/templates/commits.html @@ -178,6 +178,8 @@ $('#diff_commits_link').click(function(){ $('#diff_commits').toggle(); }); + + {# Show branch labels in commit list #} $.ajax({ url: '{{ url_for("internal_ns.get_branches_head") }}' , type: 'POST', @@ -208,6 +210,28 @@ } }, }); + + {# Show tags in commit list #} + $.ajax({ + url: '{{ url_for("api_ns.api_git_tags", + repo=repo.name, + username=repo.user.user if repo.is_fork else none, + namespace=repo.namespace) }}' + '?with_commits=1' , + type: 'GET', + dataType: 'json', + success: function(res) { + for (var _t in res.tags) { + var _c = res.tags[_t]; + var html = '' + + ' ' + + _t + ' '; + var el = $('#c_' + _c); + el.parent().before(html); + } + }, + }); + }); {% endblock %} From fd729fe7583e3ffb4645de521f1d985d7fd1c99b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 08 2018 10:32:46 +0000 Subject: [PATCH 3/3] Check how starts the reference rather than its content Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index f04ca57..7328874 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1618,7 +1618,7 @@ def get_git_tags(project, with_commits=False): if with_commits: tags = {} for tag in repo_obj.listall_references(): - if 'refs/tags/' in tag: + if tag.startswith('refs/tags/'): ref = repo_obj.lookup_reference(tag) if ref: com = ref.get_object() @@ -1628,7 +1628,7 @@ def get_git_tags(project, with_commits=False): tags = [ tag.split('refs/tags/')[1] for tag in repo_obj.listall_references() - if 'refs/tags/' in tag + if tag.startswith('refs/tags/') ] return tags