github.com/google/go-github/v49@v49.1.0/github/projects.go (about) 1 // Copyright 2016 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 provides access to the projects functions in the 14 // GitHub API. 15 // 16 // GitHub API docs: https://docs.github.com/en/rest/projects 17 type ProjectsService service 18 19 // Project represents a GitHub Project. 20 type Project struct { 21 ID *int64 `json:"id,omitempty"` 22 URL *string `json:"url,omitempty"` 23 HTMLURL *string `json:"html_url,omitempty"` 24 ColumnsURL *string `json:"columns_url,omitempty"` 25 OwnerURL *string `json:"owner_url,omitempty"` 26 Name *string `json:"name,omitempty"` 27 Body *string `json:"body,omitempty"` 28 Number *int `json:"number,omitempty"` 29 State *string `json:"state,omitempty"` 30 CreatedAt *Timestamp `json:"created_at,omitempty"` 31 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 32 NodeID *string `json:"node_id,omitempty"` 33 OrganizationPermission *string `json:"organization_permission,omitempty"` 34 Private *bool `json:"private,omitempty"` 35 36 // The User object that generated the project. 37 Creator *User `json:"creator,omitempty"` 38 } 39 40 func (p Project) String() string { 41 return Stringify(p) 42 } 43 44 // GetProject gets a GitHub Project for a repo. 45 // 46 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#get-a-project 47 func (s *ProjectsService) GetProject(ctx context.Context, id int64) (*Project, *Response, error) { 48 u := fmt.Sprintf("projects/%v", id) 49 req, err := s.client.NewRequest("GET", u, nil) 50 if err != nil { 51 return nil, nil, err 52 } 53 54 // TODO: remove custom Accept headers when APIs fully launch. 55 req.Header.Set("Accept", mediaTypeProjectsPreview) 56 57 project := &Project{} 58 resp, err := s.client.Do(ctx, req, project) 59 if err != nil { 60 return nil, resp, err 61 } 62 63 return project, resp, nil 64 } 65 66 // ProjectOptions specifies the parameters to the 67 // RepositoriesService.CreateProject and 68 // ProjectsService.UpdateProject methods. 69 type ProjectOptions struct { 70 // The name of the project. (Required for creation; optional for update.) 71 Name *string `json:"name,omitempty"` 72 // The body of the project. (Optional.) 73 Body *string `json:"body,omitempty"` 74 75 // The following field(s) are only applicable for update. 76 // They should be left with zero values for creation. 77 78 // State of the project. Either "open" or "closed". (Optional.) 79 State *string `json:"state,omitempty"` 80 // The permission level that all members of the project's organization 81 // will have on this project. 82 // Setting the organization permission is only available 83 // for organization projects. (Optional.) 84 OrganizationPermission *string `json:"organization_permission,omitempty"` 85 // Sets visibility of the project within the organization. 86 // Setting visibility is only available 87 // for organization projects.(Optional.) 88 Private *bool `json:"private,omitempty"` 89 } 90 91 // UpdateProject updates a repository project. 92 // 93 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#update-a-project 94 func (s *ProjectsService) UpdateProject(ctx context.Context, id int64, opts *ProjectOptions) (*Project, *Response, error) { 95 u := fmt.Sprintf("projects/%v", id) 96 req, err := s.client.NewRequest("PATCH", u, opts) 97 if err != nil { 98 return nil, nil, err 99 } 100 101 // TODO: remove custom Accept headers when APIs fully launch. 102 req.Header.Set("Accept", mediaTypeProjectsPreview) 103 104 project := &Project{} 105 resp, err := s.client.Do(ctx, req, project) 106 if err != nil { 107 return nil, resp, err 108 } 109 110 return project, resp, nil 111 } 112 113 // DeleteProject deletes a GitHub Project from a repository. 114 // 115 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#delete-a-project 116 func (s *ProjectsService) DeleteProject(ctx context.Context, id int64) (*Response, error) { 117 u := fmt.Sprintf("projects/%v", id) 118 req, err := s.client.NewRequest("DELETE", u, nil) 119 if err != nil { 120 return nil, err 121 } 122 123 // TODO: remove custom Accept header when this API fully launches. 124 req.Header.Set("Accept", mediaTypeProjectsPreview) 125 126 return s.client.Do(ctx, req, nil) 127 } 128 129 // ProjectColumn represents a column of a GitHub Project. 130 // 131 // GitHub API docs: https://docs.github.com/en/rest/repos/projects/ 132 type ProjectColumn struct { 133 ID *int64 `json:"id,omitempty"` 134 Name *string `json:"name,omitempty"` 135 URL *string `json:"url,omitempty"` 136 ProjectURL *string `json:"project_url,omitempty"` 137 CardsURL *string `json:"cards_url,omitempty"` 138 CreatedAt *Timestamp `json:"created_at,omitempty"` 139 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 140 NodeID *string `json:"node_id,omitempty"` 141 } 142 143 // ListProjectColumns lists the columns of a GitHub Project for a repo. 144 // 145 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#list-project-columns 146 func (s *ProjectsService) ListProjectColumns(ctx context.Context, projectID int64, opts *ListOptions) ([]*ProjectColumn, *Response, error) { 147 u := fmt.Sprintf("projects/%v/columns", projectID) 148 u, err := addOptions(u, opts) 149 if err != nil { 150 return nil, nil, err 151 } 152 153 req, err := s.client.NewRequest("GET", u, nil) 154 if err != nil { 155 return nil, nil, err 156 } 157 158 // TODO: remove custom Accept headers when APIs fully launch. 159 req.Header.Set("Accept", mediaTypeProjectsPreview) 160 161 columns := []*ProjectColumn{} 162 resp, err := s.client.Do(ctx, req, &columns) 163 if err != nil { 164 return nil, resp, err 165 } 166 167 return columns, resp, nil 168 } 169 170 // GetProjectColumn gets a column of a GitHub Project for a repo. 171 // 172 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#get-a-project-column 173 func (s *ProjectsService) GetProjectColumn(ctx context.Context, id int64) (*ProjectColumn, *Response, error) { 174 u := fmt.Sprintf("projects/columns/%v", id) 175 req, err := s.client.NewRequest("GET", u, nil) 176 if err != nil { 177 return nil, nil, err 178 } 179 180 // TODO: remove custom Accept headers when APIs fully launch. 181 req.Header.Set("Accept", mediaTypeProjectsPreview) 182 183 column := &ProjectColumn{} 184 resp, err := s.client.Do(ctx, req, column) 185 if err != nil { 186 return nil, resp, err 187 } 188 189 return column, resp, nil 190 } 191 192 // ProjectColumnOptions specifies the parameters to the 193 // ProjectsService.CreateProjectColumn and 194 // ProjectsService.UpdateProjectColumn methods. 195 type ProjectColumnOptions struct { 196 // The name of the project column. (Required for creation and update.) 197 Name string `json:"name"` 198 } 199 200 // CreateProjectColumn creates a column for the specified (by number) project. 201 // 202 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#create-a-project-column 203 func (s *ProjectsService) CreateProjectColumn(ctx context.Context, projectID int64, opts *ProjectColumnOptions) (*ProjectColumn, *Response, error) { 204 u := fmt.Sprintf("projects/%v/columns", projectID) 205 req, err := s.client.NewRequest("POST", u, opts) 206 if err != nil { 207 return nil, nil, err 208 } 209 210 // TODO: remove custom Accept headers when APIs fully launch. 211 req.Header.Set("Accept", mediaTypeProjectsPreview) 212 213 column := &ProjectColumn{} 214 resp, err := s.client.Do(ctx, req, column) 215 if err != nil { 216 return nil, resp, err 217 } 218 219 return column, resp, nil 220 } 221 222 // UpdateProjectColumn updates a column of a GitHub Project. 223 // 224 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#update-an-existing-project-column 225 func (s *ProjectsService) UpdateProjectColumn(ctx context.Context, columnID int64, opts *ProjectColumnOptions) (*ProjectColumn, *Response, error) { 226 u := fmt.Sprintf("projects/columns/%v", columnID) 227 req, err := s.client.NewRequest("PATCH", u, opts) 228 if err != nil { 229 return nil, nil, err 230 } 231 232 // TODO: remove custom Accept headers when APIs fully launch. 233 req.Header.Set("Accept", mediaTypeProjectsPreview) 234 235 column := &ProjectColumn{} 236 resp, err := s.client.Do(ctx, req, column) 237 if err != nil { 238 return nil, resp, err 239 } 240 241 return column, resp, nil 242 } 243 244 // DeleteProjectColumn deletes a column from a GitHub Project. 245 // 246 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#delete-a-project-column 247 func (s *ProjectsService) DeleteProjectColumn(ctx context.Context, columnID int64) (*Response, error) { 248 u := fmt.Sprintf("projects/columns/%v", columnID) 249 req, err := s.client.NewRequest("DELETE", u, nil) 250 if err != nil { 251 return nil, err 252 } 253 254 // TODO: remove custom Accept header when this API fully launches. 255 req.Header.Set("Accept", mediaTypeProjectsPreview) 256 257 return s.client.Do(ctx, req, nil) 258 } 259 260 // ProjectColumnMoveOptions specifies the parameters to the 261 // ProjectsService.MoveProjectColumn method. 262 type ProjectColumnMoveOptions struct { 263 // Position can be one of "first", "last", or "after:<column-id>", where 264 // <column-id> is the ID of a column in the same project. (Required.) 265 Position string `json:"position"` 266 } 267 268 // MoveProjectColumn moves a column within a GitHub Project. 269 // 270 // GitHub API docs: https://docs.github.com/en/rest/projects/columns#move-a-project-column 271 func (s *ProjectsService) MoveProjectColumn(ctx context.Context, columnID int64, opts *ProjectColumnMoveOptions) (*Response, error) { 272 u := fmt.Sprintf("projects/columns/%v/moves", columnID) 273 req, err := s.client.NewRequest("POST", u, opts) 274 if err != nil { 275 return nil, err 276 } 277 278 // TODO: remove custom Accept header when this API fully launches. 279 req.Header.Set("Accept", mediaTypeProjectsPreview) 280 281 return s.client.Do(ctx, req, nil) 282 } 283 284 // ProjectCard represents a card in a column of a GitHub Project. 285 // 286 // GitHub API docs: https://docs.github.com/en/rest/projects/cards/#get-a-project-card 287 type ProjectCard struct { 288 URL *string `json:"url,omitempty"` 289 ColumnURL *string `json:"column_url,omitempty"` 290 ContentURL *string `json:"content_url,omitempty"` 291 ID *int64 `json:"id,omitempty"` 292 Note *string `json:"note,omitempty"` 293 Creator *User `json:"creator,omitempty"` 294 CreatedAt *Timestamp `json:"created_at,omitempty"` 295 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 296 NodeID *string `json:"node_id,omitempty"` 297 Archived *bool `json:"archived,omitempty"` 298 299 // The following fields are only populated by Webhook events. 300 ColumnID *int64 `json:"column_id,omitempty"` 301 302 // The following fields are only populated by Events API. 303 ProjectID *int64 `json:"project_id,omitempty"` 304 ProjectURL *string `json:"project_url,omitempty"` 305 ColumnName *string `json:"column_name,omitempty"` 306 PreviousColumnName *string `json:"previous_column_name,omitempty"` // Populated in "moved_columns_in_project" event deliveries. 307 } 308 309 // ProjectCardListOptions specifies the optional parameters to the 310 // ProjectsService.ListProjectCards method. 311 type ProjectCardListOptions struct { 312 // ArchivedState is used to list all, archived, or not_archived project cards. 313 // Defaults to not_archived when you omit this parameter. 314 ArchivedState *string `url:"archived_state,omitempty"` 315 316 ListOptions 317 } 318 319 // ListProjectCards lists the cards in a column of a GitHub Project. 320 // 321 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#list-project-cards 322 func (s *ProjectsService) ListProjectCards(ctx context.Context, columnID int64, opts *ProjectCardListOptions) ([]*ProjectCard, *Response, error) { 323 u := fmt.Sprintf("projects/columns/%v/cards", columnID) 324 u, err := addOptions(u, opts) 325 if err != nil { 326 return nil, nil, err 327 } 328 329 req, err := s.client.NewRequest("GET", u, nil) 330 if err != nil { 331 return nil, nil, err 332 } 333 334 // TODO: remove custom Accept headers when APIs fully launch. 335 req.Header.Set("Accept", mediaTypeProjectsPreview) 336 337 cards := []*ProjectCard{} 338 resp, err := s.client.Do(ctx, req, &cards) 339 if err != nil { 340 return nil, resp, err 341 } 342 343 return cards, resp, nil 344 } 345 346 // GetProjectCard gets a card in a column of a GitHub Project. 347 // 348 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#get-a-project-card 349 func (s *ProjectsService) GetProjectCard(ctx context.Context, cardID int64) (*ProjectCard, *Response, error) { 350 u := fmt.Sprintf("projects/columns/cards/%v", cardID) 351 req, err := s.client.NewRequest("GET", u, nil) 352 if err != nil { 353 return nil, nil, err 354 } 355 356 // TODO: remove custom Accept headers when APIs fully launch. 357 req.Header.Set("Accept", mediaTypeProjectsPreview) 358 359 card := &ProjectCard{} 360 resp, err := s.client.Do(ctx, req, card) 361 if err != nil { 362 return nil, resp, err 363 } 364 365 return card, resp, nil 366 } 367 368 // ProjectCardOptions specifies the parameters to the 369 // ProjectsService.CreateProjectCard and 370 // ProjectsService.UpdateProjectCard methods. 371 type ProjectCardOptions struct { 372 // The note of the card. Note and ContentID are mutually exclusive. 373 Note string `json:"note,omitempty"` 374 // The ID (not Number) of the Issue to associate with this card. 375 // Note and ContentID are mutually exclusive. 376 ContentID int64 `json:"content_id,omitempty"` 377 // The type of content to associate with this card. Possible values are: "Issue" and "PullRequest". 378 ContentType string `json:"content_type,omitempty"` 379 // Use true to archive a project card. 380 // Specify false if you need to restore a previously archived project card. 381 Archived *bool `json:"archived,omitempty"` 382 } 383 384 // CreateProjectCard creates a card in the specified column of a GitHub Project. 385 // 386 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#create-a-project-card 387 func (s *ProjectsService) CreateProjectCard(ctx context.Context, columnID int64, opts *ProjectCardOptions) (*ProjectCard, *Response, error) { 388 u := fmt.Sprintf("projects/columns/%v/cards", columnID) 389 req, err := s.client.NewRequest("POST", u, opts) 390 if err != nil { 391 return nil, nil, err 392 } 393 394 // TODO: remove custom Accept headers when APIs fully launch. 395 req.Header.Set("Accept", mediaTypeProjectsPreview) 396 397 card := &ProjectCard{} 398 resp, err := s.client.Do(ctx, req, card) 399 if err != nil { 400 return nil, resp, err 401 } 402 403 return card, resp, nil 404 } 405 406 // UpdateProjectCard updates a card of a GitHub Project. 407 // 408 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#update-an-existing-project-card 409 func (s *ProjectsService) UpdateProjectCard(ctx context.Context, cardID int64, opts *ProjectCardOptions) (*ProjectCard, *Response, error) { 410 u := fmt.Sprintf("projects/columns/cards/%v", cardID) 411 req, err := s.client.NewRequest("PATCH", u, opts) 412 if err != nil { 413 return nil, nil, err 414 } 415 416 // TODO: remove custom Accept headers when APIs fully launch. 417 req.Header.Set("Accept", mediaTypeProjectsPreview) 418 419 card := &ProjectCard{} 420 resp, err := s.client.Do(ctx, req, card) 421 if err != nil { 422 return nil, resp, err 423 } 424 425 return card, resp, nil 426 } 427 428 // DeleteProjectCard deletes a card from a GitHub Project. 429 // 430 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#delete-a-project-card 431 func (s *ProjectsService) DeleteProjectCard(ctx context.Context, cardID int64) (*Response, error) { 432 u := fmt.Sprintf("projects/columns/cards/%v", cardID) 433 req, err := s.client.NewRequest("DELETE", u, nil) 434 if err != nil { 435 return nil, err 436 } 437 438 // TODO: remove custom Accept header when this API fully launches. 439 req.Header.Set("Accept", mediaTypeProjectsPreview) 440 441 return s.client.Do(ctx, req, nil) 442 } 443 444 // ProjectCardMoveOptions specifies the parameters to the 445 // ProjectsService.MoveProjectCard method. 446 type ProjectCardMoveOptions struct { 447 // Position can be one of "top", "bottom", or "after:<card-id>", where 448 // <card-id> is the ID of a card in the same project. 449 Position string `json:"position"` 450 // ColumnID is the ID of a column in the same project. Note that ColumnID 451 // is required when using Position "after:<card-id>" when that card is in 452 // another column; otherwise it is optional. 453 ColumnID int64 `json:"column_id,omitempty"` 454 } 455 456 // MoveProjectCard moves a card within a GitHub Project. 457 // 458 // GitHub API docs: https://docs.github.com/en/rest/projects/cards#move-a-project-card 459 func (s *ProjectsService) MoveProjectCard(ctx context.Context, cardID int64, opts *ProjectCardMoveOptions) (*Response, error) { 460 u := fmt.Sprintf("projects/columns/cards/%v/moves", cardID) 461 req, err := s.client.NewRequest("POST", u, opts) 462 if err != nil { 463 return nil, err 464 } 465 466 // TODO: remove custom Accept header when this API fully launches. 467 req.Header.Set("Accept", mediaTypeProjectsPreview) 468 469 return s.client.Do(ctx, req, nil) 470 } 471 472 // ProjectCollaboratorOptions specifies the optional parameters to the 473 // ProjectsService.AddProjectCollaborator method. 474 type ProjectCollaboratorOptions struct { 475 // Permission specifies the permission to grant to the collaborator. 476 // Possible values are: 477 // "read" - can read, but not write to or administer this project. 478 // "write" - can read and write, but not administer this project. 479 // "admin" - can read, write and administer this project. 480 // 481 // Default value is "write" 482 Permission *string `json:"permission,omitempty"` 483 } 484 485 // AddProjectCollaborator adds a collaborator to an organization project and sets 486 // their permission level. You must be an organization owner or a project admin to add a collaborator. 487 // 488 // GitHub API docs: https://docs.github.com/en/rest/projects/collaborators#add-project-collaborator 489 func (s *ProjectsService) AddProjectCollaborator(ctx context.Context, id int64, username string, opts *ProjectCollaboratorOptions) (*Response, error) { 490 u := fmt.Sprintf("projects/%v/collaborators/%v", id, username) 491 req, err := s.client.NewRequest("PUT", u, opts) 492 if err != nil { 493 return nil, err 494 } 495 496 // TODO: remove custom Accept header when this API fully launches. 497 req.Header.Set("Accept", mediaTypeProjectsPreview) 498 499 return s.client.Do(ctx, req, nil) 500 } 501 502 // RemoveProjectCollaborator removes a collaborator from an organization project. 503 // You must be an organization owner or a project admin to remove a collaborator. 504 // 505 // GitHub API docs: https://docs.github.com/en/rest/projects/collaborators#remove-user-as-a-collaborator 506 func (s *ProjectsService) RemoveProjectCollaborator(ctx context.Context, id int64, username string) (*Response, error) { 507 u := fmt.Sprintf("projects/%v/collaborators/%v", id, username) 508 req, err := s.client.NewRequest("DELETE", u, nil) 509 if err != nil { 510 return nil, err 511 } 512 513 // TODO: remove custom Accept header when this API fully launches. 514 req.Header.Set("Accept", mediaTypeProjectsPreview) 515 516 return s.client.Do(ctx, req, nil) 517 } 518 519 // ListCollaboratorOptions specifies the optional parameters to the 520 // ProjectsService.ListProjectCollaborators method. 521 type ListCollaboratorOptions struct { 522 // Affiliation specifies how collaborators should be filtered by their affiliation. 523 // Possible values are: 524 // "outside" - All outside collaborators of an organization-owned repository 525 // "direct" - All collaborators with permissions to an organization-owned repository, 526 // regardless of organization membership status 527 // "all" - All collaborators the authenticated user can see 528 // 529 // Default value is "all". 530 Affiliation *string `url:"affiliation,omitempty"` 531 532 ListOptions 533 } 534 535 // ListProjectCollaborators lists the collaborators for an organization project. For a project, 536 // the list of collaborators includes outside collaborators, organization members that are direct 537 // collaborators, organization members with access through team memberships, organization members 538 // with access through default organization permissions, and organization owners. You must be an 539 // organization owner or a project admin to list collaborators. 540 // 541 // GitHub API docs: https://docs.github.com/en/rest/projects/collaborators#list-project-collaborators 542 func (s *ProjectsService) ListProjectCollaborators(ctx context.Context, id int64, opts *ListCollaboratorOptions) ([]*User, *Response, error) { 543 u := fmt.Sprintf("projects/%v/collaborators", id) 544 u, err := addOptions(u, opts) 545 if err != nil { 546 return nil, nil, err 547 } 548 549 req, err := s.client.NewRequest("GET", u, nil) 550 if err != nil { 551 return nil, nil, err 552 } 553 554 // TODO: remove custom Accept header when this API fully launches. 555 req.Header.Set("Accept", mediaTypeProjectsPreview) 556 557 var users []*User 558 resp, err := s.client.Do(ctx, req, &users) 559 if err != nil { 560 return nil, resp, err 561 } 562 563 return users, resp, nil 564 } 565 566 // ProjectPermissionLevel represents the permission level an organization 567 // member has for a given project. 568 type ProjectPermissionLevel struct { 569 // Possible values: "admin", "write", "read", "none" 570 Permission *string `json:"permission,omitempty"` 571 572 User *User `json:"user,omitempty"` 573 } 574 575 // ReviewProjectCollaboratorPermission returns the collaborator's permission level for an organization 576 // project. Possible values for the permission key: "admin", "write", "read", "none". 577 // You must be an organization owner or a project admin to review a user's permission level. 578 // 579 // GitHub API docs: https://docs.github.com/en/rest/projects/collaborators#get-project-permission-for-a-user 580 func (s *ProjectsService) ReviewProjectCollaboratorPermission(ctx context.Context, id int64, username string) (*ProjectPermissionLevel, *Response, error) { 581 u := fmt.Sprintf("projects/%v/collaborators/%v/permission", id, username) 582 req, err := s.client.NewRequest("GET", u, nil) 583 if err != nil { 584 return nil, nil, err 585 } 586 587 // TODO: remove custom Accept header when this API fully launches. 588 req.Header.Set("Accept", mediaTypeProjectsPreview) 589 590 ppl := new(ProjectPermissionLevel) 591 resp, err := s.client.Do(ctx, req, ppl) 592 if err != nil { 593 return nil, resp, err 594 } 595 return ppl, resp, nil 596 }