Skip to content

Commit

Permalink
Add method to check if repo vulnerability alerts are enabled (#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
lboynton authored Jul 6, 2022
1 parent f317f9d commit 23a91a4
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public interface IObservableRepositoriesClient
/// <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>
Expand Down
17 changes: 17 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ public IObservable<Repository> Transfer(long repositoryId, RepositoryTransfer re
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>
Expand Down
11 changes: 11 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
52 changes: 52 additions & 0 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
Expand Down Expand Up @@ -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<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]
Expand Down
25 changes: 25 additions & 0 deletions Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +90,30 @@ public void CallsIntoClientById()
}
}

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]
Expand Down
11 changes: 11 additions & 0 deletions Octokit/Clients/IRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ public interface IRepositoriesClient
/// <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>
Expand Down
26 changes: 26 additions & 0 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ public Task<Repository> Transfer(long repositoryId, RepositoryTransfer repositor
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>
Expand Down
11 changes: 11 additions & 0 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,17 @@ public static Uri RepositoryDeployKeys(string owner, string name)
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>
Expand Down

0 comments on commit 23a91a4

Please sign in to comment.