|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package tasks |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + |
| 24 | + "github.com/apache/incubator-devlake/core/dal" |
| 25 | + "github.com/apache/incubator-devlake/core/errors" |
| 26 | + "github.com/apache/incubator-devlake/core/models/domainlayer" |
| 27 | + "github.com/apache/incubator-devlake/core/models/domainlayer/didgen" |
| 28 | + "github.com/apache/incubator-devlake/core/models/domainlayer/qa" |
| 29 | + "github.com/apache/incubator-devlake/core/plugin" |
| 30 | + helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 31 | + testmoModels "github.com/apache/incubator-devlake/plugins/testmo/models" |
| 32 | +) |
| 33 | + |
| 34 | +var ConvertProjectsMeta = plugin.SubTaskMeta{ |
| 35 | + Name: "convertProjects", |
| 36 | + EntryPoint: ConvertProjects, |
| 37 | + EnabledByDefault: true, |
| 38 | + Description: "Convert tool layer table testmo_projects into domain layer table qa_projects", |
| 39 | + DomainTypes: []string{plugin.DOMAIN_TYPE_CODE_QUALITY}, |
| 40 | +} |
| 41 | + |
| 42 | +func ConvertProjects(taskCtx plugin.SubTaskContext) errors.Error { |
| 43 | + data := taskCtx.GetData().(*TestmoTaskData) |
| 44 | + db := taskCtx.GetDal() |
| 45 | + |
| 46 | + cursor, err := db.Cursor(dal.From(&testmoModels.TestmoProject{}), dal.Where("connection_id = ? AND id = ?", data.Options.ConnectionId, data.Options.ProjectId)) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + defer cursor.Close() |
| 51 | + |
| 52 | + converter, err := helper.NewDataConverter(helper.DataConverterArgs{ |
| 53 | + RawDataSubTaskArgs: helper.RawDataSubTaskArgs{ |
| 54 | + Ctx: taskCtx, |
| 55 | + Params: TestmoApiParams{ |
| 56 | + ConnectionId: data.Options.ConnectionId, |
| 57 | + ProjectId: data.Options.ProjectId, |
| 58 | + }, |
| 59 | + Table: RAW_PROJECT_TABLE, |
| 60 | + }, |
| 61 | + InputRowType: reflect.TypeOf(testmoModels.TestmoProject{}), |
| 62 | + Input: cursor, |
| 63 | + Convert: func(inputRow interface{}) ([]interface{}, errors.Error) { |
| 64 | + project := inputRow.(*testmoModels.TestmoProject) |
| 65 | + |
| 66 | + // Convert to domain layer QA project |
| 67 | + projectName := project.Name |
| 68 | + if projectName == "" { |
| 69 | + projectName = fmt.Sprintf("Project %d", project.Id) |
| 70 | + } |
| 71 | + |
| 72 | + qaProject := &qa.QaProject{ |
| 73 | + DomainEntityExtended: domainlayer.DomainEntityExtended{ |
| 74 | + Id: didgen.NewDomainIdGenerator(&testmoModels.TestmoProject{}).Generate(data.Options.ConnectionId, project.Id), |
| 75 | + }, |
| 76 | + Name: projectName, |
| 77 | + } |
| 78 | + |
| 79 | + return []interface{}{qaProject}, nil |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + return converter.Execute() |
| 88 | +} |
0 commit comments