From d78e69bf71c024c32053900938264432f8927ad2 Mon Sep 17 00:00:00 2001 From: Lee Boynton Date: Wed, 9 Jun 2021 20:54:57 +0100 Subject: [PATCH 1/2] Add method to check if repo vulnerability alerts are enabled --- .../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 | 27 ++++++++++ Octokit/Helpers/AcceptHeaders.cs | 2 + Octokit/Helpers/ApiUrls.cs | 11 ++++ 9 files changed, 167 insertions(+) diff --git a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs index ff3c79ff43..101f36a71a 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs @@ -63,6 +63,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 6b835c29e1..9ff1859d69 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -133,6 +133,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 432deb99c3..2422f39960 100644 --- a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs @@ -2010,4 +2010,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 c518b7152c..64b1d31b3f 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 { /// @@ -357,6 +360,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, AcceptHeaders.DependencyAlertsPreview).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 dd291cfd1e..7c775002a1 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -123,6 +123,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 256b24fbd9..36176361ce 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -208,6 +208,33 @@ 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. + [Preview("dorian")] + [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, AcceptHeaders.DependencyAlertsPreview).ConfigureAwait(false); + return response.HttpResponse.IsTrue(); + } + catch (NotFoundException) + { + return false; + } + } + /// /// Updates the specified repository with the values given in /// diff --git a/Octokit/Helpers/AcceptHeaders.cs b/Octokit/Helpers/AcceptHeaders.cs index 09826011e3..fb74669955 100644 --- a/Octokit/Helpers/AcceptHeaders.cs +++ b/Octokit/Helpers/AcceptHeaders.cs @@ -54,6 +54,8 @@ public static class AcceptHeaders public const string VisibilityPreview = "application/vnd.github.nebula-preview+json"; + public const string DependencyAlertsPreview = "application/vnd.github.dorian-preview+json"; + /// /// Combines multiple preview headers. GitHub API supports Accept header with multiple /// values separated by comma. diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 74f0ff3f9c..7c7aa4cb10 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -2246,6 +2246,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. /// From bd23bb0d3e95111f4461f77c00670650d1914812 Mon Sep 17 00:00:00 2001 From: Lee Boynton Date: Sun, 3 Jul 2022 11:53:55 +0100 Subject: [PATCH 2/2] Remove preview header, no longer required --- Octokit.Tests/Clients/RepositoriesClientTests.cs | 2 +- Octokit/Clients/RepositoriesClient.cs | 3 +-- Octokit/Helpers/AcceptHeaders.cs | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index 05ac4def61..9d95cd1cee 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -409,7 +409,7 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool var responseTask = Task.FromResult>(new ApiResponse(response)); var connection = Substitute.For(); connection.Get(Arg.Is(u => u.ToString() == "repos/owner/name/vulnerability-alerts"), - null, AcceptHeaders.DependencyAlertsPreview).Returns(responseTask); + null, null).Returns(responseTask); var apiConnection = Substitute.For(); apiConnection.Connection.Returns(connection); var client = new RepositoriesClient(apiConnection); diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index f5766b429b..612de5d15e 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -238,7 +238,6 @@ public Task Transfer(long repositoryId, RepositoryTransfer repositor /// The current owner of the repository /// The name of the repository /// A bool indicating if alerts are turned on or not. - [Preview("dorian")] [ManualRoute("GET", "/repos/{owner}/{repo}/vulnerability-alerts")] public async Task AreVulnerabilityAlertsEnabled(string owner, string name) { @@ -247,7 +246,7 @@ public async Task AreVulnerabilityAlertsEnabled(string owner, string name) try { - var response = await Connection.Get(ApiUrls.RepositoryVulnerabilityAlerts(owner, name), null, AcceptHeaders.DependencyAlertsPreview).ConfigureAwait(false); + var response = await Connection.Get(ApiUrls.RepositoryVulnerabilityAlerts(owner, name), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) diff --git a/Octokit/Helpers/AcceptHeaders.cs b/Octokit/Helpers/AcceptHeaders.cs index e6f99b1fba..a2723a8e27 100644 --- a/Octokit/Helpers/AcceptHeaders.cs +++ b/Octokit/Helpers/AcceptHeaders.cs @@ -56,8 +56,6 @@ public static class AcceptHeaders public const string TemplatePreview = "application/vnd.github.baptiste-preview+json"; - public const string DependencyAlertsPreview = "application/vnd.github.dorian-preview+json"; - /// /// Combines multiple preview headers. GitHub API supports Accept header with multiple /// values separated by comma.