diff --git a/Octokit.Reactive/Clients/Enterprise/IObservableEnterpriseClient.cs b/Octokit.Reactive/Clients/Enterprise/IObservableEnterpriseClient.cs
index f3aa7fdcc3..ed7cf6467e 100644
--- a/Octokit.Reactive/Clients/Enterprise/IObservableEnterpriseClient.cs
+++ b/Octokit.Reactive/Clients/Enterprise/IObservableEnterpriseClient.cs
@@ -63,5 +63,13 @@ public interface IObservableEnterpriseClient
/// See the Enterprise Pre-receive Environments API documentation for more information.
///
IObservableEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; }
+
+ ///
+ /// A client for GitHub's Enterprise Pre-receive Hooks API
+ ///
+ ///
+ /// See the Enterprise Pre-receive Hooks API documentation for more information.
+ ///
+ IObservableEnterprisePreReceiveHooksClient PreReceiveHook { get; }
}
}
diff --git a/Octokit.Reactive/Clients/Enterprise/IObservableEnterprisePreReceiveHooksClient.cs b/Octokit.Reactive/Clients/Enterprise/IObservableEnterprisePreReceiveHooksClient.cs
new file mode 100644
index 0000000000..1cd389481f
--- /dev/null
+++ b/Octokit.Reactive/Clients/Enterprise/IObservableEnterprisePreReceiveHooksClient.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Reactive;
+
+namespace Octokit.Reactive
+{
+ ///
+ /// A client for GitHub's Enterprise Pre-receive Hooks API
+ ///
+ ///
+ /// See the Enterprise Pre-receive Hooks API documentation for more information.
+ ///
+ public interface IObservableEnterprisePreReceiveHooksClient
+ {
+ ///
+ /// Gets all s.
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// Thrown when a general API error occurs.
+ IObservable GetAll();
+
+ ///
+ /// Gets all s.
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// Options for changing the API response
+ /// Thrown when a general API error occurs.
+ IObservable GetAll(ApiOptions options);
+
+ ///
+ /// Gets a single .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ IObservable Get(long hookId);
+
+ ///
+ /// Creates a new .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// A description of the pre-receive hook to create
+ /// Thrown when a general API error occurs.
+ IObservable Create(NewPreReceiveHook newPreReceiveHook);
+
+ ///
+ /// Edits an existing .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// A description of the pre-receive hook to edit
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ IObservable Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook);
+
+ ///
+ /// Deletes an existing .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ IObservable Delete(long hookId);
+ }
+}
diff --git a/Octokit.Reactive/Clients/Enterprise/ObservableEnterpriseClient.cs b/Octokit.Reactive/Clients/Enterprise/ObservableEnterpriseClient.cs
index e9d01ae29e..44ddd5f528 100644
--- a/Octokit.Reactive/Clients/Enterprise/ObservableEnterpriseClient.cs
+++ b/Octokit.Reactive/Clients/Enterprise/ObservableEnterpriseClient.cs
@@ -19,6 +19,7 @@ public ObservableEnterpriseClient(IGitHubClient client)
Organization = new ObservableEnterpriseOrganizationClient(client);
SearchIndexing = new ObservableEnterpriseSearchIndexingClient(client);
PreReceiveEnvironment = new ObservableEnterprisePreReceiveEnvironmentsClient(client);
+ PreReceiveHook = new ObservableEnterprisePreReceiveHooksClient(client);
}
///
@@ -76,5 +77,13 @@ public ObservableEnterpriseClient(IGitHubClient client)
/// See the Enterprise Pre-receive Environments API documentation for more information.
///
public IObservableEnterprisePreReceiveEnvironmentsClient PreReceiveEnvironment { get; private set; }
+
+ ///
+ /// A client for GitHub's Enterprise Pre-receive Hooks API
+ ///
+ ///
+ /// See the Enterprise Pre-receive Hooks API documentation for more information.
+ ///
+ public IObservableEnterprisePreReceiveHooksClient PreReceiveHook { get; private set; }
}
}
diff --git a/Octokit.Reactive/Clients/Enterprise/ObservableEnterprisePreReceiveHooksClient.cs b/Octokit.Reactive/Clients/Enterprise/ObservableEnterprisePreReceiveHooksClient.cs
new file mode 100644
index 0000000000..7320aaa067
--- /dev/null
+++ b/Octokit.Reactive/Clients/Enterprise/ObservableEnterprisePreReceiveHooksClient.cs
@@ -0,0 +1,114 @@
+using System;
+using System.Reactive;
+using System.Reactive.Threading.Tasks;
+using Octokit.Reactive.Internal;
+
+namespace Octokit.Reactive
+{
+ ///
+ /// A client for GitHub's Enterprise Pre-receive Hooks API
+ ///
+ ///
+ /// See the Enterprise Pre-receive Hooks API documentation for more information.
+ ///
+ 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;
+ }
+
+ ///
+ /// Gets all s.
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// Thrown when a general API error occurs.
+ public IObservable GetAll()
+ {
+ return GetAll(ApiOptions.None);
+ }
+
+ ///
+ /// Gets all s.
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// Options for changing the API response
+ /// Thrown when a general API error occurs.
+ public IObservable GetAll(ApiOptions options)
+ {
+ Ensure.ArgumentNotNull(options, nameof(options));
+
+ return _connection.GetAndFlattenAllPages(ApiUrls.AdminPreReceiveHooks(), null, AcceptHeaders.StableVersionJson, options);
+ }
+
+ ///
+ /// Gets a single .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ public IObservable Get(long hookId)
+ {
+ return _client.Get(hookId).ToObservable();
+ }
+
+ ///
+ /// Creates a new .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// A description of the pre-receive hook to create
+ /// Thrown when a general API error occurs.
+ public IObservable Create(NewPreReceiveHook newPreReceiveHook)
+ {
+ Ensure.ArgumentNotNull(newPreReceiveHook, nameof(newPreReceiveHook));
+
+ return _client.Create(newPreReceiveHook).ToObservable();
+ }
+
+ ///
+ /// Edits an existing .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// A description of the pre-receive hook to edit
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ public IObservable Edit(long hookId, UpdatePreReceiveHook updatePreReceiveHook)
+ {
+ Ensure.ArgumentNotNull(updatePreReceiveHook, nameof(updatePreReceiveHook));
+
+ return _client.Edit(hookId, updatePreReceiveHook).ToObservable();
+ }
+
+ ///
+ /// Deletes an existing .
+ ///
+ ///
+ /// See the API documentation for more information.
+ ///
+ /// The id of the pre-receive hook
+ /// Thrown when the specified does not exist.
+ /// Thrown when a general API error occurs.
+ public IObservable Delete(long hookId)
+ {
+ return _client.Delete(hookId).ToObservable();
+ }
+ }
+}
diff --git a/Octokit.Tests.Integration/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs b/Octokit.Tests.Integration/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs
new file mode 100644
index 0000000000..4f4583779f
--- /dev/null
+++ b/Octokit.Tests.Integration/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs
@@ -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(() => new EnterprisePreReceiveHooksClient(null));
+ }
+ }
+
+ public class TheGetAllMethod : IDisposable
+ {
+ private readonly IGitHubClient _githubEnterprise;
+ private readonly IEnterprisePreReceiveHooksClient _preReceiveHooksClient;
+ private readonly List _preReceiveHooks;
+
+ public TheGetAllMethod()
+ {
+ _githubEnterprise = EnterpriseHelper.GetAuthenticatedClient();
+ _preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
+
+ _preReceiveHooks = new List();
+ 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(() => _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(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(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(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(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(async () => await _preReceiveHooksClient.Get(preReceiveHook.Id));
+ }
+
+ [GitHubEnterpriseTest]
+ public async Task CannotDeleteWhenHookDoesNotExist()
+ {
+ await Assert.ThrowsAsync(async () => await _preReceiveHooksClient.Delete(-1));
+ }
+ }
+}
diff --git a/Octokit.Tests.Integration/EnterpriseHelper.cs b/Octokit.Tests.Integration/EnterpriseHelper.cs
index bd9f5cde1e..3d123730a4 100644
--- a/Octokit.Tests.Integration/EnterpriseHelper.cs
+++ b/Octokit.Tests.Integration/EnterpriseHelper.cs
@@ -161,6 +161,20 @@ public static void DeletePreReceiveEnvironment(IConnection connection, PreReceiv
}
}
+ 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
diff --git a/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterprisePreReceiveHooksClientTests.cs b/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterprisePreReceiveHooksClientTests.cs
new file mode 100644
index 0000000000..2ec9b074a8
--- /dev/null
+++ b/Octokit.Tests.Integration/Reactive/Enterprise/ObservableEnterprisePreReceiveHooksClientTests.cs
@@ -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(() => new EnterprisePreReceiveHooksClient(null));
+ }
+ }
+
+ public class TheGetAllMethod : IDisposable
+ {
+ private readonly IObservableGitHubClient _githubEnterprise;
+ private readonly IObservableEnterprisePreReceiveHooksClient _preReceiveHooksClient;
+ private readonly List _preReceiveHooks;
+
+ public TheGetAllMethod()
+ {
+ _githubEnterprise = new ObservableGitHubClient(EnterpriseHelper.GetAuthenticatedClient());
+ _preReceiveHooksClient = _githubEnterprise.Enterprise.PreReceiveHook;
+
+ _preReceiveHooks = new List();
+ 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(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(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(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(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(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(async () => await _preReceiveHooksClient.Get(preReceiveHook.Id));
+ }
+
+ [GitHubEnterpriseTest]
+ public async Task CannotDeleteWhenHookDoesNotExist()
+ {
+ await Assert.ThrowsAsync(async () => await _preReceiveHooksClient.Delete(-1));
+ }
+ }
+}
diff --git a/Octokit.Tests/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs b/Octokit.Tests/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs
new file mode 100644
index 0000000000..fac4f0804f
--- /dev/null
+++ b/Octokit.Tests/Clients/Enterprise/EnterprisePreReceiveHooksClientTests.cs
@@ -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(() => new EnterprisePreReceiveHooksClient(null));
+ }
+ }
+
+ public class TheGetAllMethod
+ {
+ [Fact]
+ public async Task RequestsCorrectUrl()
+ {
+ var connection = Substitute.For();
+ var client = new EnterprisePreReceiveHooksClient(connection);
+
+ await client.GetAll();
+
+ connection.Received().GetAll(Arg.Is(u => u.ToString() == "admin/pre-receive-hooks"),
+ null,
+ "application/vnd.github.v3+json",
+ Args.ApiOptions);
+ }
+
+ [Fact]
+ public async Task RequestsCorrectUrlWithApiOptions()
+ {
+ var connection = Substitute.For();
+ var client = new EnterprisePreReceiveHooksClient(connection);
+
+ var options = new ApiOptions
+ {
+ PageSize = 1,
+ PageCount = 1,
+ StartPage = 1
+ };
+
+ await client.GetAll(options);
+
+ connection.Received().GetAll(Arg.Is(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();
+ var client = new EnterprisePreReceiveHooksClient(connection);
+
+ await client.Get(1);
+
+ connection.Received().Get(Arg.Is(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();
+ var client = new EnterprisePreReceiveHooksClient(connection);
+ var data = new NewPreReceiveHook("name", "repo", "script", 1);
+
+ await client.Create(data);
+
+ connection.Received().Post(Arg.Is(u => u.ToString() == "admin/pre-receive-hooks"),
+ data,
+ "application/vnd.github.v3+json");
+ }
+
+ [Fact]
+ public async Task EnsuresNonNullArguments()
+ {
+ var client = new EnterprisePreReceiveHooksClient(Substitute.For());
+
+ await Assert.ThrowsAsync(() => client.Create(null));
+
+ Assert.Throws(() => new NewPreReceiveHook(null, "repo", "script", 1));
+ Assert.Throws(() => new NewPreReceiveHook("", "repo", "script", 1));
+ Assert.Throws(() => new NewPreReceiveHook("name", null, "script", 1));
+ Assert.Throws(() => new NewPreReceiveHook("name", "", "script", 1));
+ Assert.Throws(() => new NewPreReceiveHook("name", "repo", null, 1));
+ Assert.Throws(() => new NewPreReceiveHook("name", "repo", "", 1));
+ }
+ }
+
+ public class TheEditMethod
+ {
+ [Fact]
+ public async Task RequestsTheCorrectUrl()
+ {
+ var connection = Substitute.For();
+ 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(Arg.Is(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());
+
+ await Assert.ThrowsAsync(() => client.Edit(1, null));
+ }
+ }
+
+ public class TheDeleteMethod
+ {
+ [Fact]
+ public async Task RequestsTheCorrectUrl()
+ {
+ var connection = Substitute.For();
+ var client = new EnterprisePreReceiveHooksClient(connection);
+
+ await client.Delete(1);
+
+ connection.Received().Delete(Arg.Is(u => u.ToString() == "admin/pre-receive-hooks/1"),
+ Arg.Any