using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Repository Webhooks API. /// /// /// See the Webhooks API documentation for more information. /// public class RepositoryHooksClient : ApiClient, IRepositoryHooksClient { /// /// Initializes a new GitHub Webhooks API client. /// /// An API connection. public RepositoryHooksClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Gets the list of hooks defined for a repository /// /// The repository's owner /// The repository's name /// See API documentation for more information. public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAll(owner, name, ApiOptions.None); } /// /// Gets the list of hooks defined for a repository /// /// The repository's ID /// See API documentation for more information. public Task> GetAll(int repositoryId) { return GetAll(repositoryId, ApiOptions.None); } /// /// Gets the list of hooks defined for a repository /// /// The repository's owner /// The repository's name /// Options for changing the API response /// See API documentation for more information. public Task> GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryHooks(owner, name), options); } /// /// Gets the list of hooks defined for a repository /// /// The repository's ID /// Options for changing the API response /// See API documentation for more information. public Task> GetAll(int repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepositoryHooks(repositoryId), options); } /// /// Gets a single hook by Id /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public Task Get(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return ApiConnection.Get(ApiUrls.RepositoryHookById(owner, name, hookId)); } /// /// Gets a single hook by Id /// /// The repository's ID /// The repository's hook id /// See API documentation for more information. public Task Get(int repositoryId, int hookId) { return ApiConnection.Get(ApiUrls.RepositoryHookById(repositoryId, hookId)); } /// /// Creates a hook for a repository /// /// The repository's owner /// The repository's name /// The hook's parameters /// See API documentation for more information. public Task Create(string owner, string name, NewRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(hook, "hook"); return ApiConnection.Post(ApiUrls.RepositoryHooks(owner, name), hook.ToRequest()); } /// /// Creates a hook for a repository /// /// The repository's ID /// The hook's parameters /// See API documentation for more information. public Task Create(int repositoryId, NewRepositoryHook hook) { Ensure.ArgumentNotNull(hook, "hook"); return ApiConnection.Post(ApiUrls.RepositoryHooks(repositoryId), hook.ToRequest()); } /// /// Edits a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// The requested changes to an edit repository hook /// See API documentation for more information. public Task Edit(string owner, string name, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(hook, "hook"); return ApiConnection.Patch(ApiUrls.RepositoryHookById(owner, name, hookId), hook); } /// /// Edits a hook for a repository /// /// The repository's ID /// The repository's hook id /// The requested changes to an edit repository hook /// See API documentation for more information. public Task Edit(int repositoryId, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNull(hook, "hook"); return ApiConnection.Patch(ApiUrls.RepositoryHookById(repositoryId, hookId), hook); } /// /// Tests a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. public Task Test(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return ApiConnection.Post(ApiUrls.RepositoryHookTest(owner, name, hookId)); } /// /// Tests a hook for a repository /// /// The repository's ID /// The repository's hook id /// See API documentation for more information. /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. public Task Test(int repositoryId, int hookId) { return ApiConnection.Post(ApiUrls.RepositoryHookTest(repositoryId, hookId)); } /// /// This will trigger a ping event to be sent to the hook. /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public Task Ping(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return ApiConnection.Post(ApiUrls.RepositoryHookPing(owner, name, hookId)); } /// /// This will trigger a ping event to be sent to the hook. /// /// The repository's ID /// The repository's hook id /// See API documentation for more information. public Task Ping(int repositoryId, int hookId) { return ApiConnection.Post(ApiUrls.RepositoryHookPing(repositoryId, hookId)); } /// /// Deletes a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public Task Delete(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return ApiConnection.Delete(ApiUrls.RepositoryHookById(owner, name, hookId)); } /// /// Deletes a hook for a repository /// /// The repository's ID /// The repository's hook id /// See API documentation for more information. public Task Delete(int repositoryId, int hookId) { return ApiConnection.Delete(ApiUrls.RepositoryHookById(repositoryId, hookId)); } } }