From d80bdeaaad469319714e70463656c1d7165c6588 Mon Sep 17 00:00:00 2001 From: SemyonP Date: Sun, 13 Nov 2022 12:12:27 +0200 Subject: [PATCH 1/3] Added Environments API - GetAll list only feature --- .../Clients/DeploymentEnvironmentsClient.cs | 105 ++++++++++++++++++ Octokit/Clients/IRepositoriesClient.cs | 2 + .../IRepositoryDeployEnvironmentsClient.cs | 61 ++++++++++ Octokit/Clients/RepositoriesClient.cs | 10 ++ Octokit/Helpers/ApiUrls.cs | 21 ++++ .../Models/Response/DeploymentEnvironment.cs | 71 ++++++++++++ .../DeploymentEnvironmentsResponse.cs | 42 +++++++ 7 files changed, 312 insertions(+) create mode 100644 Octokit/Clients/DeploymentEnvironmentsClient.cs create mode 100644 Octokit/Clients/IRepositoryDeployEnvironmentsClient.cs create mode 100644 Octokit/Models/Response/DeploymentEnvironment.cs create mode 100644 Octokit/Models/Response/DeploymentEnvironmentsResponse.cs diff --git a/Octokit/Clients/DeploymentEnvironmentsClient.cs b/Octokit/Clients/DeploymentEnvironmentsClient.cs new file mode 100644 index 0000000000..21803b1356 --- /dev/null +++ b/Octokit/Clients/DeploymentEnvironmentsClient.cs @@ -0,0 +1,105 @@ +using Octokit.Models.Response; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Octokit +{ + /// + /// A client for GitHub's Repository Deployment Environments API. + /// Gets Deployment Environments. + /// + /// + /// See the Repository Deployment Environments API documentation for more information. + /// + public class DeploymentEnvironmentsClient : ApiClient, IRepositoryDeployEnvironmentsClient + { + /// + /// Instantiates a new GitHub Repository Deployments API client. + /// + /// An API connection + public DeploymentEnvironmentsClient(IApiConnection apiConnection) : base(apiConnection) { } + + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + [ManualRoute("GET", "/repos/{owner}/{repo}/environments")] + public Task GetAll(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + + return GetAll(owner, name, null); + } + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The Id of the repository + [ManualRoute("GET", "/repositories/{id}/environments")] + public Task GetAll(long repositoryId) + { + return GetAll(repositoryId, null); + } + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + /// Paging options + [ManualRoute("GET", "/repos/{owner}/{repo}/environments")] + public Task GetAll(string owner, string name, ApiOptions options) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + + Dictionary prms = null; + if (options != null) + { + prms = new Dictionary(); + prms.Add("per_page", options.PageSize.ToString()); + prms.Add("page", options.StartPage.ToString()); + } + + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(owner, name), prms); + } + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// Repository ID + /// Paging options + [ManualRoute("GET", "/repositories/{id}/environments")] + public Task GetAll(long repositoryId, ApiOptions options) + { + Dictionary prms = null; + if (options != null) + { + prms = new Dictionary(); + prms.Add("per_page", options.PageSize.ToString()); + prms.Add("page", options.StartPage.ToString()); + } + + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(repositoryId), prms); + } + } +} diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index b8976f0706..9d88202910 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -344,6 +344,8 @@ public interface IRepositoriesClient /// IDeploymentsClient Deployment { get; } + IRepositoryDeployEnvironmentsClient Environment { get; } + /// /// Client for GitHub's Repository Statistics API. /// Note that the GitHub API uses caching on these endpoints, diff --git a/Octokit/Clients/IRepositoryDeployEnvironmentsClient.cs b/Octokit/Clients/IRepositoryDeployEnvironmentsClient.cs new file mode 100644 index 0000000000..a67a2270a9 --- /dev/null +++ b/Octokit/Clients/IRepositoryDeployEnvironmentsClient.cs @@ -0,0 +1,61 @@ +using Octokit.Models.Response; +using System.Threading.Tasks; + +namespace Octokit +{ + /// + /// A client for GitHub's Repository Deployment Environments API. + /// Gets Deployment Environments. + /// + /// + /// See the Repository Deployment Environments API documentation for more information. + /// + public interface IRepositoryDeployEnvironmentsClient + { + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + Task GetAll(string owner, string name); + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The Id of the repository + Task GetAll(long repositoryId); + + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + /// Paging options + Task GetAll(string owner, string name, ApiOptions options); + + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// Repository ID + /// Paging options + Task GetAll(long repositoryId, ApiOptions options); + } +} diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index 9d208a7b6f..29e8563bf2 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using System.Diagnostics.CodeAnalysis; using System.Collections.Generic; +using System.Net.Http.Headers; namespace Octokit { @@ -27,6 +28,7 @@ public RepositoriesClient(IApiConnection apiConnection) : base(apiConnection) Collaborator = new RepoCollaboratorsClient(apiConnection); Statistics = new StatisticsClient(apiConnection); Deployment = new DeploymentsClient(apiConnection); + Environment = new DeploymentEnvironmentsClient(apiConnection); PullRequest = new PullRequestsClient(apiConnection); Comment = new RepositoryCommentsClient(apiConnection); Commit = new RepositoryCommitsClient(apiConnection); @@ -538,6 +540,14 @@ public Task> GetAllForOrg(string organization, ApiOpti /// public IDeploymentsClient Deployment { get; private set; } + /// + /// Client for GitHub's Repository Deployment Environments API + /// + /// + /// See the Deployments Environments API documentation for more details + /// + public IRepositoryDeployEnvironmentsClient Environment { get; private set; } + /// /// Client for GitHub's Repository Statistics API /// Note that the GitHub API uses caching on these endpoints, diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index c6d57704ca..88a8b95270 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -2478,6 +2478,17 @@ public static Uri Deployments(string owner, string name) return "repos/{0}/{1}/deployments".FormatUri(owner, name); } + /// + /// Returns the for the Deployment Environments API for the given repository. + /// + /// Owner of the repository + /// Name of the repository + /// The for the Deployments API for the given repository. + public static Uri DeploymentEnvironments(string owner, string name) + { + return "repos/{0}/{1}/environments".FormatUri(owner, name); + } + /// /// Returns the for the Deployment Statuses API for the given deployment. /// @@ -3137,6 +3148,16 @@ public static Uri Deployments(long repositoryId) return "repositories/{0}/deployments".FormatUri(repositoryId); } + /// + /// Returns the for the Deployment Environments API for the given repository. + /// + /// The Id of the repository + /// The for the Deployments API for the given repository. + public static Uri DeploymentEnvironments(long repositoryId) + { + return "repositories/{0}/environments".FormatUri(repositoryId); + } + /// /// Returns the for the Deployment Statuses API for the given deployment. /// diff --git a/Octokit/Models/Response/DeploymentEnvironment.cs b/Octokit/Models/Response/DeploymentEnvironment.cs new file mode 100644 index 0000000000..88fe5c97d7 --- /dev/null +++ b/Octokit/Models/Response/DeploymentEnvironment.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; +using System.Text; +using System.Globalization; + +namespace Octokit.Models.Response +{ + [SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", + Justification = "People can use fully qualified names if they want to use both.")] + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class DeploymentEnvironment + { + public DeploymentEnvironment() { } + + public DeploymentEnvironment(int id, string nodeID, string name, string url, string htmlUrl, DateTimeOffset createdAt, DateTimeOffset updatedAt) + { + Id = id; + NodeId = nodeID; + Name = name; + Url = url; + HtmlUrl = htmlUrl; + CreatedAt = createdAt; + UpdatedAt = updatedAt; + } + + /// + /// Id of this deployment environment + /// + public int Id { get; private set; } + + /// + /// GraphQL Node Id + /// + public string NodeId{ get; private set; } + + /// + /// Environment Name + /// + public string Name { get; private set; } + + /// + /// Environment API URL + /// + public string Url { get; private set; } + + /// + /// Environment HTML URL + /// + public string HtmlUrl { get; private set; } + + /// + /// Date and time that the environment was created. + /// + public DateTimeOffset CreatedAt { get; private set; } + + /// + /// Date and time that the environment was updated. + /// + public DateTimeOffset UpdatedAt { get; private set; } + + internal string DebuggerDisplay + { + get + { + return string.Format(CultureInfo.InvariantCulture, "Name: {0}, CreatedAt: {1}", Name, CreatedAt); + } + } + } +} \ No newline at end of file diff --git a/Octokit/Models/Response/DeploymentEnvironmentsResponse.cs b/Octokit/Models/Response/DeploymentEnvironmentsResponse.cs new file mode 100644 index 0000000000..e4ef6bd412 --- /dev/null +++ b/Octokit/Models/Response/DeploymentEnvironmentsResponse.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; +using System.Text; +using System.Globalization; + +namespace Octokit.Models.Response +{ + [SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", + Justification = "People can use fully qualified names if they want to use both.")] + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class DeploymentEnvironmentsResponse + { + public DeploymentEnvironmentsResponse() { } + + + public DeploymentEnvironmentsResponse(int totalCount, IReadOnlyList environments) + { + TotalCount = totalCount; + Environments = environments; + } + + /// + /// The total count of secrets for the repository + /// + public int TotalCount { get; private set; } + + /// + /// The list of secrets for the repository + /// + public IReadOnlyList Environments { get; private set; } + + internal string DebuggerDisplay + { + get + { + return string.Format(CultureInfo.InvariantCulture, "Count: {0}", TotalCount); + } + } + } +} \ No newline at end of file From 3bedad93b5b8567934860ffe313aa4b4ba342549 Mon Sep 17 00:00:00 2001 From: Semyon Pivovarov Date: Wed, 16 Nov 2022 23:41:44 +0200 Subject: [PATCH 2/3] added test (debug required) --- .../Clients/IObservableEnvironmentsClient.cs | 57 +++++++ .../Clients/IObservableRepositoriesClient.cs | 10 +- .../Clients/ObservableEnvironmentsClient.cs | 52 +++++++ .../Clients/ObservableRepositoriesClient.cs | 12 +- .../Clients/EnvironmentsClientTests.cs | 147 ++++++++++++++++++ .../Clients/DeploymentEnvironmentsClient.cs | 34 ++-- Octokit/Clients/IRepositoriesClient.cs | 8 +- 7 files changed, 305 insertions(+), 15 deletions(-) create mode 100644 Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs create mode 100644 Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs create mode 100644 Octokit.Tests/Clients/EnvironmentsClientTests.cs diff --git a/Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs b/Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs new file mode 100644 index 0000000000..7d2fa632fc --- /dev/null +++ b/Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs @@ -0,0 +1,57 @@ +using Octokit.Models.Response; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit.Reactive +{ + public interface IObservableEnvironmentsClient + { + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + IObservable GetAll(string owner, string name); + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The Id of the repository + IObservable GetAll(long repositoryId); + + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// The owner of the repository + /// The name of the repository + /// Paging options + IObservable GetAll(string owner, string name, ApiOptions options); + + + /// + /// Gets all the environments for the specified repository. Any user with pull access + /// to a repository can view deployments. + /// + /// + /// https://docs.github.com/en/rest/deployments/environments#list-environments + /// + /// Repository ID + /// Paging options + IObservable GetAll(long repositoryId, ApiOptions options); + } +} diff --git a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs index 169f86e2ac..ecae4629d3 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs @@ -226,10 +226,18 @@ public interface IObservableRepositoriesClient /// Client for GitHub's Repository Deployments API /// /// - /// See the Collaborators API documentation for more details + /// See the Deployments API documentation for more details /// IObservableDeploymentsClient Deployment { get; } + /// + /// Client for GitHub's Repository Envirinments API + /// + /// + /// See the Envirinments API documentation for more details + /// + IObservableEnvironmentsClient Environments { get; } + /// /// Client for GitHub's Repository Statistics API. /// Note that the GitHub API uses caching on these endpoints, diff --git a/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs b/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs new file mode 100644 index 0000000000..9ed9a25b08 --- /dev/null +++ b/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs @@ -0,0 +1,52 @@ +using System; +using System.Reactive.Threading.Tasks; +using Octokit.Models.Response; +using Octokit.Reactive.Internal; + +namespace Octokit.Reactive.Clients +{ + public class ObservableEnvironmentsClient : IObservableEnvironmentsClient + { + readonly IRepositoryDeployEnvironmentsClient _client; + readonly IConnection _connection; + + public ObservableEnvironmentsClient(IGitHubClient client) + { + Ensure.ArgumentNotNull(client, nameof(client)); + + _client = client.Repository.Environment; + _connection = client.Connection; + } + + public IObservable GetAll(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + + return GetAll(owner, name, ApiOptions.None); + } + + public IObservable GetAll(long repositoryId) + { + return GetAll(repositoryId, ApiOptions.None); + } + + public IObservable GetAll(string owner, string name, ApiOptions options) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + Ensure.ArgumentNotNull(options, nameof(options)); + + return _connection.GetAndFlattenAllPages( + ApiUrls.DeploymentEnvironments(owner, name), options); + } + + public IObservable GetAll(long repositoryId, ApiOptions options) + { + Ensure.ArgumentNotNull(options, nameof(options)); + + return _connection.GetAndFlattenAllPages( + ApiUrls.DeploymentEnvironments(repositoryId), options); + } + } +} diff --git a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs index 199fc6fa00..93e57835af 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -27,6 +27,7 @@ public ObservableRepositoriesClient(IGitHubClient client) Forks = new ObservableRepositoryForksClient(client); Collaborator = new ObservableRepoCollaboratorsClient(client); Deployment = new ObservableDeploymentsClient(client); + Environments = new ObservableEnvironmentsClient(client); Statistics = new ObservableStatisticsClient(client); PullRequest = new ObservablePullRequestsClient(client); Branch = new ObservableRepositoryBranchesClient(client); @@ -347,10 +348,19 @@ public IObservable GetAllForOrg(string organization, ApiOptions opti /// Client for GitHub's Repository Deployments API /// /// - /// See the Collaborators API documentation for more details + /// See the Deployments API documentation for more details /// public IObservableDeploymentsClient Deployment { get; private set; } + + /// + /// Client for GitHub's Repository Environments API + /// + /// + /// See the Environments API documentation for more details + /// + public IObservableEnvironmentsClient Environments { get; private set; } + /// /// Client for GitHub's Repository Statistics API /// Note that the GitHub API uses caching on these endpoints, diff --git a/Octokit.Tests/Clients/EnvironmentsClientTests.cs b/Octokit.Tests/Clients/EnvironmentsClientTests.cs new file mode 100644 index 0000000000..144f13dc56 --- /dev/null +++ b/Octokit.Tests/Clients/EnvironmentsClientTests.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NSubstitute; +using Octokit; +using Octokit.Models.Response; +using Octokit.Tests; +using Xunit; + + +namespace Octokit.Tests.Clients +{ + public class EnvironmentsClientTests + { + public class TheGetAllMethod + { + const string name = "name"; + const string owner = "owner"; + const long repositoryId = 1; + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new DeploymentEnvironmentsClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAll(null, name)); + await Assert.ThrowsAsync(() => client.GetAll(owner, null)); + await Assert.ThrowsAsync(() => client.GetAll(owner, name, null)); + + await Assert.ThrowsAsync(() => client.GetAll(repositoryId, null)); + } + + [Fact] + public async Task EnsuresNonEmptyArguments() + { + var client = new DeploymentEnvironmentsClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAll("", name)); + await Assert.ThrowsAsync(() => client.GetAll(owner, "")); + } + + [Theory] + [InlineData(" ")] + [InlineData("\n")] + [InlineData("\t")] + [InlineData(" ")] + [InlineData("\n\r")] + public async Task EnsuresNonWhitespaceArguments(string whitespace) + { + var client = new DeploymentEnvironmentsClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAll(whitespace, name)); + await Assert.ThrowsAsync(() => client.GetAll(owner, whitespace)); + } + + [Fact] + public async Task RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new DeploymentEnvironmentsClient(connection); + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); + + var res = await client.GetAll(owner, name); + + connection.Received() + .Get( + Arg.Is(u => u.ToString() == expectedUrl)); + } + + [Fact] + public async Task RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new DeploymentEnvironmentsClient(connection); + var expectedUrl = string.Format("repositories/{0}/environments", repositoryId); + + await client.GetAll(repositoryId); + + connection.Received() + .Get(Arg.Is(u => u.ToString() == expectedUrl), + null); + } + + [Fact] + public async Task RequestsCorrectUrlWithApiOptions() + { + var connection = Substitute.For(); + var client = new DeploymentEnvironmentsClient(connection); + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); + + var options = new ApiOptions + { + PageSize = 10, + StartPage = 1 + }; + + await client.GetAll(owner, name, options); + + connection.Received() + .Get(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(u => u["per_page"] == "10" && u["page"] == "1")); + } + + [Fact] + public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions() + { + var connection = Substitute.For(); + var client = new DeploymentEnvironmentsClient(connection); + var expectedUrl = string.Format("repositories/{0}/environments", repositoryId); + + var options = new ApiOptions + { + PageSize = 10, + StartPage = 1 + }; + + await client.GetAll(repositoryId, options); + + connection.Received() + .Get(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(u => u["per_page"] == "10" && u["page"] == "1")); + } + + [Fact] + public void RequestsCorrectUrlWithPreviewAcceptHeaders() + { + var connection = Substitute.For(); + var client = new DeploymentEnvironmentsClient(connection); + var expectedUrl = string.Format("repos/{0}/{1}/environments", owner, name); + + client.GetAll(owner, name); + connection.Received() + .Get(Arg.Is(u => u.ToString() == expectedUrl), + null); + } + } + + public class TheCtor + { + [Fact] + public void EnsuresNonNullArguments() + { + Assert.Throws(() => new DeploymentEnvironmentsClient(null)); + } + } + } +} diff --git a/Octokit/Clients/DeploymentEnvironmentsClient.cs b/Octokit/Clients/DeploymentEnvironmentsClient.cs index 21803b1356..b5a245fa1c 100644 --- a/Octokit/Clients/DeploymentEnvironmentsClient.cs +++ b/Octokit/Clients/DeploymentEnvironmentsClient.cs @@ -1,6 +1,7 @@ using Octokit.Models.Response; using System.Collections.Generic; using System.Threading.Tasks; +using System.Xml.Linq; namespace Octokit { @@ -14,7 +15,7 @@ namespace Octokit public class DeploymentEnvironmentsClient : ApiClient, IRepositoryDeployEnvironmentsClient { /// - /// Instantiates a new GitHub Repository Deployments API client. + /// Instantiates a new GitHub Repository Environments API client. /// /// An API connection public DeploymentEnvironmentsClient(IApiConnection apiConnection) : base(apiConnection) { } @@ -22,7 +23,7 @@ public DeploymentEnvironmentsClient(IApiConnection apiConnection) : base(apiConn /// /// Gets all the environments for the specified repository. Any user with pull access - /// to a repository can view deployments. + /// to a repository can view environments. /// /// /// https://docs.github.com/en/rest/deployments/environments#list-environments @@ -35,12 +36,12 @@ public Task GetAll(string owner, string name) Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); - return GetAll(owner, name, null); + return GetAll(owner, name, ApiOptions.None); } /// /// Gets all the environments for the specified repository. Any user with pull access - /// to a repository can view deployments. + /// to a repository can view environments. /// /// /// https://docs.github.com/en/rest/deployments/environments#list-environments @@ -49,12 +50,12 @@ public Task GetAll(string owner, string name) [ManualRoute("GET", "/repositories/{id}/environments")] public Task GetAll(long repositoryId) { - return GetAll(repositoryId, null); + return GetAll(repositoryId, ApiOptions.None); } /// /// Gets all the environments for the specified repository. Any user with pull access - /// to a repository can view deployments. + /// to a repository can view environments. /// /// /// https://docs.github.com/en/rest/deployments/environments#list-environments @@ -67,21 +68,25 @@ public Task GetAll(string owner, string name, Ap { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + Ensure.ArgumentNotNull(options, nameof(options)); Dictionary prms = null; - if (options != null) + if (options != ApiOptions.None) { prms = new Dictionary(); prms.Add("per_page", options.PageSize.ToString()); prms.Add("page", options.StartPage.ToString()); } - return ApiConnection.Get(ApiUrls.DeploymentEnvironments(owner, name), prms); + if (prms != null) + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(owner, name), prms); + else + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(owner, name)); } /// /// Gets all the environments for the specified repository. Any user with pull access - /// to a repository can view deployments. + /// to a repository can view environments. /// /// /// https://docs.github.com/en/rest/deployments/environments#list-environments @@ -91,15 +96,20 @@ public Task GetAll(string owner, string name, Ap [ManualRoute("GET", "/repositories/{id}/environments")] public Task GetAll(long repositoryId, ApiOptions options) { + Ensure.ArgumentNotNull(options, nameof(options)); + Dictionary prms = null; - if (options != null) + if (options != ApiOptions.None) { prms = new Dictionary(); prms.Add("per_page", options.PageSize.ToString()); prms.Add("page", options.StartPage.ToString()); } - return ApiConnection.Get(ApiUrls.DeploymentEnvironments(repositoryId), prms); + if (prms != null) + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(repositoryId), prms); + else + return ApiConnection.Get(ApiUrls.DeploymentEnvironments(repositoryId)); } } -} +} \ No newline at end of file diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index 9d88202910..5ccb62c744 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -344,6 +344,12 @@ public interface IRepositoriesClient /// IDeploymentsClient Deployment { get; } + /// + /// Client for GitHub's Repository Environments API + /// + /// + /// See the Environments API documentation for more details + /// IRepositoryDeployEnvironmentsClient Environment { get; } /// @@ -741,4 +747,4 @@ public interface IRepositoriesClient [ManualRoute("GET", "/repositories/{id}/codeowners/errors")] Task GetAllCodeOwnersErrors(long repositoryId); } -} +} \ No newline at end of file From 7d82bcb03d4c8cafbcdc63f94910de118213d15c Mon Sep 17 00:00:00 2001 From: SemyonP Date: Thu, 17 Nov 2022 11:32:46 +0200 Subject: [PATCH 3/3] Feature complete --- Octokit.Reactive/Clients/IObservableRepositoriesClient.cs | 2 +- ....cs => IObservableRepositoryDeployEnvironmentsClient.cs} | 2 +- Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs | 2 +- Octokit.Reactive/Clients/ObservableRepositoriesClient.cs | 4 ++-- Octokit.Tests/Clients/EnvironmentsClientTests.cs | 6 ++---- Octokit/Models/Request/ApiOptions.cs | 4 +++- 6 files changed, 10 insertions(+), 10 deletions(-) rename Octokit.Reactive/Clients/{IObservableEnvironmentsClient.cs => IObservableRepositoryDeployEnvironmentsClient.cs} (97%) diff --git a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs index ecae4629d3..8e22e4622e 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs @@ -236,7 +236,7 @@ public interface IObservableRepositoriesClient /// /// See the Envirinments API documentation for more details /// - IObservableEnvironmentsClient Environments { get; } + IObservableRepositoryDeployEnvironmentsClient Environment { get; } /// /// Client for GitHub's Repository Statistics API. diff --git a/Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryDeployEnvironmentsClient.cs similarity index 97% rename from Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs rename to Octokit.Reactive/Clients/IObservableRepositoryDeployEnvironmentsClient.cs index 7d2fa632fc..4ac2577dec 100644 --- a/Octokit.Reactive/Clients/IObservableEnvironmentsClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryDeployEnvironmentsClient.cs @@ -6,7 +6,7 @@ namespace Octokit.Reactive { - public interface IObservableEnvironmentsClient + public interface IObservableRepositoryDeployEnvironmentsClient { /// /// Gets all the environments for the specified repository. Any user with pull access diff --git a/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs b/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs index 9ed9a25b08..f100b0708c 100644 --- a/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs +++ b/Octokit.Reactive/Clients/ObservableEnvironmentsClient.cs @@ -5,7 +5,7 @@ namespace Octokit.Reactive.Clients { - public class ObservableEnvironmentsClient : IObservableEnvironmentsClient + public class ObservableEnvironmentsClient : IObservableRepositoryDeployEnvironmentsClient { readonly IRepositoryDeployEnvironmentsClient _client; readonly IConnection _connection; diff --git a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs index 93e57835af..bbef7b3968 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -27,7 +27,7 @@ public ObservableRepositoriesClient(IGitHubClient client) Forks = new ObservableRepositoryForksClient(client); Collaborator = new ObservableRepoCollaboratorsClient(client); Deployment = new ObservableDeploymentsClient(client); - Environments = new ObservableEnvironmentsClient(client); + Environment = new ObservableEnvironmentsClient(client); Statistics = new ObservableStatisticsClient(client); PullRequest = new ObservablePullRequestsClient(client); Branch = new ObservableRepositoryBranchesClient(client); @@ -359,7 +359,7 @@ public IObservable GetAllForOrg(string organization, ApiOptions opti /// /// See the Environments API documentation for more details /// - public IObservableEnvironmentsClient Environments { get; private set; } + public IObservableRepositoryDeployEnvironmentsClient Environment { get; private set; } /// /// Client for GitHub's Repository Statistics API diff --git a/Octokit.Tests/Clients/EnvironmentsClientTests.cs b/Octokit.Tests/Clients/EnvironmentsClientTests.cs index 144f13dc56..629845ddac 100644 --- a/Octokit.Tests/Clients/EnvironmentsClientTests.cs +++ b/Octokit.Tests/Clients/EnvironmentsClientTests.cs @@ -77,8 +77,7 @@ public async Task RequestsCorrectUrlWithRepositoryId() await client.GetAll(repositoryId); connection.Received() - .Get(Arg.Is(u => u.ToString() == expectedUrl), - null); + .Get(Arg.Is(u => u.ToString() == expectedUrl)); } [Fact] @@ -130,8 +129,7 @@ public void RequestsCorrectUrlWithPreviewAcceptHeaders() client.GetAll(owner, name); connection.Received() - .Get(Arg.Is(u => u.ToString() == expectedUrl), - null); + .Get(Arg.Is(u => u.ToString() == expectedUrl)); } } diff --git a/Octokit/Models/Request/ApiOptions.cs b/Octokit/Models/Request/ApiOptions.cs index cdc3cbf379..3dbfd1d40a 100644 --- a/Octokit/Models/Request/ApiOptions.cs +++ b/Octokit/Models/Request/ApiOptions.cs @@ -7,9 +7,11 @@ namespace Octokit [DebuggerDisplay("{DebuggerDisplay,nq}")] public class ApiOptions { + private static ApiOptions emptyOptions = new ApiOptions(); + public static ApiOptions None { - get { return new ApiOptions(); } + get { return emptyOptions; } } ///