[FEAT] Added enterprise pre-receive hooks client (#2375)

This commit is contained in:
tasadar2
2022-07-11 10:59:24 -04:00
committed by GitHub
parent 2179065201
commit 8e6bcd1e49
20 changed files with 1787 additions and 0 deletions

View File

@@ -63,5 +63,13 @@
/// See the <a href="https://developer.github.com/v3/enterprise-admin/pre_receive_environments/">Enterprise Pre-receive Environments API documentation</a> for more information.
///</remarks>
IObservableEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; }
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
IObservableEnterprisePreReceiveHooksClient PreReceiveHook { get; }
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Reactive;
namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public interface IObservableEnterprisePreReceiveHooksClient
{
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<PreReceiveHook> GetAll();
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<PreReceiveHook> GetAll(ApiOptions options);
/// <summary>
/// Gets a single <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#get-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<PreReceiveHook> Get(long hookId);
/// <summary>
/// Creates a new <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#create-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="newPreReceiveHook">A description of the pre-receive hook to create</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<PreReceiveHook> Create(NewPreReceiveHook newPreReceiveHook);
/// <summary>
/// Edits an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#update-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <param name="updatePreReceiveHook">A description of the pre-receive hook to edit</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<PreReceiveHook> Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook);
/// <summary>
/// Deletes an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#delete-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
IObservable<Unit> Delete(long hookId);
}
}

View File

@@ -19,6 +19,7 @@
Organization = new ObservableEnterpriseOrganizationClient(client);
SearchIndexing = new ObservableEnterpriseSearchIndexingClient(client);
PreReceiveEnvironment = new ObservableEnterprisePreReceiveEnvironmentsClient(client);
PreReceiveHook = new ObservableEnterprisePreReceiveHooksClient(client);
}
/// <summary>
@@ -76,5 +77,13 @@
/// See the <a href="https://developer.github.com/v3/enterprise-admin/pre_receive_environments/">Enterprise Pre-receive Environments API documentation</a> for more information.
///</remarks>
public IObservableEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; private set; }
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public IObservableEnterprisePreReceiveHooksClient PreReceiveHook { get; private set; }
}
}

View File

@@ -0,0 +1,114 @@
using System;
using System.Reactive;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public class ObservableEnterprisePreReceiveHooksClient : IObservableEnterprisePreReceiveHooksClient
{
readonly IEnterprisePreReceiveHooksClient _client;
readonly IConnection _connection;
public ObservableEnterprisePreReceiveHooksClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, nameof(client));
_client = client.Enterprise.PreReceiveHook;
_connection = client.Connection;
}
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<PreReceiveHook> GetAll()
{
return GetAll(ApiOptions.None);
}
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<PreReceiveHook> GetAll(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages<PreReceiveHook>(ApiUrls.AdminPreReceiveHooks(), null, AcceptHeaders.StableVersionJson, options);
}
/// <summary>
/// Gets a single <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#get-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<PreReceiveHook> Get(long hookId)
{
return _client.Get(hookId).ToObservable();
}
/// <summary>
/// Creates a new <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#create-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="newPreReceiveHook">A description of the pre-receive hook to create</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<PreReceiveHook> Create(NewPreReceiveHook newPreReceiveHook)
{
Ensure.ArgumentNotNull(newPreReceiveHook, nameof(newPreReceiveHook));
return _client.Create(newPreReceiveHook).ToObservable();
}
/// <summary>
/// Edits an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#update-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <param name="updatePreReceiveHook">A description of the pre-receive hook to edit</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<PreReceiveHook> Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook)
{
Ensure.ArgumentNotNull(updatePreReceiveHook, nameof(updatePreReceiveHook));
return _client.Edit(hookId, updatePreReceiveHook).ToObservable();
}
/// <summary>
/// Deletes an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#delete-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
public IObservable<Unit> Delete(long hookId)
{
return _client.Delete(hookId).ToObservable();
}
}
}

View File

