Skip to content

Commit

Permalink
add support for multiple extension filters (#2019)
Browse files Browse the repository at this point in the history
* add support for multiple extension filters

* update documentation, use ienumerable instead of ilist
  • Loading branch information
ecarlson94 authored and shiftkey committed Oct 3, 2019
1 parent ce5ea64 commit 3e7c70c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
33 changes: 27 additions & 6 deletions Octokit.Tests/Clients/SearchClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,14 +1801,33 @@ public void TestingTheExtensionQualifier()
{
var connection = Substitute.For<IApiConnection>();
var client = new SearchClient(connection);
var request = new SearchCodeRequest("something");
request.Extension = "cs";
var request = new SearchCodeRequest("something")
{
Extensions = new[] { "txt" }
};

client.SearchCode(request);

connection.Received().Get<SearchCodeResult>(
Arg.Is<Uri>(u => u.ToString() == "search/code"),
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+extension:txt"));
}

[Fact]
public void TestingTheExtensionQualifier_Multiple()
{
var connection = Substitute.For<IApiConnection>();
var client = new SearchClient(connection);
var request = new SearchCodeRequest("something")
{
Extensions = new[] { "cs", "lol" }
};

client.SearchCode(request);

connection.Received().Get<SearchCodeResult>(
Arg.Is<Uri>(u => u.ToString() == "search/code"),
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+extension:cs"));
Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+extension:cs+extension:lol"));
}

[Fact]
Expand Down Expand Up @@ -1875,16 +1894,18 @@ public void TestingTheRepoAndPathAndExtensionQualifiers()
{
var connection = Substitute.For<IApiConnection>();
var client = new SearchClient(connection);
var request = new SearchCodeRequest("something", "octokit", "octokit.net");
var request = new SearchCodeRequest("something", "octokit", "octokit.net")
{
Extensions = new[] { "fs", "cs" }
};
request.Path = "tools/FAKE.core";
request.Extension = "fs";

client.SearchCode(request);

connection.Received().Get<SearchCodeResult>(
Arg.Is<Uri>(u => u.ToString() == "search/code"),
Arg.Is<Dictionary<string, string>>(d =>
d["q"] == "something+path:tools/FAKE.core+extension:fs+repo:octokit/octokit.net"));
d["q"] == "something+path:tools/FAKE.core+extension:fs+extension:cs+repo:octokit/octokit.net"));
}

[Fact]
Expand Down
11 changes: 7 additions & 4 deletions Octokit/Models/Request/SearchCodeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ public IEnumerable<CodeInQualifier> In
public string Path { get; set; }

/// <summary>
/// Matches files with a certain extension.
/// Matches files with a certain extensions.
/// </summary>
/// <remarks>
/// https://help.github.com/articles/searching-code#extension
/// </remarks>
public string Extension { get; set; }
public IEnumerable<string> Extensions { get; set; } = new List<string>();

/// <summary>
/// Matches specific file names
Expand Down Expand Up @@ -189,9 +189,12 @@ public override IReadOnlyList<string> MergedQualifiers()
parameters.Add(string.Format(CultureInfo.InvariantCulture, "path:{0}", Path));
}

if (Extension.IsNotBlank())
if (Extensions.Any())
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "extension:{0}", Extension));
foreach (var extension in Extensions)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "extension:{0}", extension));
}
}

if (FileName.IsNotBlank())
Expand Down
2 changes: 1 addition & 1 deletion docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var request = new SearchCodeRequest("auth")
Path = "app/assets",

// we may want to restrict the file based on file extension
Extension = "json",
Extensions = new[] { "json", "sql" },

// restrict search to a specific file name
FileName = "app.json",
Expand Down

0 comments on commit 3e7c70c

Please sign in to comment.