mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-07 20:30:41 +00:00
Add ToParamater extension method on Enums
To extract the Value field from the Parameter Attribute
This commit is contained in:
@@ -192,7 +192,7 @@ namespace Octokit.Tests.Clients
|
||||
//get repos where the Description contains rails and user/org is 'github'
|
||||
var request = new SearchRepositoriesRequest("rails");
|
||||
request.Sort = RepoSearchSort.Forks;
|
||||
|
||||
|
||||
client.SearchRepo(request);
|
||||
|
||||
connection.Received().GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
|
||||
@@ -244,7 +244,7 @@ namespace Octokit.Tests.Clients
|
||||
connection.Received().GetAll<Issue>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
|
||||
Arg.Is<Dictionary<string, string>>(d =>
|
||||
d["sort"] == "Comments"));
|
||||
d["sort"] == "comments"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -254,15 +254,30 @@ namespace Octokit.Tests.Clients
|
||||
var client = new SearchClient(connection);
|
||||
var request = new SearchIssuesRequest("something");
|
||||
request.Sort = IssueSearchSort.Updated;
|
||||
request.Order = SortDirection.Descending;
|
||||
request.Order = SortDirection.Ascending;
|
||||
|
||||
client.SearchIssues(request);
|
||||
|
||||
connection.Received().GetAll<Issue>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
|
||||
Arg.Is<Dictionary<string, string>>(d =>
|
||||
d["sort"] == "updated" &&
|
||||
d["order"] == "asc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingTheDefaultOrderParameter()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new SearchClient(connection);
|
||||
var request = new SearchIssuesRequest("something");
|
||||
|
||||
client.SearchIssues(request);
|
||||
|
||||
connection.Received().GetAll<Issue>(
|
||||
Arg.Is<Uri>(u => u.ToString() == "search/issues"),
|
||||
Arg.Is<Dictionary<string, string>>(d =>
|
||||
d["sort"] == "Updated" &&
|
||||
d["order"] == "Descending"));
|
||||
d["order"] == "desc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Octokit.Internal;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
static class EnumExtensions
|
||||
{
|
||||
public static string ToParameter(this Enum prop)
|
||||
{
|
||||
if (prop == null) return null;
|
||||
|
||||
var member = prop.GetType().GetMember(prop.ToString()).FirstOrDefault();
|
||||
if (member == null) return null;
|
||||
|
||||
var attribute = member.GetCustomAttributes(typeof(ParameterAttribute), false)
|
||||
.Cast<ParameterAttribute>()
|
||||
.FirstOrDefault();
|
||||
|
||||
return attribute != null ? attribute.Value : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Globalization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
@@ -69,7 +70,7 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// https://help.github.com/articles/searching-issues#type
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
|
||||
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
|
||||
public IssueTypeQualifier? Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -233,7 +234,7 @@ namespace Octokit
|
||||
return String.Join("+", parameters);
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
|
||||
[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
|
||||
public IDictionary<string, string> Parameters
|
||||
{
|
||||
get
|
||||
@@ -241,8 +242,8 @@ namespace Octokit
|
||||
var d = new Dictionary<string, string>();
|
||||
d.Add("page", Page.ToString());
|
||||
d.Add("per_page", PerPage.ToString());
|
||||
d.Add("sort", Sort.ToString());
|
||||
d.Add("order", Order.ToString());
|
||||
d.Add("sort", Sort.ToParameter());
|
||||
d.Add("order", Order.ToParameter());
|
||||
var mergedParameters = MergeParameters();
|
||||
d.Add("q", Term + (mergedParameters.IsNotBlank() ? "+" + mergedParameters : ""));
|
||||
return d;
|
||||
@@ -254,14 +255,17 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// search by number of comments
|
||||
/// </summary>
|
||||
[Parameter(Value = "comments")]
|
||||
Comments,
|
||||
/// <summary>
|
||||
/// search by created
|
||||
/// </summary>
|
||||
[Parameter(Value = "created")]
|
||||
Created,
|
||||
/// <summary>
|
||||
/// search by last updated
|
||||
/// </summary>
|
||||
[Parameter(Value = "updated")]
|
||||
Updated
|
||||
}
|
||||
|
||||
|
||||
@@ -117,6 +117,7 @@
|
||||
<Compile Include="Exceptions\LoginAttemptsExceededException.cs" />
|
||||
<Compile Include="Exceptions\RateLimitExceededException.cs" />
|
||||
<Compile Include="Helpers\CollectionExtensions.cs" />
|
||||
<Compile Include="Helpers\EnumExtensions.cs" />
|
||||
<Compile Include="Helpers\IApiPagination.cs" />
|
||||
<Compile Include="Helpers\IReadOnlyPagedCollection.cs" />
|
||||
<Compile Include="Helpers\ModelExtensions.cs" />
|
||||
|
||||
@@ -57,6 +57,9 @@
|
||||
<Compile Include="Clients\SearchClient.cs" />
|
||||
<Compile Include="Clients\ISearchClient.cs" />
|
||||
<Compile Include="Models\Request\BaseSearchRequest.cs" />
|
||||
<Compile Include="Helpers\EnumExtensions.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Models\Request\SearchCodeRequest.cs" />
|
||||
<Compile Include="Models\Request\SearchIssuesRequest.cs" />
|
||||
<Compile Include="Models\Request\SearchQualifierOperator.cs" />
|
||||
|
||||
Reference in New Issue
Block a user