本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.9KB

  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. NodeID *string `json:"node_id,omitempty"`
  20. Owner *User `json:"owner,omitempty"`
  21. Name *string `json:"name,omitempty"`
  22. Description *string `json:"description,omitempty"`
  23. ExternalURL *string `json:"external_url,omitempty"`
  24. HTMLURL *string `json:"html_url,omitempty"`
  25. CreatedAt *time.Time `json:"created_at,omitempty"`
  26. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  27. }
  28. // InstallationToken represents an installation token.
  29. type InstallationToken struct {
  30. Token *string `json:"token,omitempty"`
  31. ExpiresAt *time.Time `json:"expires_at,omitempty"`
  32. }
  33. // InstallationPermissions lists the permissions for metadata, contents, issues and single file for an installation.
  34. type InstallationPermissions struct {
  35. Metadata *string `json:"metadata,omitempty"`
  36. Contents *string `json:"contents,omitempty"`
  37. Issues *string `json:"issues,omitempty"`
  38. SingleFile *string `json:"single_file,omitempty"`
  39. }
  40. // Installation represents a GitHub Apps installation.
  41. type Installation struct {
  42. ID *int64 `json:"id,omitempty"`
  43. AppID *int64 `json:"app_id,omitempty"`
  44. TargetID *int64 `json:"target_id,omitempty"`
  45. Account *User `json:"account,omitempty"`
  46. AccessTokensURL *string `json:"access_tokens_url,omitempty"`
  47. RepositoriesURL *string `json:"repositories_url,omitempty"`
  48. HTMLURL *string `json:"html_url,omitempty"`
  49. TargetType *string `json:"target_type,omitempty"`
  50. SingleFileName *string `json:"single_file_name,omitempty"`
  51. RepositorySelection *string `json:"repository_selection,omitempty"`
  52. Events []string `json:"events,omitempty"`
  53. Permissions *InstallationPermissions `json:"permissions,omitempty"`
  54. CreatedAt *Timestamp `json:"created_at,omitempty"`
  55. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  56. }
  57. func (i Installation) String() string {
  58. return Stringify(i)
  59. }
  60. // Get a single GitHub App. Passing the empty string will get
  61. // the authenticated GitHub App.
  62. //
  63. // Note: appSlug is just the URL-friendly name of your GitHub App.
  64. // You can find this on the settings page for your GitHub App
  65. // (e.g., https://github.com/settings/apps/:app_slug).
  66. //
  67. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-github-app
  68. func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
  69. var u string
  70. if appSlug != "" {
  71. u = fmt.Sprintf("apps/%v", appSlug)
  72. } else {
  73. u = "app"
  74. }
  75. req, err := s.client.NewRequest("GET", u, nil)
  76. if err != nil {
  77. return nil, nil, err
  78. }
  79. // TODO: remove custom Accept header when this API fully launches.
  80. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  81. app := new(App)
  82. resp, err := s.client.Do(ctx, req, app)
  83. if err != nil {
  84. return nil, resp, err
  85. }
  86. return app, resp, nil
  87. }
  88. // ListInstallations lists the installations that the current GitHub App has.
  89. //
  90. // GitHub API docs: https://developer.github.com/v3/apps/#find-installations
  91. func (s *AppsService) ListInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  92. u, err := addOptions("app/installations", opt)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. req, err := s.client.NewRequest("GET", u, nil)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. // TODO: remove custom Accept header when this API fully launches.
  101. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  102. var i []*Installation
  103. resp, err := s.client.Do(ctx, req, &i)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return i, resp, nil
  108. }
  109. // GetInstallation returns the specified installation.
  110. //
  111. // GitHub API docs: https://developer.github.com/v3/apps/#get-a-single-installation
  112. func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
  113. return s.getInstallation(ctx, fmt.Sprintf("app/installations/%v", id))
  114. }
  115. // ListUserInstallations lists installations that are accessible to the authenticated user.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/apps/#list-installations-for-user
  118. func (s *AppsService) ListUserInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
  119. u, err := addOptions("user/installations", opt)
  120. if err != nil {
  121. return nil, nil, err
  122. }
  123. req, err := s.client.NewRequest("GET", u, nil)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. // TODO: remove custom Accept header when this API fully launches.
  128. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  129. var i struct {
  130. Installations []*Installation `json:"installations"`
  131. }
  132. resp, err := s.client.Do(ctx, req, &i)
  133. if err != nil {
  134. return nil, resp, err
  135. }
  136. return i.Installations, resp, nil
  137. }
  138. // CreateInstallationToken creates a new installation token.
  139. //
  140. // GitHub API docs: https://developer.github.com/v3/apps/#create-a-new-installation-token
  141. func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64) (*InstallationToken, *Response, error) {
  142. u := fmt.Sprintf("app/installations/%v/access_tokens", id)
  143. req, err := s.client.NewRequest("POST", u, nil)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. // TODO: remove custom Accept header when this API fully launches.
  148. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  149. t := new(InstallationToken)
  150. resp, err := s.client.Do(ctx, req, t)
  151. if err != nil {
  152. return nil, resp, err
  153. }
  154. return t, resp, nil
  155. }
  156. // FindOrganizationInstallation finds the organization's installation information.
  157. //
  158. // GitHub API docs: https://developer.github.com/v3/apps/#find-organization-installation
  159. func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error) {
  160. return s.getInstallation(ctx, fmt.Sprintf("orgs/%v/installation", org))
  161. }
  162. // FindRepositoryInstallation finds the repository's installation information.
  163. //
  164. // GitHub API docs: https://developer.github.com/v3/apps/#find-repository-installation
  165. func (s *AppsService) FindRepositoryInstallation(ctx context.Context, owner, repo string) (*Installation, *Response, error) {
  166. return s.getInstallation(ctx, fmt.Sprintf("repos/%v/%v/installation", owner, repo))
  167. }
  168. // FindRepositoryInstallationByID finds the repository's installation information.
  169. //
  170. // Note: FindRepositoryInstallationByID uses the undocumented GitHub API endpoint /repositories/:id/installation.
  171. func (s *AppsService) FindRepositoryInstallationByID(ctx context.Context, id int64) (*Installation, *Response, error) {
  172. return s.getInstallation(ctx, fmt.Sprintf("repositories/%d/installation", id))
  173. }
  174. // FindUserInstallation finds the user's installation information.
  175. //
  176. // GitHub API docs: https://developer.github.com/v3/apps/#find-repository-installation
  177. func (s *AppsService) FindUserInstallation(ctx context.Context, user string) (*Installation, *Response, error) {
  178. return s.getInstallation(ctx, fmt.Sprintf("users/%v/installation", user))
  179. }
  180. func (s *AppsService) getInstallation(ctx context.Context, url string) (*Installation, *Response, error) {
  181. req, err := s.client.NewRequest("GET", url, nil)
  182. if err != nil {
  183. return nil, nil, err
  184. }
  185. // TODO: remove custom Accept header when this API fully launches.
  186. req.Header.Set("Accept", mediaTypeIntegrationPreview)
  187. i := new(Installation)
  188. resp, err := s.client.Do(ctx, req, i)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return i, resp, nil
  193. }
上海开阖软件有限公司 沪ICP备12045867号-1