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

Fix #1915: Allow result message to be truncated #1932

Merged
merged 5 commits into from
Jun 25, 2020
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
2 changes: 2 additions & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SARIF Package Release History (SDK, Driver, Converters, and Multitool)

## **v2.3.1** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.1) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.1) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.1) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.1)
* FEATURE: Allow result messages to be truncated for display. [#1915](https://github.com/microsoft/sarif-sdk/issues/1915)
* BUGFIX: Rebase URI command now honors `--insert` and `--remove` arguments for injecting or eliding optional data (such as region snippets).

## **v2.3.1** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.1) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.1) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.1) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.1)
Expand Down
6 changes: 5 additions & 1 deletion src/Sarif/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static string GetMessageText(this Result result, ReportingDescriptor rule
return GetMessageText(result, rule, concise: false);
}

public static string GetMessageText(this Result result, ReportingDescriptor rule, bool concise = false)
public static string GetMessageText(this Result result, ReportingDescriptor rule, bool concise = false, int maxLength = 120)
{
if (result == null)
{
Expand Down Expand Up @@ -333,6 +333,10 @@ public static string GetMessageText(this Result result, ReportingDescriptor rule
if (concise)
{
text = GetFirstSentence(text);
if (text.Length > maxLength)
{
text = text.Substring(0, maxLength) + "\u2026"; // \u2026 is Unicode "horizontal ellipsis".
}
}

return text;
Expand Down
25 changes: 25 additions & 0 deletions src/Test.UnitTests.Sarif/SarifExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,30 @@ public void SarifExtensions_Result_GetMessageText_Concise()
string actual = result.GetMessageText(rule, concise: true);
Assert.Equal(expected, actual);
}

[Fact]
public void SarifExtensions_Result_GetMessageText_Concise_Truncated()
{
var result = new Result
{
Message = new Message
{
Id = "ruleStr1"
}
};

var rule = new ReportingDescriptor
{
MessageStrings = new Dictionary<string, MultiformatMessageString>
{
["ruleStr1"] = new MultiformatMessageString { Text = "First sentence is very long. Second sentence." }
}
};

const string Expected = "First sentence is ve\u2026"; // \u2026 is Unicode "horizontal ellipsis".
int maxLength = Expected.Length - 1; // The -1 is for the ellipsis character.
string actual = result.GetMessageText(rule, concise: true, maxLength);
Assert.Equal(Expected, actual);
}
}
}