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(SearchIssuesRequest): wrap label in quotes if it contains spaces #2767

Merged
merged 3 commits into from
Aug 31, 2023
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
6 changes: 3 additions & 3 deletions Octokit.Tests/Clients/SearchClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ public void TestingTheLabelsQualifier()

connection.Received().Get<SearchIssuesResult>(
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+label:\"bug\""));
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+label:bug"));
}

[Fact]
Expand All @@ -1013,7 +1013,7 @@ public void TestingTheLabelsQualifier_Multiple()

connection.Received().Get<SearchIssuesResult>(
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+label:\"bug\"+label:\"feature\""));
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+label:bug+label:feature"));
}

[Fact]
Expand Down Expand Up @@ -1596,7 +1596,7 @@ public void TestingTheRepoAndUserAndLabelQualifier()
connection.Received().Get<SearchIssuesResult>(
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
Arg.Is<Dictionary<string, string>>(d => d["q"] ==
"something+label:\"bug\"+user:alfhenrik+repo:octokit/octokit.net"));
"something+label:bug+user:alfhenrik+repo:octokit/octokit.net"));
}
}

Expand Down
10 changes: 7 additions & 3 deletions Octokit.Tests/Models/SearchIssuesRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ public void HandlesLabelsAttributeCorrectly()
var request = new SearchIssuesRequest("test");
Assert.DoesNotContain(request.MergedQualifiers(), x => x.Contains("label:"));

request.Labels = new[] { "label1", "label 2" };
Assert.Contains("label:\"label1\"", request.MergedQualifiers());
Assert.Contains("label:\"label 2\"", request.MergedQualifiers());
request.Labels = new[] { "label1", "label 2", "label3,label 4" };

var qualifiers = request.MergedQualifiers();

Assert.Contains("label:label1", qualifiers);
Assert.Contains("label:\"label 2\"", qualifiers);
Assert.Contains("label:label3,\"label 4\"", qualifiers);
}

[Fact]
Expand Down
25 changes: 24 additions & 1 deletion Octokit/Models/Request/SearchIssuesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public override IReadOnlyList<string> MergedQualifiers()

if (Labels != null)
{
parameters.AddRange(Labels.Select(label => string.Format(CultureInfo.InvariantCulture, "label:\"{0}\"", label)));
parameters.AddRange(Labels.Select(label => string.Format(CultureInfo.InvariantCulture, "label:{0}", FormatLabel(label))));
}

if (No.HasValue)
Expand Down Expand Up @@ -428,6 +428,29 @@ internal string DebuggerDisplay
return string.Format(CultureInfo.InvariantCulture, "Search: {0} {1}", Term, string.Join(" ", MergedQualifiers()));
}
}

/// <summary>
/// Wrap the label in quotes if it contains a space
/// </summary>
/// <param name="label">The input label</param>
/// <returns></returns>
private static string FormatLabel(string label)
{
// singleLabel
if (!label.Contains(","))
{
return WrapInQuotesIfContainsSpace(label);
}

if (label.Contains(" "))
{
return string.Join(",", label.Split(',').Select(WrapInQuotesIfContainsSpace));
}

return label;

string WrapInQuotesIfContainsSpace(string l) => l.Contains(" ") ? $"\"{l}\"" : l;
}
}

public enum IssueSearchSort
Expand Down