On Thursday, December 14, 2017 at 7:13:25 AM UTC-8, Florian Schricker wrote:
>
> Hi!
>
>
> I am looking for a way to show the current ticket version number in the 
> ticket description using a macro. Something like [[TicketVersion]] printing 
> "18" for a ticket w/ the current version being 18 would suit my needs just 
> fine.
>
> Has anybody ever written such a thing and is willing to share?
>
>
> Thank you and kind regards,
> Florian Schricker
>


Hi Florian,

Macros like the one you have requested are pretty easy to write if you are 
familiar with Python. Take a look at (1).

Please try the following (also attached in TicketVersionMacro.py)

[BOF]

# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.

from trac.ticket import Ticket
from trac.util import as_int
from trac.util.html import tag
from trac.wiki.formatter import MacroError
from trac.wiki.macros import WikiMacroBase

"""Ticket version macro for Trac 1.0.16"""

revision = '1.0'
author = 'Edgewall Trac'
url = 'https://trac.edgewall.org/wiki/WikiMacros'


class TicketVersionMacro(WikiMacroBase):
    """Display ticket version inline.

    Can be used from the ticket page to show the version of the ticket:
    {{{
    [[TicketVersion]]
    }}}

    Can be used anywhere that accepts wiki content when specifying a
    ticket id:
    {{{
    [[TicketVersion(1)]]
    }}}
    """

    def expand_macro(self, formatter, name, content, args=None):
        content = content or ''
        if not content:  # [[TicketVersion]] from ticket page
            realm = formatter.resource.realm
            rid = formatter.resource.id
            if realm == 'ticket' and rid:
                tid = rid
            else:
                raise MacroError("Invalid realm (%s) and/or resource id 
(%s) "
                                 "for macro call with no arguments."
                                 % (realm, rid))
        else:  # [[TicketVersion(N)]] from any wiki content
            content = content.strip()
            tid = as_int(content, default=None)
            if tid is None:
                raise MacroError("'%s' is not a valid ticket id." % content)
        ticket = Ticket(self.env, tid)
        return tag.span(ticket['version'], class_='trac-ticket-version')


[EOF]

It should work with 1.0.11+, but would need to be adapted for a least some 
early 1.0.x versions because MacroError (2) was used.

Deploy the single file plugin as described in (3). Let me know if you have 
any questions.

- Ryan

(1) https://trac.edgewall.org/wiki/WikiMacros#DevelopingCustomMacros
(2) https://trac.edgewall.org/ticket/12359
(3) https://trac.edgewall.org/wiki/TracPlugins#InstallingaTracplugin

-- 
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 trac-users+unsubscr...@googlegroups.com.
To post to this group, send email to trac-users@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.

from trac.ticket import Ticket
from trac.util import as_int
from trac.util.html import tag
from trac.wiki.formatter import MacroError
from trac.wiki.macros import WikiMacroBase

"""Ticket version macro for Trac 1.0.16"""

revision = '1.0'
author = 'Edgewall Trac'
url = 'https://trac.edgewall.org/wiki/WikiMacros'


class TicketVersionMacro(WikiMacroBase):
    """Display ticket version inline.

    Can be used from the ticket page to show the version of the ticket:
    {{{
    [[TicketVersion]]
    }}}

    Can be used anywhere that accepts wiki content when specifying a
    ticket id:
    {{{
    [[TicketVersion(1)]]
    }}}
    """

    def expand_macro(self, formatter, name, content, args=None):
        content = content or ''
        if not content:  # [[TicketVersion]] from ticket page
            realm = formatter.resource.realm
            rid = formatter.resource.id
            if realm == 'ticket' and rid:
                tid = rid
            else:
                raise MacroError("Invalid realm (%s) and/or resource id (%s) "
                                 "for macro call with no arguments."
                                 % (realm, rid))
        else:  # [[TicketVersion(N)]] from any wiki content
            content = content.strip()
            tid = as_int(content, default=None)
            if tid is None:
                raise MacroError("'%s' is not a valid ticket id." % content)
        ticket = Ticket(self.env, tid)
        return tag.span(ticket['version'], class_='trac-ticket-version')

Reply via email to