def make_summary()

in sync/notify/bugs.py [0:0]


def make_summary(test_results: list[ResultsEntry],
                 prefix: str,
                 max_length: int = 255,
                 max_tests: int = 3,
                 ) -> str:
    """Construct a summary for the bugs based on the test results.

    The approach here is to start building the string up using the
    LengthCappedStringBuilder and when we get to an optional part check
    if we have the capacity to add that part in, otherwise use an
    alternative.

    :param test_results: List of (test_id, subtest, result)
    :param prefix: String prefix to use at the start of the summary
    :param max_length: Maximum length of the summary to create
    :param max_tests: Maximum number of tests names to include in the
                      output
    :returns: String containing a constructed bug summary
    """
    if len(prefix) > max_length:
        raise ValueError("Prefix is too long")

    # Start with the prefix
    summary = LengthCappedStringBuilder(max_length)
    summary.append(prefix)

    # If we can fit some of the common path prefix, add that
    split_names, common_test_prefix = get_common_prefix(item[0] for item in test_results)
    joiner = " in "
    if not summary.has_capacity(len(joiner) + len(common_test_prefix[0]) + 1):
        return summary.get()

    # Keep adding as much of the common path prefix as possible
    summary.append(joiner)
    for path_part in common_test_prefix:
        if not summary.append("%s/" % path_part):
            return summary.get()

    test_names = ["/".join(item[len(common_test_prefix):]) for item in split_names]

    # If there's a single test name add that and we're done
    if len(test_names) == 1:
        summary.append(test_names[0])
        return summary.get()

    # If there are multiple test names, add up to max_tests of those names
    # and a suffix
    prefix = " ["
    # suffix is ", and N others]", N is at most len(test_results) so reserve that many
    # characters
    tests_remaining = len(test_names)
    suffix_length = len(", and  others]") + len(str(tests_remaining))
    if summary.has_capacity(len(test_names[0]) + len(prefix) + suffix_length):
        summary.append(prefix)
        summary.append(test_names[0])
        tests_remaining -= 1
        for test_name in test_names[1:max_tests]:
            if summary.has_capacity(2 + len(test_name) + suffix_length):
                summary.append(", %s" % test_name)
                tests_remaining -= 1
        if tests_remaining > 0:
            summary.append(", and %s others]" % tests_remaining)
        else:
            summary.append("]")
    else:
        # If we couldn't fit any test names in try just adding the number of tests
        summary.append(" [%s tests]" % tests_remaining)
    return summary.get()