@@ -0,0 +1,357 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Xunit;
public class EnterprisePreReceiveHooksClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new EnterprisePreReceiveHooksClient(null));
}
}
public class TheGetAllMethod : IDisposable
{
private readonly IGitHubClient _githubEnterprise;
private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly List<PreReceiveHook> _preReceiveHooks;
public TheGetAllMethod()
{
_githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
_preReceiveHooks = new List<PreReceiveHook>();
for (var count = 0; count < 3; count++)
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
_preReceiveHooks.Add(_preReceiveHooksClient.Create(newPreReceiveHook).Result);
}
}
[GitHubEnterpriseTest]
public async Task ReturnsPreReceiveHooks()
{
var preReceiveHooks = await _preReceiveHooksClient.GetAll();
Assert.NotEmpty(preReceiveHooks);
}
[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfPreReceiveHooksWithoutStart()
{
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1
};
var preReceiveHooks = await _preReceiveHooksClient.GetAll(options);
Assert.Equal(1, preReceiveHooks.Count);
}
[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfPreReceiveHooksWithStart()
{
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};
var preReceiveHooks = await _preReceiveHooksClient.GetAll(options);
Assert.Equal(1, preReceiveHooks.Count);
}
[GitHubEnterpriseTest]
public async Task ReturnsDistinctResultsBasedOnStartPage()
{
var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1
};
var firstPage = await _preReceiveHooksClient.GetAll(startOptions);
var skipStartOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};
var secondPage = await _preReceiveHooksClient.GetAll(skipStartOptions);
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
public void Dispose()
{
foreach (var preReceiveHook in _preReceiveHooks)
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
}
public class TheGetMethod : IDisposable
{
private readonly IGitHubClient _githubEnterprise;
private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly NewPreReceiveHook _expectedPreReceiveHook;
private readonly PreReceiveHook _preReceiveHook;
public TheGetMethod()
{
_githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
_expectedPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1)
{
AllowDownstreamConfiguration = true,
Enforcement = PreReceiveHookEnforcement.Testing,
};
_preReceiveHook = _preReceiveHooksClient.Create(_expectedPreReceiveHook).Result;
}
[GitHubEnterpriseTest]
public async Task ReturnsName()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Name, preReceiveHook.Name);
}
[GitHubEnterpriseTest]
public async Task ReturnsScript()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Script, preReceiveHook.Script);
}
[GitHubEnterpriseTest]
public async Task ReturnsRepository()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.NotNull(preReceiveHook.ScriptRepository);
Assert.Equal(_expectedPreReceiveHook.ScriptRepository.FullName, preReceiveHook.ScriptRepository.FullName);
}
[GitHubEnterpriseTest]
public async Task ReturnsEnvironment()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.NotNull(preReceiveHook.Environment);
Assert.Equal(_expectedPreReceiveHook.Environment.Id, preReceiveHook.Environment.Id);
}
[GitHubEnterpriseTest]
public async Task ReturnsAllowDownstreamConfiguration()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.AllowDownstreamConfiguration, preReceiveHook.AllowDownstreamConfiguration);
}
[GitHubEnterpriseTest]
public async Task ReturnsEnforcement()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Enforcement.Value, preReceiveHook.Enforcement);
}
[GitHubEnterpriseTest]
public async Task NoHookExists()
{
await Assert.ThrowsAsync<NotFoundException>(() => _preReceiveHooksClient.Get(-1));
}
public void Dispose()
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, _preReceiveHook);
}
}
public class TheCreateMethod
{
private readonly IGitHubClient _githubEnterprise;
private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
public TheCreateMethod()
{
_githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
}
[GitHubEnterpriseTest]
public async Task CanCreatePreReceiveHook()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
Assert.NotNull(preReceiveHook);
Assert.Equal(newPreReceiveHook.Name, preReceiveHook.Name);
Assert.Equal(newPreReceiveHook.Script, preReceiveHook.Script);
Assert.Equal(newPreReceiveHook.ScriptRepository.FullName, preReceiveHook.ScriptRepository.FullName);
Assert.Equal(newPreReceiveHook.Environment.Id, preReceiveHook.Environment.Id);
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
[GitHubEnterpriseTest]
public async Task CannotCreateWhenRepoDoesNotExist()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "doesntExist/repo", Helper.MakeNameWithTimestamp("script"), 1);
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
[GitHubEnterpriseTest]
public async Task CannotCreateWhenEnvironmentDoesNotExist()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), -1);
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
[GitHubEnterpriseTest]
public async Task CannotCreateWithSameName()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
newPreReceiveHook.Script = Helper.MakeNameWithTimestamp("script");
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
[GitHubEnterpriseTest]
public async Task CannotCreateWithSameScript()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
newPreReceiveHook.Name = Helper.MakeNameWithTimestamp("hook");
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
}
public class TheEditMethod : IDisposable
{
private readonly IGitHubClient _githubEnterprise;
private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly PreReceiveHook _preReceiveHook;
public TheEditMethod()
{
_githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
_preReceiveHook = _preReceiveHooksClient.Create(newPreReceiveHook).Result;
}
[GitHubEnterpriseTest]
public async Task CanChangeName()
{
var updatePreReceiveHook = new UpdatePreReceiveHook
{
Name = Helper.MakeNameWithTimestamp("hook")
};
var updatedPreReceiveHook = await _preReceiveHooksClient.Edit(_preReceiveHook.Id, updatePreReceiveHook);
Assert.Equal(_preReceiveHook.Id, updatedPreReceiveHook.Id);
Assert.Equal(updatePreReceiveHook.Name, updatedPreReceiveHook.Name);
}
[GitHubEnterpriseTest]
public async Task CanChangeScript()
{
var updatePreReceiveHook = new UpdatePreReceiveHook
{
Script = Helper.MakeNameWithTimestamp("script")
};
var updatedPreReceiveHook = await _preReceiveHooksClient.Edit(_preReceiveHook.Id, updatePreReceiveHook);
Assert.Equal(_preReceiveHook.Id, updatedPreReceiveHook.Id);
Assert.Equal(updatePreReceiveHook.Script, updatedPreReceiveHook.Script);
}
public void Dispose()
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, _preReceiveHook);
}
}
public class TheDeleteMethod
{
private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
public TheDeleteMethod()
{
var githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
_preReceiveHooksClient = githubEnterprise.Enterprise.PreReceiveHook;
}
[GitHubEnterpriseTest]
public async Task CanDelete()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
var preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
await _preReceiveHooksClient.Delete(preReceiveHook.Id);
await Assert.ThrowsAsync<NotFoundException>(async () => await _preReceiveHooksClient.Get(preReceiveHook.Id));
}
[GitHubEnterpriseTest]
public async Task CannotDeleteWhenHookDoesNotExist()
{
await Assert.ThrowsAsync<NotFoundException>(async () => await _preReceiveHooksClient.Delete(-1));
}
}
}

