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