def reviewer_match()

in libmozdata/patchanalysis.py [0:0]


def reviewer_match(short_name, bugzilla_reviewers, cc_list, reviewer_cache={}):
    if short_name in reviewer_cache:
        if reviewer_cache[short_name] not in bugzilla_reviewers:
            warnings.warn(
                "Reviewer "
                + reviewer_cache[short_name]
                + " is not in the list of reviewers on Bugzilla ("
                + ", ".join(sorted(bugzilla_reviewers))
                + ").",
                stacklevel=3,
            )

        return reviewer_cache[short_name]

    found = set()
    bugzilla_users = []

    # Check if we can find the reviewer in the list of reviewers from the bug.
    for bugzilla_name in bugzilla_reviewers:
        if bugzilla_name.startswith(short_name):
            found.add(bugzilla_name)

    if len(found) == 0:
        # Otherwise, check if we can find him/her in the CC list.
        found |= set(
            [
                entry["email"]
                for entry in cc_list
                if short_name_match(short_name, entry["real_name"], entry["email"])
            ]
        )

    if len(found) == 0:
        # Otherwise, find matching users on Bugzilla.
        def user_handler(u):
            bugzilla_users.append(u)

        INCLUDE_FIELDS = ["email", "real_name"]

        BugzillaUser(
            search_strings="match="
            + short_name
            + "&include_fields="
            + ",".join(INCLUDE_FIELDS),
            user_handler=user_handler,
        ).wait()
        for bugzilla_name in bugzilla_reviewers:
            BugzillaUser(
                bugzilla_name, include_fields=INCLUDE_FIELDS, user_handler=user_handler
            ).wait()

        found |= set(
            [
                user["email"]
                for user in bugzilla_users
                if short_name_match(short_name, user["real_name"], user["email"])
            ]
        )

    if len(found) == 0:
        # Otherwise, check if we can find him/her in the CC list with a relaxed matching algorithm.
        found |= set(
            [
                entry["email"]
                for entry in cc_list
                if short_name_match(
                    short_name, entry["real_name"], entry["email"], False
                )
            ]
        )

    # We should always find a matching reviewer name.
    # If we're unable to find it, add a static entry in the
    # reviewer_cache dict or find a new clever way to retrieve it.
    if len(found) == 0:
        warnings.warn("Reviewer " + short_name + " could not be found.", stacklevel=3)
        return None

    for elem in found:
        if elem not in bugzilla_reviewers:
            warnings.warn(
                "Reviewer "
                + elem
                + " is not in the list of reviewers on Bugzilla ("
                + ", ".join(sorted(bugzilla_reviewers))
                + ").",
                stacklevel=3,
            )

    assert len(found) <= 1, (
        "Too many matching reviewers (" + ", ".join(found) + ") found for " + short_name
    )

    assert short_name not in reviewer_cache
    reviewer_cache[short_name] = found.pop()
    return reviewer_cache[short_name]