From 23a91a492e22d4d363091b96b2fb79b1d18050f7 Mon Sep 17 00:00:00 2001 From: Lee Boynton Date: Wed, 6 Jul 2022 17:29:21 +0100 Subject: [PATCH] Add method to check if repo vulnerability alerts are enabled (#2453) --- .../Clients/IObservableRepositoriesClient.cs | 11 ++++ .../Clients/ObservableRepositoriesClient.cs | 17 ++++++ .../Clients/RepositoriesClientTests.cs | 11 ++++ .../Clients/RepositoriesClientTests.cs | 52 +++++++++++++++++++ .../ObservableRepositoriesClientTests.cs | 25 +++++++++ Octokit/Clients/IRepositoriesClient.cs | 11 ++++ Octokit/Clients/RepositoriesClient.cs | 26 ++++++++++ Octokit/Helpers/ApiUrls.cs | 11 ++++ 8 files changed, 164 insertions(+) diff --git a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs index d9da62cbc0..dd5c78bc85 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs @@ -72,6 +72,17 @@ public interface IObservableRepositoriesClient /// A IObservable Transfer(long repositoryId, RepositoryTransfer repositoryTransfer); + /// + /// Checks if vulnerability alerts are enabled for the specified repository. + /// + /// + /// See the API documentation for more information. + /// + /// The current owner of the repository + /// The name of the repository + /// A bool indicating if alerts are turned on or not. + IObservable AreVulnerabilityAlertsEnabled(string owner, string name); + /// /// Retrieves the for the specified owner and name. /// diff --git a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs index 9fbb2157b1..80f68d1009 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -151,6 +151,23 @@ public IObservable Transfer(long repositoryId, RepositoryTransfer re return _client.Transfer(repositoryId, repositoryTransfer).ToObservable(); } + /// + /// Checks if vulnerability alerts are enabled for the specified repository. + /// + /// + /// See the API documentation for more information. + /// + /// The current owner of the repository + /// The name of the repository + /// A bool indicating if alerts are turned on or not. + public IObservable AreVulnerabilityAlertsEnabled(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + + return _client.AreVulnerabilityAlertsEnabled(owner, name).ToObservable(); + } + /// /// Retrieves the for the specified owner and name. /// diff --git a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs index cd1fffea94..cd0c7eb64e 100644 --- a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs @@ -2055,4 +2055,15 @@ public async Task TransfersFromUserToOrgWithTeamsById() } } } + + public class TheAreVulnerabilityAlertsEnabledMethod + { + [IntegrationTest] + public async Task AreVulnerabilityAlertsEnabledReturnsTrue() + { + var github = Helper.GetAuthenticatedClient(); + var enabled = await github.Repository.AreVulnerabilityAlertsEnabled("owner", "name"); + Assert.True(enabled); + } + } } diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index 00b3b520bf..9d95cd1cee 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -3,8 +3,11 @@ using System.Net; using System.Threading.Tasks; using NSubstitute; +using Octokit.Internal; using Xunit; +using static Octokit.Internal.TestSetup; + namespace Octokit.Tests.Clients { /// @@ -395,6 +398,55 @@ public async Task SendsPreviewHeaderById() } } + public class TheAreVulnerabilityAlertsEnabledMethod + { + [Theory] + [InlineData(HttpStatusCode.NoContent, true)] + [InlineData(HttpStatusCode.NotFound, false)] + public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected) + { + var response = CreateResponse(status); + var responseTask = Task.FromResult>(new ApiResponse(response)); + var connection = Substitute.For(); + connection.Get(Arg.Is(u => u.ToString() == "repos/owner/name/vulnerability-alerts"), + null, null).Returns(responseTask); + var apiConnection = Substitute.For(); + apiConnection.Connection.Returns(connection); + var client = new RepositoriesClient(apiConnection); + + var result = await client.AreVulnerabilityAlertsEnabled("owner", "name"); + + Assert.Equal(expected, result); + } + + [Fact] + public async Task ThrowsExceptionForInvalidStatusCode() + { + var response = CreateResponse(HttpStatusCode.Conflict); + var responseTask = Task.FromResult>(new ApiResponse(response)); + var connection = Substitute.For(); + connection.Get(Arg.Is(u => u.ToString() == "repos/owner/name/vulnerability-alerts"), + null, null).Returns(responseTask); + var apiConnection = Substitute.For(); + apiConnection.Connection.Returns(connection); + var client = new RepositoriesClient(apiConnection); + + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("owner", "name")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled(null, "name")); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("", "name")); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled( "owner", null)); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("owner", "")); + } + } + public class TheDeleteMethod { [Fact] diff --git a/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs index ca4ffb13eb..8bac0894d9 100644 --- a/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net; using System.Reactive.Linq; +using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; @@ -89,6 +90,30 @@ public void CallsIntoClientById() } } + public class TheIsFollowingMethod + { + [Fact] + public void CallsIntoClient() + { + var githubClient = Substitute.For(); + var client = new ObservableRepositoriesClient(githubClient); + + client.AreVulnerabilityAlertsEnabled("owner", "name"); + githubClient.Repository.Received().AreVulnerabilityAlertsEnabled("owner", "name"); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new ObservableRepositoriesClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled(null, "name").ToTask()); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("", "name").ToTask()); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("owner", null).ToTask()); + await Assert.ThrowsAsync(() => client.AreVulnerabilityAlertsEnabled("owner", "").ToTask()); + } + } + public class TheDeleteMethod { [Fact] diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index 976766f96c..773b76353c 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -133,6 +133,17 @@ public interface IRepositoriesClient /// A Task Transfer(long repositoryId, RepositoryTransfer repositoryTransfer); + /// + /// Checks if vulnerability alerts are enabled for the specified repository. + /// + /// + /// See the API documentation for more information. + /// + /// The current owner of the repository + /// The name of the repository + /// A bool indicating if alerts are turned on or not. + Task AreVulnerabilityAlertsEnabled(string owner, string name); + /// /// Gets the specified repository. /// diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index 5a279837c0..612de5d15e 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -229,6 +229,32 @@ public Task Transfer(long repositoryId, RepositoryTransfer repositor return ApiConnection.Post(ApiUrls.RepositoryTransfer(repositoryId), repositoryTransfer); } + /// + /// Checks if vulnerability alerts are enabled for the specified repository. + /// + /// + /// See the API documentation for more information. + /// + /// The current owner of the repository + /// The name of the repository + /// A bool indicating if alerts are turned on or not. + [ManualRoute("GET", "/repos/{owner}/{repo}/vulnerability-alerts")] + public async Task AreVulnerabilityAlertsEnabled(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); + Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); + + try + { + var response = await Connection.Get(ApiUrls.RepositoryVulnerabilityAlerts(owner, name), null, null).ConfigureAwait(false); + return response.HttpResponse.IsTrue(); + } + catch (NotFoundException) + { + return false; + } + } + /// /// Updates the specified repository with the values given in /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 1637866a51..92616ad1e2 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -2255,6 +2255,17 @@ public static Uri RepositoryDeployKeys(string owner, string name) return "repos/{0}/{1}/keys".FormatUri(owner, name); } + /// + /// Returns the for checking vulnerability alerts for a repository. + /// + /// + /// + /// + public static Uri RepositoryVulnerabilityAlerts(string owner, string name) + { + return "repos/{0}/{1}/vulnerability-alerts".FormatUri(owner, name); + } + /// /// Returns the for the Deployments API for the given repository. ///