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

add support for multiple extension filters #2019

Merged
merged 2 commits into from
Oct 3, 2019
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
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