View File

@@ -161,6 +161,20 @@ namespace Octokit.Tests.Integration
}
}
public static void DeletePreReceiveHook(IConnection connection, PreReceiveHook preReceiveHook)
{
if (preReceiveHook != null)
{
try
{
var client = new GitHubClient(connection);
client.Enterprise.PreReceiveHook.Delete(preReceiveHook.Id).Wait(TimeSpan.FromSeconds(15));
}
catch
{ }
}
}
public static void SetMaintenanceMode(IConnection connection, bool enabled)
{
try

View File

@@ -0,0 +1,359 @@
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit;
using Octokit.Reactive;
using Octokit.Tests.Integration;
using Xunit;
public class ObservableEnterprisePreReceiveHooksClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new EnterprisePreReceiveHooksClient(null));
}
}
public class TheGetAllMethod : IDisposable
{
private readonly IObservableGitHubClient _githubEnterprise;
private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly List<PreReceiveHook> _preReceiveHooks;
public TheGetAllMethod()
{
_githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
_preReceiveHooks = new List<PreReceiveHook>();
for (var count = 0; count < 3; count++)
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
_preReceiveHooks.Add(_preReceiveHooksClient.Create(newPreReceiveHook).Wait());
}
}
[GitHubEnterpriseTest]
public async Task ReturnsPreReceiveHooks()
{
var preReceiveHooks = await _preReceiveHooksClient.GetAll().ToList();
Assert.NotEmpty(preReceiveHooks);
}
[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfPreReceiveHooksWithoutStart()
{
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1
};
var preReceiveHooks = await _preReceiveHooksClient.GetAll(options).ToList();
Assert.Equal(1, preReceiveHooks.Count);
}
[GitHubEnterpriseTest]
public async Task ReturnsCorrectCountOfPreReceiveHooksWithStart()
{
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};
var preReceiveHooks = await _preReceiveHooksClient.GetAll(options).ToList();
Assert.Equal(1, preReceiveHooks.Count);
}
[GitHubEnterpriseTest]
public async Task ReturnsDistinctResultsBasedOnStartPage()
{
var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1
};
var firstPage = await _preReceiveHooksClient.GetAll(startOptions).ToList();
var skipStartOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};
var secondPage = await _preReceiveHooksClient.GetAll(skipStartOptions).ToList();
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
public void Dispose()
{
foreach (var preReceiveHook in _preReceiveHooks)
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
}
public class TheGetMethod : IDisposable
{
private readonly IObservableGitHubClient _githubEnterprise;
private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly NewPreReceiveHook _expectedPreReceiveHook;
private readonly PreReceiveHook _preReceiveHook;
public TheGetMethod()
{
_githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
_expectedPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1)
{
AllowDownstreamConfiguration = true,
Enforcement = PreReceiveHookEnforcement.Testing,
};
_preReceiveHook = _preReceiveHooksClient.Create(_expectedPreReceiveHook).Wait();
}
[GitHubEnterpriseTest]
public async Task ReturnsName()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Name, preReceiveHook.Name);
}
[GitHubEnterpriseTest]
public async Task ReturnsScript()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Script, preReceiveHook.Script);
}
[GitHubEnterpriseTest]
public async Task ReturnsRepository()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.NotNull(preReceiveHook.ScriptRepository);
Assert.Equal(_expectedPreReceiveHook.ScriptRepository.FullName, preReceiveHook.ScriptRepository.FullName);
}
[GitHubEnterpriseTest]
public async Task ReturnsEnvironment()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.NotNull(preReceiveHook.Environment);
Assert.Equal(_expectedPreReceiveHook.Environment.Id, preReceiveHook.Environment.Id);
}
[GitHubEnterpriseTest]
public async Task ReturnsAllowDownstreamConfiguration()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.AllowDownstreamConfiguration, preReceiveHook.AllowDownstreamConfiguration);
}
[GitHubEnterpriseTest]
public async Task ReturnsEnforcement()
{
var preReceiveHook = await _preReceiveHooksClient.Get(_preReceiveHook.Id);
Assert.NotNull(preReceiveHook);
Assert.Equal(_expectedPreReceiveHook.Enforcement.Value, preReceiveHook.Enforcement);
}
[GitHubEnterpriseTest]
public async Task NoHookExists()
{
await Assert.ThrowsAsync<NotFoundException>(async () => await _preReceiveHooksClient.Get(-1));
}
public void Dispose()
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, _preReceiveHook);
}
}
public class TheCreateMethod
{
private readonly IObservableGitHubClient _githubEnterprise;
private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
public TheCreateMethod()
{
_githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
}
[GitHubEnterpriseTest]
public async Task CanCreatePreReceiveHook()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
Assert.NotNull(preReceiveHook);
Assert.Equal(newPreReceiveHook.Name, preReceiveHook.Name);
Assert.Equal(newPreReceiveHook.Script, preReceiveHook.Script);
Assert.Equal(newPreReceiveHook.ScriptRepository.FullName, preReceiveHook.ScriptRepository.FullName);
Assert.Equal(newPreReceiveHook.Environment.Id, preReceiveHook.Environment.Id);
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
[GitHubEnterpriseTest]
public async Task CannotCreateWhenRepoDoesNotExist()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "doesntExist/repo", Helper.MakeNameWithTimestamp("script"), 1);
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
[GitHubEnterpriseTest]
public async Task CannotCreateWhenEnvironmentDoesNotExist()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), -1);
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
[GitHubEnterpriseTest]
public async Task CannotCreateWithSameName()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
newPreReceiveHook.Script = Helper.MakeNameWithTimestamp("script");
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
[GitHubEnterpriseTest]
public async Task CannotCreateWithSameScript()
{
PreReceiveHook preReceiveHook = null;
try
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
newPreReceiveHook.Name = Helper.MakeNameWithTimestamp("hook");
await Assert.ThrowsAsync<ApiValidationException>(async () => await _preReceiveHooksClient.Create(newPreReceiveHook));
}
finally
{
//Cleanup
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, preReceiveHook);
}
}
}
public class TheEditMethod : IDisposable
{
private readonly IObservableGitHubClient _githubEnterprise;
private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
private readonly PreReceiveHook _preReceiveHook;
public TheEditMethod()
{
_githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
_preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
_preReceiveHook = _preReceiveHooksClient.Create(newPreReceiveHook).Wait();
}
[GitHubEnterpriseTest]
public async Task CanChangeName()
{
var updatePreReceiveHook = new UpdatePreReceiveHook
{
Name = Helper.MakeNameWithTimestamp("hook")
};
var updatedPreReceiveHook = await _preReceiveHooksClient.Edit(_preReceiveHook.Id, updatePreReceiveHook);
Assert.Equal(_preReceiveHook.Id, updatedPreReceiveHook.Id);
Assert.Equal(updatePreReceiveHook.Name, updatedPreReceiveHook.Name);
}
[GitHubEnterpriseTest]
public async Task CanChangeScript()
{
var updatePreReceiveHook = new UpdatePreReceiveHook
{
Script = Helper.MakeNameWithTimestamp("script")
};
var updatedPreReceiveHook = await _preReceiveHooksClient.Edit(_preReceiveHook.Id, updatePreReceiveHook);
Assert.Equal(_preReceiveHook.Id, updatedPreReceiveHook.Id);
Assert.Equal(updatePreReceiveHook.Script, updatedPreReceiveHook.Script);
}
public void Dispose()
{
EnterpriseHelper.DeletePreReceiveHook(_githubEnterprise.Connection, _preReceiveHook);
}
}
public class TheDeleteMethod
{
private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
public TheDeleteMethod()
{
var githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
_preReceiveHooksClient = githubEnterprise.Enterprise.PreReceiveHook;
}
[GitHubEnterpriseTest]
public async Task CanDelete()
{
var newPreReceiveHook = new NewPreReceiveHook(Helper.MakeNameWithTimestamp("hook"), "octokit/octokit.net", Helper.MakeNameWithTimestamp("script"), 1);
var preReceiveHook = await _preReceiveHooksClient.Create(newPreReceiveHook);
await _preReceiveHooksClient.Delete(preReceiveHook.Id);
await Assert.ThrowsAsync<NotFoundException>(async () => await _preReceiveHooksClient.Get(preReceiveHook.Id));
}
[GitHubEnterpriseTest]
public async Task CannotDeleteWhenHookDoesNotExist()
{
await Assert.ThrowsAsync<NotFoundException>(async () => await _preReceiveHooksClient.Delete(-1));
}
}
}

