|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// ProjectImportExportService handles communication with the project |
| 10 | +// import/export related methods of the GitLab API. |
| 11 | +// |
| 12 | +// GitLab API docs: |
| 13 | +// https://docs.gitlab.com/ce/user/project/settings/import_export.html |
| 14 | +type ProjectImportExportService struct { |
| 15 | + client *Client |
| 16 | +} |
| 17 | + |
| 18 | +// ImportStatus represents a project import status. |
| 19 | +// |
| 20 | +// GitLab API docs: |
| 21 | +// https://docs.gitlab.com/ce/api/project_import_export.html#import-status |
| 22 | +type ImportStatus struct { |
| 23 | + ID int `json:"id"` |
| 24 | + Description string `json:"description"` |
| 25 | + Name string `json:"name"` |
| 26 | + NameWithNamespace string `json:"name_with_namespace"` |
| 27 | + Path string `json:"path"` |
| 28 | + PathWithNamespace string `json:"path_with_namespace"` |
| 29 | + CreateAt *time.Time `json:"create_at"` |
| 30 | + ImportStatus string `json:"import_status"` |
| 31 | +} |
| 32 | + |
| 33 | +func (s ImportStatus) String() string { |
| 34 | + return Stringify(s) |
| 35 | +} |
| 36 | + |
| 37 | +// ExportStatus represents a project export status. |
| 38 | +// |
| 39 | +// GitLab API docs: |
| 40 | +// https://docs.gitlab.com/ce/api/project_import_export.html#export-status |
| 41 | +type ExportStatus struct { |
| 42 | + ID int `json:"id"` |
| 43 | + Description string `json:"description"` |
| 44 | + Name string `json:"name"` |
| 45 | + NameWithNamespace string `json:"name_with_namespace"` |
| 46 | + Path string `json:"path"` |
| 47 | + PathWithNamespace string `json:"path_with_namespace"` |
| 48 | + CreatedAt *time.Time `json:"created_at"` |
| 49 | + ExportStatus string `json:"export_status"` |
| 50 | + Message string `json:"message"` |
| 51 | + Links struct { |
| 52 | + APIURL string `json:"api_url"` |
| 53 | + WebURL string `json:"web_url"` |
| 54 | + } `json:"_links"` |
| 55 | +} |
| 56 | + |
| 57 | +func (s ExportStatus) String() string { |
| 58 | + return Stringify(s) |
| 59 | +} |
| 60 | + |
| 61 | +// ScheduleExportOptions represents the available ScheduleExport() options. |
| 62 | +// |
| 63 | +// GitLab API docs: |
| 64 | +// https://docs.gitlab.com/ce/api/project_import_export.html#schedule-an-export |
| 65 | +type ScheduleExportOptions struct { |
| 66 | + Description *string `url:"description,omitempty" json:"description,omitempty"` |
| 67 | + Upload struct { |
| 68 | + URL *string `url:"url,omitempty" json:"url,omitempty"` |
| 69 | + HTTPMethod *string `url:"http_method,omitempty" json:"http_method,omitempty"` |
| 70 | + } `url:"upload,omitempty" json:"upload,omitempty"` |
| 71 | +} |
| 72 | + |
| 73 | +// ScheduleExport schedules a project export. |
| 74 | +// |
| 75 | +// GitLab API docs: |
| 76 | +// https://docs.gitlab.com/ce/api/project_import_export.html#schedule-an-export |
| 77 | +func (s *ProjectImportExportService) ScheduleExport(pid interface{}, opt *ScheduleExportOptions, options ...OptionFunc) (*Response, error) { |
| 78 | + project, err := parseID(pid) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + u := fmt.Sprintf("projects/%s/export", pathEscape(project)) |
| 83 | + |
| 84 | + req, err := s.client.NewRequest("POST", u, opt, options) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + return s.client.Do(req, nil) |
| 90 | +} |
| 91 | + |
| 92 | +// ExportStatus get the status of export. |
| 93 | +// |
| 94 | +// GitLab API docs: |
| 95 | +// https://docs.gitlab.com/ce/api/project_import_export.html#export-status |
| 96 | +func (s *ProjectImportExportService) ExportStatus(pid interface{}, options ...OptionFunc) (*ExportStatus, *Response, error) { |
| 97 | + project, err := parseID(pid) |
| 98 | + if err != nil { |
| 99 | + return nil, nil, err |
| 100 | + } |
| 101 | + u := fmt.Sprintf("projects/%s/export", pathEscape(project)) |
| 102 | + |
| 103 | + req, err := s.client.NewRequest("GET", u, nil, options) |
| 104 | + if err != nil { |
| 105 | + return nil, nil, err |
| 106 | + } |
| 107 | + |
| 108 | + es := new(ExportStatus) |
| 109 | + resp, err := s.client.Do(req, es) |
| 110 | + if err != nil { |
| 111 | + return nil, resp, err |
| 112 | + } |
| 113 | + |
| 114 | + return es, resp, err |
| 115 | +} |
| 116 | + |
| 117 | +// ExportDownload download the finished export. |
| 118 | +// |
| 119 | +// GitLab API docs: |
| 120 | +// https://docs.gitlab.com/ce/api/project_import_export.html#export-download |
| 121 | +func (s *ProjectImportExportService) ExportDownload(pid interface{}, options ...OptionFunc) ([]byte, *Response, error) { |
| 122 | + project, err := parseID(pid) |
| 123 | + if err != nil { |
| 124 | + return nil, nil, err |
| 125 | + } |
| 126 | + u := fmt.Sprintf("projects/%s/export/download", pathEscape(project)) |
| 127 | + |
| 128 | + req, err := s.client.NewRequest("GET", u, nil, options) |
| 129 | + if err != nil { |
| 130 | + return nil, nil, err |
| 131 | + } |
| 132 | + |
| 133 | + var b bytes.Buffer |
| 134 | + resp, err := s.client.Do(req, &b) |
| 135 | + if err != nil { |
| 136 | + return nil, resp, err |
| 137 | + } |
| 138 | + |
| 139 | + return b.Bytes(), resp, err |
| 140 | +} |
| 141 | + |
| 142 | +// ImportFileOptions represents the available ImportFile() options. |
| 143 | +// |
| 144 | +// GitLab API docs: |
| 145 | +// https://docs.gitlab.com/ce/api/project_import_export.html#import-a-file |
| 146 | +type ImportFileOptions struct { |
| 147 | + Namespace *string `url:"namespace,omitempty" json:"namespace,omitempty"` |
| 148 | + File *string `url:"file,omitempty" json:"file,omitempty"` |
| 149 | + Path *string `url:"path,omitempty" json:"path,omitempty"` |
| 150 | + Overwrite *bool `url:"overwrite,omitempty" json:"overwrite,omitempty"` |
| 151 | + OverrideParams *CreateProjectOptions `url:"override_params,omitempty" json:"override_params,omitempty"` |
| 152 | +} |
| 153 | + |
| 154 | +// ImportProject import the project. |
| 155 | +// |
| 156 | +// GitLab API docs: |
| 157 | +// https://docs.gitlab.com/ce/api/project_import_export.html#import-a-file |
| 158 | +func (s *ProjectImportExportService) ImportProject(opt *ImportFileOptions, options ...OptionFunc) (*ImportStatus, *Response, error) { |
| 159 | + req, err := s.client.NewRequest("POST", "/projects/import", opt, options) |
| 160 | + if err != nil { |
| 161 | + return nil, nil, err |
| 162 | + } |
| 163 | + |
| 164 | + is := new(ImportStatus) |
| 165 | + resp, err := s.client.Do(req, is) |
| 166 | + if err != nil { |
| 167 | + return nil, resp, err |
| 168 | + } |
| 169 | + |
| 170 | + return is, resp, err |
| 171 | +} |
| 172 | + |
| 173 | +// ImportStatus get the status of an import. |
| 174 | +// |
| 175 | +// GitLab API docs: |
| 176 | +// https://docs.gitlab.com/ce/api/project_import_export.html#import-status |
| 177 | +func (s *ProjectImportExportService) ImportStatus(pid interface{}, options ...OptionFunc) (*ImportStatus, *Response, error) { |
| 178 | + project, err := parseID(pid) |
| 179 | + if err != nil { |
| 180 | + return nil, nil, err |
| 181 | + } |
| 182 | + u := fmt.Sprintf("projects/%s/import", pathEscape(project)) |
| 183 | + |
| 184 | + req, err := s.client.NewRequest("GET", u, nil, options) |
| 185 | + if err != nil { |
| 186 | + return nil, nil, err |
| 187 | + } |
| 188 | + |
| 189 | + is := new(ImportStatus) |
| 190 | + resp, err := s.client.Do(req, is) |
| 191 | + if err != nil { |
| 192 | + return nil, resp, err |
| 193 | + } |
| 194 | + |
| 195 | + return is, resp, err |
| 196 | +} |
0 commit comments