Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Tweak PR a bit
Browse files Browse the repository at this point in the history
By using `query.Values` we are using the URL tags and are omitting any
empty values. This seems to be inline with what the API expects. Could
you both this this version @dheitman42 and @zbindenren?
  • Loading branch information
svanharmelen committed Oct 19, 2021
1 parent 49d8e56 commit d7ed40c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 75 deletions.
6 changes: 3 additions & 3 deletions group_import_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ type GroupImportFileOptions struct {
// https://docs.gitlab.com/ce/api/group_import_export.html#import-a-file
func (s *GroupImportExportService) ImportFile(opt *GroupImportFileOptions, options ...RequestOptionFunc) (*Response, error) {
// First check if we got all required options.
if opt.Name == nil || *opt.Name == "" {
if opt == nil || opt.Name == nil || *opt.Name == "" {
return nil, fmt.Errorf("Missing required option: Name")
}
if opt.Path == nil || *opt.Path == "" {
if opt == nil || opt.Path == nil || *opt.Path == "" {
return nil, fmt.Errorf("Missing required option: Path")
}
if opt.File == nil || *opt.File == "" {
if opt == nil || opt.File == nil || *opt.File == "" {
return nil, fmt.Errorf("Missing required option: File")
}

Expand Down
43 changes: 28 additions & 15 deletions project_import_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package gitlab

import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
"time"

"github.com/google/go-querystring/query"
)

// ProjectImportExportService handles communication with the project
Expand Down Expand Up @@ -179,41 +179,54 @@ type ImportFileOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/ce/api/project_import_export.html#import-a-file
func (s *ProjectImportExportService) ImportFile(opt *ImportFileOptions, options ...RequestOptionFunc) (*ImportStatus, *Response, error) {
// First check if we got all required options.
if opt == nil || opt.File == nil || *opt.File == "" {
return nil, nil, fmt.Errorf("Missing required option: File")
}
if opt == nil || opt.Path == nil || *opt.Path == "" {
return nil, nil, fmt.Errorf("Missing required option: Path")
}

file, err := os.Open(*opt.File)
if err != nil {
return nil, nil, err
}
defer file.Close()
var b bytes.Buffer
writer := multipart.NewWriter(&b)
var optMap map[string]interface{}
optMarshalled, err := json.Marshal(opt)

b := &bytes.Buffer{}
w := multipart.NewWriter(b)

fields, err := query.Values(opt)
if err != nil {
return nil, nil, err
}
json.Unmarshal(optMarshalled, &optMap)

for key, value := range optMap {
if strings.ToLower(key) == "file" {
part, err := writer.CreateFormFile("file", file.Name())
for name := range fields {
if name == "file" {
part, err := w.CreateFormFile("file", file.Name())
if err != nil {
return nil, nil, err
}
_, err = io.Copy(part, file)
if _, err = io.Copy(part, file); err != nil {
return nil, nil, err
}
} else {
_ = writer.WriteField(strings.ToLower(key), fmt.Sprintf("%v", value))
if err = w.WriteField(name, fmt.Sprintf("%v", fields.Get(name))); err != nil {
return nil, nil, err
}
}
}

err = writer.Close()
err = w.Close()
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPost, "projects/import", &b, options)

req, err := s.client.NewRequest(http.MethodPost, "projects/import", b, options)
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("Content-Type", w.FormDataContentType())

is := new(ImportStatus)
resp, err := s.client.Do(req, is)
Expand Down
57 changes: 0 additions & 57 deletions project_import_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,63 +128,6 @@ func TestProjectImportExportService_ExportDownload(t *testing.T) {
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestProjectImportExportService_ImportFile(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/import", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprintf(w, `
{
"id": 1,
"description": null,
"name": "api-project",
"name_with_namespace": "Administrator / api-project",
"path": "api-project",
"path_with_namespace": "root/api-project",
"import_status": "scheduled",
"correlation_id": "mezklWso3Za",
"failed_relations": []
}
`)
})

want := &ImportStatus{
ID: 1,
Description: "",
Name: "api-project",
NameWithNamespace: "Administrator / api-project",
Path: "api-project",
PathWithNamespace: "root/api-project",
ImportStatus: "scheduled",
}

es, resp, err := client.ProjectImportExport.ImportFile(nil, nil)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, es)

es, resp, err = client.ProjectImportExport.ImportFile(nil, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, es)
}

func TestProjectImportExportService_ImportFile_NotFound(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/import", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusNotFound)
})

es, resp, err := client.ProjectImportExport.ImportFile(nil, nil)
require.Error(t, err)
require.Nil(t, es)
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestProjectImportExportService_ImportStatus(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
Expand Down

0 comments on commit d7ed40c

Please sign in to comment.