View File

@@ -0,0 +1,158 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;
namespace Octokit.Tests.Clients
{
public class EnterprisePreReceiveHooksClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new EnterprisePreReceiveHooksClient(null));
}
}
public class TheGetAllMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
await client.GetAll();
connection.Received().GetAll<PreReceiveHook>(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks"),
null,
"application/vnd.github.v3+json",
Args.ApiOptions);
}
[Fact]
public async Task RequestsCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};
await client.GetAll(options);
connection.Received().GetAll<PreReceiveHook>(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks"),
null,
"application/vnd.github.v3+json",
options);
}
}
public class TheGetMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
await client.Get(1);
connection.Received().Get<PreReceiveHook>(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks/1"),
null,
"application/vnd.github.v3+json");
}
}
public class TheCreateMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
var data = new NewPreReceiveHook("name", "repo", "script", 1);
await client.Create(data);
connection.Received().Post<PreReceiveHook>(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks"),
data,
"application/vnd.github.v3+json");
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new EnterprisePreReceiveHooksClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook(null, "repo", "script", 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("", "repo", "script", 1));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook("name", null, "script", 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("name", "", "script", 1));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook("name", "repo", null, 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("name", "repo", "", 1));
}
}
public class TheEditMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
var data = new UpdatePreReceiveHook
{
Name = "name",
ScriptRepository = new RepositoryReference
{
FullName = "repo"
},
Script = "script",
Environment = new PreReceiveEnvironmentReference
{
Id = 1
}
};
await client.Edit(1, data);
connection.Received().Patch<PreReceiveHook>(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks/1"),
data,
"application/vnd.github.v3+json");
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new EnterprisePreReceiveHooksClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(1, null));
}
}
public class TheDeleteMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new EnterprisePreReceiveHooksClient(connection);
await client.Delete(1);
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "admin/pre-receive-hooks/1"),
Arg.Any<object>(),
"application/vnd.github.v3+json");
}
}
}
}

