github.com/google/go-github/v65@v65.0.0/github/orgs_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  // ListProjects lists the projects for an organization.
    14  //
    15  // GitHub API docs: https://docs.github.com/rest/projects/projects#list-organization-projects
    16  //
    17  //meta:operation GET /orgs/{org}/projects
    18  func (s *OrganizationsService) ListProjects(ctx context.Context, org string, opts *ProjectListOptions) ([]*Project, *Response, error) {
    19  	u := fmt.Sprintf("orgs/%v/projects", org)
    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  // CreateProject creates a GitHub Project for the specified organization.
    43  //
    44  // GitHub API docs: https://docs.github.com/rest/projects/projects#create-an-organization-project
    45  //
    46  //meta:operation POST /orgs/{org}/projects
    47  func (s *OrganizationsService) CreateProject(ctx context.Context, org string, opts *ProjectOptions) (*Project, *Response, error) {
    48  	u := fmt.Sprintf("orgs/%v/projects", org)
    49  	req, err := s.client.NewRequest("POST", u, opts)
    50  	if err != nil {
    51  		return nil, nil, err
    52  	}
    53  
    54  	// TODO: remove custom Accept header when this API fully launches.
    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  }