...

Source file src/github.com/google/go-github/v47/github/apps_installation.go

Documentation: github.com/google/go-github/v47/github

     1  // Copyright 2016 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  	"strings"
    12  )
    13  
    14  // ListRepositories represents the response from the list repos endpoints.
    15  type ListRepositories struct {
    16  	TotalCount   *int          `json:"total_count,omitempty"`
    17  	Repositories []*Repository `json:"repositories"`
    18  }
    19  
    20  // ListRepos lists the repositories that are accessible to the authenticated installation.
    21  //
    22  // GitHub API docs: https://docs.github.com/en/rest/apps/installations#list-repositories-accessible-to-the-app-installation
    23  func (s *AppsService) ListRepos(ctx context.Context, opts *ListOptions) (*ListRepositories, *Response, error) {
    24  	u, err := addOptions("installation/repositories", opts)
    25  	if err != nil {
    26  		return nil, nil, err
    27  	}
    28  
    29  	req, err := s.client.NewRequest("GET", u, nil)
    30  	if err != nil {
    31  		return nil, nil, err
    32  	}
    33  
    34  	// TODO: remove custom Accept headers when APIs fully launch.
    35  	acceptHeaders := []string{
    36  		mediaTypeTopicsPreview,
    37  		mediaTypeRepositoryVisibilityPreview,
    38  		mediaTypeRepositoryTemplatePreview,
    39  	}
    40  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
    41  
    42  	var r *ListRepositories
    43  
    44  	resp, err := s.client.Do(ctx, req, &r)
    45  	if err != nil {
    46  		return nil, resp, err
    47  	}
    48  
    49  	return r, resp, nil
    50  }
    51  
    52  // ListUserRepos lists repositories that are accessible
    53  // to the authenticated user for an installation.
    54  //
    55  // GitHub API docs: https://docs.github.com/en/rest/apps/installations#list-repositories-accessible-to-the-user-access-token
    56  func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opts *ListOptions) (*ListRepositories, *Response, error) {
    57  	u := fmt.Sprintf("user/installations/%v/repositories", id)
    58  	u, err := addOptions(u, opts)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	req, err := s.client.NewRequest("GET", u, nil)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	// TODO: remove custom Accept headers when APIs fully launch.
    69  	acceptHeaders := []string{
    70  		mediaTypeTopicsPreview,
    71  		mediaTypeRepositoryVisibilityPreview,
    72  		mediaTypeRepositoryTemplatePreview,
    73  	}
    74  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
    75  
    76  	var r *ListRepositories
    77  	resp, err := s.client.Do(ctx, req, &r)
    78  	if err != nil {
    79  		return nil, resp, err
    80  	}
    81  
    82  	return r, resp, nil
    83  }
    84  
    85  // AddRepository adds a single repository to an installation.
    86  //
    87  // GitHub API docs: https://docs.github.com/en/rest/apps/installations#add-a-repository-to-an-app-installation
    88  func (s *AppsService) AddRepository(ctx context.Context, instID, repoID int64) (*Repository, *Response, error) {
    89  	u := fmt.Sprintf("user/installations/%v/repositories/%v", instID, repoID)
    90  	req, err := s.client.NewRequest("PUT", u, nil)
    91  	if err != nil {
    92  		return nil, nil, err
    93  	}
    94  
    95  	r := new(Repository)
    96  	resp, err := s.client.Do(ctx, req, r)
    97  	if err != nil {
    98  		return nil, resp, err
    99  	}
   100  
   101  	return r, resp, nil
   102  }
   103  
   104  // RemoveRepository removes a single repository from an installation.
   105  //
   106  // GitHub API docs: https://docs.github.com/en/rest/apps/installations#remove-a-repository-from-an-app-installation
   107  func (s *AppsService) RemoveRepository(ctx context.Context, instID, repoID int64) (*Response, error) {
   108  	u := fmt.Sprintf("user/installations/%v/repositories/%v", instID, repoID)
   109  	req, err := s.client.NewRequest("DELETE", u, nil)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return s.client.Do(ctx, req, nil)
   115  }
   116  
   117  // RevokeInstallationToken revokes an installation token.
   118  //
   119  // GitHub API docs: https://docs.github.com/en/rest/apps/installations#revoke-an-installation-access-token
   120  func (s *AppsService) RevokeInstallationToken(ctx context.Context) (*Response, error) {
   121  	u := "installation/token"
   122  	req, err := s.client.NewRequest("DELETE", u, nil)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return s.client.Do(ctx, req, nil)
   128  }
   129  

View as plain text