View File

@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using NSubstitute;
using Octokit.Reactive;
using Xunit;
namespace Octokit.Tests.Reactive
{
public class ObservableEnterprisePreReceiveHooksClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new ObservableEnterprisePreReceiveHooksClient(null));
}
}
public class TheGetAllMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
client.GetAll();
gitHubClient.Connection.Received(1).Get<List<PreReceiveHook>>(
new Uri("admin/pre-receive-hooks", UriKind.Relative),
Args.EmptyDictionary,
"application/vnd.github.v3+json");
}
[Fact]
public void RequestsTheCorrectUrlWithApiOptions()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
var options = new ApiOptions
{
PageCount = 1,
PageSize = 1,
StartPage = 1
};
client.GetAll(options);
gitHubClient.Connection.Received(1).Get<List<PreReceiveHook>>(
new Uri("admin/pre-receive-hooks", UriKind.Relative),
Arg.Is<IDictionary<string, string>>(d => d.Count == 2),
"application/vnd.github.v3+json");
}
[Fact]
public void EnsuresNonNullArguments()
{
var client = new ObservableEnterprisePreReceiveHooksClient(Substitute.For<IGitHubClient>());
Assert.Throws<ArgumentNullException>(() => client.GetAll(null));
}
}
public class TheGetMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
client.Get(1);
gitHubClient.Enterprise.PreReceiveHook.Received(1).Get(1);
}
}
public class TheCreateMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
var data = new NewPreReceiveHook("name", "repo", "script", 1);
client.Create(data);
gitHubClient.Enterprise.PreReceiveHook.Received(1).Create(data);
}
[Fact]
public void EnsuresNonNullArguments()
{
var client = new ObservableEnterprisePreReceiveHooksClient(Substitute.For<IGitHubClient>());
Assert.Throws<ArgumentNullException>(() => client.Create(null));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook(null, "repo", "script", 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("", "repo", "script", 1));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook("name", null, "script", 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("name", "", "script", 1));
Assert.Throws<ArgumentNullException>(() => new NewPreReceiveHook("name", "repo", null, 1));
Assert.Throws<ArgumentException>(() => new NewPreReceiveHook("name", "repo", "", 1));
}
}
public class TheEditMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var releasesClient = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
var data = new UpdatePreReceiveHook
{
Name = "name",
ScriptRepository = new RepositoryReference
{
FullName = "repo"
},
Script = "script",
Environment = new PreReceiveEnvironmentReference
{
Id = 1
}
};
releasesClient.Edit(1, data);
gitHubClient.Enterprise.PreReceiveHook.Received(1).Edit(1, data);
}
[Fact]
public void EnsuresNonNullArguments()
{
var client = new ObservableEnterprisePreReceiveHooksClient(Substitute.For<IGitHubClient>());
Assert.Throws<ArgumentNullException>(() => client.Edit(1, null));
}
}
public class TheDeleteMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableEnterprisePreReceiveHooksClient(gitHubClient);
client.Delete(1);
gitHubClient.Enterprise.PreReceiveHook.Received(1).Delete(1);
}
}
}
}

