Genericize the change list and add inter-change links

Make the change list display not only changes for a project, but
any arbitrary set of changes represented by a query.  This will
enable searching, inter-change linking, and custom dashboards.

The search syntax is extremly basic at the moment, but will be
extended to attempt to match the Gerrit webui search syntax as
closely as possible.

Use the new feature to implement a new kind of commentlink that
matches on Iabcdef style change-ids to "search" for matching
changes.

Change-Id: Icbcb166962fd96718ab56eb4118fc5ea43a4ba8b
This commit is contained in:
James E. Blair 2014-07-12 18:11:51 -07:00
parent 2d018c40ff
commit 6d95ea2dff
6 changed files with 74 additions and 16 deletions

View File

@ -1,4 +1,5 @@
# Copyright 2014 OpenStack Foundation # Copyright 2014 OpenStack Foundation
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
@ -192,6 +193,22 @@ class App(object):
lambda button: self.backScreen()) lambda button: self.backScreen())
self.popup(dialog, min_width=76, min_height=len(lines)+4) self.popup(dialog, min_width=76, min_height=len(lines)+4)
def search(self, query):
self.log.debug("Search query: %s" % query)
with self.db.getSession() as session:
changes = session.getChanges(query)
change_key = None
if len(changes) == 1:
change_key = changes[0].key
try:
if change_key:
view = view_change.ChangeView(self, change_key)
else:
view = view_change_list.ChangeListView(self, query)
self.changeScreen(view)
except gertty.view.DisplayError as e:
self.error(e.message)
def openChange(self): def openChange(self):
dialog = OpenChangeDialog() dialog = OpenChangeDialog()
urwid.connect_signal(dialog, 'cancel', urwid.connect_signal(dialog, 'cancel',
@ -231,7 +248,7 @@ class App(object):
view = view_change.ChangeView(self, change_key) view = view_change.ChangeView(self, change_key)
self.changeScreen(view) self.changeScreen(view)
except gertty.view.DisplayError as e: except gertty.view.DisplayError as e:
self.app.error(e.message) self.error(e.message)
def error(self, message): def error(self, message):
dialog = mywid.MessageDialog('Error', message) dialog = mywid.MessageDialog('Error', message)

View File

@ -43,6 +43,17 @@ class LinkReplacement(object):
lambda link:app.openURL(self.url.format(**data))) lambda link:app.openURL(self.url.format(**data)))
return link return link
class SearchReplacement(object):
def __init__(self, config):
self.query = config['query']
self.text = config['text']
def replace(self, app, data):
link = mywid.Link(self.text.format(**data), 'link', 'focused-link')
urwid.connect_signal(link, 'selected',
lambda link:app.search(self.query.format(**data)))
return link
class CommentLink(object): class CommentLink(object):
def __init__(self, config): def __init__(self, config):
self.match = re.compile(config['match'], re.M) self.match = re.compile(config['match'], re.M)
@ -52,6 +63,8 @@ class CommentLink(object):
self.replacements.append(TextReplacement(r['text'])) self.replacements.append(TextReplacement(r['text']))
if 'link' in r: if 'link' in r:
self.replacements.append(LinkReplacement(r['link'])) self.replacements.append(LinkReplacement(r['link']))
if 'search' in r:
self.replacements.append(SearchReplacement(r['search']))
def run(self, app, chunks): def run(self, app, chunks):
ret = [] ret = []

View File

@ -43,7 +43,10 @@ class ConfigSchema(object):
link_replacement = {'link': {v.Required('url'): str, link_replacement = {'link': {v.Required('url'): str,
v.Required('text'): str}} v.Required('text'): str}}
replacement = v.Any(text_replacement, link_replacement) search_replacement = {'search': {v.Required('query'): str,
v.Required('text'): str}}
replacement = v.Any(text_replacement, link_replacement, search_replacement)
palette = {v.Required('name'): str, palette = {v.Required('name'): str,
v.Match('(?!name)'): [str]} v.Match('(?!name)'): [str]}

View File

