This is an automated email from the ASF dual-hosted git repository.

zhangliang2022 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 0debdb73 feat: add bp v200 for jira (#3839)
0debdb73 is described below

commit 0debdb736ec23ff6dcbf4d2cf77411fbf4f3ffa1
Author: Likyh <[email protected]>
AuthorDate: Fri Dec 2 10:52:39 2022 +0800

    feat: add bp v200 for jira (#3839)
---
 plugins/jenkins/api/blueprint_v200_test.go         |   4 +-
 .../jira/api/{blueprint.go => blueprint_v100.go}   |   2 +-
 plugins/jira/api/blueprint_v200.go                 | 126 +++++++++++++++++++++
 .../{jenkins => jira}/api/blueprint_v200_test.go   |  49 ++++----
 plugins/jira/impl/impl.go                          |   6 +-
 plugins/jira/models/board.go                       |   2 +-
 plugins/jira/models/transformation_rules.go        |  12 +-
 7 files changed, 169 insertions(+), 32 deletions(-)

diff --git a/plugins/jenkins/api/blueprint_v200_test.go 
b/plugins/jenkins/api/blueprint_v200_test.go
index d536390f..641d2996 100644
--- a/plugins/jenkins/api/blueprint_v200_test.go
+++ b/plugins/jenkins/api/blueprint_v200_test.go
@@ -55,7 +55,7 @@ func TestMakeDataSourcePipelinePlanV200(t *testing.T) {
                        Model: common.Model{
                                ID: 1,
                        },
-                       Name:              "github transformation rule",
+                       Name:              "jenkins transformation rule",
                        DeploymentPattern: "hey,man,wasup",
                }
 
@@ -74,7 +74,7 @@ func TestMakeDataSourcePipelinePlanV200(t *testing.T) {
                                        "connectionId": uint64(1),
                                        "fullName":     "a/b/ccc",
                                        "transformationRules": 
map[string]interface{}{
-                                               "name":              "github 
transformation rule",
+                                               "name":              "jenkins 
transformation rule",
                                                "deploymentPattern": 
"hey,man,wasup",
                                        },
                                },
diff --git a/plugins/jira/api/blueprint.go b/plugins/jira/api/blueprint_v100.go
similarity index 93%
rename from plugins/jira/api/blueprint.go
rename to plugins/jira/api/blueprint_v100.go
index 776d59fd..6ab62b18 100644
--- a/plugins/jira/api/blueprint.go
+++ b/plugins/jira/api/blueprint_v100.go
@@ -25,7 +25,7 @@ import (
        "github.com/apache/incubator-devlake/plugins/jira/tasks"
 )
 
-func MakePipelinePlan(subtaskMetas []core.SubTaskMeta, connectionId uint64, 
scope []*core.BlueprintScopeV100) (core.PipelinePlan, errors.Error) {
+func MakePipelinePlanV100(subtaskMetas []core.SubTaskMeta, connectionId 
uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, errors.Error) {
        var err errors.Error
        plan := make(core.PipelinePlan, len(scope))
        for i, scopeElem := range scope {
diff --git a/plugins/jira/api/blueprint_v200.go 
b/plugins/jira/api/blueprint_v200.go
new file mode 100644
index 00000000..901bf480
--- /dev/null
+++ b/plugins/jira/api/blueprint_v200.go
@@ -0,0 +1,126 @@
+/*
+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 api
+
+import (
+       goerror "errors"
+       "github.com/apache/incubator-devlake/models/domainlayer/ticket"
+       "github.com/apache/incubator-devlake/plugins/helper"
+       "gorm.io/gorm"
+
+       "github.com/apache/incubator-devlake/errors"
+       "github.com/apache/incubator-devlake/models/domainlayer"
+       "github.com/apache/incubator-devlake/models/domainlayer/didgen"
+       "github.com/apache/incubator-devlake/plugins/core"
+       "github.com/apache/incubator-devlake/plugins/core/dal"
+       "github.com/apache/incubator-devlake/plugins/jira/models"
+       "github.com/apache/incubator-devlake/plugins/jira/tasks"
+       "github.com/apache/incubator-devlake/utils"
+       "github.com/mitchellh/mapstructure"
+)
+
+func MakeDataSourcePipelinePlanV200(subtaskMetas []core.SubTaskMeta, 
connectionId uint64, bpScopes []*core.BlueprintScopeV200) (core.PipelinePlan, 
[]core.Scope, errors.Error) {
+       db := basicRes.GetDal()
+       // get the connection info for url
+       connection := &models.JiraConnection{}
+       err := connectionHelper.FirstById(connection, connectionId)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       plan := make(core.PipelinePlan, 0, len(bpScopes))
+       scopes := make([]core.Scope, 0, len(bpScopes))
+       for i, bpScope := range bpScopes {
+               var jiraBoard *models.JiraBoard
+               // get repo from db
+               err = db.First(jiraBoard, dal.Where(`id = ?`, bpScope.Id))
+               if err != nil {
+                       return nil, nil, err
+               }
+               var transformationRule *models.JiraTransformationRule
+               // get transformation rules from db
+               err = db.First(transformationRule, dal.Where(`id = ?`, 
jiraBoard.TransformationRuleId))
+               if err != nil && goerror.Is(err, gorm.ErrRecordNotFound) {
+                       return nil, nil, err
+               }
+               var scope []core.Scope
+               // make pipeline for each bpScope
+               plan[i], scope, err = 
makeDataSourcePipelinePlanV200(subtaskMetas, bpScope, jiraBoard, 
transformationRule)
+               if err != nil {
+                       return nil, nil, err
+               }
+               if len(scope) > 0 {
+                       scopes = append(scopes, scope...)
+               }
+
+       }
+
+       return plan, scopes, nil
+}
+
+func makeDataSourcePipelinePlanV200(
+       subtaskMetas []core.SubTaskMeta,
+       bpScope *core.BlueprintScopeV200,
+       jiraBoard *models.JiraBoard,
+       transformationRule *models.JiraTransformationRule,
+) (core.PipelineStage, []core.Scope, errors.Error) {
+       var err errors.Error
+       var stage core.PipelineStage
+       scopes := make([]core.Scope, 0)
+
+       // construct task options for jenkins
+       var options map[string]interface{}
+       err = errors.Convert(mapstructure.Decode(jiraBoard, &options))
+       if err != nil {
+               return nil, nil, err
+       }
+       // make sure task options is valid
+       _, err = tasks.DecodeAndValidateTaskOptions(options)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       var transformationRuleMap map[string]interface{}
+       err = errors.Convert(mapstructure.Decode(transformationRule, 
&transformationRuleMap))
+       if err != nil {
+               return nil, nil, err
+       }
+       options["transformationRules"] = transformationRuleMap
+       subtasks, err := helper.MakePipelinePlanSubtasks(subtaskMetas, 
bpScope.Entities)
+       if err != nil {
+               return nil, nil, err
+       }
+       stage = append(stage, &core.PipelineTask{
+               Plugin:   "jira",
+               Subtasks: subtasks,
+               Options:  options,
+       })
+
+       // add cicd_scope to scopes
+       if utils.StringsContains(bpScope.Entities, core.DOMAIN_TYPE_TICKET) {
+               scopeCICD := &ticket.Board{
+                       DomainEntity: domainlayer.DomainEntity{
+                               Id: 
didgen.NewDomainIdGenerator(&models.JiraBoard{}).Generate(jiraBoard.ConnectionId,
 jiraBoard.BoardId),
+                       },
+                       Name: jiraBoard.Name,
+               }
+               scopes = append(scopes, scopeCICD)
+       }
+
+       return stage, scopes, nil
+}
diff --git a/plugins/jenkins/api/blueprint_v200_test.go 
b/plugins/jira/api/blueprint_v200_test.go
similarity index 69%
copy from plugins/jenkins/api/blueprint_v200_test.go
copy to plugins/jira/api/blueprint_v200_test.go
index d536390f..672541c2 100644
--- a/plugins/jenkins/api/blueprint_v200_test.go
+++ b/plugins/jira/api/blueprint_v200_test.go
@@ -21,20 +21,20 @@ import (
        "github.com/apache/incubator-devlake/mocks"
        "github.com/apache/incubator-devlake/models/common"
        "github.com/apache/incubator-devlake/models/domainlayer"
-       "github.com/apache/incubator-devlake/models/domainlayer/devops"
+       "github.com/apache/incubator-devlake/models/domainlayer/ticket"
        "github.com/apache/incubator-devlake/plugins/core"
-       "github.com/apache/incubator-devlake/plugins/jenkins/models"
+       "github.com/apache/incubator-devlake/plugins/jira/models"
        "github.com/stretchr/testify/assert"
        "testing"
 )
 
 func TestMakeDataSourcePipelinePlanV200(t *testing.T) {
        mockMeta := mocks.NewPluginMeta(t)
-       
mockMeta.On("RootPkgPath").Return("github.com/apache/incubator-devlake/plugins/jenkins")
-       err := core.RegisterPlugin("jenkins", mockMeta)
+       
mockMeta.On("RootPkgPath").Return("github.com/apache/incubator-devlake/plugins/jira")
+       err := core.RegisterPlugin("jira", mockMeta)
        assert.Nil(t, err)
        bs := &core.BlueprintScopeV200{
-               Entities: []string{"CICD"},
+               Entities: []string{"TICKET"},
                Id:       "",
                Name:     "",
        }
@@ -44,39 +44,46 @@ func TestMakeDataSourcePipelinePlanV200(t *testing.T) {
        plan := make(core.PipelinePlan, len(bpScopes))
        scopes := make([]core.Scope, 0, len(bpScopes))
        for i, bpScope := range bpScopes {
-               jenkinsJob := &models.JenkinsJob{
+               jiraBoard := &models.JiraBoard{
                        ConnectionId: 1,
-                       FullName:     "a/b/ccc",
-                       Path:         "a/b/",
-                       Name:         "ccc",
+                       BoardId:      10,
+                       Name:         "a",
+                       ProjectId:    20,
+                       Self:         "self",
+                       Type:         "type",
                }
 
-               transformationRule := &models.JenkinsTransformationRule{
+               transformationRule := &models.JiraTransformationRule{
                        Model: common.Model{
                                ID: 1,
                        },
-                       Name:              "github transformation rule",
-                       DeploymentPattern: "hey,man,wasup",
+                       Name:         "jira transformation rule",
+                       EpicKeyField: "hey,man,wasup",
                }
 
                var scope []core.Scope
-               plan[i], scope, err = makeDataSourcePipelinePlanV200(nil, 
bpScope, jenkinsJob, transformationRule)
+               plan[i], scope, err = makeDataSourcePipelinePlanV200(nil, 
bpScope, jiraBoard, transformationRule)
                assert.Nil(t, err)
                scopes = append(scopes, scope...)
        }
        expectPlan := core.PipelinePlan{
                core.PipelineStage{
                        {
-                               Plugin:     "jenkins",
+                               Plugin:     "jira",
                                Subtasks:   []string{},
                                SkipOnFail: false,
                                Options: map[string]interface{}{
-                                       "connectionId": uint64(1),
-                                       "fullName":     "a/b/ccc",
+                                       "connectionId":         uint64(1),
+                                       "boardId":              uint64(10),
+                                       "projectId":            uint(20),
+                                       "name":                 "a",
+                                       "self":                 "self",
+                                       "transformationRuleId": uint64(0),
                                        "transformationRules": 
map[string]interface{}{
-                                               "name":              "github 
transformation rule",
-                                               "deploymentPattern": 
"hey,man,wasup",
+                                               "name":         "jira 
transformation rule",
+                                               "epicKeyField": "hey,man,wasup",
                                        },
+                                       "type": "type",
                                },
                        },
                },
@@ -84,11 +91,11 @@ func TestMakeDataSourcePipelinePlanV200(t *testing.T) {
        assert.Equal(t, expectPlan, plan)
 
        expectScopes := make([]core.Scope, 0)
-       scopeCicd := &devops.CicdScope{
+       scopeCicd := &ticket.Board{
                DomainEntity: domainlayer.DomainEntity{
-                       Id: "jenkins:JenkinsJob:1:a/b/ccc",
+                       Id: "jira:JiraBoard:1:10",
                },
-               Name:        "a/b/ccc",
+               Name:        "a",
                Description: "",
                Url:         "",
                CreatedDate: nil,
diff --git a/plugins/jira/impl/impl.go b/plugins/jira/impl/impl.go
index 2aff6321..8d02d463 100644
--- a/plugins/jira/impl/impl.go
+++ b/plugins/jira/impl/impl.go
@@ -217,7 +217,11 @@ func (plugin Jira) PrepareTaskData(taskCtx 
core.TaskContext, options map[string]
 }
 
 func (plugin Jira) MakePipelinePlan(connectionId uint64, scope 
[]*core.BlueprintScopeV100) (core.PipelinePlan, errors.Error) {
-       return api.MakePipelinePlan(plugin.SubTaskMetas(), connectionId, scope)
+       return api.MakePipelinePlanV100(plugin.SubTaskMetas(), connectionId, 
scope)
+}
+
+func (plugin Jira) MakeDataSourcePipelinePlanV200(connectionId uint64, scopes 
[]*core.BlueprintScopeV200) (pp core.PipelinePlan, sc []core.Scope, err 
errors.Error) {
+       return api.MakeDataSourcePipelinePlanV200(plugin.SubTaskMetas(), 
connectionId, scopes)
 }
 
 func (plugin Jira) RootPkgPath() string {
diff --git a/plugins/jira/models/board.go b/plugins/jira/models/board.go
index 317ffd89..c1a3ab65 100644
--- a/plugins/jira/models/board.go
+++ b/plugins/jira/models/board.go
@@ -22,7 +22,7 @@ import (
 )
 
 type JiraBoard struct {
-       common.NoPKModel
+       common.NoPKModel     `mapstructure:"-"`
        ConnectionId         uint64 `json:"connectionId" 
mapstructure:"connectionId" gorm:"primaryKey"`
        BoardId              uint64 `json:"boardId" mapstructure:"boardId" 
gorm:"primaryKey"`
        TransformationRuleId uint64 `json:"transformationRuleId" 
mapstructure:"transformationRuleId"`
diff --git a/plugins/jira/models/transformation_rules.go 
b/plugins/jira/models/transformation_rules.go
index 0c10f5df..448bcab6 100644
--- a/plugins/jira/models/transformation_rules.go
+++ b/plugins/jira/models/transformation_rules.go
@@ -24,12 +24,12 @@ import (
 )
 
 type JiraTransformationRule struct {
-       common.Model
-       Name                       string          `gorm:"type:varchar(255)"`
-       EpicKeyField               string          `json:"epicKeyField" 
gorm:"type:varchar(255)"`
-       StoryPointField            string          `json:"storyPointField" 
gorm:"type:varchar(255)"`
-       RemotelinkCommitShaPattern string          
`json:"remotelinkCommitShaPattern" gorm:"type:varchar(255)"`
-       TypeMappings               json.RawMessage `json:"typeMappings"`
+       common.Model               `mapstructure:"-"`
+       Name                       string          `mapstructure:"name" 
json:"name" gorm:"type:varchar(255)"`
+       EpicKeyField               string          
`mapstructure:"epicKeyField,omitempty" json:"epicKeyField" 
gorm:"type:varchar(255)"`
+       StoryPointField            string          
`mapstructure:"storyPointField,omitempty" json:"storyPointField" 
gorm:"type:varchar(255)"`
+       RemotelinkCommitShaPattern string          
`mapstructure:"remotelinkCommitShaPattern,omitempty" 
json:"remotelinkCommitShaPattern" gorm:"type:varchar(255)"`
+       TypeMappings               json.RawMessage 
`mapstructure:"typeMappings,omitempty" json:"typeMappings"`
 }
 
 func (JiraTransformationRule) TableName() string {

Reply via email to