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