def addmetadata()

in hgext/hgmo/__init__.py [0:0]


def addmetadata(repo, ctx, d, onlycheap=False):
    """Add changeset metadata for hgweb templates."""
    description = encoding.fromlocal(ctx.description())

    def bugsgen(_context):
        """Generator for bugs list"""
        for bug in commitparser.parse_bugs(description):
            bug = pycompat.bytestr(bug)
            yield {
                b"no": bug,
                b"url": b"https://bugzilla.mozilla.org/show_bug.cgi?id=%s" % bug,
            }

    def reviewersgen(_context):
        """Generator for reviewers list"""
        for reviewer in commitparser.parse_reviewers(description):
            yield {
                b"name": reviewer,
                b"revset": b"reviewer(%s)" % reviewer,
            }

    def backoutsgen(_context):
        """Generator for backouts list"""
        backouts = commitparser.parse_backouts(description)
        if backouts:
            for node in backouts[0]:
                try:
                    bctx = scmutil.revsymbol(repo, node)
                    yield {b"node": bctx.hex()}
                except error.RepoLookupError:
                    pass

    d[b"reviewers"] = templateutil.mappinggenerator(reviewersgen)
    d[b"bugs"] = templateutil.mappinggenerator(bugsgen)
    d[b"backsoutnodes"] = templateutil.mappinggenerator(backoutsgen)

    # Repositories can define which TreeHerder repository they are associated
    # with.
    treeherder = repo.ui.config(b"mozilla", b"treeherder_repo")
    if treeherder:
        d[b"treeherderrepourl"] = (
            b"https://treeherder.mozilla.org/jobs?repo=%s" % treeherder
        )
        d[b"treeherderrepo"] = treeherder

        push = repo.pushlog.pushfromchangeset(ctx)
        # Don't print Perfherder link on non-publishing repos (like Try)
        # because the previous push likely has nothing to do with this
        # push.
        # Changeset on autoland are in the phase 'draft' until they get merged
        # to mozilla-central.
        if (
            push
            and push.nodes
            and (
                repo.ui.configbool(b"phases", b"publish", True)
                or treeherder == b"autoland"
            )
        ):
            lastpushhead = repo[push.nodes[0]].hex()
            d[b"perfherderurl"] = (
                b"https://treeherder.mozilla.org/perf.html#/compare?"
                b"originalProject=%s&"
                b"originalRevision=%s&"
                b"newProject=%s&"
                b"newRevision=%s"
            ) % (treeherder, push.nodes[-1], treeherder, lastpushhead)

    # If this changeset was converted from another one and we know which repo
    # it came from, add that metadata.
    convertrevision = ctx.extra().get(b"convert_revision")
    if convertrevision:
        sourcerepo = repo.ui.config(b"hgmo", b"convertsource")
        if sourcerepo:
            d[b"convertsourcepath"] = sourcerepo
            d[b"convertsourcenode"] = convertrevision

    # Did the push to this repo included extra data about the automated landing
    # system used?
    # We omit the key if it has no value so that the 'json' filter function in
    # the map file will return null for the key's value.  Otherwise the filter
    # will return a JSON empty string, even for False-y values like None.
    landingsystem = ctx.extra().get(b"moz-landing-system")
    if landingsystem:
        d[b"landingsystem"] = landingsystem

    git_commit = ctx.extra().get(b"git_commit")
    if git_commit:
        d[b"git_commit"] = git_commit

        git_repo_url = repo.ui.config(
            b"mozilla",
            b"git_repo_url",
            b"https://github.com/mozilla-firefox/firefox",
        )
        if git_repo_url:
            d[b"git_repo_url"] = git_repo_url

    if onlycheap:
        return

    # Obtain the Gecko/app version/milestone.
    #
    # We could probably only do this if the repo is a known app repo (by
    # looking at the initial changeset). But, path based lookup is relatively
    # fast, so just do it. However, we need this in the "onlycheap"
    # section because resolving manifests is relatively slow and resolving
    # several on changelist pages may add seconds to page load times.
    try:
        fctx = repo.filectx(b"config/milestone.txt", changeid=ctx.node())
        lines = fctx.data().splitlines()
        lines = [l for l in lines if not l.startswith(b"#") and l.strip()]

        if lines:
            d[b"milestone"] = lines[0].strip()
    except error.LookupError:
        pass

    backout_node = get_backoutbynode(b"hgmo", repo, ctx)
    if backout_node is not None:
        d[b"backedoutbynode"] = backout_node