using System; using System.Linq; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Check Suites API. /// /// /// See the Check Suites API documentation for more information. /// public class CheckSuitesClient : ApiClient, ICheckSuitesClient { /// /// Initializes a new GitHub Check Suites API client. /// /// An API connection public CheckSuitesClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Gets a single Check Suite by Id /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The Id of the check suite [Preview("antiope")] [ManualRoute("GET", "/repos/{owner}/{repo}/check-suites/{id}")] public Task Get(string owner, string name, long checkSuiteId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return ApiConnection.Get(ApiUrls.CheckSuite(owner, name, checkSuiteId), null, AcceptHeaders.ChecksApiPreview); } /// /// Gets a single Check Suite by Id /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The Id of the check suite [Preview("antiope")] [ManualRoute("GET", "/repositories/{id}/check-suites/{check_suite_id}")] public Task Get(long repositoryId, long checkSuiteId) { return ApiConnection.Get(ApiUrls.CheckSuite(repositoryId, checkSuiteId), null, AcceptHeaders.ChecksApiPreview); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The reference (SHA, branch name or tag name) to list check suites for [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{ref}/check-suites")] public Task GetAllForReference(string owner, string name, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return GetAllForReference(owner, name, reference, new CheckSuiteRequest(), ApiOptions.None); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The reference (SHA, branch name or tag name) to list check suites for [ManualRoute("GET", "/repositories/{id}/commits/{ref}/check-suites")] public Task GetAllForReference(long repositoryId, string reference) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return GetAllForReference(repositoryId, reference, new CheckSuiteRequest(), ApiOptions.None); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The reference (SHA, branch name or tag name) to list check suites for /// Details to filter the request, such as by App Id or Check Name [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{ref}/check-suites")] public Task GetAllForReference(string owner, string name, string reference, CheckSuiteRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNull(request, nameof(request)); return GetAllForReference(owner, name, reference, request, ApiOptions.None); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The reference (SHA, branch name or tag name) to list check suites for /// Details to filter the request, such as by App Id or Check Name [ManualRoute("GET", "/repositories/{id}/commits/{ref}/check-suites")] public Task GetAllForReference(long repositoryId, string reference, CheckSuiteRequest request) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNull(request, nameof(request)); return GetAllForReference(repositoryId, reference, request, ApiOptions.None); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The reference (SHA, branch name or tag name) to list check suites for /// Details to filter the request, such as by App Id or Check Name /// Options to change the API response [Preview("antiope")] [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{ref}/check-suites")] public async Task GetAllForReference(string owner, string name, string reference, CheckSuiteRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); var results = await ApiConnection.GetAll(ApiUrls.CheckSuitesForReference(owner, name, reference), request.ToParametersDictionary(), AcceptHeaders.ChecksApiPreview, options).ConfigureAwait(false); return new CheckSuitesResponse( results.Count > 0 ? results.Max(x => x.TotalCount) : 0, results.SelectMany(x => x.CheckSuites).ToList()); } /// /// Lists Check Suites for a commit reference (SHA, branch name or tag name) /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The reference (SHA, branch name or tag name) to list check suites for /// Details to filter the request, such as by App Id or Check Name /// Options to change the API response [Preview("antiope")] [ManualRoute("GET", "/repositories/{id}/commits/{ref}/check-suites")] public async Task GetAllForReference(long repositoryId, string reference, CheckSuiteRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); var results = await ApiConnection.GetAll(ApiUrls.CheckSuitesForReference(repositoryId, reference), request.ToParametersDictionary(), AcceptHeaders.ChecksApiPreview, options).ConfigureAwait(false); return new CheckSuitesResponse( results.Count > 0 ? results.Max(x => x.TotalCount) : 0, results.SelectMany(x => x.CheckSuites).ToList()); } /// /// Updates Check Suites preferences on a repository, such as disabling automatic creation when code is pushed /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The check suite preferences [Preview("antiope")] [ManualRoute("PATCH", "/repos/{owner}/{repo}/check-suites/preferences")] public Task UpdatePreferences(string owner, string name, CheckSuitePreferences preferences) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(preferences, nameof(preferences)); return ApiConnection.Patch(ApiUrls.CheckSuitePreferences(owner, name), preferences, AcceptHeaders.ChecksApiPreview); } /// /// Updates Check Suites preferences on a repository, such as disabling automatic creation when code is pushed /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The check suite preferences [Preview("antiope")] [ManualRoute("GET", "/repositories/{id}/check-suites/preferences")] public Task UpdatePreferences(long repositoryId, CheckSuitePreferences preferences) { Ensure.ArgumentNotNull(preferences, nameof(preferences)); return ApiConnection.Patch(ApiUrls.CheckSuitePreferences(repositoryId), preferences, AcceptHeaders.ChecksApiPreview); } /// /// Creates a new Check Suite /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Details of the Check Suite to create [Preview("antiope")] [ManualRoute("POST", "/repos/{owner}/{repo}/check-suites")] public Task Create(string owner, string name, NewCheckSuite newCheckSuite) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(newCheckSuite, nameof(newCheckSuite)); return ApiConnection.Post(ApiUrls.CheckSuites(owner, name), newCheckSuite, AcceptHeaders.ChecksApiPreview); } /// /// Creates a new Check Suite /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// Details of the Check Suite to create [Preview("antiope")] [ManualRoute("GET", "/repositories/{id}/check-suites")] public Task Create(long repositoryId, NewCheckSuite newCheckSuite) { Ensure.ArgumentNotNull(newCheckSuite, nameof(newCheckSuite)); return ApiConnection.Post(ApiUrls.CheckSuites(repositoryId), newCheckSuite, AcceptHeaders.ChecksApiPreview); } /// /// Triggers GitHub to rerequest an existing check suite, without pushing new code to a repository /// /// /// See the Check Suites API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The Id of the check suite [Preview("antiope")] [ManualRoute("GET", "/repos/{owner}/{repo}/check-suites/{2}/rerequest")] public async Task Rerequest(string owner, string name, long checkSuiteId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); var httpStatusCode = await Connection.Post(ApiUrls.CheckSuiteRerequest(owner, name, checkSuiteId), null, AcceptHeaders.ChecksApiPreview).ConfigureAwait(false); if (httpStatusCode != HttpStatusCode.Created) { throw new ApiException("Invalid Status Code returned. Expected a 201", httpStatusCode); } return httpStatusCode == HttpStatusCode.Created; } /// /// Triggers GitHub to rerequest an existing check suite, without pushing new code to a repository /// /// /// See the Check Suites API documentation for more information. /// /// The Id of the repository /// The Id of the check suite [Preview("antiope")] [ManualRoute("GET", "/repositories/{id}/check-suites/{2}/rerequest")] public async Task Rerequest(long repositoryId, long checkSuiteId) { var httpStatusCode = await Connection.Post(ApiUrls.CheckSuiteRerequest(repositoryId, checkSuiteId), null, AcceptHeaders.ChecksApiPreview).ConfigureAwait(false); if (httpStatusCode != HttpStatusCode.Created) { throw new ApiException("Invalid Status Code returned. Expected a 201", httpStatusCode); } return httpStatusCode == HttpStatusCode.Created; } } }