apps.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "time"
  10. )
  11. // AppsService provides access to the installation related functions
  12. // in the GitHub API.
  13. //
  14. // GitHub API docs: https://developer.github.com/v3/apps/
  15. type AppsService service
  16. // App represents a GitHub App.
  17. type App struct {
  18. ID *int64 `json:"id,omitempty"`
  19. Owner *User `json:"owner,omitempty"`
  20. Name *string `json:"name,omitempty"`
  21. Description *string `json:"description,omitempty"`
  22. ExternalURL *string `json:"external_url,omitempty"`
  23. HTMLURL *string `json:"html_url,omitempty"`
  24. CreatedAt *time.Time `json:"created_at,omitempty"`
  25. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  26. }
  27. // InstallationToken represents an installation token.
  28. type InstallationToken struct {
  29. Token *string `json:"token,omitempty"`
  30. ExpiresAt *time.Time `json:"expires_at,omitempty"`
  31. }
  32. // Get a single GitHub App. Passing the empty string will get
  33. // the authenticated GitHub App.
  34. //
  35. // Note: appSlug is just the URL-friendly name of your GitHub App.
  36. // You can find this on the settings page for your GitHub App
  37. // (e.g., https://github.com/settings/apps/:app_slug).
  38. //
  39. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-github-app
  40. func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
  41. var u string
  42. if appSlug != "" {
  43. u = fmt.Sprintf("apps/%v", appSlug)
  44. } else {
  45. u = "app"
  46. }
  47. req, err := s.client.NewRequest("GET", u, nil)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. // TODO: remove custom Accept header when this API fully launches.
  52. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  53. app := new(App)
  54. resp, err := s.client.Do(ctx, req, app)
  55. if err != nil {
  56. return nil, resp, err
  57. }
  58. return app, resp, nil
  59. }
  60. // ListInstallations lists the installations that the current GitHub App has.
  61. //
  62. // GitHub API docs: https://developer.github.com/v3/apps/#find-installations
  63. func (s *AppsService) ListInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  64. u, err := addOptions("app/installations", opt)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. req, err := s.client.NewRequest("GET", u, nil)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. // TODO: remove custom Accept header when this API fully launches.
  73. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  74. var i []*Installation
  75. resp, err := s.client.Do(ctx, req, &i)
  76. if err != nil {
  77. return nil, resp, err
  78. }
  79. return i, resp, nil
  80. }
  81. // GetInstallation returns the specified installation.
  82. //
  83. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-installation
  84. func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
  85. u := fmt.Sprintf("app/installations/%v", id)
  86. req, err := s.client.NewRequest("GET", u, nil)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. // TODO: remove custom Accept header when this API fully launches.
  91. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  92. i := new(Installation)
  93. resp, err := s.client.Do(ctx, req, i)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return i, resp, nil
  98. }
  99. // ListUserInstallations lists installations that are accessible to the authenticated user.
  100. //
  101. // GitHub API docs: https://developer.github.com/v3/apps/#list-installations-for-user
  102. func (s *AppsService) ListUserInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  103. u, err := addOptions("user/installations", opt)
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. req, err := s.client.NewRequest("GET", u, nil)
  108. if err != nil {
  109. return nil, nil, err
  110. }
  111. // TODO: remove custom Accept header when this API fully launches.
  112. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  113. var i struct {
  114. Installations []*Installation `json:"installations"`
  115. }
  116. resp, err := s.client.Do(ctx, req, &i)
  117. if err != nil {
  118. return nil, resp, err
  119. }
  120. return i.Installations, resp, nil
  121. }
  122. // CreateInstallationToken creates a new installation token.
  123. //
  124. // GitHub API docs: https://developer.github.com/v3/apps/#create-a-new-installation-token
  125. func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64) (*InstallationToken, *Response, error) {
  126. u := fmt.Sprintf("installations/%v/access_tokens", id)
  127. req, err := s.client.NewRequest("POST", u, nil)
  128. if err != nil {
  129. return nil, nil, err
  130. }
  131. // TODO: remove custom Accept header when this API fully launches.
  132. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  133. t := new(InstallationToken)
  134. resp, err := s.client.Do(ctx, req, t)
  135. if err != nil {
  136. return nil, resp, err
  137. }
  138. return t, resp, nil
  139. }