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. [ManualRoute("GET", "/repos/{owner}/{repo}/hooks")] public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetAll(owner, name, ApiOptions.None); } /// /// Gets the list of hooks defined for a repository /// /// The Id of the repository /// See API documentation for more information. [ManualRoute("GET", "/repositories/{id}/hooks")] public Task> GetAll(long 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. [ManualRoute("GET", "/repos/{owner}/{repo}/hooks")] public Task> GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryHooks(owner, name), options); } /// /// Gets the list of hooks defined for a repository /// /// The Id of the repository /// Options for changing the API response /// See API documentation for more information. [ManualRoute("GET", "/repositories/{id}/hooks")] public Task> GetAll(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(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. [ManualRoute("GET", "/repos/{owner}/{repo}/hooks/{id}")] public Task Get(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return ApiConnection.Get(ApiUrls.RepositoryHookById(owner, name, hookId)); } /// /// Gets a single hook by Id /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. [ManualRoute("GET", "/repositories/{id}/hooks/{id}")] public Task Get(long 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. [ManualRoute("POST", "/repos/{owner}/{repo}/hooks")] public Task Create(string owner, string name, NewRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(hook, nameof(hook)); return ApiConnection.Post(ApiUrls.RepositoryHooks(owner, name), hook.ToRequest()); } /// /// Creates a hook for a repository /// /// The Id of the repository /// The hook's parameters /// See API documentation for more information. [ManualRoute("POST", "/repositories/{id}/hooks")] public Task Create(long repositoryId, NewRepositoryHook hook) { Ensure.ArgumentNotNull(hook, nameof(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. [ManualRoute("PATCH", "/repos/{owner}/{repo}/hooks/{id}")] public Task Edit(string owner, string name, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(hook, nameof(hook)); return ApiConnection.Patch(ApiUrls.RepositoryHookById(owner, name, hookId), hook); } /// /// Edits a hook for a repository /// /// The Id of the repository /// The repository's hook id /// The requested changes to an edit repository hook /// See API documentation for more information. [ManualRoute("POST", "/repositories/{id}/hooks/{hook_id}")] public Task Edit(long repositoryId, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNull(hook, nameof(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. [ManualRoute("POST", "/repos/{owner}/{repo}/hooks/{id}/tests")] public Task Test(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return ApiConnection.Post(ApiUrls.RepositoryHookTest(owner, name, hookId)); } /// /// Tests a hook for a repository /// /// The Id of the repository /// 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. [ManualRoute("POST", "/repositories/{id}/hooks/{hook_id}/tests")] public Task Test(long 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. [ManualRoute("POST", "/repos/{owner}/{repo}/hooks/{id}/pings")] public Task Ping(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return ApiConnection.Post(ApiUrls.RepositoryHookPing(owner, name, hookId)); } /// /// This will trigger a ping event to be sent to the hook. /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. [ManualRoute("POST", "/repositories/{id}/hooks/{hook_id}/pings")] public Task Ping(long 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. [ManualRoute("DELETE", "/repos/{owner}/{repo}/hooks/{id}")] public Task Delete(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return ApiConnection.Delete(ApiUrls.RepositoryHookById(owner, name, hookId)); } /// /// Deletes a hook for a repository /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. [ManualRoute("DELETE", "/repositories/{id}/hooks/{hook_id}")] public Task Delete(long repositoryId, int hookId) { return ApiConnection.Delete(ApiUrls.RepositoryHookById(repositoryId, hookId)); } } }