mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
escape value in json (#4102)
* escape value in json * delete old code * replace complete parsing by json.Marshal * delete old code and add header Co-authored-by: rosemarieB <45030247+rosemarieB@users.noreply.github.com>
This commit is contained in:
parent
25216b3ef8
commit
8bc0fb05a7
@ -100,6 +100,13 @@ func abapEnvironmentBuild(config abapEnvironmentBuildOptions, telemetryData *tel
|
||||
}
|
||||
|
||||
func runAbapEnvironmentBuild(config *abapEnvironmentBuildOptions, telemetryData *telemetry.CustomData, utils abapEnvironmentBuildUtils, cpe *abapEnvironmentBuildCommonPipelineEnvironment) error {
|
||||
|
||||
log.Entry().Info("╔════════════════════════════════╗")
|
||||
log.Entry().Info("║ abapEnvironmentBuild ║")
|
||||
log.Entry().Info("╠════════════════════════════════╣")
|
||||
log.Entry().Infof("║ %-30v ║", config.Phase)
|
||||
log.Entry().Info("╙────────────────────────────────╜")
|
||||
|
||||
conn := new(abapbuild.Connector)
|
||||
if err := initConnection(conn, config, utils); err != nil {
|
||||
return errors.Wrap(err, "Connector initialization for communication with the ABAP system failed")
|
||||
|
@ -146,7 +146,7 @@ type Result struct {
|
||||
// Value : Returns Build Runtime Value
|
||||
type Value struct {
|
||||
connector Connector
|
||||
BuildID string `json:"build_id"`
|
||||
BuildID string `json:"build_id,omitempty"`
|
||||
ValueID string `json:"value_id"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
@ -156,9 +156,9 @@ type Values struct {
|
||||
Values []Value `json:"results"`
|
||||
}
|
||||
|
||||
type inputForPost struct {
|
||||
phase string
|
||||
values Values
|
||||
type InputForPost struct {
|
||||
Phase string `json:"phase"`
|
||||
Values []Value `json:"values"`
|
||||
}
|
||||
|
||||
// *********************************************************************
|
||||
@ -170,12 +170,16 @@ func (b *Build) Start(phase string, inputValues Values) error {
|
||||
if err := b.Connector.GetToken(""); err != nil {
|
||||
return err
|
||||
}
|
||||
importBody := inputForPost{
|
||||
phase: phase,
|
||||
values: inputValues,
|
||||
}.String()
|
||||
inputForPost := InputForPost{
|
||||
Phase: phase,
|
||||
Values: inputValues.Values,
|
||||
}
|
||||
importBody, err := json.Marshal(inputForPost)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Generating Post Request Body failed")
|
||||
}
|
||||
|
||||
body, err := b.Connector.Post("/builds", importBody)
|
||||
body, err := b.Connector.Post("/builds", string(importBody))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Start of build failed: "+string(body))
|
||||
}
|
||||
@ -553,29 +557,6 @@ func (logging *logStruct) print() {
|
||||
}
|
||||
}
|
||||
|
||||
// ******** parsing ********
|
||||
func (v Value) String() string {
|
||||
return fmt.Sprintf(
|
||||
`{ "value_id": "%s", "value": "%s" }`,
|
||||
v.ValueID,
|
||||
v.Value)
|
||||
}
|
||||
|
||||
func (vs Values) String() string {
|
||||
returnString := ""
|
||||
for _, value := range vs.Values {
|
||||
returnString = returnString + value.String() + ",\n"
|
||||
}
|
||||
if len(returnString) > 0 {
|
||||
returnString = returnString[:len(returnString)-2] //removes last ,
|
||||
}
|
||||
return returnString
|
||||
}
|
||||
|
||||
func (in inputForPost) String() string {
|
||||
return fmt.Sprintf(`{ "phase": "%s", "values": [%s]}`, in.phase, in.values.String())
|
||||
}
|
||||
|
||||
// ******** unmarshal function ************
|
||||
func unmarshalTasks(body []byte, connector Connector) ([]task, error) {
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@ -47,6 +48,19 @@ func TestStart(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStartValueGeneration(t *testing.T) {
|
||||
myValue := string(`{ "ARES_EC_ATTRIBUTES": [ { "ATTRIBUTE": "A", "VALUE": "B" } ] }`)
|
||||
|
||||
inputForPost := InputForPost{
|
||||
Phase: "HUGO",
|
||||
Values: []Value{{ValueID: "myJson", Value: myValue}},
|
||||
}
|
||||
|
||||
importBody, err := json.Marshal(inputForPost)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"phase":"HUGO","values":[{"value_id":"myJson","value":"{ \"ARES_EC_ATTRIBUTES\": [ { \"ATTRIBUTE\": \"A\", \"VALUE\": \"B\" } ] }"}]}`, string(importBody))
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
t.Run("Run Get", func(t *testing.T) {
|
||||
b := testSetup(&ClMock{}, "ABIFNLDCSQPOVMXK4DNPBDRW2M")
|
||||
|
Loading…
Reference in New Issue
Block a user