Debian Codegraph @

github/repos_projects.go

raw
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/rest/projects/projects#list-repository-projects
25 //
26 //meta:operation GET /repos/{owner}/{repo}/projects
27 func (s *RepositoriesService) ListProjects(ctx context.Context, owner, repo string, opts *ProjectListOptions) ([]*Project, *Response, error) {
28 u := fmt.Sprintf("repos/%v/%v/projects", owner, repo)
29 u, err := addOptions(u, opts)
30 if err != nil {
31 return nil, nil, err
32 }
33
34 req, err := s.client.NewRequest("GET", u, nil)
35 if err != nil {
36 return nil, nil, err
37 }
38
39 // TODO: remove custom Accept headers when APIs fully launch.
40 req.Header.Set("Accept", mediaTypeProjectsPreview)
41
42 var projects []*Project
43 resp, err := s.client.Do(ctx, req, &projects)
44 if err != nil {
45 return nil, resp, err
46 }
47
48 return projects, resp, nil
49 }
50
51 // CreateProject creates a GitHub Project for the specified repository.
52 //
53 // GitHub API docs: https://docs.github.com/rest/projects/projects#create-a-repository-project
54 //
55 //meta:operation POST /repos/{owner}/{repo}/projects
56 func (s *RepositoriesService) CreateProject(ctx context.Context, owner, repo string, opts *ProjectOptions) (*Project, *Response, error) {
57 u := fmt.Sprintf("repos/%v/%v/projects", owner, repo)
58 req, err := s.client.NewRequest("POST", u, opts)
59 if err != nil {
60 return nil, nil, err
61 }
62
63 // TODO: remove custom Accept headers when APIs fully launch.
64 req.Header.Set("Accept", mediaTypeProjectsPreview)
65
66 project := &Project{}
67 resp, err := s.client.Do(ctx, req, project)
68 if err != nil {
69 return nil, resp, err
70 }
71
72 return project, resp, nil
73 }