apps.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // InstallationPermissions lists the permissions for metadata, contents, issues and single file for an installation.
  33. type InstallationPermissions struct {
  34. Metadata *string `json:"metadata,omitempty"`
  35. Contents *string `json:"contents,omitempty"`
  36. Issues *string `json:"issues,omitempty"`
  37. SingleFile *string `json:"single_file,omitempty"`
  38. }
  39. // Installation represents a GitHub Apps installation.
  40. type Installation struct {
  41. ID *int64 `json:"id,omitempty"`
  42. AppID *int64 `json:"app_id,omitempty"`
  43. TargetID *int64 `json:"target_id,omitempty"`
  44. Account *User `json:"account,omitempty"`
  45. AccessTokensURL *string `json:"access_tokens_url,omitempty"`
  46. RepositoriesURL *string `json:"repositories_url,omitempty"`
  47. HTMLURL *string `json:"html_url,omitempty"`
  48. TargetType *string `json:"target_type,omitempty"`
  49. SingleFileName *string `json:"single_file_name,omitempty"`
  50. RepositorySelection *string `json:"repository_selection,omitempty"`
  51. Events []string `json:"events,omitempty"`
  52. Permissions *InstallationPermissions `json:"permissions,omitempty"`
  53. }
  54. func (i Installation) String() string {
  55. return Stringify(i)
  56. }
  57. // Get a single GitHub App. Passing the empty string will get
  58. // the authenticated GitHub App.
  59. //
  60. // Note: appSlug is just the URL-friendly name of your GitHub App.
  61. // You can find this on the settings page for your GitHub App
  62. // (e.g., https://github.com/settings/apps/:app_slug).
  63. //
  64. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-github-app
  65. func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
  66. var u string
  67. if appSlug != "" {
  68. u = fmt.Sprintf("apps/%v", appSlug)
  69. } else {
  70. u = "app"
  71. }
  72. req, err := s.client.NewRequest("GET", u, nil)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. // TODO: remove custom Accept header when this API fully launches.
  77. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  78. app := new(App)
  79. resp, err := s.client.Do(ctx, req, app)
  80. if err != nil {
  81. return nil, resp, err
  82. }
  83. return app, resp, nil
  84. }
  85. // ListInstallations lists the installations that the current GitHub App has.
  86. //
  87. // GitHub API docs: https://developer.github.com/v3/apps/#find-installations
  88. func (s *AppsService) ListInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  89. u, err := addOptions("app/installations", opt)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. req, err := s.client.NewRequest("GET", u, nil)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. // TODO: remove custom Accept header when this API fully launches.
  98. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  99. var i []*Installation
  100. resp, err := s.client.Do(ctx, req, &i)
  101. if err != nil {
  102. return nil, resp, err
  103. }
  104. return i, resp, nil
  105. }
  106. // GetInstallation returns the specified installation.
  107. //
  108. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-installation
  109. func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
  110. u := fmt.Sprintf("app/installations/%v", id)
  111. req, err := s.client.NewRequest("GET", u, nil)
  112. if err != nil {
  113. return nil, nil, err
  114. }
  115. // TODO: remove custom Accept header when this API fully launches.
  116. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  117. i := new(Installation)
  118. resp, err := s.client.Do(ctx, req, i)
  119. if err != nil {
  120. return nil, resp, err
  121. }
  122. return i, resp, nil
  123. }
  124. // ListUserInstallations lists installations that are accessible to the authenticated user.
  125. //
  126. // GitHub API docs: https://developer.github.com/v3/apps/#list-installations-for-user
  127. func (s *AppsService) ListUserInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  128. u, err := addOptions("user/installations", opt)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. req, err := s.client.NewRequest("GET", u, nil)
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. // TODO: remove custom Accept header when this API fully launches.
  137. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  138. var i struct {
  139. Installations []*Installation `json:"installations"`
  140. }
  141. resp, err := s.client.Do(ctx, req, &i)
  142. if err != nil {
  143. return nil, resp, err
  144. }
  145. return i.Installations, resp, nil
  146. }
  147. // CreateInstallationToken creates a new installation token.
  148. //
  149. // GitHub API docs: https://developer.github.com/v3/apps/#create-a-new-installation-token
  150. func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64) (*InstallationToken, *Response, error) {
  151. u := fmt.Sprintf("installations/%v/access_tokens", id)
  152. req, err := s.client.NewRequest("POST", u, nil)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. // TODO: remove custom Accept header when this API fully launches.
  157. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  158. t := new(InstallationToken)
  159. resp, err := s.client.Do(ctx, req, t)
  160. if err != nil {
  161. return nil, resp, err
  162. }
  163. return t, resp, nil
  164. }