Skip to content

Commit

Permalink
get execute env from report env (erda-project#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjoey authored and erda-bot committed Sep 24, 2021
1 parent d112e2e commit 7d5da49
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package executeInfo
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/sirupsen/logrus"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/dop/services/autotest"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/types"
)
Expand Down Expand Up @@ -188,7 +190,7 @@ Label:
return err
}
i.State.EnvData = config
i.Data["executeEnv"] = "执行参数"
i.Data["executeEnv"] = getApiConfigName(env)
i.Props = make(map[string]interface{})
i.Props["fields"] = []PropColumn{
{
Expand Down Expand Up @@ -310,3 +312,22 @@ func convertReportToConfig(env apistructs.PipelineReport) (apistructs.AutoTestAP

return config, nil
}

func getApiConfigName(env apistructs.PipelineReport) string {
if env.ID == 0 {
return ""
}
envByte, err := json.Marshal(env)
if err != nil {
return ""
}
configData := apistructs.PipelineReport{}
err = json.Unmarshal(envByte, &configData)
if err != nil {
return ""
}
if envName, ok := configData.Meta[autotest.CmsCfgKeyDisplayName]; ok {
return fmt.Sprintf("%v", envName)
}
return ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ func Test_convertReportToConfig(t *testing.T) {
}}
assert.Equal(t, want, c)
}

func TestGetApiConfigName(t *testing.T) {
m := apistructs.PipelineReport{}
bt := `{"id":123,"pipelineID":123,"type":"auto-test-plan","meta":{"AUTOTEST_DISPLAY_NAME":"执行参数-custom","data":"{\"domain\":\"domain\",\"header\":{\"Cookie\":\"cookie\",\"cluster-id\":\"2\",\"cluster-name\":\"name\",\"org\":\"erda\",\"project-id\":\"13\"},\"global\":{\"111\":{\"name\":\"111\",\"type\":\"string\",\"value\":\"111\",\"desc\":\"111\"}}}"},"creatorID":"","updaterID":"","createdAt":"2021-09-03T17:25:48+08:00","updatedAt":"2021-09-03T17:25:48+08:00"}`
err := json.Unmarshal([]byte(bt), &m)
assert.NoError(t, err)
executeEnv := getApiConfigName(m)
assert.Equal(t, "执行参数-custom", executeEnv)

m1 := apistructs.PipelineReport{}
emptyMeta := `{"id":123,"pipelineID":123,"type":"auto-test-plan","creatorID":"","updaterID":"","createdAt":"2021-09-03T17:25:48+08:00","updatedAt":"2021-09-03T17:25:48+08:00"}`
err = json.Unmarshal([]byte(emptyMeta), &m1)
assert.NoError(t, err)
executeEnv = getApiConfigName(m1)
assert.Equal(t, "", executeEnv)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func (p *provider) Handle(ctx *aoptypes.TuneContext) error {

meta := make(apistructs.PipelineReportMeta)
meta["data"] = ctx.SDK.Pipeline.Snapshot.Secrets[autotest.CmsCfgKeyAPIGlobalConfig]
// add cfg display name to auto-test-execute-config report
meta[autotest.CmsCfgKeyDisplayName] = ctx.SDK.Pipeline.Snapshot.Secrets[autotest.CmsCfgKeyDisplayName]

// report
_, err := ctx.SDK.Report.Create(apistructs.PipelineReportCreateRequest{
Expand Down

0 comments on commit 7d5da49

Please sign in to comment.