[PL-24913]: Handle the error raised while creating a multipart input (#181)

pull/182/head
Deepak Patankar 2 years ago committed by GitHub
parent 5a5c5ca5c5
commit 766414330d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,6 +22,7 @@ func TestCreateBranch(t *testing.T) {
},
}
commitId, _ := GetCurrentCommitOfBranch(client, "master")
input := &scm.CreateBranch{
Name: "test_branch",
Sha: commitId,

@ -14,7 +14,6 @@ var (
endpoint = "https://bitbucket.dev.harness.io/"
repoID = "har/scm-integration-test-repo"
username = "harnessadmin"
commitId = "f675c4b55841908d7c338c500c8f4cb844fd9be7"
)
func GetCurrentCommitOfBranch(client *scm.Client, branch string) (string, error) {

@ -0,0 +1,26 @@
package stash
import "mime/multipart"
type MultipartWriter struct {
Writer *multipart.Writer
Error error
}
func (mw *MultipartWriter) Write(f, v string) {
if mw.Error != nil {
return
}
if v == "" {
return
}
mw.Error = mw.Writer.WriteField(f, v)
}
func (mw *MultipartWriter) Close() {
mw.Writer.Close()
}
func (mw *MultipartWriter) FormDataContentType() string {
return mw.Writer.FormDataContentType()
}

@ -79,26 +79,22 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
case *contentCreateUpdate:
// add the content to the multipart
var b bytes.Buffer
w := multipart.NewWriter(&b)
mw := &MultipartWriter{Writer: multipart.NewWriter(&b)}
// add the other fields
if content.Message != "" {
_ = w.WriteField("content", string(content.Content))
// The Write function doesn't write the string value to multipart if it is Empty
mw.Write("content", string(content.Content))
mw.Write("message", content.Message)
mw.Write("branch", content.Branch)
mw.Write("sourceCommitId", content.Sha)
if mw.Error != nil {
return nil, fmt.Errorf("error writing multipart-content. err: %s", mw.Error)
}
if content.Message != "" {
_ = w.WriteField("message", content.Message)
}
if content.Branch != "" {
_ = w.WriteField("branch", content.Branch)
}
if content.Sha != "" {
_ = w.WriteField("sourceCommitId", content.Sha)
}
w.Close()
mw.Close()
// write the multipart response to the body
req.Body = &b
// write the content type that contains the length of the multipart
req.Header = map[string][]string{
"Content-Type": {w.FormDataContentType()},
"Content-Type": {mw.FormDataContentType()},
}
default:
buf := new(bytes.Buffer)

Loading…
Cancel
Save