On Tuesday, December 11, 2018 at 5:48:05 PM UTC-5, Peter Suter wrote:
>
> Hello 
>
> On 11.12.2018 22:22, Clemens Feige wrote: 
> > Hello 
> > 
> > How can one display a list of all ticket numbers which are mentioned 
> > in the current ticket (i.e. in description text, ticket comments, 
> > ticket fields and ticket custom fields)? 
> > 
> > It might be something like the [[TicketQuery]] macro, but it shall 
> > work only on the current ticket, instead on all tickets. 
> > 
> > Background: 
> > Relations between tickets are important. If for example the user views 
> > ticket X, then he or she may also want to know about related tickets Y 
> > and Z. The related tickets are mentioned somewhere deep in the long 
> > description text of the current ticket. Now the user likes to see a 
> > distilled list of all mentioned tickets. 
> > Once again please note that I am talking about data extraction within 
> > the same ticket, not globally across all tickets. 
> > 
> > I am aware of the PageTicketsMacro: 
> >  "wiki macro that expands to a TicketQuery of tickets mentioned on the 
> > current wiki page." 
> >  https://trac-hacks.org/wiki/PageTicketsMacro 
> > Unfortunately the PageTicketsMacro does not work on tickets, only on 
> > wiki pages. Otherwise it is more or less what I want. 
> > 
> > Do you have any suggestions? 
>
>
> Maybe something like this: 
>
>
> import re 
>
> from trac.core import * 
> from trac.util.html  import tag 
> from trac.web.api import IRequestFilter 
>
> author = "Lucid" 
> version = "1.0 ($Rev$)" 
> license = "BSD" 
> url = "https://trac-hacks.org/wiki/MentionedTickets"; 
>
>
> class MentionedTickets(Component): 
>      """List all tickets mentioned in the current ticket.""" 
>
>      implements(IRequestFilter) 
>
>      tickets_re = re.compile('(?:#|(?:ticket:|bug:))(\d+)') 
>
>      def pre_process_request(self, req, handler): 
>          return handler 
>
>      def post_process_request(self, req, template, data, content_type): 
>          path = req.path_info 
>          if path.startswith('/ticket/'): 
>              if data and 'ticket' in data and 'fields' in data: 
>                  self._append_related_tickets(req, data) 
>          return template, data, content_type 
>
>      def _append_related_tickets(self, req, data): 
>          rendered = '' 
>          ticket = data['ticket'] 
>          comments = [c[4] for c in ticket.get_changelog() 
>                           if c[2] == 'comment'] 
>          text_fields = [field['name'] for field in ticket.fields 
>                                       if field['type'] == 'textarea'] 
>          values = [ticket[name] for name in text_fields] + comments 
>          mentions = [int(match) for value in values 
>                                 if value is not None 
>                                 for match in 
> MentionedTickets.tickets_re.findall(value)] 
>          if mentions: 
>              ticket.values['Mentioned Tickets'] = True # Activates field 
>              results = [] 
>              for id in sorted(set(mentions)): 
>                  label = '#%s' % (id,) 
>                  href = req.href.ticket(id) 
>                  link = tag.a(label, href=href) 
>                  results.append(link) 
>              rendered = tag.span(*[e for pair in zip(results, [' '] * 
> len(results)) for e in pair][:-1]) 
>          data['fields'].append({ 
>              'name': 'Mentioned Tickets', 
>              'rendered': rendered, 
>              'type': 'textarea', # Full row 
>          }) 
>
>
> (It's not a macro. It just adds the mentioned tickets to the box.) 
>
> Cheers! 
>

Nice that you didn't even need to depend on Genshi template filters!

Two suggestions for anyone taking this further:
1. Search ticket custom fields of type Text with format=wiki
2. Include "issue:" TracLinks in the regex: 
https://trac.edgewall.org/browser/tags/trac-1.2.3/tracopt/ticket/commit_updater.py?marks=140#L139

For more complex formatting of the ticket list, the user has the option to 
add the ticket numbers as script data and format the list using JavaScript.

- Ryan

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to