github.com/google/go-github/v71@v71.0.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/rest/migrations/users#start-a-user-migration
    71  //
    72  //meta:operation POST /user/migrations
    73  func (s *MigrationService) StartUserMigration(ctx context.Context, repos []string, opts *UserMigrationOptions) (*UserMigration, *Response, error) {
    74  	u := "user/migrations"
    75  
    76  	body := &startUserMigration{Repositories: repos}
    77  	if opts != nil {
    78  		body.LockRepositories = Ptr(opts.LockRepositories)
    79  		body.ExcludeAttachments = Ptr(opts.ExcludeAttachments)
    80  	}
    81  
    82  	req, err := s.client.NewRequest("POST", u, body)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	// TODO: remove custom Accept header when this API fully launches.
    88  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
    89  
    90  	m := &UserMigration{}
    91  	resp, err := s.client.Do(ctx, req, m)
    92  	if err != nil {
    93  		return nil, resp, err
    94  	}
    95  
    96  	return m, resp, nil
    97  }
    98  
    99  // ListUserMigrations lists the most recent migrations.
   100  //
   101  // GitHub API docs: https://docs.github.com/rest/migrations/users#list-user-migrations
   102  //
   103  //meta:operation GET /user/migrations
   104  func (s *MigrationService) ListUserMigrations(ctx context.Context, opts *ListOptions) ([]*UserMigration, *Response, error) {
   105  	u := "user/migrations"
   106  	u, err := addOptions(u, opts)
   107  	if err != nil {
   108  		return nil, nil, err
   109  	}
   110  
   111  	req, err := s.client.NewRequest("GET", u, nil)
   112  	if err != nil {
   113  		return nil, nil, err
   114  	}
   115  
   116  	// TODO: remove custom Accept header when this API fully launches.
   117  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
   118  
   119  	var m []*UserMigration
   120  	resp, err := s.client.Do(ctx, req, &m)
   121  	if err != nil {
   122  		return nil, resp, err
   123  	}
   124  
   125  	return m, resp, nil
   126  }
   127  
   128  // UserMigrationStatus gets the status of a specific migration archive.
   129  // id is the migration ID.
   130  //
   131  // GitHub API docs: https://docs.github.com/rest/migrations/users#get-a-user-migration-status
   132  //
   133  //meta:operation GET /user/migrations/{migration_id}
   134  func (s *MigrationService) UserMigrationStatus(ctx context.Context, id int64) (*UserMigration, *Response, error) {
   135  	u := fmt.Sprintf("user/migrations/%v", id)
   136  
   137  	req, err := s.client.NewRequest("GET", u, nil)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	// TODO: remove custom Accept header when this API fully launches.
   143  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
   144  
   145  	m := &UserMigration{}
   146  	resp, err := s.client.Do(ctx, req, m)
   147  	if err != nil {
   148  		return nil, resp, err
   149  	}
   150  
   151  	return m, resp, nil
   152  }
   153  
   154  // UserMigrationArchiveURL gets the URL for a specific migration archive.
   155  // id is the migration ID.
   156  //
   157  // GitHub API docs: https://docs.github.com/rest/migrations/users#download-a-user-migration-archive
   158  //
   159  //meta:operation GET /user/migrations/{migration_id}/archive
   160  func (s *MigrationService) UserMigrationArchiveURL(ctx context.Context, id int64) (string, error) {
   161  	url := fmt.Sprintf("user/migrations/%v/archive", id)
   162  
   163  	req, err := s.client.NewRequest("GET", url, nil)
   164  	if err != nil {
   165  		return "", err
   166  	}
   167  
   168  	// TODO: remove custom Accept header when this API fully launches.
   169  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
   170  
   171  	m := &UserMigration{}
   172  
   173  	var loc string
   174  	originalRedirect := s.client.client.CheckRedirect
   175  	s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
   176  		loc = req.URL.String()
   177  		return http.ErrUseLastResponse
   178  	}
   179  	defer func() {
   180  		s.client.client.CheckRedirect = originalRedirect
   181  	}()
   182  	resp, err := s.client.Do(ctx, req, m)
   183  	if err == nil {
   184  		return "", errors.New("expected redirect, none provided")
   185  	}
   186  	loc = resp.Header.Get("Location")
   187  	return loc, nil
   188  }
   189  
   190  // DeleteUserMigration will delete a previous migration archive.
   191  // id is the migration ID.
   192  //
   193  // GitHub API docs: https://docs.github.com/rest/migrations/users#delete-a-user-migration-archive
   194  //
   195  //meta:operation DELETE /user/migrations/{migration_id}/archive
   196  func (s *MigrationService) DeleteUserMigration(ctx context.Context, id int64) (*Response, error) {
   197  	url := fmt.Sprintf("user/migrations/%v/archive", id)
   198  
   199  	req, err := s.client.NewRequest("DELETE", url, nil)
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  
   204  	// TODO: remove custom Accept header when this API fully launches.
   205  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
   206  
   207  	return s.client.Do(ctx, req, nil)
   208  }
   209  
   210  // UnlockUserRepo will unlock a repo that was locked for migration.
   211  // id is migration ID.
   212  // You should unlock each migrated repository and delete them when the migration
   213  // is complete and you no longer need the source data.
   214  //
   215  // GitHub API docs: https://docs.github.com/rest/migrations/users#unlock-a-user-repository
   216  //
   217  //meta:operation DELETE /user/migrations/{migration_id}/repos/{repo_name}/lock
   218  func (s *MigrationService) UnlockUserRepo(ctx context.Context, id int64, repo string) (*Response, error) {
   219  	url := fmt.Sprintf("user/migrations/%v/repos/%v/lock", id, repo)
   220  
   221  	req, err := s.client.NewRequest("DELETE", url, nil)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  
   226  	// TODO: remove custom Accept header when this API fully launches.
   227  	req.Header.Set("Accept", mediaTypeMigrationsPreview)
   228  
   229  	return s.client.Do(ctx, req, nil)
   230  }