From 3e45833b11d763f28d5a681de489ef4541ccfa40 Mon Sep 17 00:00:00 2001 From: Slavek Kabrda Date: Mar 09 2018 15:39:20 +0000 Subject: Show summary of flags on page with commits list --- diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index dc7e1b9..73dc342 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -456,6 +456,7 @@ def api(): api_fork_project_doc = load_doc(project.api_fork_project) api_generate_acls_doc = load_doc(project.api_generate_acls) api_new_branch_doc = load_doc(project.api_new_branch) + api_commit_flags_doc = load_doc(project.api_commit_flags) api_commit_add_flag_doc = load_doc(project.api_commit_add_flag) issues = [] @@ -529,6 +530,7 @@ def api(): api_fork_project_doc, api_generate_acls_doc, api_new_branch_doc, + api_commit_flags_doc, api_commit_add_flag_doc, ], issues=issues, diff --git a/pagure/api/project.py b/pagure/api/project.py index d0910ca..6a48491 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -1307,6 +1307,87 @@ def api_new_branch(repo, username=None, namespace=None): jsonout = flask.jsonify(output) return jsonout +@API.route('//c//flag') +@API.route('///c//flag') +@API.route('/fork///c//flag') +@API.route('/fork////c//flag') +@api_method +def api_commit_flags(repo, commit_hash, username=None, namespace=None): + """ + Flags for a commit + ------------------ + Return all flags for given commit of given project + + :: + + GET /api/0//c//flag + GET /api/0///c//flag + + :: + + GET /api/0/fork///c//flag + GET /api/0/fork////c//flag + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "flags": [ + { + "comment": "flag-comment", + "commit_hash": "28f1f7fe844301f0e5f7aecacae0a1e5ec50a090", + "date_created": "1520341983", + "percent": null, + "status": "success", + "url": "https://some.url.com", + "user": { + "fullname": "Full name", + "name": "fname" + }, + "username": "somename" + }, + { + "comment": "different-comment", + "commit_hash": "28f1f7fe844301f0e5f7aecacae0a1e5ec50a090", + "date_created": "1520512543", + "percent": null, + "status": "pending", + "url": "https://other.url.com", + "user": { + "fullname": "Other Name", + "name": "oname" + }, + "username": "differentname" + } + ], + "total_flags": 2 + } + + """ + 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) + + reponame = pagure.utils.get_repo_path(repo) + repo_obj = Repository(reponame) + try: + repo_obj.get(commit_hash) + except ValueError: + raise pagure.exceptions.APIError( + 404, error_code=APIERROR.ENOCOMMIT) + + flags = pagure.lib.get_commit_flag(flask.g.session, repo, commit_hash) + flags = [f.to_json(public=True) for f in flags] + return flask.jsonify( + { + 'total_flags': len(flags), + 'flags': flags + } + ) + @API.route('//c//flag', methods=['POST']) @API.route('///c//flag', methods=['POST']) diff --git a/pagure/templates/commits.html b/pagure/templates/commits.html index 68cc836..77ce14d 100644 --- a/pagure/templates/commits.html +++ b/pagure/templates/commits.html @@ -232,6 +232,44 @@ }, }); + var statusesLabels = {{ flag_statuses_labels|safe }} + + {# Show tags in commit list #} + $(".commithash").each(function(idx, item) { + var url = '{{ url_for("api_ns.api_commit_flags", + repo=repo.name, + username=repo.user.user if repo.is_fork else none, + namespace=repo.namespace, + commit_hash='COMMIT_HASH') }}' + url = url.replace("COMMIT_HASH", item.id.substring(2)) + $.ajax({ + url: url, + type: 'GET', + dataType: 'json', + success: function(res) { + var statuses = {} + for (var i in res['flags']) { + var f = res['flags'][i] + if (!(f['status'] in statuses)) { + statuses[f['status']] = [] + } + statuses[f['status']].push(f) + } + var html = '' + var sortedStatuses = Object.keys(statuses).sort() + for (var i in sortedStatuses) { + s = sortedStatuses[i] + numStatuses = statuses[s].length + html += '' + html += statuses[s].length + '\n' + } + html += '\n' + $(html).insertBefore(item) + } + }); + }); }); {% endblock %} diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index c74b117..fa8e83f 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -378,6 +378,7 @@ def view_commits(repo, branchname=None, username=None, namespace=None): page=page, total_page=total_page, form=pagure.forms.ConfirmationForm(), + flag_statuses_labels=json.dumps(pagure_config['FLAG_STATUSES_LABELS']), ) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 665bef8..f8ddac1 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -3158,6 +3158,87 @@ class PagureFlaskApiProjectFlagtests(tests.Modeltests): } ) + def test_commit_flags(self): + """ Test retrieving commit flags. """ + repo = pagure.lib.get_authorized_project(self.session, 'test') + repo_obj = pygit2.Repository(self.git_path) + commit = repo_obj.revparse_single('HEAD') + + # test with no flags + output = self.app.get('/api/0/test/c/%s/flag' % commit.oid.hex) + self.assertEqual(json.loads(output.data), {'total_flags': 0, 'flags': []}) + self.assertEqual(output.status_code, 200) + + # add some flags and retrieve them + pagure.lib.add_commit_flag( + session=self.session, + repo=repo, + commit_hash=commit.oid.hex, + username='simple-koji-ci', + status='pending', + percent=None, + comment='Build is running', + url='https://koji.fp.o/koji...', + uid='uid', + user='foo', + token='aaabbbcccddd' + ) + + pagure.lib.add_commit_flag( + session=self.session, + repo=repo, + commit_hash=commit.oid.hex, + username='complex-koji-ci', + status='success', + percent=None, + comment='Build succeeded', + url='https://koji.fp.o/koji...', + uid='uid2', + user='foo', + token='aaabbbcccddd' + ) + self.session.commit() + + output = self.app.get('/api/0/test/c/%s/flag' % commit.oid.hex) + data = json.loads(output.data) + + for f in data['flags']: + f['date_created'] = u'1510742565' + f['commit_hash'] = u'62b49f00d489452994de5010565fab81' + expected_output = { + u"flags": [ + { + u"comment": u"Build is running", + u"commit_hash": u"62b49f00d489452994de5010565fab81", + u"date_created": u"1510742565", + u"percent": None, + u"status": u"pending", + u"url": u"https://koji.fp.o/koji...", + u"user": { + u"fullname": u"foo bar", + u"name": u"foo" + }, + u"username": u"simple-koji-ci" + }, + { + u"comment": u"Build succeeded", + u"commit_hash": u"62b49f00d489452994de5010565fab81", + u"date_created": u"1510742565", + u"percent": None, + u"status": u"success", + u"url": u"https://koji.fp.o/koji...", + u"user": { + u"fullname": u"foo bar", + u"name": u"foo" + }, + u"username": u"complex-koji-ci" + } + ], + u"total_flags": 2 + } + + self.assertEqual(data, expected_output) + if __name__ == '__main__': unittest.main(verbosity=2)