mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-05 11:40:42 +00:00
Merge branch 'search-repos-improved' of https://github.com/hahmed/octokit.net into hahmed-search-repos-improved
This commit is contained in:
@@ -336,7 +336,8 @@ namespace Octokit.Tests.Clients
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
client.SearchRepo(new SearchRepositoriesRequest("something"));
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Any<Dictionary<string, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -351,13 +352,12 @@ namespace Octokit.Tests.Clients
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//check sizes for repos that are greater than 50 MB
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Size = Range.GreaterThan(50);
|
||||
|
||||
request.Size = Range.GreaterThan(1);
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+size:>1"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -368,10 +368,38 @@ namespace Octokit.Tests.Clients
|
||||
//get repos whos stargazers are greater than 500
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Stars = Range.GreaterThan(500);
|
||||
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+stars:>500"));
|
||||
}
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
[Fact]
|
||||
public void TestingTheStarsQualifier_LessThan()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos whos stargazers are less than 500
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Stars = Range.LessThan(500);
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+stars:<500"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheStarsQualifier_LessThanOrEquals()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos whos stargazers are less than 500 or equal to
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Stars = Range.LessThanOrEquals(500);
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+stars:<=500"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -382,10 +410,9 @@ namespace Octokit.Tests.Clients
|
||||
//get repos which has forks that are greater than 50
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Forks = Range.GreaterThan(50);
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+forks:>50"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -394,12 +421,11 @@ namespace Octokit.Tests.Clients
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//search repos that contains rails and forks are included in the search
|
||||
var request = new SearchRepositoriesRequest("rails");
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Fork = ForkQualifier.IncludeForks;
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+fork:IncludeForks"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -410,10 +436,9 @@ namespace Octokit.Tests.Clients
|
||||
//get repos whos language is Ruby
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Language = Language.Ruby;
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+language:Ruby"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -425,8 +450,47 @@ namespace Octokit.Tests.Clients
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.In = new[] { InQualifier.Description };
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Description"));
|
||||
}
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
[Fact]
|
||||
public void TestingTheInQualifier_Name()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.In = new[] { InQualifier.Name };
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Name"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheInQualifier_Readme()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.In = new[] { InQualifier.Readme };
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Readme"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheInQualifier_Multiple()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.In = new[] { InQualifier.Readme, InQualifier.Description, InQualifier.Name };
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Readme,Description,Name"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -437,12 +501,52 @@ namespace Octokit.Tests.Clients
|
||||
//get repos where the search contains 'github' and has been created after year jan 1 2011
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Created = DateRange.GreaterThan(new DateTime(2011, 1, 1));
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+created:>2011-01-01"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheCreatedQualifier_GreaterThanOrEquals()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been created after year jan 1 2011
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Created = DateRange.GreaterThanOrEquals(new DateTime(2011, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+created:>=2011-01-01"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheCreatedQualifier_LessThan()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been created after year jan 1 2011
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Created = DateRange.LessThan(new DateTime(2011, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+created:<2011-01-01"));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TestingTheCreatedQualifier_LessThanOrEquals()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been created after year jan 1 2011
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Created = DateRange.LessThanOrEquals(new DateTime(2011, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+created:<=2011-01-01"));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TestingTheUpdatedQualifier()
|
||||
{
|
||||
@@ -450,11 +554,49 @@ namespace Octokit.Tests.Clients
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been pushed before year jan 1 2013
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Updated = DateRange.LessThan(new DateTime(2013, 1, 1));
|
||||
|
||||
request.Updated = DateRange.GreaterThan(new DateTime(2013, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+pushed:>2013-01-01"));
|
||||
}
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
[Fact]
|
||||
public void TestingTheUpdatedQualifier_GreaterThanOrEquals()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been pushed before year jan 1 2013
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Updated = DateRange.GreaterThanOrEquals(new DateTime(2013, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+pushed:>=2013-01-01"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheUpdatedQualifier_LessThan()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been pushed before year jan 1 2013
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Updated = DateRange.LessThan(new DateTime(2013, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+pushed:<2013-01-01"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheUpdatedQualifier_LessThanOrEquals()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the search contains 'github' and has been pushed before year jan 1 2013
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.Updated = DateRange.LessThanOrEquals(new DateTime(2013, 1, 1));
|
||||
client.SearchRepo(request);
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+pushed:<=2013-01-01"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -462,13 +604,12 @@ namespace Octokit.Tests.Clients
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the Description contains rails and user/org is 'github'
|
||||
var request = new SearchRepositoriesRequest("rails");
|
||||
request.User = "github";
|
||||
|
||||
//get repos where search contains 'github' and user/org is 'github'
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.User = "rails";
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+user:rails"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -476,13 +617,16 @@ namespace Octokit.Tests.Clients
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
//get repos where the Description contains rails and user/org is 'github'
|
||||
var request = new SearchRepositoriesRequest("rails");
|
||||
request.Sort = RepoSearchSort.Forks;
|
||||
|
||||
var request = new SearchRepositoriesRequest("github");
|
||||
request.SortField = RepoSearchSort.Stars;
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
|
||||
connection.Received().Get<SearchRepositoryResult>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/repositories"),
|
||||
Arg.Is<Dictionary<string, string>>(d =>
|
||||
d["q"] == "github" &&
|
||||
d["sort"] == "stars"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,43 +13,24 @@ namespace Octokit
|
||||
/// http://developer.github.com/v3/search/#search-repositories
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{DebuggerDisplay,nq}")]
|
||||
public class SearchRepositoriesRequest
|
||||
public class SearchRepositoriesRequest : BaseSearchRequest
|
||||
{
|
||||
public SearchRepositoriesRequest(string term)
|
||||
: base(term)
|
||||
{
|
||||
Term = term;
|
||||
Page = 1;
|
||||
PerPage = 100;
|
||||
Fork = ForkQualifier.ExcludeForks;
|
||||
Order = SortDirection.Descending;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The search terms. This can be any combination of the supported repository search parameters:
|
||||
/// http://developer.github.com/v3/search/#search-repositories
|
||||
/// </summary>
|
||||
public string Term { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// For https://help.github.com/articles/searching-repositories#sorting
|
||||
/// Optional Sort field. One of stars, forks, or updated. If not provided, results are sorted by best match.
|
||||
/// </summary>
|
||||
public RepoSearchSort? Sort { get; set; }
|
||||
public RepoSearchSort? SortField { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sort order one of asc or desc - the default is desc.
|
||||
/// </summary>
|
||||
public SortDirection Order { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Page of paginated results
|
||||
/// </summary>
|
||||
public int Page { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of items per page
|
||||
/// </summary>
|
||||
public int PerPage { get; set; }
|
||||
public override string Sort
|
||||
{
|
||||
get { return SortField.ToParameter(); }
|
||||
}
|
||||
|
||||
private IEnumerable<InQualifier> _inQualifier;
|
||||
|
||||
@@ -82,7 +63,7 @@ namespace Octokit
|
||||
/// Defaults to ExcludeForks
|
||||
/// https://help.github.com/articles/searching-repositories#forks
|
||||
/// </summary>
|
||||
public ForkQualifier Fork { get; set; }
|
||||
public ForkQualifier? Fork { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The size qualifier finds repository's that match a certain size (in kilobytes).
|
||||
@@ -120,7 +101,7 @@ namespace Octokit
|
||||
/// </summary>
|
||||
public DateRange Updated { get; set; }
|
||||
|
||||
public string MergeParameters()
|
||||
public override IReadOnlyCollection<string> MergedQualifiers()
|
||||
{
|
||||
var parameters = new List<string>();
|
||||
|
||||
@@ -139,7 +120,10 @@ namespace Octokit
|
||||
parameters.Add(String.Format(CultureInfo.InvariantCulture, "forks:{0}", Forks));
|
||||
}
|
||||
|
||||
parameters.Add(String.Format(CultureInfo.InvariantCulture, "fork:{0}", Fork));
|
||||
if (Fork != null)
|
||||
{
|
||||
parameters.Add(String.Format(CultureInfo.InvariantCulture, "fork:{0}", Fork));
|
||||
}
|
||||
|
||||
if (Stars != null)
|
||||
{
|
||||
@@ -165,26 +149,7 @@ namespace Octokit
|
||||
{
|
||||
parameters.Add(String.Format(CultureInfo.InvariantCulture, "pushed:{0}", Updated));
|
||||
}
|
||||
|
||||
return String.Join("+", parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get the params in the correct format...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
|
||||
public IDictionary<string, string> Parameters
|
||||
{
|
||||
get
|
||||
{
|
||||
var d = new System.Collections.Generic.Dictionary<string, string>();
|
||||
d.Add("page", Page.ToString());
|
||||
d.Add("per_page", PerPage.ToString());
|
||||
d.Add("sort", Sort.ToString());
|
||||
d.Add("q", Term + " " + MergeParameters()); //add qualifiers onto the search term
|
||||
return d;
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
internal string DebuggerDisplay
|
||||
@@ -754,14 +719,17 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// search by number of stars
|
||||
/// </summary>
|
||||
[Parameter(Value = "stars")]
|
||||
Stars,
|
||||
/// <summary>
|
||||
/// search by number of forks
|
||||
/// </summary>
|
||||
[Parameter(Value = "forks")]
|
||||
Forks,
|
||||
/// <summary>
|
||||
/// search by last updated
|
||||
/// </summary>
|
||||
[Parameter(Value = "updated")]
|
||||
Updated
|
||||
}
|
||||
|
||||
@@ -774,14 +742,12 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// only search for forked repos
|
||||
/// </summary>
|
||||
[Parameter(Value = "Only")]
|
||||
OnlyForks,
|
||||
/// <summary>
|
||||
/// include forked repos into the search
|
||||
/// </summary>
|
||||
IncludeForks,
|
||||
/// <summary>
|
||||
/// forks are not included in the search (default behaviour)
|
||||
/// </summary>
|
||||
ExcludeForks
|
||||
[Parameter(Value = "True")]
|
||||
IncludeForks
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user