From 1981ff7b5f02bc245d40159d604e6c6e80260c3b Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Wed, 23 Jul 2014 16:00:48 -0700 Subject: [PATCH] Add open reviews to elastic-recheck status page List open gerrit reviews on the elastic_recheck status page. Listing the reviews here can help us prioritize reviews of them. Currently one has to manually dig through the launchpad notes for gerrit review links, this automates that process. Instead of looking for jenkins comments in launchpad about open patches, directly query gerrit for open patches related to the bug on hand. Since gerrit only supports string matching and not regex for messages, just look for the bug number in the commit message. Our bug numbers are long enough that it unlikely the number will come up in other cases. Change-Id: Ida2144ccc5f0fdf91e2bd70714de80c83f4c1200 --- elastic_recheck/cmd/graph.py | 18 +++++++++ .../tests/unit/samples/gerrit-bug-query.json | 21 +++++++++++ elastic_recheck/tests/unit/test_graph.py | 37 +++++++++++++++++++ web/share/elastic-recheck.js | 17 +++++++++ 4 files changed, 93 insertions(+) create mode 100644 elastic_recheck/tests/unit/samples/gerrit-bug-query.json create mode 100644 elastic_recheck/tests/unit/test_graph.py diff --git a/elastic_recheck/cmd/graph.py b/elastic_recheck/cmd/graph.py index 689fa37a..80b4b1a6 100755 --- a/elastic_recheck/cmd/graph.py +++ b/elastic_recheck/cmd/graph.py @@ -21,6 +21,7 @@ import json import os from launchpadlib import launchpad +import requests import elastic_recheck.elasticRecheck as er from elastic_recheck import results as er_results @@ -40,9 +41,26 @@ def get_launchpad_bug(bug): (x.bug_target_name, x.status), lp_bug.bug_tasks)) bugdata['affects'] = projects + bugdata['reviews'] = get_open_reviews(bug) return bugdata +def get_open_reviews(bug_number): + "return list of open gerrit reviews for a given bug.""" + r = requests.get("https://review.openstack.org:443/changes/" + "?q=status:open++message:`%s`" % bug_number) + # strip off first few chars because 'the JSON response body starts with a + # magic prefix line that must be stripped before feeding the rest of the + # response body to a JSON parser' + # https://review.openstack.org/Documentation/rest-api.html + reviews = [] + result = None + result = json.loads(r.text[4:]) + for review in result: + reviews.append(review['_number']) + return reviews + + def main(): parser = argparse.ArgumentParser(description='Generate data for graphs.') parser.add_argument(dest='queries', diff --git a/elastic_recheck/tests/unit/samples/gerrit-bug-query.json b/elastic_recheck/tests/unit/samples/gerrit-bug-query.json new file mode 100644 index 00000000..8ac33d51 --- /dev/null +++ b/elastic_recheck/tests/unit/samples/gerrit-bug-query.json @@ -0,0 +1,21 @@ +[ + { + "kind": "gerritcodereview#change", + "id": "openstack%2Fneutron~master~Iefb30452083747b45496600c81f8d0a6f378bd08", + "project": "openstack/neutron", + "branch": "master", + "topic": "bug/1288393", + "change_id": "Iefb30452083747b45496600c81f8d0a6f378bd08", + "subject": "Change nexus_dict values to be array", + "status": "NEW", + "created": "2014-08-08 20:04:30.000000000", + "updated": "2014-08-08 21:06:08.835000000", + "mergeable": false, + "_sortkey": "002ef9920001b971", + "_number": 113009, + "owner": { + "name": "Britt Houser" + } + } +] + diff --git a/elastic_recheck/tests/unit/test_graph.py b/elastic_recheck/tests/unit/test_graph.py new file mode 100644 index 00000000..d151ff6c --- /dev/null +++ b/elastic_recheck/tests/unit/test_graph.py @@ -0,0 +1,37 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from elastic_recheck.cmd import graph +from elastic_recheck.tests import unit + + +class FakeResponse(object): + def __init__(self, response_text): + super(FakeResponse, self).__init__() + # magic gerrit prefix + self.text = ")]}'\n" + response_text + + +class TestGraphCmd(unit.UnitTestCase): + def test_get_open_reviews_empty(self): + with mock.patch('requests.get') as mock_get: + mock_get.return_value = FakeResponse("[]\n") + self.assertEqual(graph.get_open_reviews('1353131'), []) + + def test_get_open_reviews(self): + with mock.patch('requests.get') as mock_get: + with open('elastic_recheck/tests/unit/samples/' + 'gerrit-bug-query.json') as f: + mock_get.return_value = FakeResponse(f.read()) + self.assertEqual(graph.get_open_reviews('1288393'), [113009]) diff --git a/web/share/elastic-recheck.js b/web/share/elastic-recheck.js index 053923ee..0214520c 100644 --- a/web/share/elastic-recheck.js +++ b/web/share/elastic-recheck.js @@ -48,6 +48,23 @@ function update() { $('

', { text: 'Projects: ' + bug['bug_data']['affects'] }).appendTo(div); + var reviews = bug['bug_data']['reviews']; + if (reviews.length>0) { + $('

', { + text: 'Open reviews: ' + }).appendTo($('', { + 'class': 'extlink' + }).appendTo(div)); + } + for (var i = 0; i < reviews.length ; i++) { + $('', { + href: 'https://review.openstack.org/#/c/'+reviews[i], + text: reviews[i] + }).appendTo($('', { + 'class': 'extlink' + }).appendTo(div)); + + } $('
', {'class': 'graph'}).appendTo(div); $('', { href: 'http://logstash.openstack.org/#'+bug['logstash_query'],