View File

@@ -21,6 +21,7 @@
Organization = new EnterpriseOrganizationClient(apiConnection);
SearchIndexing = new EnterpriseSearchIndexingClient(apiConnection);
PreReceiveEnvironment = new EnterprisePreReceiveEnvironmentsClient(apiConnection);
PreReceiveHook = new EnterprisePreReceiveHooksClient(apiConnection);
}
/// <summary>
@@ -78,5 +79,13 @@
/// See the <a href="https://developer.github.com/v3/enterprise-admin/pre_receive_environments/">Enterprise Pre-receive Environments API documentation</a> for more information.
///</remarks>
public IEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; private set; }
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public IEnterprisePreReceiveHooksClient PreReceiveHook { get; private set; }
}
}

View File

@@ -0,0 +1,118 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public class EnterprisePreReceiveHooksClient : ApiClient, IEnterprisePreReceiveHooksClient
{
/// <summary>
/// Initializes a new instance of <see cref="EnterprisePreReceiveHooksClient"/>.
/// </summary>
/// <param name="apiConnection">An API connection</param>
public EnterprisePreReceiveHooksClient(IApiConnection apiConnection)
: base(apiConnection)
{ }
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("GET", "/admin/pre-receive-hooks")]
public Task<IReadOnlyList<PreReceiveHook>> GetAll()
{
return GetAll(ApiOptions.None);
}
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("GET", "/admin/pre-receive-hooks")]
public Task<IReadOnlyList<PreReceiveHook>> GetAll(ApiOptions options)
{
var endpoint = ApiUrls.AdminPreReceiveHooks();
return ApiConnection.GetAll<PreReceiveHook>(endpoint, null, AcceptHeaders.StableVersionJson, options);
}
/// <summary>
/// Gets a single <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#get-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("GET", "/admin/pre-receive-hooks/{pre_receive_hook_id}")]
public Task<PreReceiveHook> Get(long hookId)
{
var endpoint = ApiUrls.AdminPreReceiveHooks(hookId);
return ApiConnection.Get<PreReceiveHook>(endpoint, null, AcceptHeaders.StableVersionJson);
}
/// <summary>
/// Creates a new <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#create-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="newPreReceiveHook">A description of the pre-receive hook to create</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("POST", "/admin/pre-receive-hooks")]
public Task<PreReceiveHook> Create(NewPreReceiveHook newPreReceiveHook)
{
Ensure.ArgumentNotNull(newPreReceiveHook, nameof(newPreReceiveHook));
var endpoint = ApiUrls.AdminPreReceiveHooks();
return ApiConnection.Post<PreReceiveHook>(endpoint, newPreReceiveHook, AcceptHeaders.StableVersionJson);
}
/// <summary>
/// Edits an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#update-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <param name="updatePreReceiveHook">A description of the pre-receive hook to edit</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("PATCH", "/admin/pre-receive-hooks/{pre_receive_hook_id}")]
public Task<PreReceiveHook> Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook)
{
Ensure.ArgumentNotNull(updatePreReceiveHook, nameof(updatePreReceiveHook));
var endpoint = ApiUrls.AdminPreReceiveHooks(hookId);
return ApiConnection.Patch<PreReceiveHook>(endpoint, updatePreReceiveHook, AcceptHeaders.StableVersionJson);
}
/// <summary>
/// Deletes an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#delete-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
[ManualRoute("DELETE", "/admin/pre-receive-hooks/{pre_receive_hook_id}")]
public Task Delete(long hookId)
{
var endpoint = ApiUrls.AdminPreReceiveHooks(hookId);
return ApiConnection.Delete(endpoint, new object(), AcceptHeaders.StableVersionJson);
}
}
}

