github.com/google/go-github/v74@v74.0.0/github/apps_installation.go (about)

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