@ -1,4 +1,5 @@
# Copyright 2014 OpenStack Foundation # Copyright 2014 OpenStack Foundation
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
@ -435,6 +436,25 @@ class DatabaseSession(object):
except sqlalchemy.orm.exc.NoResultFound: except sqlalchemy.orm.exc.NoResultFound:
return None return None
def getChanges(self, query, unreviewed=False):
#TODO(jeblair): use a real parser that supports the full gerrit query syntax
q = self.session().query(Change)
for term in query.split():
key, data = term.split(':')
if key == 'changeid':
q = q.filter(change_table.c.change_id==data)
elif key == 'project_key':
q = q.filter(change_table.c.project_key==data)
elif key == 'status':
if data == 'open':
q = q.filter(change_table.c.status.notin_(['MERGED', 'ABANDONED']))
if unreviewed:
q = q.filter(change_table.c.hidden==False, change_table.c.reviewed==False)
try:
return q.order_by(change_table.c.number).all()
except sqlalchemy.orm.exc.NoResultFound:
return []
def getRevision(self, key): def getRevision(self, key):
try: try:
return self.session().query(Revision).filter_by(key=key).one() return self.session().query(Revision).filter_by(key=key).one()

View File

@ -1,4 +1,5 @@
# Copyright 2014 OpenStack Foundation # Copyright 2014 OpenStack Foundation
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
@ -74,11 +75,12 @@ This Screen
<v> Toggle the reviewed flag for the currently selected change. <v> Toggle the reviewed flag for the currently selected change.
""" """
def __init__(self, app, project_key): def __init__(self, app, query, query_desc=None, unreviewed=True):
super(ChangeListView, self).__init__(urwid.Pile([])) super(ChangeListView, self).__init__(urwid.Pile([]))
self.app = app self.app = app
self.project_key = project_key self.query = query
self.unreviewed = True self.query_desc = query_desc or query
self.unreviewed = unreviewed
self.change_rows = {} self.change_rows = {}
self.listbox = urwid.ListBox(urwid.SimpleFocusListWalker([])) self.listbox = urwid.ListBox(urwid.SimpleFocusListWalker([]))
self.header = ChangeListHeader() self.header = ChangeListHeader()
@ -92,14 +94,11 @@ This Screen
def refresh(self): def refresh(self):
unseen_keys = set(self.change_rows.keys()) unseen_keys = set(self.change_rows.keys())
with self.app.db.getSession() as session: with self.app.db.getSession() as session:
project = session.getProject(self.project_key) lst = session.getChanges(self.query, self.unreviewed)
self.project_name = project.name
if self.unreviewed: if self.unreviewed:
self.title = u'Unreviewed changes in %s' % project.name self.title = u'Unreviewed changes in %s' % self.query_desc
lst = project.unreviewed_changes
else: else:
self.title = u'Open changes in %s' % project.name self.title = u'Open changes in %s' % self.query_desc
lst = project.open_changes
self.app.status.update(title=self.title) self.app.status.update(title=self.title)
i = 0 i = 0
for change in lst: for change in lst:
@ -112,8 +111,8 @@ This Screen
row.update(change) row.update(change)
unseen_keys.remove(change.key) unseen_keys.remove(change.key)
i += 1 i += 1
if project.changes: if lst:
self.header.update(project.changes[0]) self.header.update(lst[0])
for key in unseen_keys: for key in unseen_keys:
row = self.change_rows[key] row = self.change_rows[key]
self.listbox.body.remove(row) self.listbox.body.remove(row)

View File

@ -1,4 +1,5 @@
# Copyright 2014 OpenStack Foundation # Copyright 2014 OpenStack Foundation
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
@ -29,7 +30,8 @@ class ProjectRow(urwid.Button):
return True return True
def __init__(self, project, callback=None): def __init__(self, project, callback=None):
super(ProjectRow, self).__init__('', on_press=callback, user_data=project.key) super(ProjectRow, self).__init__('', on_press=callback,
user_data=(project.key, project.name))
self.project_key = project.key self.project_key = project.key
name = urwid.Text(project.name) name = urwid.Text(project.name)
name.set_wrap_mode('clip') name.set_wrap_mode('clip')
@ -121,8 +123,12 @@ This Screen
ret = project.subscribed ret = project.subscribed
return ret return ret
def onSelect(self, button, project_key): def onSelect(self, button, data):
self.app.changeScreen(view_change_list.ChangeListView(self.app, project_key)) project_key, project_name = data
self.app.changeScreen(view_change_list.ChangeListView(
self.app,
"project_key:%s status:open" % project_key,
project_name, unreviewed=True))
def keypress(self, size, key): def keypress(self, size, key):
if key=='l': if key=='l':