|
| 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 | + "strings" |
| 24 | + |
| 25 | + "github.com/apache/incubator-devlake/core/dal" |
| 26 | + "github.com/apache/incubator-devlake/core/errors" |
| 27 | + "github.com/apache/incubator-devlake/core/models/domainlayer" |
| 28 | + "github.com/apache/incubator-devlake/core/models/domainlayer/devops" |
| 29 | + "github.com/apache/incubator-devlake/core/models/domainlayer/didgen" |
| 30 | + "github.com/apache/incubator-devlake/core/plugin" |
| 31 | + "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 32 | + "github.com/apache/incubator-devlake/plugins/argocd/models" |
| 33 | +) |
| 34 | + |
| 35 | +var ConvertApplicationsMeta = plugin.SubTaskMeta{ |
| 36 | + Name: "convertApplications", |
| 37 | + EntryPoint: ConvertApplications, |
| 38 | + EnabledByDefault: true, |
| 39 | + Description: "Convert ArgoCD applications into CICD scopes", |
| 40 | + DomainTypes: []string{plugin.DOMAIN_TYPE_CICD}, |
| 41 | + DependencyTables: []string{models.ArgocdApplication{}.TableName()}, |
| 42 | + ProductTables: []string{devops.CicdScope{}.TableName()}, |
| 43 | +} |
| 44 | + |
| 45 | +func ConvertApplications(taskCtx plugin.SubTaskContext) errors.Error { |
| 46 | + data := taskCtx.GetData().(*ArgocdTaskData) |
| 47 | + db := taskCtx.GetDal() |
| 48 | + |
| 49 | + cursor, err := db.Cursor( |
| 50 | + dal.From(&models.ArgocdApplication{}), |
| 51 | + dal.Where("connection_id = ?", data.Options.ConnectionId), |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + defer cursor.Close() |
| 57 | + |
| 58 | + scopeIdGen := didgen.NewDomainIdGenerator(&models.ArgocdApplication{}) |
| 59 | + |
| 60 | + converter, err := api.NewDataConverter(api.DataConverterArgs{ |
| 61 | + InputRowType: reflect.TypeOf(models.ArgocdApplication{}), |
| 62 | + Input: cursor, |
| 63 | + RawDataSubTaskArgs: api.RawDataSubTaskArgs{ |
| 64 | + Ctx: taskCtx, |
| 65 | + Table: RAW_APPLICATION_TABLE, |
| 66 | + Params: models.ArgocdApiParams{ |
| 67 | + ConnectionId: data.Options.ConnectionId, |
| 68 | + Name: data.Options.ApplicationName, |
| 69 | + }, |
| 70 | + }, |
| 71 | + Convert: func(inputRow interface{}) ([]interface{}, errors.Error) { |
| 72 | + application := inputRow.(*models.ArgocdApplication) |
| 73 | + scopeId := scopeIdGen.Generate(application.ConnectionId, application.Name) |
| 74 | + scope := buildCicdScopeFromApplication(application, scopeId) |
| 75 | + return []interface{}{scope}, nil |
| 76 | + }, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + return converter.Execute() |
| 83 | +} |
| 84 | + |
| 85 | +func buildCicdScopeFromApplication(app *models.ArgocdApplication, scopeId string) *devops.CicdScope { |
| 86 | + scope := &devops.CicdScope{ |
| 87 | + DomainEntity: domainlayer.NewDomainEntity(scopeId), |
| 88 | + Name: app.Name, |
| 89 | + Description: describeApplicationScope(app), |
| 90 | + Url: firstNonEmpty(app.RepoURL, app.DestServer), |
| 91 | + CreatedDate: app.CreatedDate, |
| 92 | + } |
| 93 | + return scope |
| 94 | +} |
| 95 | + |
| 96 | +func describeApplicationScope(app *models.ArgocdApplication) string { |
| 97 | + parts := make([]string, 0, 3) |
| 98 | + if app.Project != "" { |
| 99 | + parts = append(parts, fmt.Sprintf("Project: %s", app.Project)) |
| 100 | + } |
| 101 | + if app.Namespace != "" { |
| 102 | + parts = append(parts, fmt.Sprintf("Namespace: %s", app.Namespace)) |
| 103 | + } |
| 104 | + if app.DestNamespace != "" || app.DestServer != "" { |
| 105 | + dest := strings.TrimSpace(fmt.Sprintf("%s/%s", app.DestServer, app.DestNamespace)) |
| 106 | + dest = strings.Trim(dest, "/") |
| 107 | + if dest != "" { |
| 108 | + parts = append(parts, fmt.Sprintf("Destination: %s", dest)) |
| 109 | + } |
| 110 | + } |
| 111 | + return strings.Join(parts, " | ") |
| 112 | +} |
| 113 | + |
| 114 | +func firstNonEmpty(values ...string) string { |
| 115 | + for _, v := range values { |
| 116 | + if strings.TrimSpace(v) != "" { |
| 117 | + return v |
| 118 | + } |
| 119 | + } |
| 120 | + return "" |
| 121 | +} |
0 commit comments