Add method to check if repo vulnerability alerts are enabled (#2453)

This commit is contained in:
Lee Boynton
2022-07-06 17:29:21 +01:00
committed by GitHub
parent f317f9dadc
commit 23a91a492e
8 changed files with 164 additions and 0 deletions

View File

@@ -72,6 +72,17 @@ namespace Octokit.Reactive
/// <returns>A <see cref="Repository"/></returns>
IObservable<Repository> Transfer(long repositoryId, RepositoryTransfer repositoryTransfer);
/// <summary>
/// Checks if vulnerability alerts are enabled for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/reference/repos#check-if-vulnerability-alerts-are-enabled-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The current owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>A <c>bool</c> indicating if alerts are turned on or not.</returns>
IObservable<bool> AreVulnerabilityAlertsEnabled(string owner, string name);
/// <summary>
/// Retrieves the <see cref="Repository"/> for the specified owner and name.
/// </summary>

View File

@@ -151,6 +151,23 @@ namespace Octokit.Reactive
return _client.Transfer(repositoryId, repositoryTransfer).ToObservable();
}
/// <summary>
/// Checks if vulnerability alerts are enabled for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/reference/repos#check-if-vulnerability-alerts-are-enabled-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The current owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>A <c>bool</c> indicating if alerts are turned on or not.</returns>
public IObservable<bool> AreVulnerabilityAlertsEnabled(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
return _client.AreVulnerabilityAlertsEnabled(owner, name).ToObservable();
}
/// <summary>
/// Retrieves the <see cref="Repository"/> for the specified owner and name.
/// </summary>

View File

@@ -2055,4 +2055,15 @@ public class RepositoriesClientTests
}
}
}
public class TheAreVulnerabilityAlertsEnabledMethod
{
[IntegrationTest]
public async Task AreVulnerabilityAlertsEnabledReturnsTrue()
{
var github = Helper.GetAuthenticatedClient();
var enabled = await github.Repository.AreVulnerabilityAlertsEnabled("owner", "name");
Assert.True(enabled);
}
}
}

View File

@@ -3,8 +3,11 @@ using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Xunit;
using static Octokit.Internal.TestSetup;
namespace Octokit.Tests.Clients
{
/// <summary>
@@ -395,6 +398,55 @@ namespace Octokit.Tests.Clients
}
}
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<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/vulnerability-alerts"),
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
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<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/vulnerability-alerts"),
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new RepositoriesClient(apiConnection);
await Assert.ThrowsAsync<ApiException>(() => client.AreVulnerabilityAlertsEnabled("owner", "name"));
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.AreVulnerabilityAlertsEnabled(null, "name"));
await Assert.ThrowsAsync<ArgumentException>(() => client.AreVulnerabilityAlertsEnabled("", "name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.AreVulnerabilityAlertsEnabled( "owner", null));
await Assert.ThrowsAsync<ArgumentException>(() => client.AreVulnerabilityAlertsEnabled("owner", ""));
}
}
public class TheDeleteMethod
{
[Fact]

View File

@@ -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 @@ namespace Octokit.Tests.Reactive
}
}
public class TheIsFollowingMethod
{
[Fact]
public void CallsIntoClient()
{
var githubClient = Substitute.For<IGitHubClient>();
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<IGitHubClient>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.AreVulnerabilityAlertsEnabled(null, "name").ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.AreVulnerabilityAlertsEnabled("", "name").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.AreVulnerabilityAlertsEnabled("owner", null).ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.AreVulnerabilityAlertsEnabled("owner", "").ToTask());
}
}
public class TheDeleteMethod
{
[Fact]

View File

@@ -133,6 +133,17 @@ namespace Octokit
/// <returns>A <see cref="Repository"/></returns>
Task<Repository> Transfer(long repositoryId, RepositoryTransfer repositoryTransfer);
/// <summary>
/// Checks if vulnerability alerts are enabled for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/reference/repos#check-if-vulnerability-alerts-are-enabled-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The current owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>A <c>bool</c> indicating if alerts are turned on or not.</returns>
Task<bool> AreVulnerabilityAlertsEnabled(string owner, string name);
/// <summary>
/// Gets the specified repository.
/// </summary>

View File

@@ -229,6 +229,32 @@ namespace Octokit
return ApiConnection.Post<Repository>(ApiUrls.RepositoryTransfer(repositoryId), repositoryTransfer);
}
/// <summary>
/// Checks if vulnerability alerts are enabled for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/reference/repos#check-if-vulnerability-alerts-are-enabled-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The current owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>A <c>bool</c> indicating if alerts are turned on or not.</returns>
[ManualRoute("GET", "/repos/{owner}/{repo}/vulnerability-alerts")]
public async Task<bool> AreVulnerabilityAlertsEnabled(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
try
{
var response = await Connection.Get<object>(ApiUrls.RepositoryVulnerabilityAlerts(owner, name), null, null).ConfigureAwait(false);
return response.HttpResponse.IsTrue();
}
catch (NotFoundException)
{
return false;
}
}
/// <summary>
/// Updates the specified repository with the values given in <paramref name="update"/>
/// </summary>

View File

@@ -2255,6 +2255,17 @@ namespace Octokit
return "repos/{0}/{1}/keys".FormatUri(owner, name);
}
/// <summary>
/// Returns the <see cref="Uri"/> for checking vulnerability alerts for a repository.
/// </summary>
/// <param name="owner"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Uri RepositoryVulnerabilityAlerts(string owner, string name)
{
return "repos/{0}/{1}/vulnerability-alerts".FormatUri(owner, name);
}
/// <summary>
/// Returns the <see cref="System.Uri"/> for the Deployments API for the given repository.
/// </summary>