Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions backend/plugins/argocd/tasks/application_convertor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tasks

import (
"fmt"
"reflect"
"strings"

"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/domainlayer"
"github.com/apache/incubator-devlake/core/models/domainlayer/devops"
"github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/argocd/models"
)

var ConvertApplicationsMeta = plugin.SubTaskMeta{
Name: "convertApplications",
EntryPoint: ConvertApplications,
EnabledByDefault: true,
Description: "Convert ArgoCD applications into CICD scopes",
DomainTypes: []string{plugin.DOMAIN_TYPE_CICD},
DependencyTables: []string{models.ArgocdApplication{}.TableName()},
ProductTables: []string{devops.CicdScope{}.TableName()},
}

func ConvertApplications(taskCtx plugin.SubTaskContext) errors.Error {
data := taskCtx.GetData().(*ArgocdTaskData)
db := taskCtx.GetDal()

cursor, err := db.Cursor(
dal.From(&models.ArgocdApplication{}),
dal.Where("connection_id = ?", data.Options.ConnectionId),
)
if err != nil {
return err
}
defer cursor.Close()

scopeIdGen := didgen.NewDomainIdGenerator(&models.ArgocdApplication{})

converter, err := api.NewDataConverter(api.DataConverterArgs{
InputRowType: reflect.TypeOf(models.ArgocdApplication{}),
Input: cursor,
RawDataSubTaskArgs: api.RawDataSubTaskArgs{
Ctx: taskCtx,
Table: RAW_APPLICATION_TABLE,
Params: models.ArgocdApiParams{
ConnectionId: data.Options.ConnectionId,
Name: data.Options.ApplicationName,
},
},
Convert: func(inputRow interface{}) ([]interface{}, errors.Error) {
application := inputRow.(*models.ArgocdApplication)
scopeId := scopeIdGen.Generate(application.ConnectionId, application.Name)
scope := buildCicdScopeFromApplication(application, scopeId)
return []interface{}{scope}, nil
},
})
if err != nil {
return err
}

return converter.Execute()
}

func buildCicdScopeFromApplication(app *models.ArgocdApplication, scopeId string) *devops.CicdScope {
scope := &devops.CicdScope{
DomainEntity: domainlayer.NewDomainEntity(scopeId),
Name: app.Name,
Description: describeApplicationScope(app),
Url: firstNonEmpty(app.RepoURL, app.DestServer),
CreatedDate: app.CreatedDate,
}
return scope
}

func describeApplicationScope(app *models.ArgocdApplication) string {
parts := make([]string, 0, 3)
if app.Project != "" {
parts = append(parts, fmt.Sprintf("Project: %s", app.Project))
}
if app.Namespace != "" {
parts = append(parts, fmt.Sprintf("Namespace: %s", app.Namespace))
}
if app.DestNamespace != "" || app.DestServer != "" {
dest := strings.TrimSpace(fmt.Sprintf("%s/%s", app.DestServer, app.DestNamespace))
dest = strings.Trim(dest, "/")
if dest != "" {
parts = append(parts, fmt.Sprintf("Destination: %s", dest))
}
}
return strings.Join(parts, " | ")
}

func firstNonEmpty(values ...string) string {
for _, v := range values {
if strings.TrimSpace(v) != "" {
return v
}
}
return ""
}
52 changes: 52 additions & 0 deletions backend/plugins/argocd/tasks/application_convertor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tasks

import (
"testing"
"time"

"github.com/apache/incubator-devlake/plugins/argocd/models"
"github.com/stretchr/testify/assert"
)

func TestDescribeApplicationScopeBuildsSummary(t *testing.T) {
desc := describeApplicationScope(&models.ArgocdApplication{
Project: "observability",
Namespace: "argocd",
DestServer: "https://k8s.example.com",
DestNamespace: "prod",
})
assert.Equal(t, "Project: observability | Namespace: argocd | Destination: https://k8s.example.com/prod", desc)
}

func TestBuildCicdScopeFromApplication(t *testing.T) {
created := time.Now()
scope := buildCicdScopeFromApplication(&models.ArgocdApplication{
Name: "test-app",
RepoURL: "https://git.example.com/app.git",
CreatedDate: &created,
}, "argocd:app:1")

assert.Equal(t, "argocd:app:1", scope.Id)
assert.Equal(t, "test-app", scope.Name)
assert.Equal(t, "https://git.example.com/app.git", scope.Url)
if assert.NotNil(t, scope.CreatedDate) {
assert.Equal(t, created.UTC(), scope.CreatedDate.UTC())
}
}
1 change: 1 addition & 0 deletions backend/plugins/argocd/tasks/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func CollectDataTaskMetas() []plugin.SubTaskMeta {
return []plugin.SubTaskMeta{
CollectApplicationsMeta,
ExtractApplicationsMeta,
ConvertApplicationsMeta,
CollectSyncOperationsMeta,
ExtractSyncOperationsMeta,
ConvertSyncOperationsMeta,
Expand Down
11 changes: 10 additions & 1 deletion config-ui/src/plugins/components/scope-config-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { AzureTransformation } from '@/plugins/register/azure';
import { TapdTransformation } from '@/plugins/register/tapd';
import { BambooTransformation } from '@/plugins/register/bamboo';
import { CircleCITransformation } from '@/plugins/register/circleci';
import { ArgoCDTransformation } from '@/plugins/register/argocd';
import { DOC_URL } from '@/release';
import { operator } from '@/utils';

Expand Down Expand Up @@ -86,7 +87,7 @@ export const ScopeConfigForm = ({
setName(forceCreate ? `${res.name}-copy` : res.name);
setEntities(res.entities ?? []);
setTransformation(omit(res, ['id', 'connectionId', 'name', 'entities', 'createdAt', 'updatedAt']));
} catch {}
} catch { }
})();
}, [scopeConfigId]);

