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