View File

@@ -63,5 +63,13 @@
/// See the <a href="https://developer.github.com/v3/enterprise-admin/pre_receive_environments/">Enterprise Pre-receive Environments API documentation</a> for more information.
///</remarks>
IEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; }
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server@3.3/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
IEnterprisePreReceiveHooksClient PreReceiveHook { get; }
}
}

View File

@@ -0,0 +1,77 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// A client for GitHub's Enterprise Pre-receive Hooks API
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#pre-receive-hooks">Enterprise Pre-receive Hooks API documentation</a> for more information.
///</remarks>
public interface IEnterprisePreReceiveHooksClient
{
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<IReadOnlyList<PreReceiveHook>> GetAll();
/// <summary>
/// Gets all <see cref="PreReceiveHook"/>s.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#list-pre-receive-hooks">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<IReadOnlyList<PreReceiveHook>> GetAll(ApiOptions options);
/// <summary>
/// Gets a single <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#get-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<PreReceiveHook> Get(long hookId);
/// <summary>
/// Creates a new <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#create-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="newPreReceiveHook">A description of the pre-receive hook to create</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<PreReceiveHook> Create(NewPreReceiveHook newPreReceiveHook);
/// <summary>
/// Edits an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#update-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <param name="updatePreReceiveHook">A description of the pre-receive hook to edit</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task<PreReceiveHook> Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook);
/// <summary>
/// Deletes an existing <see cref="PreReceiveHook"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/enterprise-server/rest/reference/enterprise-admin#delete-a-pre-receive-hook">API documentation</a> for more information.
/// </remarks>
/// <param name="hookId">The id of the pre-receive hook</param>
/// <exception cref="NotFoundException">Thrown when the specified <paramref name="hookId"/> does not exist.</exception>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
Task Delete(long hookId);
}
}

View File

@@ -2724,6 +2724,24 @@ namespace Octokit
return "admin/pre-receive-environments/{0}/downloads/latest".FormatUri(environmentId);
}
/// <summary>
/// Creates the <see cref="Uri"/> for pre-receive hooks.
/// </summary>
/// <returns></returns>
public static Uri AdminPreReceiveHooks()
{
return "admin/pre-receive-hooks".FormatUri();
}
/// <summary>
/// Creates the <see cref="Uri"/> for pre-receive hooks.
/// </summary>
/// <returns></returns>
public static Uri AdminPreReceiveHooks(long hookId)
{
return "admin/pre-receive-hooks/{0}".FormatUri(hookId);
}
/// <summary>
/// Creates the relative <see cref="Uri"/> for altering administration status of a user.
/// </summary>

View File

