github.com/google/go-github/v49@v49.1.0/github/repos_projects.go (about) 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 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // ProjectListOptions specifies the optional parameters to the 14 // OrganizationsService.ListProjects and RepositoriesService.ListProjects methods. 15 type ProjectListOptions struct { 16 // Indicates the state of the projects to return. Can be either open, closed, or all. Default: open 17 State string `url:"state,omitempty"` 18 19 ListOptions 20 } 21 22 // ListProjects lists the projects for a repo. 23 // 24 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#list-repository-projects 25 func (s *RepositoriesService) ListProjects(ctx context.Context, owner, repo string, opts *ProjectListOptions) ([]*Project, *Response, error) { 26 u := fmt.Sprintf("repos/%v/%v/projects", owner, repo) 27 u, err := addOptions(u, opts) 28 if err != nil { 29 return nil, nil, err 30 } 31 32 req, err := s.client.NewRequest("GET", u, nil) 33 if err != nil { 34 return nil, nil, err 35 } 36 37 // TODO: remove custom Accept headers when APIs fully launch. 38 req.Header.Set("Accept", mediaTypeProjectsPreview) 39 40 var projects []*Project 41 resp, err := s.client.Do(ctx, req, &projects) 42 if err != nil { 43 return nil, resp, err 44 } 45 46 return projects, resp, nil 47 } 48 49 // CreateProject creates a GitHub Project for the specified repository. 50 // 51 // GitHub API docs: https://docs.github.com/en/rest/projects/projects#create-a-repository-project 52 func (s *RepositoriesService) CreateProject(ctx context.Context, owner, repo string, opts *ProjectOptions) (*Project, *Response, error) { 53 u := fmt.Sprintf("repos/%v/%v/projects", owner, repo) 54 req, err := s.client.NewRequest("POST", u, opts) 55 if err != nil { 56 return nil, nil, err 57 } 58 59 // TODO: remove custom Accept headers when APIs fully launch. 60 req.Header.Set("Accept", mediaTypeProjectsPreview) 61 62 project := &Project{} 63 resp, err := s.client.Do(ctx, req, project) 64 if err != nil { 65 return nil, resp, err 66 } 67 68 return project, resp, nil 69 }