github.com/google/go-github/v49@v49.1.0/github/users_projects.go (about) 1 // Copyright 2019 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 // ListProjects lists the projects for the specified user. 14 // 15 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#list-user-projects 16 func (s *UsersService) ListProjects(ctx context.Context, user string, opts *ProjectListOptions) ([]*Project, *Response, error) { 17 u := fmt.Sprintf("users/%v/projects", user) 18 u, err := addOptions(u, opts) 19 if err != nil { 20 return nil, nil, err 21 } 22 23 req, err := s.client.NewRequest("GET", u, nil) 24 if err != nil { 25 return nil, nil, err 26 } 27 28 // TODO: remove custom Accept header when this API fully launches. 29 req.Header.Set("Accept", mediaTypeProjectsPreview) 30 31 var projects []*Project 32 resp, err := s.client.Do(ctx, req, &projects) 33 if err != nil { 34 return nil, resp, err 35 } 36 37 return projects, resp, nil 38 } 39 40 // CreateUserProjectOptions specifies the parameters to the UsersService.CreateProject method. 41 type CreateUserProjectOptions struct { 42 // The name of the project. (Required.) 43 Name string `json:"name"` 44 // The description of the project. (Optional.) 45 Body *string `json:"body,omitempty"` 46 } 47 48 // CreateProject creates a GitHub Project for the current user. 49 // 50 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#create-a-user-project 51 func (s *UsersService) CreateProject(ctx context.Context, opts *CreateUserProjectOptions) (*Project, *Response, error) { 52 u := "user/projects" 53 req, err := s.client.NewRequest("POST", u, opts) 54 if err != nil { 55 return nil, nil, err 56 } 57 58 // TODO: remove custom Accept header when this API fully launches. 59 req.Header.Set("Accept", mediaTypeProjectsPreview) 60 61 project := &Project{} 62 resp, err := s.client.Do(ctx, req, project) 63 if err != nil { 64 return nil, resp, err 65 } 66 67 return project, resp, nil 68 }