From 7d5da49effd6800003466f1cb50345ce54bfa780 Mon Sep 17 00:00:00 2001 From: chengjoey <30427474+chengjoey@users.noreply.github.com> Date: Fri, 24 Sep 2021 14:34:21 +0800 Subject: [PATCH] get execute env from report env (#2064) --- .../components/executeInfo/render.go | 23 ++++++++++++++++++- .../components/executeInfo/render_test.go | 16 +++++++++++++ .../pipeline/testplan_before/plugin.go | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render.go index aa95395752e..31345671728 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render.go @@ -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" ) @@ -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{ { @@ -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 "" +} diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render_test.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render_test.go index 5e9d2bb8873..1a6acf3f265 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render_test.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/executeInfo/render_test.go @@ -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) +} diff --git a/modules/pipeline/aop/plugins/pipeline/testplan_before/plugin.go b/modules/pipeline/aop/plugins/pipeline/testplan_before/plugin.go index 7f9fede8961..7fa8bea00a7 100644 --- a/modules/pipeline/aop/plugins/pipeline/testplan_before/plugin.go +++ b/modules/pipeline/aop/plugins/pipeline/testplan_before/plugin.go @@ -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{