Skip to content

Commit

Permalink
[FEAT]: Search based on repository custom property (#2936)
Browse files Browse the repository at this point in the history
search based on repository custom property
  • Loading branch information
colbylwilliams committed Jun 21, 2024
1 parent 9a3177e commit 1053a20
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
23 changes: 23 additions & 0 deletions Octokit.Tests/Clients/SearchClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,29 @@ public void TestingTheTopicsQualifierWithTwoOrLessTopics()

Assert.Contains("topics:<=2", request.MergedQualifiers());
}

[Fact]
public void TestingTheCustomPropertiesQualifier()
{
var request = new SearchRepositoriesRequest("github");
request.CustomProperties = new Dictionary<string, string> { { "custom", "value" } };

Assert.Contains("props.custom:value", request.MergedQualifiers());
}

[Fact]
public void TestingMultipleCustomPropertiesQualifiers()
{
var request = new SearchRepositoriesRequest("github");
request.CustomProperties = new Dictionary<string, string> {
{ "custom_one", "value_one" },
{ "custom_two", "value_two" }
};

var merged = request.MergedQualifiers();
Assert.Contains("props.custom_one:value_one", merged);
Assert.Contains("props.custom_two:value_two", merged);
}
}

public class TheSearchIssuesMethod
Expand Down
10 changes: 9 additions & 1 deletion Octokit.Tests/Models/SearchRepositoryRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Collections.Generic;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;
Expand Down Expand Up @@ -41,5 +41,13 @@ public void LicenseUsesParameterTranslation()
var result = request.MergedQualifiers();
Assert.Contains(result, x => string.Equals(x, "license:apache-2.0"));
}

[Fact]
public void CustomPropertiesPrependsProps()
{
var request = new SearchRepositoriesRequest() { CustomProperties = new Dictionary<string, string> { { "name", "value" } } };
var result = request.MergedQualifiers();
Assert.Contains(result, x => string.Equals(x, "props.name:value"));
}
}
}
67 changes: 46 additions & 21 deletions Octokit/Models/Request/SearchRepositoriesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Octokit
{
/// <summary>
/// Searching Repositories
/// http://developer.github.com/v3/search/#search-repositories
/// https://docs.github.com/rest/search/search#search-repositories
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class SearchRepositoriesRequest : BaseSearchRequest
Expand All @@ -33,7 +33,7 @@ public SearchRepositoriesRequest(string term)
}

/// <summary>
/// For https://help.github.com/articles/searching-repositories#sorting
/// For https://docs.github.com/search-github/getting-started-with-searching-on-github/sorting-search-results
/// Optional Sort field. One of stars, forks, or updated. If not provided, results are sorted by best match.
/// </summary>
public RepoSearchSort? SortField { get; set; }
Expand All @@ -46,9 +46,9 @@ public override string Sort
private IEnumerable<InQualifier> _inQualifier;

/// <summary>
/// The in qualifier limits what fields are searched. With this qualifier you can restrict the search to just the repository name, description, README, or any combination of these.
/// The in qualifier limits what fields are searched. With this qualifier you can restrict the search to just the repository name, description, README, or any combination of these.
/// Without the qualifier, only the name and description are searched.
/// https://help.github.com/articles/searching-repositories#search-in
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-repository-name-description-or-contents-of-the-readme-file
/// </summary>
public IEnumerable<InQualifier> In
{
Expand All @@ -65,74 +65,83 @@ public IEnumerable<InQualifier> In

/// <summary>
/// Filters repositories based on the number of forks, and/or whether forked repositories should be included in the results at all.
/// https://help.github.com/articles/searching-repositories#forks
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-number-of-forks
/// </summary>
public Range Forks { get; set; }

/// <summary>
/// Filters repositories based whether forked repositories should be included in the results at all.
/// Defaults to ExcludeForks
/// https://help.github.com/articles/searching-repositories#forks
/// https://docs.github.com/search-github/searching-on-github/searching-in-forks
/// </summary>
public ForkQualifier? Fork { get; set; }

/// <summary>
/// The size qualifier finds repository's that match a certain size (in kilobytes).
/// https://help.github.com/articles/searching-repositories#size
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-repository-size
/// </summary>
public Range Size { get; set; }

/// <summary>
/// Searches repositories based on the language they’re written in.
/// https://help.github.com/articles/searching-repositories#languages
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-language
/// </summary>
public Language? Language { get; set; }

/// <summary>
/// Searches repositories based on the number of stars.
/// https://help.github.com/articles/searching-repositories#stars
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-number-of-stars
/// </summary>
public Range Stars { get; set; }

/// <summary>
/// Limits searches to a specific user or repository.
/// https://help.github.com/articles/searching-repositories#users-organizations-and-repositories
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-within-a-users-or-organizations-repositories
/// </summary>
public string User { get; set; }

/// <summary>
/// Filters repositories based on times of creation.
/// https://help.github.com/articles/searching-repositories#created-and-last-updated
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-when-a-repository-was-created-or-last-updated
/// </summary>
public DateRange Created { get; set; }

/// <summary>
/// Filters repositories based on when they were last updated.
/// https://help.github.com/articles/searching-repositories#created-and-last-updated
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-when-a-repository-was-created-or-last-updated
/// </summary>
public DateRange Updated { get; set; }

/// <summary>
/// Filters repositories based on license
/// https://help.github.com/articles/searching-repositories#search-by-license
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-license
/// </summary>
public RepoSearchLicense? License { get; set; }

/// <summary>
/// Filters whether archived repositories should be included (true) or not (false).
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-based-on-whether-a-repository-is-archived
/// </summary>
public bool? Archived { get; set; }

/// <summary>
/// Filters on whether repositories are tagged with the given topic.
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-topic
/// </summary>
public string Topic { get; set; }

/// <summary>
/// Filters on the number of topics that a repository is associated with.
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-number-of-topics
/// </summary>
public Range Topics { get; set; }

/// <summary>
/// Filters on repositories based on custom properties.
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-based-on-repository-custom-property
/// </summary>
public IDictionary<string, string> CustomProperties { get; set; }

public override IReadOnlyList<string> MergedQualifiers()
{
var parameters = new List<string>();
Expand Down Expand Up @@ -200,6 +209,13 @@ public override IReadOnlyList<string> MergedQualifiers()
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "license:{0}", License.ToParameter()));
}
if (CustomProperties != null)
{
foreach (var customProperty in CustomProperties)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "props.{0}:{1}", customProperty.Key, customProperty.Value));
}
}

