Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 @@ -133,6 +133,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 @@ -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);
}
}
}
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 @@ -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<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, AcceptHeaders.DependencyAlertsPreview).Returns(responseTask);
nickfloyd marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -123,6 +123,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
27 changes: 27 additions & 0 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,33 @@ 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>
[Preview("dorian")]
nickfloyd marked this conversation as resolved.
Show resolved Hide resolved
[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, AcceptHeaders.DependencyAlertsPreview).ConfigureAwait(false);
nickfloyd marked this conversation as resolved.
Show resolved Hide resolved
return response.HttpResponse.IsTrue();
}
catch (NotFoundException)
{
return false;
}
}

/// <summary>
/// Updates the specified repository with the values given in <paramref name="update"/>
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Helpers/AcceptHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dorian preview was promoted in 09/2021. This header is no longer needed to be passed over. Additionally, this endpoint is versioned for api.github.com only, it doesn't need an enterprise implementation and therefore can be completely removed from this SDK. Let me know if you have any questions regarding the state of previews.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


/// <summary>
/// Combines multiple preview headers. GitHub API supports Accept header with multiple
/// values separated by comma.
Expand Down
11 changes: 11 additions & 0 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,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