repos_projects.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2017 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "strings"
  10. )
  11. // ProjectListOptions specifies the optional parameters to the
  12. // OrganizationsService.ListProjects and RepositoriesService.ListProjects methods.
  13. type ProjectListOptions struct {
  14. // Indicates the state of the projects to return. Can be either open, closed, or all. Default: open
  15. State string `url:"state,omitempty"`
  16. ListOptions
  17. }
  18. // ListProjects lists the projects for a repo.
  19. //
  20. // GitHub API docs: https://developer.github.com/v3/projects/#list-repository-projects
  21. func (s *RepositoriesService) ListProjects(ctx context.Context, owner, repo string, opt *ProjectListOptions) ([]*Project, *Response, error) {
  22. u := fmt.Sprintf("repos/%v/%v/projects", owner, repo)
  23. u, err := addOptions(u, opt)
  24. if err != nil {
  25. return nil, nil, err
  26. }
  27. req, err := s.client.NewRequest("GET", u, nil)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. // TODO: remove custom Accept headers when APIs fully launch.
  32. acceptHeaders := []string{mediaTypeProjectsPreview, mediaTypeGraphQLNodeIDPreview}
  33. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  34. var projects []*Project
  35. resp, err := s.client.Do(ctx, req, &projects)
  36. if err != nil {
  37. return nil, resp, err
  38. }
  39. return projects, resp, nil
  40. }
  41. // CreateProject creates a GitHub Project for the specified repository.
  42. //
  43. // GitHub API docs: https://developer.github.com/v3/projects/#create-a-repository-project
  44. func (s *RepositoriesService) CreateProject(ctx context.Context, owner, repo string, opt *ProjectOptions) (*Project, *Response, error) {
  45. u := fmt.Sprintf("repos/%v/%v/projects", owner, repo)
  46. req, err := s.client.NewRequest("POST", u, opt)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. // TODO: remove custom Accept headers when APIs fully launch.
  51. acceptHeaders := []string{mediaTypeProjectsPreview, mediaTypeGraphQLNodeIDPreview}
  52. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  53. project := &Project{}
  54. resp, err := s.client.Do(ctx, req, project)
  55. if err != nil {
  56. return nil, resp, err
  57. }
  58. return project, resp, nil
  59. }