Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate long summaries #44

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0](releases/tag/v2.1.0) - Unreleased

### Fixed

- #39 Truncate long summaries.

### Compatibility

- Python 3.8+

## [2.0.0](releases/tag/v2.0.0) - 2022-11-07

### Breaking Changes
Expand Down
933 changes: 437 additions & 496 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sarif-tools"
version = "2.0.0"
version = "2.1.0"
description = "SARIF tools"
authors = ["Microsoft"]
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions sarif/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Top-level version information for sarif-tools.
"""

import importlib.metadata


Expand Down
8 changes: 5 additions & 3 deletions sarif/operations/upgrade_filter_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def upgrade_filter_file(old_filter_file, output_file):
exclude_patterns,
) = _load_blame_filter_file(old_filter_file)
new_filter_definition = {
"description": filter_description
if filter_description
else f"Migrated from {os.path.basename(old_filter_file)}",
"description": (
filter_description
if filter_description
else f"Migrated from {os.path.basename(old_filter_file)}"
),
"configuration": {"default-include": True, "check-line-number": True},
}
if include_patterns:
Expand Down
44 changes: 32 additions & 12 deletions sarif/sarif_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import datetime
import os
import re
import textwrap
from typing import Dict, Iterator, List, Optional, Tuple

from sarif import sarif_file_utils
from sarif.filter.general_filter import GeneralFilter
from sarif.filter.filter_stats import FilterStats

# SARIF severity levels as per
# https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html#_Toc141790898
SARIF_SEVERITIES = ["error", "warning", "note"]

BASIC_RECORD_ATTRIBUTES = [
Expand Down Expand Up @@ -62,7 +65,7 @@ def _count_records_by_issue_code(records, severity) -> List[Tuple]:
code_to_count = {}
for record in records:
if record["Severity"] == severity:
code = combine_code_and_description(record)
code = record["Code"].strip()
code_to_count[code] = code_to_count.get(code, 0) + 1
return sorted(code_to_count.items(), key=lambda x: x[1], reverse=True)

Expand All @@ -86,12 +89,25 @@ def combine_code_and_description(record: dict) -> str:
Combine code and description fields into one string.
"""
(code, description) = (record["Code"], record["Description"])
if code and description:
return f"{code.strip()} {description.strip()}"
if code:
return code.strip()
if description:
return description.strip()
if "\n" in description:
description = description[: description.index("\n")]
description = description.strip()
if description:
if len(description) > 120:
shorter_description = textwrap.shorten(
description, width=103, placeholder="..."
)
if len(shorter_description) < 40:
description = description[:100] + "..."
else:
description = shorter_description
if code:
return f"{code.strip()} {description}"
else:
return description
elif code:
return code.strip()
return "<NONE>"


Expand Down Expand Up @@ -139,9 +155,11 @@ def __init__(self, sarif_file_object, run_index, run_data):
filter_date = conversion_driver["properties"].get("processed", None)
self._filter.rehydrate_filter_stats(
dehydrated_filter_stats,
datetime.datetime.fromisoformat(filter_date)
if filter_date
else None,
(
datetime.datetime.fromisoformat(filter_date)
if filter_date
else None
),
)

def init_path_prefix_stripping(self, autotrim=False, path_prefixes=None):
Expand Down Expand Up @@ -311,9 +329,11 @@ def result_to_record(self, result, include_blame_info=False):
"Line": line_number,
"Severity": severity,
"Code": error_id,
"Description": message[len(error_id) + 1].strip()
if message.startswith(error_id) and len(message) > len(error_id) + 1
else message,
"Description": (
message[len(error_id) + 1].strip()
if message.startswith(error_id) and len(message) > len(error_id) + 1
else message
),
}
if include_blame_info:
record["Author"] = _get_author_mail_from_blame_info(
Expand Down