apps_installation.go 2.9 KB

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