Expand Down Expand Up @@ -193,6 +194,14 @@ export const ScopeConfigForm = ({
)}

<Form labelCol={{ span: 4 }} wrapperCol={{ span: 16 }}>
{plugin === 'argocd' && (
<ArgoCDTransformation
entities={entities}
transformation={transformation}
setTransformation={setTransformation}
/>
)}

{plugin === 'azuredevops' && (
<AzureTransformation
entities={entities}
Expand Down
2 changes: 1 addition & 1 deletion config-ui/src/plugins/register/argocd/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ArgoCDConfig: IPluginConfig = {
sort: 1,
isBeta: true,
connection: {
docLink: '',
docLink: DOC_URL.PLUGIN.ARGOCD.BASIS,
initialValues: {
endpoint: 'https://',
},
Expand Down
4 changes: 4 additions & 0 deletions config-ui/src/release/stable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const URLS = {
},
DORA: 'https://devlake.apache.org/docs/DORA/',
PLUGIN: {
ARGOCD: {
BASIS: 'https://devlake.apache.org/docs/Configuration/ArgoCD',
TRANSFORMATION: 'https://devlake.apache.org/docs/Configuration/ArgoCD#step-3---adding-transformation-rules-optional',
},
AZUREDEVOPS: {
BASIS: 'https://devlake.apache.org/docs/Configuration/AzureDevOps',
RATE_LIMIT: 'https://devlake.apache.org/docs/Configuration/AzureDevOps/#custom-rate-limit-optional',
Expand Down
44 changes: 34 additions & 10 deletions grafana/dashboards/ArgoCD.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@
},
{
"collapsed": false,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 9
},
"id": 6,
"panels": [],
"title": "2. Deployment Trends",
Expand All @@ -328,7 +334,7 @@
"h": 8,
"w": 8,
"x": 0,
"y": 9
"y": 10
},
"id": 7,
"options": {
Expand Down Expand Up @@ -394,7 +400,7 @@
"h": 8,
"w": 8,
"x": 8,
"y": 9
"y": 10
},
"id": 8,
"options": {
Expand Down Expand Up @@ -460,7 +466,7 @@
"h": 8,
"w": 8,
"x": 16,
"y": 9
"y": 10
},
"id": 15,
"options": {
Expand Down Expand Up @@ -505,6 +511,12 @@
},
{
"collapsed": false,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 18
},
"id": 9,
"panels": [],
"title": "3. Deployment Durations",
Expand Down Expand Up @@ -533,7 +545,7 @@
"h": 8,
"w": 6,
"x": 0,
"y": 17
"y": 19
},
"id": 10,
"options": {
Expand Down Expand Up @@ -588,7 +600,7 @@
"h": 8,
"w": 9,
"x": 6,
"y": 17
"y": 19
},
"id": 11,
"options": {
Expand Down Expand Up @@ -662,7 +674,7 @@
"h": 8,
"w": 9,
"x": 15,
"y": 17
"y": 19
},
"id": 12,
"options": {
Expand Down Expand Up @@ -722,7 +734,7 @@
"h": 7,
"w": 24,
"x": 0,
"y": 25
"y": 27
},
"id": 16,
"options": {
Expand Down Expand Up @@ -755,6 +767,12 @@
},
{
"collapsed": false,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 34
},
"id": 13,
"panels": [],
"title": "4. Deployment Details",
Expand Down Expand Up @@ -791,7 +809,7 @@
"h": 9,
"w": 24,
"x": 0,
"y": 32
"y": 35
},
"id": 14,
"options": {
Expand Down Expand Up @@ -830,6 +848,12 @@
},
{
"collapsed": false,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 44
},
"id": 17,
"panels": [],
"title": "5. Images",
Expand All @@ -856,7 +880,7 @@
"h": 8,
"w": 12,
"x": 0,
"y": 41
"y": 45
},
"id": 18,
"options": {
Expand Down Expand Up @@ -915,7 +939,7 @@
"h": 8,
"w": 12,
"x": 12,
"y": 41
"y": 45
},
"id": 19,
"options": {
Expand Down
Loading