123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package github
- import (
- "context"
- "errors"
- "fmt"
- "net/http"
- "strings"
- )
- type MigrationService service
- type Migration struct {
- ID *int64 `json:"id,omitempty"`
- GUID *string `json:"guid,omitempty"`
-
-
-
-
-
-
- State *string `json:"state,omitempty"`
-
-
- LockRepositories *bool `json:"lock_repositories,omitempty"`
-
-
- ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
- URL *string `json:"url,omitempty"`
- CreatedAt *string `json:"created_at,omitempty"`
- UpdatedAt *string `json:"updated_at,omitempty"`
- Repositories []*Repository `json:"repositories,omitempty"`
- }
- func (m Migration) String() string {
- return Stringify(m)
- }
- type MigrationOptions struct {
-
-
- LockRepositories bool
-
-
- ExcludeAttachments bool
- }
- type startMigration struct {
-
- Repositories []string `json:"repositories,omitempty"`
-
-
- LockRepositories *bool `json:"lock_repositories,omitempty"`
-
-
- ExcludeAttachments *bool `json:"exclude_attachments,omitempty"`
- }
- func (s *MigrationService) StartMigration(ctx context.Context, org string, repos []string, opt *MigrationOptions) (*Migration, *Response, error) {
- u := fmt.Sprintf("orgs/%v/migrations", org)
- body := &startMigration{Repositories: repos}
- if opt != nil {
- body.LockRepositories = Bool(opt.LockRepositories)
- body.ExcludeAttachments = Bool(opt.ExcludeAttachments)
- }
- req, err := s.client.NewRequest("POST", u, body)
- if err != nil {
- return nil, nil, err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- m := &Migration{}
- resp, err := s.client.Do(ctx, req, m)
- if err != nil {
- return nil, resp, err
- }
- return m, resp, nil
- }
- func (s *MigrationService) ListMigrations(ctx context.Context, org string) ([]*Migration, *Response, error) {
- u := fmt.Sprintf("orgs/%v/migrations", org)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- var m []*Migration
- resp, err := s.client.Do(ctx, req, &m)
- if err != nil {
- return nil, resp, err
- }
- return m, resp, nil
- }
- func (s *MigrationService) MigrationStatus(ctx context.Context, org string, id int64) (*Migration, *Response, error) {
- u := fmt.Sprintf("orgs/%v/migrations/%v", org, id)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- m := &Migration{}
- resp, err := s.client.Do(ctx, req, m)
- if err != nil {
- return nil, resp, err
- }
- return m, resp, nil
- }
- func (s *MigrationService) MigrationArchiveURL(ctx context.Context, org string, id int64) (url string, err error) {
- u := fmt.Sprintf("orgs/%v/migrations/%v/archive", org, id)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return "", err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- s.client.clientMu.Lock()
- defer s.client.clientMu.Unlock()
-
- var loc string
- saveRedirect := s.client.client.CheckRedirect
- s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
- loc = req.URL.String()
- return errors.New("disable redirect")
- }
- defer func() { s.client.client.CheckRedirect = saveRedirect }()
- _, err = s.client.Do(ctx, req, nil)
- if err == nil {
- return "", errors.New("expected redirect, none provided")
- }
- if !strings.Contains(err.Error(), "disable redirect") {
- return "", err
- }
- return loc, nil
- }
- func (s *MigrationService) DeleteMigration(ctx context.Context, org string, id int64) (*Response, error) {
- u := fmt.Sprintf("orgs/%v/migrations/%v/archive", org, id)
- req, err := s.client.NewRequest("DELETE", u, nil)
- if err != nil {
- return nil, err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- return s.client.Do(ctx, req, nil)
- }
- func (s *MigrationService) UnlockRepo(ctx context.Context, org string, id int64, repo string) (*Response, error) {
- u := fmt.Sprintf("orgs/%v/migrations/%v/repos/%v/lock", org, id, repo)
- req, err := s.client.NewRequest("DELETE", u, nil)
- if err != nil {
- return nil, err
- }
-
- req.Header.Set("Accept", mediaTypeMigrationsPreview)
- return s.client.Do(ctx, req, nil)
- }
|