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

225 lines
7.4KB

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