本站源代码
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.

215 lines
7.0KB

  1. // Copyright 2018 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. "errors"
  9. "fmt"
  10. "net/http"
  11. )
  12. // UserMigration represents a GitHub migration (archival).
  13. type UserMigration struct {
  14. ID *int64 `json:"id,omitempty"`
  15. GUID *string `json:"guid,omitempty"`
  16. // State is the current state of a migration.
  17. // Possible values are:
  18. // "pending" which means the migration hasn't started yet,
  19. // "exporting" which means the migration is in progress,
  20. // "exported" which means the migration finished successfully, or
  21. // "failed" which means the migration failed.
  22. State *string `json:"state,omitempty"`
  23. // LockRepositories indicates whether repositories are locked (to prevent
  24. // manipulation) while migrating data.
  25. LockRepositories *bool `json:"lock_repositories,omitempty"`
  26. // ExcludeAttachments indicates whether attachments should be excluded from
  27. // the migration (to reduce migration archive file size).
  28. ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
  29. URL *string `json:"url,omitempty"`
  30. CreatedAt *string `json:"created_at,omitempty"`
  31. UpdatedAt *string `json:"updated_at,omitempty"`
  32. Repositories []*Repository `json:"repositories,omitempty"`
  33. }
  34. func (m UserMigration) String() string {
  35. return Stringify(m)
  36. }
  37. // UserMigrationOptions specifies the optional parameters to Migration methods.
  38. type UserMigrationOptions struct {
  39. // LockRepositories indicates whether repositories should be locked (to prevent
  40. // manipulation) while migrating data.
  41. LockRepositories bool
  42. // ExcludeAttachments indicates whether attachments should be excluded from
  43. // the migration (to reduce migration archive file size).
  44. ExcludeAttachments bool
  45. }
  46. // startUserMigration represents the body of a StartMigration request.
  47. type startUserMigration struct {
  48. // Repositories is a slice of repository names to migrate.
  49. Repositories []string `json:"repositories,omitempty"`
  50. // LockRepositories indicates whether repositories should be locked (to prevent
  51. // manipulation) while migrating data.
  52. LockRepositories *bool `json:"lock_repositories,omitempty"`
  53. // ExcludeAttachments indicates whether attachments should be excluded from
  54. // the migration (to reduce migration archive file size).
  55. ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
  56. }
  57. // StartUserMigration starts the generation of a migration archive.
  58. // repos is a slice of repository names to migrate.
  59. //
  60. // GitHub API docs: https://developer.github.com/v3/migrations/users/#start-a-user-migration
  61. func (s *MigrationService) StartUserMigration(ctx context.Context, repos []string, opt *UserMigrationOptions) (*UserMigration, *Response, error) {
  62. u := "user/migrations"
  63. body := &startUserMigration{Repositories: repos}
  64. if opt != nil {
  65. body.LockRepositories = Bool(opt.LockRepositories)
  66. body.ExcludeAttachments = Bool(opt.ExcludeAttachments)
  67. }
  68. req, err := s.client.NewRequest("POST", u, body)
  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", mediaTypeMigrationsPreview)
  74. m := &UserMigration{}
  75. resp, err := s.client.Do(ctx, req, m)
  76. if err != nil {
  77. return nil, resp, err
  78. }
  79. return m, resp, nil
  80. }
  81. // ListUserMigrations lists the most recent migrations.
  82. //
  83. // GitHub API docs: https://developer.github.com/v3/migrations/users/#get-a-list-of-user-migrations
  84. func (s *MigrationService) ListUserMigrations(ctx context.Context) ([]*UserMigration, *Response, error) {
  85. u := "user/migrations"
  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", mediaTypeMigrationsPreview)
  92. var m []*UserMigration
  93. resp, err := s.client.Do(ctx, req, &m)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return m, resp, nil
  98. }
  99. // UserMigrationStatus gets the status of a specific migration archive.
  100. // id is the migration ID.
  101. //
  102. // GitHub API docs: https://developer.github.com/v3/migrations/users/#get-the-status-of-a-user-migration
  103. func (s *MigrationService) UserMigrationStatus(ctx context.Context, id int64) (*UserMigration, *Response, error) {
  104. u := fmt.Sprintf("user/migrations/%v", id)
  105. req, err := s.client.NewRequest("GET", u, nil)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. // TODO: remove custom Accept header when this API fully launches.
  110. req.Header.Set("Accept", mediaTypeMigrationsPreview)
  111. m := &UserMigration{}
  112. resp, err := s.client.Do(ctx, req, m)
  113. if err != nil {
  114. return nil, resp, err
  115. }
  116. return m, resp, nil
  117. }
  118. // UserMigrationArchiveURL gets the URL for a specific migration archive.
  119. // id is the migration ID.
  120. //
  121. // GitHub API docs: https://developer.github.com/v3/migrations/users/#download-a-user-migration-archive
  122. func (s *MigrationService) UserMigrationArchiveURL(ctx context.Context, id int64) (string, error) {
  123. url := fmt.Sprintf("user/migrations/%v/archive", id)
  124. req, err := s.client.NewRequest("GET", url, nil)
  125. if err != nil {
  126. return "", err
  127. }
  128. // TODO: remove custom Accept header when this API fully launches.
  129. req.Header.Set("Accept", mediaTypeMigrationsPreview)
  130. m := &UserMigration{}
  131. var loc string
  132. originalRedirect := s.client.client.CheckRedirect
  133. s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  134. loc = req.URL.String()
  135. return http.ErrUseLastResponse
  136. }
  137. defer func() {
  138. s.client.client.CheckRedirect = originalRedirect
  139. }()
  140. resp, err := s.client.Do(ctx, req, m)
  141. if err == nil {
  142. return "", errors.New("expected redirect, none provided")
  143. }
  144. loc = resp.Header.Get("Location")
  145. return loc, nil
  146. }
  147. // DeleteUserMigration will delete a previous migration archive.
  148. // id is the migration ID.
  149. //
  150. // GitHub API docs: https://developer.github.com/v3/migrations/users/#delete-a-user-migration-archive
  151. func (s *MigrationService) DeleteUserMigration(ctx context.Context, id int64) (*Response, error) {
  152. url := fmt.Sprintf("user/migrations/%v/archive", id)
  153. req, err := s.client.NewRequest("DELETE", url, nil)
  154. if err != nil {
  155. return nil, err
  156. }
  157. // TODO: remove custom Accept header when this API fully launches.
  158. req.Header.Set("Accept", mediaTypeMigrationsPreview)
  159. return s.client.Do(ctx, req, nil)
  160. }
  161. // UnlockUserRepo will unlock a repo that was locked for migration.
  162. // id is migration ID.
  163. // You should unlock each migrated repository and delete them when the migration
  164. // is complete and you no longer need the source data.
  165. //
  166. // GitHub API docs: https://developer.github.com/v3/migrations/users/#unlock-a-user-repository
  167. func (s *MigrationService) UnlockUserRepo(ctx context.Context, id int64, repo string) (*Response, error) {
  168. url := fmt.Sprintf("user/migrations/%v/repos/%v/lock", id, repo)
  169. req, err := s.client.NewRequest("DELETE", url, nil)
  170. if err != nil {
  171. return nil, err
  172. }
  173. // TODO: remove custom Accept header when this API fully launches.
  174. req.Header.Set("Accept", mediaTypeMigrationsPreview)
  175. return s.client.Do(ctx, req, nil)
  176. }
上海开阖软件有限公司 沪ICP备12045867号-1