apps_installation.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // Installation represents a GitHub Apps installation.
  11. type Installation struct {
  12. ID *int64 `json:"id,omitempty"`
  13. Account *User `json:"account,omitempty"`
  14. AccessTokensURL *string `json:"access_tokens_url,omitempty"`
  15. RepositoriesURL *string `json:"repositories_url,omitempty"`
  16. HTMLURL *string `json:"html_url,omitempty"`
  17. }
  18. func (i Installation) String() string {
  19. return Stringify(i)
  20. }
  21. // ListRepos lists the repositories that are accessible to the authenticated installation.
  22. //
  23. // GitHub API docs: https://developer.github.com/v3/apps/installations/#list-repositories
  24. func (s *AppsService) ListRepos(ctx context.Context, opt *ListOptions) ([]*Repository, *Response, error) {
  25. u, err := addOptions("installation/repositories", opt)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. req, err := s.client.NewRequest("GET", u, nil)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. // TODO: remove custom Accept header when this API fully launches.
  34. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  35. var r struct {
  36. Repositories []*Repository `json:"repositories"`
  37. }
  38. resp, err := s.client.Do(ctx, req, &r)
  39. if err != nil {
  40. return nil, resp, err
  41. }
  42. return r.Repositories, resp, nil
  43. }
  44. // ListUserRepos lists repositories that are accessible
  45. // to the authenticated user for an installation.
  46. //
  47. // GitHub API docs: https://developer.github.com/v3/apps/installations/#list-repositories-accessible-to-the-user-for-an-installation
  48. func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opt *ListOptions) ([]*Repository, *Response, error) {
  49. u := fmt.Sprintf("user/installations/%v/repositories", id)
  50. u, err := addOptions(u, opt)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. req, err := s.client.NewRequest("GET", u, nil)
  55. if err != nil {
  56. return nil, nil, err
  57. }
  58. // TODO: remove custom Accept header when this API fully launches.
  59. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  60. var r struct {
  61. Repositories []*Repository `json:"repositories"`
  62. }
  63. resp, err := s.client.Do(ctx, req, &r)
  64. if err != nil {
  65. return nil, resp, err
  66. }
  67. return r.Repositories, resp, nil
  68. }
  69. // AddRepository adds a single repository to an installation.
  70. //
  71. // GitHub API docs: https://developer.github.com/v3/apps/installations/#add-repository-to-installation
  72. func (s *AppsService) AddRepository(ctx context.Context, instID, repoID int64) (*Repository, *Response, error) {
  73. u := fmt.Sprintf("apps/installations/%v/repositories/%v", instID, repoID)
  74. req, err := s.client.NewRequest("PUT", u, nil)
  75. if err != nil {
  76. return nil, nil, err
  77. }
  78. r := new(Repository)
  79. resp, err := s.client.Do(ctx, req, r)
  80. if err != nil {
  81. return nil, resp, err
  82. }
  83. return r, resp, nil
  84. }
  85. // RemoveRepository removes a single repository from an installation.
  86. //
  87. // GitHub docs: https://developer.github.com/v3/apps/installations/#remove-repository-from-installation
  88. func (s *AppsService) RemoveRepository(ctx context.Context, instID, repoID int64) (*Response, error) {
  89. u := fmt.Sprintf("apps/installations/%v/repositories/%v", instID, repoID)
  90. req, err := s.client.NewRequest("DELETE", u, nil)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return s.client.Do(ctx, req, nil)
  95. }