@@ -0,0 +1,98 @@
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// Describes a new pre-receive hook.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class NewPreReceiveHook
{
/// <summary>
/// Initializes a new instance of the <see cref="NewPreReceiveHook"/> class.
/// </summary>
/// <param name="name">The name of the hook.</param>
/// <param name="script">The script that the hook runs.</param>
/// <param name="scriptRepository">The repository where the script is kept.</param>
/// <param name="environment">The pre-receive environment where the script is executed.</param>
public NewPreReceiveHook(string name, Repository scriptRepository, string script, PreReceiveEnvironment environment)
{
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(scriptRepository, nameof(scriptRepository));
Ensure.ArgumentNotNullOrEmptyString(script, nameof(script));
Ensure.ArgumentNotNull(environment, nameof(environment));
Name = name;
ScriptRepository = new RepositoryReference
{
FullName = scriptRepository.FullName
};
Script = script;
Environment = new PreReceiveEnvironmentReference
{
Id = environment.Id
};
}
/// <summary>
/// Initializes a new instance of the <see cref="NewPreReceiveHook"/> class.
/// </summary>
/// <param name="name">The name of the hook.</param>
/// <param name="script">The script that the hook runs.</param>
/// <param name="scriptRepositoryFullName">The <see cref="Repository.FullName"/> of a repository where the script is kept.</param>
/// <param name="environmentId">The <see cref="PreReceiveEnvironment.Id"/> of a pre-receive environment where the script is executed.</param>
public NewPreReceiveHook(string name, string scriptRepositoryFullName, string script, long environmentId)
{
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(script, nameof(script));
Ensure.ArgumentNotNullOrEmptyString(scriptRepositoryFullName, nameof(scriptRepositoryFullName));
Name = name;
ScriptRepository = new RepositoryReference
{
FullName = scriptRepositoryFullName
};
Script = script;
Environment = new PreReceiveEnvironmentReference
{
Id = environmentId
};
}
/// <summary>
/// The name of the hook.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public string Script { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public RepositoryReference ScriptRepository { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public PreReceiveEnvironmentReference Environment { get; set; }
/// <summary>
/// The state of enforcement for this hook. default: <see cref="PreReceiveHookEnforcement.Disabled"/>
/// </summary>
public PreReceiveHookEnforcement? Enforcement { get; set; }
/// <summary>
/// Whether enforcement can be overridden at the org or repo level. default: false.
/// </summary>
public bool? AllowDownstreamConfiguration { get; set; }
internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "Name: {0} Repo: {1} Script: {2}", Name, ScriptRepository.FullName, Script); }
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// A reference to a pre-receive environment.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PreReceiveEnvironmentReference
{
/// <summary>
/// The identifier for the pre-receive environment.
/// </summary>
public long Id { get; set; }
internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "Id: {0}", Id); }
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// A Reference to a repository.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RepositoryReference
{
/// <summary>
/// The full name for the repository.
/// </summary>
public string FullName { get; set; }
internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "FullName: {0}", FullName); }
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// Describes a new pre-receive hook.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class UpdatePreReceiveHook
{
/// <summary>
/// The name of the hook.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public string Script { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public RepositoryReference ScriptRepository { get; set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public PreReceiveEnvironmentReference Environment { get; set; }
/// <summary>
/// The state of enforcement for this hook. Defaults is <see cref="PreReceiveHookEnforcement.Disabled"/>
/// </summary>
public PreReceiveHookEnforcement? Enforcement { get; set; }
/// <summary>
/// Whether enforcement can be overridden at the org or repo level. Default is false.
/// </summary>
public bool? AllowDownstreamConfiguration { get; set; }
internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "Name: {0} Script: {1}", Name, Script); }
}
}
}

View File

@@ -0,0 +1,85 @@
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
/// <summary>
/// Describes a pre-receive hook.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PreReceiveHook
{
public PreReceiveHook()
{ }
public PreReceiveHook(int id, string name, StringEnum<PreReceiveHookEnforcement> enforcement, string script, Repository scriptRepository, PreReceiveEnvironment environment, bool allowDownstreamConfiguration)
{
Id = id;
Name = name;
Enforcement = enforcement;
Script = script;
ScriptRepository = scriptRepository;
Environment = environment;
AllowDownstreamConfiguration = allowDownstreamConfiguration;
}
/// <summary>
/// The identifier for the pre-receive hook.
/// </summary>
public int Id { get; protected set; }
/// <summary>
/// The name of the hook.
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// The state of enforcement for this hook.
/// </summary>
public StringEnum<PreReceiveHookEnforcement> Enforcement { get; protected set; }
/// <summary>
/// The script that the hook runs.
/// </summary>
public string Script { get; protected set; }
/// <summary>
/// The GitHub repository where the script is kept.
/// </summary>
public Repository ScriptRepository { get; protected set; }
/// <summary>
/// The pre-receive environment where the script is executed.
/// </summary>
public PreReceiveEnvironment Environment { get; protected set; }
/// <summary>
/// Whether enforcement can be overridden at the org or repo level.
/// </summary>
public bool AllowDownstreamConfiguration { get; protected set; }
public UpdatePreReceiveHook ToUpdate()
{
return new UpdatePreReceiveHook
{
Name = Name,
Enforcement = Enforcement.Value,
Script = Script,
ScriptRepository = new RepositoryReference
{
FullName = ScriptRepository.FullName
},
Environment = new PreReceiveEnvironmentReference
{
Id = Environment.Id
},
AllowDownstreamConfiguration = AllowDownstreamConfiguration
};
}
internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "Id: {0} Name: {1} Script: {2}", Id, Name, Script); }
}
}
}

View File

@@ -0,0 +1,28 @@
using Octokit.Internal;
namespace Octokit
{
/// <summary>
/// The state of enforcement for a hook.
/// </summary>
public enum PreReceiveHookEnforcement
{
/// <summary>
/// Indicates the pre-receive hook will not run.
/// </summary>
[Parameter(Value = "disabled")]
Disabled,
/// <summary>
/// Indicates it will run and reject any pushes that result in a non-zero status.
/// </summary>
[Parameter(Value = "enabled")]
Enabled,
/// <summary>
/// Means the script will run but will not cause any pushes to be rejected.
/// </summary>
[Parameter(Value = "testing")]
Testing
}
}