|
| 1 | +// Copyright 2013 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 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | +) |
| 12 | + |
| 13 | +// ProjectsService handles communication with the project V2 |
| 14 | +// methods of the GitHub API. |
| 15 | +// |
| 16 | +// GitHub API docs: https://docs.github.com/rest/projects/projects |
| 17 | +type ProjectsService service |
| 18 | + |
| 19 | +func (p ProjectV2) String() string { return Stringify(p) } |
| 20 | + |
| 21 | +// ListProjectsOptions specifies optional parameters to list organization projects. |
| 22 | +type ListProjectsOptions struct { |
| 23 | + // Q is an optional query string to filter/search projects (when supported). |
| 24 | + Q string `url:"q,omitempty"` |
| 25 | + ListOptions |
| 26 | + ListCursorOptions |
| 27 | +} |
| 28 | + |
| 29 | +// ListOrganizationProjects lists Projects V2 for an organization. |
| 30 | +// |
| 31 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#list-organization-projects |
| 32 | +// |
| 33 | +//meta:operation GET /orgs/{org}/projectsV2 |
| 34 | +func (s *ProjectsService) ListOrganizationProjects(ctx context.Context, org string, opts *ListProjectsOptions) ([]*ProjectV2, *Response, error) { |
| 35 | + u := fmt.Sprintf("orgs/%v/projectsV2", org) |
| 36 | + u, err := addOptions(u, opts) |
| 37 | + if err != nil { |
| 38 | + return nil, nil, err |
| 39 | + } |
| 40 | + |
| 41 | + req, err := s.client.NewRequest("GET", u, nil) |
| 42 | + if err != nil { |
| 43 | + return nil, nil, err |
| 44 | + } |
| 45 | + req.Header.Set("Accept", mediaTypeProjectsPreview) |
| 46 | + |
| 47 | + var projects []*ProjectV2 |
| 48 | + resp, err := s.client.Do(ctx, req, &projects) |
| 49 | + if err != nil { |
| 50 | + return nil, resp, err |
| 51 | + } |
| 52 | + return projects, resp, nil |
| 53 | +} |
| 54 | + |
| 55 | +// GetByOrg gets a Projects V2 project for an organization by ID. |
| 56 | +// |
| 57 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#get-project-for-organization |
| 58 | +// |
| 59 | +//meta:operation GET /orgs/{org}/projectsV2/{project_id} |
| 60 | +func (s *ProjectsService) GetByOrg(ctx context.Context, org string, projectID int64) (*ProjectV2, *Response, error) { |
| 61 | + u := fmt.Sprintf("orgs/%v/projectsV2/%v", org, projectID) |
| 62 | + req, err := s.client.NewRequest("GET", u, nil) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + req.Header.Set("Accept", mediaTypeProjectsPreview) |
| 67 | + |
| 68 | + project := new(ProjectV2) |
| 69 | + resp, err := s.client.Do(ctx, req, project) |
| 70 | + if err != nil { |
| 71 | + return nil, resp, err |
| 72 | + } |
| 73 | + return project, resp, nil |
| 74 | +} |
| 75 | + |
| 76 | +// ListByUser lists Projects V2 for a user. |
| 77 | +// |
| 78 | +// GitHub API docs: https://docs.github.com/en/rest/projects/projects#list-projects-for-user |
| 79 | +// |
| 80 | +//meta:operation GET /users/{username}/projectsV2 |
| 81 | +func (s *ProjectsService) ListByUser(ctx context.Context, username string, opts *ListProjectsOptions) ([]*ProjectV2, *Response, error) { |
| 82 | + u := fmt.Sprintf("users/%v/projectsV2", username) |
| 83 | + u, err := addOptions(u, opts) |
| 84 | + if err != nil { |
| 85 | + return nil, nil, err |
| 86 | + } |
| 87 | + req, err := s.client.NewRequest("GET", u, nil) |
| 88 | + if err != nil { |
| 89 | + return nil, nil, err |
| 90 | + } |
| 91 | + req.Header.Set("Accept", mediaTypeProjectsPreview) |
| 92 | + |
| 93 | + var projects []*ProjectV2 |
| 94 | + resp, err := s.client.Do(ctx, req, &projects) |
| 95 | + if err != nil { |
| 96 | + return nil, resp, err |
| 97 | + } |
| 98 | + return projects, resp, nil |
| 99 | +} |
| 100 | + |
| 101 | +// GetUserProject gets a Projects V2 project for a user by ID. |
| 102 | +// |
| 103 | +// GitHub API docs: https://docs.github.com/en/rest/projects/projects#get-project-for-user |
| 104 | +// |
| 105 | +//meta:operation GET /users/{username}/projectsV2/{project_id} |
| 106 | +func (s *ProjectsService) GetUserProject(ctx context.Context, username string, projectID int64) (*ProjectV2, *Response, error) { |
| 107 | + u := fmt.Sprintf("users/%v/projectsV2/%v", username, projectID) |
| 108 | + req, err := s.client.NewRequest("GET", u, nil) |
| 109 | + if err != nil { |
| 110 | + return nil, nil, err |
| 111 | + } |
| 112 | + req.Header.Set("Accept", mediaTypeProjectsPreview) |
| 113 | + |
| 114 | + project := new(ProjectV2) |
| 115 | + resp, err := s.client.Do(ctx, req, project) |
| 116 | + if err != nil { |
| 117 | + return nil, resp, err |
| 118 | + } |
| 119 | + return project, resp, nil |
| 120 | +} |
0 commit comments