From d5ffdb74b0ee8c19048abd48d61868e5286d5a3e Mon Sep 17 00:00:00 2001 From: capdiem Date: Sun, 27 Aug 2023 01:12:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(SearchIssuesRequest):=20?= =?UTF-8?q?wrap=20label=20in=20quotes=20if=20it=20contains=20spaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Octokit.Tests/Clients/SearchClientTests.cs | 6 ++--- .../Models/SearchIssuesRequestTests.cs | 10 ++++--- Octokit/Models/Request/SearchIssuesRequest.cs | 27 ++++++++++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Octokit.Tests/Clients/SearchClientTests.cs b/Octokit.Tests/Clients/SearchClientTests.cs index d6a481766e..2ade8ef144 100644 --- a/Octokit.Tests/Clients/SearchClientTests.cs +++ b/Octokit.Tests/Clients/SearchClientTests.cs @@ -998,7 +998,7 @@ public void TestingTheLabelsQualifier() connection.Received().Get( Arg.Is(u => u.ToString() == "search/issues"), - Arg.Is>(d => d["q"] == "something+label:\"bug\"")); + Arg.Is>(d => d["q"] == "something+label:bug")); } [Fact] @@ -1013,7 +1013,7 @@ public void TestingTheLabelsQualifier_Multiple() connection.Received().Get( Arg.Is(u => u.ToString() == "search/issues"), - Arg.Is>(d => d["q"] == "something+label:\"bug\"+label:\"feature\"")); + Arg.Is>(d => d["q"] == "something+label:bug+label:feature")); } [Fact] @@ -1596,7 +1596,7 @@ public void TestingTheRepoAndUserAndLabelQualifier() connection.Received().Get( Arg.Is(u => u.ToString() == "search/issues"), Arg.Is>(d => d["q"] == - "something+label:\"bug\"+user:alfhenrik+repo:octokit/octokit.net")); + "something+label:bug+user:alfhenrik+repo:octokit/octokit.net")); } } diff --git a/Octokit.Tests/Models/SearchIssuesRequestTests.cs b/Octokit.Tests/Models/SearchIssuesRequestTests.cs index dfa5e55a69..1b6219fcd2 100644 --- a/Octokit.Tests/Models/SearchIssuesRequestTests.cs +++ b/Octokit.Tests/Models/SearchIssuesRequestTests.cs @@ -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] diff --git a/Octokit/Models/Request/SearchIssuesRequest.cs b/Octokit/Models/Request/SearchIssuesRequest.cs index 391e04130a..0b151138c0 100644 --- a/Octokit/Models/Request/SearchIssuesRequest.cs +++ b/Octokit/Models/Request/SearchIssuesRequest.cs @@ -328,7 +328,7 @@ public override IReadOnlyList 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) @@ -428,6 +428,31 @@ internal string DebuggerDisplay return string.Format(CultureInfo.InvariantCulture, "Search: {0} {1}", Term, string.Join(" ", MergedQualifiers())); } } + + /// + /// Wrap the label in quotes if it contains a space + /// + /// The input label + /// + private static string FormatLabel(string label) + { + var singleLabel = !label.Contains(","); + var containSpace = label.Contains(" "); + + if (singleLabel) + { + return WrapInQuotesIfContainsSpace(label); + } + + if (containSpace) + { + return string.Join(",", label.Split(',').Select(WrapInQuotesIfContainsSpace)); + } + + return label; + + string WrapInQuotesIfContainsSpace(string l) => l.Contains(" ") ? $"\"{l}\"" : l; + } } public enum IssueSearchSort From f5790e25b78fd85829afd903e8eb6d1d2604fa3f Mon Sep 17 00:00:00 2001 From: capdiem Date: Wed, 30 Aug 2023 00:43:49 +0800 Subject: [PATCH 2/2] Update SearchIssuesRequest.cs --- Octokit/Models/Request/SearchIssuesRequest.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Octokit/Models/Request/SearchIssuesRequest.cs b/Octokit/Models/Request/SearchIssuesRequest.cs index 0b151138c0..0ee61a2875 100644 --- a/Octokit/Models/Request/SearchIssuesRequest.cs +++ b/Octokit/Models/Request/SearchIssuesRequest.cs @@ -436,15 +436,13 @@ internal string DebuggerDisplay /// private static string FormatLabel(string label) { - var singleLabel = !label.Contains(","); - var containSpace = label.Contains(" "); - - if (singleLabel) + // singleLabel + if (!label.Contains(",")) { return WrapInQuotesIfContainsSpace(label); } - if (containSpace) + if (label.Contains(" ")) { return string.Join(",", label.Split(',').Select(WrapInQuotesIfContainsSpace)); }