return parameters;
}
Expand All @@ -214,8 +230,8 @@ internal string DebuggerDisplay
}

/// <summary>
/// https://help.github.com/articles/searching-repositories#search-in
/// The in qualifier limits what fields are searched. With this qualifier you can restrict the search to just the
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-repository-name-description-or-contents-of-the-readme-file
/// The in qualifier limits what fields are searched. With this qualifier you can restrict the search to just the
/// repository name, description, README, or any combination of these.
/// </summary>
public enum InQualifier
Expand All @@ -226,6 +242,9 @@ public enum InQualifier
[Parameter(Value = "description")]
Description,

[Parameter(Value = "topics")]
Topics,

[Parameter(Value = "readme")]
Readme
}
Expand Down Expand Up @@ -256,7 +275,7 @@ public Range(int minSize, int maxSize)
}

/// <summary>
/// Matches repositories with regards to the size <param name="size"/>
/// Matches repositories with regards to the size <param name="size"/>
/// We will use the <param name="op"/> to see what operator will be applied to the size qualifier
/// </summary>
public Range(int size, SearchQualifierOperator op)
Expand Down Expand Up @@ -323,7 +342,7 @@ public override string ToString()

/// <summary>
/// helper class in generating the date range values for the date qualifier e.g.
/// https://help.github.com/articles/searching-repositories#created-and-last-updated
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-when-a-repository-was-created-or-last-updated
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class DateRange
Expand Down Expand Up @@ -532,7 +551,7 @@ public override string ToString()

/// <summary>
/// Languages that can be searched on in GitHub
/// https://help.github.com/articles/searching-repositories#languages
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-language
/// </summary>
public enum Language
{
Expand Down Expand Up @@ -897,7 +916,7 @@ public enum Language

/// <summary>
/// Licenses than can be searched on GitHub
/// https://help.github.com/articles/searching-repositories#search-by-license
/// https://docs.github.com/search-github/searching-on-github/searching-for-repositories#search-by-license
/// </summary>
public enum RepoSearchLicense
{
Expand Down Expand Up @@ -989,7 +1008,8 @@ public enum RepoSearchLicense

/// <summary>
/// sorting repositories by any of below
/// https://help.github.com/articles/searching-repositories#sorting
/// https://docs.github.com/rest/search/search?#search-repositories
/// https://docs.github.com/search-github/getting-started-with-searching-on-github/sorting-search-results
/// </summary>
public enum RepoSearchSort
{
Expand All @@ -1004,14 +1024,19 @@ public enum RepoSearchSort
[Parameter(Value = "forks")]
Forks,
/// <summary>
/// search by number of help-wanted-issues
/// </summary>
[Parameter(Value = "help-wanted-issues")]
HelpWantedIssues,
/// <summary>
/// search by last updated
/// </summary>
[Parameter(Value = "updated")]
Updated
}

/// <summary>
/// https://help.github.com/articles/searching-repositories#forks
/// https://docs.github.com/search-github/searching-on-github/searching-in-forks
/// Specifying whether forked repositories should be included in results or not.
/// </summary>
public enum ForkQualifier
Expand Down

0 comments on commit 1053a20

Please sign in to comment.