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