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 a004ebc
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 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 @@ -184,32 +184,37 @@ func (s *ProjectImportExportService) ImportFile(opt *ImportFileOptions, options
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)

body := new(bytes.Buffer)
writer := multipart.NewWriter(body)

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" {
for name := range fields {
if name == "file" {
part, err := writer.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 = writer.WriteField(name, fmt.Sprintf("%v", fields.Get(name))); err != nil {
return nil, nil, err
}
}
}

err = writer.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", body, options)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit a004ebc

Please sign in to comment.