using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Repository Projects API. /// /// /// See the Repository Projects API documentation for more information. /// public class ProjectsClient : ApiClient, IProjectsClient { public ProjectsClient(IApiConnection apiConnection) : base(apiConnection) { Card = new ProjectCardsClient(apiConnection); Column = new ProjectColumnsClient(apiConnection); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository [ManualRoute("GET", "/repos/{owner}/{repo}/projects")] public Task> GetAllForRepository(string owner, string name) { return GetAllForRepository(owner, name, ApiOptions.None); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/repos/{owner}/{repo}/projects")] public Task> GetAllForRepository(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.RepositoryProjects(owner, name), new Dictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Used to filter the list of projects returned [ManualRoute("GET", "/repos/{owner}/{repo}/projects")] public Task> GetAllForRepository(string owner, string name, ProjectRequest request) { return GetAllForRepository(owner, name, request, ApiOptions.None); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Used to filter the list of projects returned /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/repos/{owner}/{repo}/projects")] public Task> GetAllForRepository(string owner, string name, ProjectRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryProjects(owner, name), request.ToParametersDictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository [ManualRoute("GET", "/repositories/{id}/projects")] public Task> GetAllForRepository(long repositoryId) { return GetAllForRepository(repositoryId, ApiOptions.None); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/repositories/{id}/projects")] public Task> GetAllForRepository(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryProjects(repositoryId), new Dictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository /// Used to filter the list of projects returned [ManualRoute("GET", "/repositories/{id}/projects")] public Task> GetAllForRepository(long repositoryId, ProjectRequest request) { return GetAllForRepository(repositoryId, request, ApiOptions.None); } /// /// Get all projects for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository /// Used to filter the list of projects returned /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/repositories/{id}/projects")] public Task> GetAllForRepository(long repositoryId, ProjectRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryProjects(repositoryId), request.ToParametersDictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Get all projects for the specified organization. /// /// /// See the API documentation for more information. /// /// The name of the organization [ManualRoute("GET", "/orgs/{org}/projects")] public Task> GetAllForOrganization(string organization) { return GetAllForOrganization(organization, ApiOptions.None); } /// /// Get all projects for the specified organization. /// /// /// See the API documentation for more information. /// /// The name of the organization /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/orgs/{org}/projects")] public Task> GetAllForOrganization(string organization, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.OrganizationProjects(organization), new Dictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Get all projects for the specified organization. /// /// /// See the API documentation for more information. /// /// The name of the organization /// Used to filter the list of projects returned [ManualRoute("GET", "/orgs/{org}/projects")] public Task> GetAllForOrganization(string organization, ProjectRequest request) { Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization)); Ensure.ArgumentNotNull(request, nameof(request)); return GetAllForOrganization(organization, request, ApiOptions.None); } /// /// Get all projects for the specified organization. /// /// /// See the API documentation for more information. /// /// The name of the organization /// Used to filter the list of projects returned /// Options for changing the API response [Preview("inertia")] [ManualRoute("GET", "/orgs/{org}/projects")] public Task> GetAllForOrganization(string organization, ProjectRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization)); Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.OrganizationProjects(organization), request.ToParametersDictionary(), AcceptHeaders.ProjectsApiPreview, options); } /// /// Gets a single project for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the project [Preview("inertia")] [ManualRoute("GET", "/projects/{project_id}")] public Task Get(int id) { return ApiConnection.Get(ApiUrls.Project(id), null, AcceptHeaders.ProjectsApiPreview); } // NOTE: I think we're missing a Task CreateForRepository(owner, name, newProject) // Can we identify this programatically? /// /// Creates a project for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the repository /// The new project to create for this repository [Preview("inertia")] [ManualRoute("POST", "/repositories/{id}/projects")] public Task CreateForRepository(long repositoryId, NewProject newProject) { Ensure.ArgumentNotNull(newProject, nameof(newProject)); return ApiConnection.Post(ApiUrls.RepositoryProjects(repositoryId), newProject, AcceptHeaders.ProjectsApiPreview); } /// /// Creates a project for the specified organization. /// /// /// See the API documentation for more information. /// /// The name of the organization /// The new project to create for this repository [Preview("inertia")] [ManualRoute("POST", "/orgs/{org}/projects")] public Task CreateForOrganization(string organization, NewProject newProject) { Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization)); Ensure.ArgumentNotNull(newProject, nameof(newProject)); return ApiConnection.Post(ApiUrls.OrganizationProjects(organization), newProject, AcceptHeaders.ProjectsApiPreview); } /// /// Updates a project for this repository. /// /// /// See the API documentation for more information. /// /// The Id of the project /// The modified project [Preview("inertia")] [ManualRoute("PATCH", "/project/{project_id}")] public Task Update(int id, ProjectUpdate projectUpdate) { Ensure.ArgumentNotNull(projectUpdate, nameof(projectUpdate)); return ApiConnection.Patch(ApiUrls.Project(id), projectUpdate, AcceptHeaders.ProjectsApiPreview); } /// /// Deletes a project. /// /// /// See the API documentation for more information. /// /// The Id of the project [Preview("inertia")] [ManualRoute("DELETE", "/project/{project_id}")] public async Task Delete(int id) { var endpoint = ApiUrls.Project(id); try { var httpStatusCode = await Connection.Delete(endpoint, new object(), AcceptHeaders.ProjectsApiPreview).ConfigureAwait(false); return httpStatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// A client for GitHub's Project Cards API. /// /// /// See the Repository Projects API documentation for more information. /// public IProjectCardsClient Card { get; private set; } /// /// A client for GitHub's Project Columns API. /// /// /// See the Repository Projects API documentation for more information. /// public IProjectColumnsClient Column { get; private set; } } }