...

Source file src/github.com/xanzy/go-gitlab/project_import_export.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"time"
    25  )
    26  
    27  // ProjectImportExportService handles communication with the project
    28  // import/export related methods of the GitLab API.
    29  //
    30  // GitLab API docs:
    31  // https://docs.gitlab.com/ee/api/project_import_export.html
    32  type ProjectImportExportService struct {
    33  	client *Client
    34  }
    35  
    36  // ImportStatus represents a project import status.
    37  //
    38  // GitLab API docs:
    39  // https://docs.gitlab.com/ee/api/project_import_export.html#import-status
    40  type ImportStatus struct {
    41  	ID                int        `json:"id"`
    42  	Description       string     `json:"description"`
    43  	Name              string     `json:"name"`
    44  	NameWithNamespace string     `json:"name_with_namespace"`
    45  	Path              string     `json:"path"`
    46  	PathWithNamespace string     `json:"path_with_namespace"`
    47  	CreateAt          *time.Time `json:"create_at"`
    48  	ImportStatus      string     `json:"import_status"`
    49  	ImportType        string     `json:"import_type"`
    50  	CorrelationID     string     `json:"correlation_id"`
    51  	ImportError       string     `json:"import_error"`
    52  }
    53  
    54  func (s ImportStatus) String() string {
    55  	return Stringify(s)
    56  }
    57  
    58  // ExportStatus represents a project export status.
    59  //
    60  // GitLab API docs:
    61  // https://docs.gitlab.com/ee/api/project_import_export.html#export-status
    62  type ExportStatus struct {
    63  	ID                int        `json:"id"`
    64  	Description       string     `json:"description"`
    65  	Name              string     `json:"name"`
    66  	NameWithNamespace string     `json:"name_with_namespace"`
    67  	Path              string     `json:"path"`
    68  	PathWithNamespace string     `json:"path_with_namespace"`
    69  	CreatedAt         *time.Time `json:"created_at"`
    70  	ExportStatus      string     `json:"export_status"`
    71  	Message           string     `json:"message"`
    72  	Links             struct {
    73  		APIURL string `json:"api_url"`
    74  		WebURL string `json:"web_url"`
    75  	} `json:"_links"`
    76  }
    77  
    78  func (s ExportStatus) String() string {
    79  	return Stringify(s)
    80  }
    81  
    82  // ScheduleExportOptions represents the available ScheduleExport() options.
    83  //
    84  // GitLab API docs:
    85  // https://docs.gitlab.com/ee/api/project_import_export.html#schedule-an-export
    86  type ScheduleExportOptions struct {
    87  	Description *string `url:"description,omitempty" json:"description,omitempty"`
    88  	Upload      struct {
    89  		URL        *string `url:"url,omitempty" json:"url,omitempty"`
    90  		HTTPMethod *string `url:"http_method,omitempty" json:"http_method,omitempty"`
    91  	} `url:"upload,omitempty" json:"upload,omitempty"`
    92  }
    93  
    94  // ScheduleExport schedules a project export.
    95  //
    96  // GitLab API docs:
    97  // https://docs.gitlab.com/ee/api/project_import_export.html#schedule-an-export
    98  func (s *ProjectImportExportService) ScheduleExport(pid interface{}, opt *ScheduleExportOptions, options ...RequestOptionFunc) (*Response, error) {
    99  	project, err := parseID(pid)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	u := fmt.Sprintf("projects/%s/export", PathEscape(project))
   104  
   105  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return s.client.Do(req, nil)
   111  }
   112  
   113  // ExportStatus get the status of export.
   114  //
   115  // GitLab API docs:
   116  // https://docs.gitlab.com/ee/api/project_import_export.html#export-status
   117  func (s *ProjectImportExportService) ExportStatus(pid interface{}, options ...RequestOptionFunc) (*ExportStatus, *Response, error) {
   118  	project, err := parseID(pid)
   119  	if err != nil {
   120  		return nil, nil, err
   121  	}
   122  	u := fmt.Sprintf("projects/%s/export", PathEscape(project))
   123  
   124  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  
   129  	es := new(ExportStatus)
   130  	resp, err := s.client.Do(req, es)
   131  	if err != nil {
   132  		return nil, resp, err
   133  	}
   134  
   135  	return es, resp, nil
   136  }
   137  
   138  // ExportDownload download the finished export.
   139  //
   140  // GitLab API docs:
   141  // https://docs.gitlab.com/ee/api/project_import_export.html#export-download
   142  func (s *ProjectImportExportService) ExportDownload(pid interface{}, options ...RequestOptionFunc) ([]byte, *Response, error) {
   143  	project, err := parseID(pid)
   144  	if err != nil {
   145  		return nil, nil, err
   146  	}
   147  	u := fmt.Sprintf("projects/%s/export/download", PathEscape(project))
   148  
   149  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   150  	if err != nil {
   151  		return nil, nil, err
   152  	}
   153  
   154  	var b bytes.Buffer
   155  	resp, err := s.client.Do(req, &b)
   156  	if err != nil {
   157  		return nil, resp, err
   158  	}
   159  
   160  	return b.Bytes(), resp, err
   161  }
   162  
   163  // ImportFileOptions represents the available ImportFile() options.
   164  //
   165  // GitLab API docs:
   166  // https://docs.gitlab.com/ee/api/project_import_export.html#import-a-file
   167  type ImportFileOptions struct {
   168  	Namespace      *string               `url:"namespace,omitempty" json:"namespace,omitempty"`
   169  	Name           *string               `url:"name,omitempty" json:"name,omitempty"`
   170  	Path           *string               `url:"path,omitempty" json:"path,omitempty"`
   171  	Overwrite      *bool                 `url:"overwrite,omitempty" json:"overwrite,omitempty"`
   172  	OverrideParams *CreateProjectOptions `url:"override_params,omitempty" json:"override_params,omitempty"`
   173  }
   174  
   175  // Import a project from an archive file.
   176  //
   177  // GitLab API docs:
   178  // https://docs.gitlab.com/ee/api/project_import_export.html#import-a-file
   179  func (s *ProjectImportExportService) ImportFromFile(archive io.Reader, opt *ImportFileOptions, options ...RequestOptionFunc) (*ImportStatus, *Response, error) {
   180  	req, err := s.client.UploadRequest(
   181  		http.MethodPost,
   182  		"projects/import",
   183  		archive,
   184  		"archive.tar.gz",
   185  		UploadFile,
   186  		opt,
   187  		options,
   188  	)
   189  	if err != nil {
   190  		return nil, nil, err
   191  	}
   192  
   193  	is := new(ImportStatus)
   194  	resp, err := s.client.Do(req, is)
   195  	if err != nil {
   196  		return nil, resp, err
   197  	}
   198  
   199  	return is, resp, nil
   200  }
   201  
   202  // ImportStatus get the status of an import.
   203  //
   204  // GitLab API docs:
   205  // https://docs.gitlab.com/ee/api/project_import_export.html#import-status
   206  func (s *ProjectImportExportService) ImportStatus(pid interface{}, options ...RequestOptionFunc) (*ImportStatus, *Response, error) {
   207  	project, err := parseID(pid)
   208  	if err != nil {
   209  		return nil, nil, err
   210  	}
   211  	u := fmt.Sprintf("projects/%s/import", PathEscape(project))
   212  
   213  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   214  	if err != nil {
   215  		return nil, nil, err
   216  	}
   217  
   218  	is := new(ImportStatus)
   219  	resp, err := s.client.Do(req, is)
   220  	if err != nil {
   221  		return nil, resp, err
   222  	}
   223  
   224  	return is, resp, nil
   225  }
   226  

View as plain text