Skip to content

Commit

Permalink
fix(SearchIssuesRequest): wrap label in quotes if it contains spaces (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Aug 31, 2023
1 parent 6c8215d commit c40c6b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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

0 comments on commit c40c6b8

Please sign in to comment.