|
| 1 | +// Copyright 2025 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 | +// ProjectV2 represents a v2 project. |
| 20 | +type ProjectV2 struct { |
| 21 | + ID *int64 `json:"id,omitempty"` |
| 22 | + NodeID *string `json:"node_id,omitempty"` |
| 23 | + Owner *User `json:"owner,omitempty"` |
| 24 | + Creator *User `json:"creator,omitempty"` |
| 25 | + Title *string `json:"title,omitempty"` |
| 26 | + Description *string `json:"description,omitempty"` |
| 27 | + Public *bool `json:"public,omitempty"` |
| 28 | + ClosedAt *Timestamp `json:"closed_at,omitempty"` |
| 29 | + CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 30 | + UpdatedAt *Timestamp `json:"updated_at,omitempty"` |
| 31 | + DeletedAt *Timestamp `json:"deleted_at,omitempty"` |
| 32 | + Number *int `json:"number,omitempty"` |
| 33 | + ShortDescription *string `json:"short_description,omitempty"` |
| 34 | + DeletedBy *User `json:"deleted_by,omitempty"` |
| 35 | + |
| 36 | + // Fields migrated from the Project (classic) struct: |
| 37 | + URL *string `json:"url,omitempty"` |
| 38 | + HTMLURL *string `json:"html_url,omitempty"` |
| 39 | + ColumnsURL *string `json:"columns_url,omitempty"` |
| 40 | + OwnerURL *string `json:"owner_url,omitempty"` |
| 41 | + Name *string `json:"name,omitempty"` |
| 42 | + Body *string `json:"body,omitempty"` |
| 43 | + State *string `json:"state,omitempty"` |
| 44 | + OrganizationPermission *string `json:"organization_permission,omitempty"` |
| 45 | + Private *bool `json:"private,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +func (p ProjectV2) String() string { return Stringify(p) } |
| 49 | + |
| 50 | +// ListProjectsPaginationOptions specifies optional parameters to list projects for user / organization. |
| 51 | +// |
| 52 | +// Note: Pagination is powered by before/after cursor-style pagination. After the initial call, |
| 53 | +// inspect the returned *Response. Use resp.After as the opts.After value to request |
| 54 | +// the next page, and resp.Before as the opts.Before value to request the previous |
| 55 | +// page. Set either Before or After for a request; if both are |
| 56 | +// supplied GitHub API will return an error. PerPage controls the number of items |
| 57 | +// per page (max 100 per GitHub API docs). |
| 58 | +type ListProjectsPaginationOptions struct { |
| 59 | + // A cursor, as given in the Link header. If specified, the query only searches for events before this cursor. |
| 60 | + Before string `url:"before,omitempty"` |
| 61 | + |
| 62 | + // A cursor, as given in the Link header. If specified, the query only searches for events after this cursor. |
| 63 | + After string `url:"after,omitempty"` |
| 64 | + |
| 65 | + // For paginated result sets, the number of results to include per page. |
| 66 | + PerPage int `url:"per_page,omitempty"` |
| 67 | +} |
| 68 | + |
| 69 | +// ListProjectsOptions specifies optional parameters to list projects for user / organization. |
| 70 | +type ListProjectsOptions struct { |
| 71 | + ListProjectsPaginationOptions |
| 72 | + |
| 73 | + // Q is an optional query string to limit results to projects of the specified type. |
| 74 | + Query string `url:"q,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +// ProjectV2FieldOption represents an option for a project field of type single_select or multi_select. |
| 78 | +// It defines the available choices that can be selected for dropdown-style fields. |
| 79 | +// |
| 80 | +// GitHub API docs: https://docs.github.com/rest/projects/fields |
| 81 | +type ProjectV2FieldOption struct { |
| 82 | + ID string `json:"id,omitempty"` |
| 83 | + // The display name of the option. |
| 84 | + Name string `json:"name,omitempty"` |
| 85 | + // The color associated with this option (e.g., "blue", "red"). |
| 86 | + Color string `json:"color,omitempty"` |
| 87 | + // An optional description for this option. |
| 88 | + Description string `json:"description,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// ProjectV2Field represents a field in a GitHub Projects V2 project. |
| 92 | +// Fields define the structure and data types for project items. |
| 93 | +// |
| 94 | +// GitHub API docs: https://docs.github.com/rest/projects/fields |
| 95 | +type ProjectV2Field struct { |
| 96 | + ID *int64 `json:"id,omitempty"` |
| 97 | + NodeID string `json:"node_id,omitempty"` |
| 98 | + Name string `json:"name,omitempty"` |
| 99 | + DataType string `json:"dataType,omitempty"` |
| 100 | + URL string `json:"url,omitempty"` |
| 101 | + Options []*any `json:"options,omitempty"` |
| 102 | + CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 103 | + UpdatedAt *Timestamp `json:"updated_at,omitempty"` |
| 104 | +} |
| 105 | + |
| 106 | +// ListProjectsForOrg lists Projects V2 for an organization. |
| 107 | +// |
| 108 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#list-projects-for-organization |
| 109 | +// |
| 110 | +//meta:operation GET /orgs/{org}/projectsV2 |
| 111 | +func (s *ProjectsService) ListProjectsForOrg(ctx context.Context, org string, opts *ListProjectsOptions) ([]*ProjectV2, *Response, error) { |
| 112 | + u := fmt.Sprintf("orgs/%v/projectsV2", org) |
| 113 | + u, err := addOptions(u, opts) |
| 114 | + if err != nil { |
| 115 | + return nil, nil, err |
| 116 | + } |
| 117 | + |
| 118 | + req, err := s.client.NewRequest("GET", u, nil) |
| 119 | + if err != nil { |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + var projects []*ProjectV2 |
| 124 | + resp, err := s.client.Do(ctx, req, &projects) |
| 125 | + if err != nil { |
| 126 | + return nil, resp, err |
| 127 | + } |
| 128 | + return projects, resp, nil |
| 129 | +} |
| 130 | + |
| 131 | +// GetProjectForOrg gets a Projects V2 project for an organization by ID. |
| 132 | +// |
| 133 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#get-project-for-organization |
| 134 | +// |
| 135 | +//meta:operation GET /orgs/{org}/projectsV2/{project_number} |
| 136 | +func (s *ProjectsService) GetProjectForOrg(ctx context.Context, org string, projectNumber int) (*ProjectV2, *Response, error) { |
| 137 | + u := fmt.Sprintf("orgs/%v/projectsV2/%v", org, projectNumber) |
| 138 | + req, err := s.client.NewRequest("GET", u, nil) |
| 139 | + if err != nil { |
| 140 | + return nil, nil, err |
| 141 | + } |
| 142 | + |
| 143 | + project := new(ProjectV2) |
| 144 | + resp, err := s.client.Do(ctx, req, project) |
| 145 | + if err != nil { |
| 146 | + return nil, resp, err |
| 147 | + } |
| 148 | + return project, resp, nil |
| 149 | +} |
| 150 | + |
| 151 | +// ListProjectsForUser lists Projects V2 for a user. |
| 152 | +// |
| 153 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#list-projects-for-user |
| 154 | +// |
| 155 | +//meta:operation GET /users/{username}/projectsV2 |
| 156 | +func (s *ProjectsService) ListProjectsForUser(ctx context.Context, username string, opts *ListProjectsOptions) ([]*ProjectV2, *Response, error) { |
| 157 | + u := fmt.Sprintf("users/%v/projectsV2", username) |
| 158 | + u, err := addOptions(u, opts) |
| 159 | + if err != nil { |
| 160 | + return nil, nil, err |
| 161 | + } |
| 162 | + req, err := s.client.NewRequest("GET", u, nil) |
| 163 | + if err != nil { |
| 164 | + return nil, nil, err |
| 165 | + } |
| 166 | + |
| 167 | + var projects []*ProjectV2 |
| 168 | + resp, err := s.client.Do(ctx, req, &projects) |
| 169 | + if err != nil { |
| 170 | + return nil, resp, err |
| 171 | + } |
| 172 | + return projects, resp, nil |
| 173 | +} |
| 174 | + |
| 175 | +// GetProjectForUser gets a Projects V2 project for a user by ID. |
| 176 | +// |
| 177 | +// GitHub API docs: https://docs.github.com/rest/projects/projects#get-project-for-user |
| 178 | +// |
| 179 | +//meta:operation GET /users/{username}/projectsV2/{project_number} |
| 180 | +func (s *ProjectsService) GetProjectForUser(ctx context.Context, username string, projectNumber int) (*ProjectV2, *Response, error) { |
| 181 | + u := fmt.Sprintf("users/%v/projectsV2/%v", username, projectNumber) |
| 182 | + req, err := s.client.NewRequest("GET", u, nil) |
| 183 | + if err != nil { |
| 184 | + return nil, nil, err |
| 185 | + } |
| 186 | + |
| 187 | + project := new(ProjectV2) |
| 188 | + resp, err := s.client.Do(ctx, req, project) |
| 189 | + if err != nil { |
| 190 | + return nil, resp, err |
| 191 | + } |
| 192 | + return project, resp, nil |
| 193 | +} |
| 194 | + |
| 195 | +// ListProjectFieldsForOrg lists Projects V2 for an organization. |
| 196 | +// |
| 197 | +// GitHub API docs: https://docs.github.com/rest/projects/fields#list-project-fields-for-organization |
| 198 | +// |
| 199 | +//meta:operation GET /orgs/{org}/projectsV2/{project_number}/fields |
| 200 | +func (s *ProjectsService) ListProjectFieldsForOrg(ctx context.Context, org string, projectNumber int, opts *ListProjectsOptions) ([]*ProjectV2Field, *Response, error) { |
| 201 | + u := fmt.Sprintf("orgs/%v/projectsV2/%v/fields", org, projectNumber) |
| 202 | + u, err := addOptions(u, opts) |
| 203 | + if err != nil { |
| 204 | + return nil, nil, err |
| 205 | + } |
| 206 | + |
| 207 | + req, err := s.client.NewRequest("GET", u, nil) |
| 208 | + if err != nil { |
| 209 | + return nil, nil, err |
| 210 | + } |
| 211 | + |
| 212 | + var fields []*ProjectV2Field |
| 213 | + resp, err := s.client.Do(ctx, req, &fields) |
| 214 | + if err != nil { |
| 215 | + return nil, resp, err |
| 216 | + } |
| 217 | + return fields, resp, nil |
| 218 | +} |
0 commit comments