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

warren 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 89f71c4b refactor: return transformationRuleName as well as 
transformationRuleId (#3853)
89f71c4b is described below

commit 89f71c4bd8fb175f612a9d509326054304309efd
Author: mindlesscloud <[email protected]>
AuthorDate: Mon Dec 5 18:25:40 2022 +0800

    refactor: return transformationRuleName as well as transformationRuleId 
(#3853)
---
 plugins/github/api/scope.go      | 41 ++++++++++++++++++++++++++++++++++++----
 plugins/github/models/repo.go    |  2 +-
 plugins/gitlab/api/scope.go      | 41 ++++++++++++++++++++++++++++++++++++----
 plugins/gitlab/models/project.go |  2 +-
 plugins/jenkins/api/scope.go     | 41 ++++++++++++++++++++++++++++++++++++----
 plugins/jenkins/models/job.go    |  4 ++--
 plugins/jira/api/scope.go        | 39 +++++++++++++++++++++++++++++++++++---
 plugins/jira/models/board.go     |  4 ++--
 8 files changed, 153 insertions(+), 21 deletions(-)

diff --git a/plugins/github/api/scope.go b/plugins/github/api/scope.go
index 284c2a00..40322387 100644
--- a/plugins/github/api/scope.go
+++ b/plugins/github/api/scope.go
@@ -30,6 +30,11 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
+type apiRepo struct {
+       models.GithubRepo
+       TransformationRuleName string `json:"transformationRuleName,omitempty"`
+}
+
 type req struct {
        Data []*models.GithubRepo `json:"data"`
 }
@@ -122,7 +127,7 @@ func UpdateScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.
 // @Param connectionId path int true "connection ID"
 // @Param pageSize query int false "page size, default 50"
 // @Param page query int false "page size, default 1"
-// @Success 200  {object} []models.GithubRepo
+// @Success 200  {object} []apiRepo
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/connections/{connectionId}/scopes/ [GET]
@@ -137,7 +142,28 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: repos, Status: http.StatusOK}, nil
+       var ruleIds []uint64
+       for _, repo := range repos {
+               if repo.TransformationRuleId > 0 {
+                       ruleIds = append(ruleIds, repo.TransformationRuleId)
+               }
+       }
+       var rules []models.GithubTransformationRule
+       if len(ruleIds) > 0 {
+               err = basicRes.GetDal().All(&rules, dal.Where("id IN (?)", 
ruleIds))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       names := make(map[uint64]string)
+       for _, rule := range rules {
+               names[rule.ID] = rule.Name
+       }
+       var apiRepos []apiRepo
+       for _, repo := range repos {
+               apiRepos = append(apiRepos, apiRepo{repo, 
names[repo.TransformationRuleId]})
+       }
+       return &core.ApiResourceOutput{Body: apiRepos, Status: http.StatusOK}, 
nil
 }
 
 // GetScope get one Github repo
@@ -146,7 +172,7 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
 // @Tags plugins/github
 // @Param connectionId path int true "connection ID"
 // @Param repoId path int true "repo ID"
-// @Success 200  {object} models.GithubRepo
+// @Success 200  {object} apiRepo
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/connections/{connectionId}/scopes/{repoId} [GET]
@@ -160,7 +186,14 @@ func GetScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.Err
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: repo, Status: http.StatusOK}, nil
+       var rule models.GithubTransformationRule
+       if repo.TransformationRuleId > 0 {
+               err = basicRes.GetDal().First(&rule, dal.Where("id = ?", 
repo.TransformationRuleId))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return &core.ApiResourceOutput{Body: apiRepo{repo, rule.Name}, Status: 
http.StatusOK}, nil
 }
 
 func extractParam(params map[string]string) (uint64, uint64) {
diff --git a/plugins/github/models/repo.go b/plugins/github/models/repo.go
index be9356d0..8f20776b 100644
--- a/plugins/github/models/repo.go
+++ b/plugins/github/models/repo.go
@@ -28,7 +28,7 @@ type GithubRepo struct {
        Name                 string     `json:"name" gorm:"type:varchar(255)" 
mapstructure:"name,omitempty"`
        HTMLUrl              string     `json:"HTMLUrl" 
gorm:"type:varchar(255)" mapstructure:"HTMLUrl,omitempty"`
        Description          string     `json:"description" 
mapstructure:"description,omitempty"`
-       TransformationRuleId uint64     `json:"transformationRuleId" 
mapstructure:"transformationRuleId,omitempty"`
+       TransformationRuleId uint64     `json:"transformationRuleId,omitempty" 
mapstructure:"transformationRuleId,omitempty"`
        OwnerId              int        `json:"ownerId" 
mapstructure:"ownerId,omitempty"`
        OwnerLogin           string     `json:"ownerLogin" 
gorm:"type:varchar(255)" mapstructure:"ownerLogin,omitempty"`
        Language             string     `json:"language" 
gorm:"type:varchar(255)" mapstructure:"language,omitempty"`
diff --git a/plugins/gitlab/api/scope.go b/plugins/gitlab/api/scope.go
index ff5c8c72..afda4a11 100644
--- a/plugins/gitlab/api/scope.go
+++ b/plugins/gitlab/api/scope.go
@@ -30,6 +30,11 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
+type apiProject struct {
+       models.GitlabProject
+       TransformationRuleName string `json:"transformationRuleName,omitempty"`
+}
+
 type req struct {
        Data []*models.GitlabProject `json:"data"`
 }
@@ -120,7 +125,7 @@ func UpdateScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.
 // @Description get Gitlab projects
 // @Tags plugins/gitlab
 // @Param connectionId path int false "connection ID"
-// @Success 200  {object} []models.GitlabProject
+// @Success 200  {object} []apiProject
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/connections/{connectionId}/scopes/ [GET]
@@ -135,7 +140,28 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: projects, Status: http.StatusOK}, 
nil
+       var ruleIds []uint64
+       for _, proj := range projects {
+               if proj.TransformationRuleId > 0 {
+                       ruleIds = append(ruleIds, proj.TransformationRuleId)
+               }
+       }
+       var rules []models.GitlabTransformationRule
+       if len(ruleIds) > 0 {
+               err = BasicRes.GetDal().All(&rules, dal.Where("id IN (?)", 
ruleIds))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       names := make(map[uint64]string)
+       for _, rule := range rules {
+               names[rule.ID] = rule.Name
+       }
+       var apiProjects []apiProject
+       for _, proj := range projects {
+               apiProjects = append(apiProjects, apiProject{proj, 
names[proj.TransformationRuleId]})
+       }
+       return &core.ApiResourceOutput{Body: apiProjects, Status: 
http.StatusOK}, nil
 }
 
 // GetScope get one Gitlab project
@@ -146,7 +172,7 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
 // @Param projectId path int false "project ID"
 // @Param pageSize query int false "page size, default 50"
 // @Param page query int false "page size, default 1"
-// @Success 200  {object} models.GitlabProject
+// @Success 200  {object} apiProject
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/connections/{connectionId}/scopes/{projectId} [GET]
@@ -160,7 +186,14 @@ func GetScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.Err
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: project, Status: http.StatusOK}, 
nil
+       var rule models.GitlabTransformationRule
+       if project.TransformationRuleId > 0 {
+               err = BasicRes.GetDal().First(&rule, dal.Where("id = ?", 
project.TransformationRuleId))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return &core.ApiResourceOutput{Body: apiProject{project, rule.Name}, 
Status: http.StatusOK}, nil
 }
 
 func extractParam(params map[string]string) (uint64, uint64) {
diff --git a/plugins/gitlab/models/project.go b/plugins/gitlab/models/project.go
index 643e8c01..2a2fa69b 100644
--- a/plugins/gitlab/models/project.go
+++ b/plugins/gitlab/models/project.go
@@ -25,7 +25,7 @@ import (
 
 type GitlabProject struct {
        ConnectionId            uint64 `json:"connectionId" 
mapstructure:"connectionId" gorm:"primaryKey"`
-       TransformationRuleId    uint64 `json:"transformationRuleId" 
mapstructure:"transformationRuleId"`
+       TransformationRuleId    uint64 `json:"transformationRuleId,omitempty" 
mapstructure:"transformationRuleId"`
        GitlabId                int    `json:"gitlabId" mapstructure:"gitlabId" 
gorm:"primaryKey"`
        Name                    string `json:"name" mapstructure:"name" 
gorm:"type:varchar(255)"`
        Description             string `json:"description" 
mapstructure:"description"`
diff --git a/plugins/jenkins/api/scope.go b/plugins/jenkins/api/scope.go
index 9517677a..9eedacf2 100644
--- a/plugins/jenkins/api/scope.go
+++ b/plugins/jenkins/api/scope.go
@@ -29,6 +29,11 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
+type apiJob struct {
+       models.JenkinsJob
+       TransformationRuleName string `json:"transformationRuleName,omitempty"`
+}
+
 type req struct {
        Data []*models.JenkinsJob `json:"data"`
 }
@@ -113,7 +118,7 @@ func UpdateScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.
 // @Param connectionId path int false "connection ID"
 // @Param pageSize query int false "page size, default 50"
 // @Param page query int false "page size, default 1"
-// @Success 200  {object} []models.JenkinsJob
+// @Success 200  {object} []apiJob
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/connections/{connectionId}/scopes/ [GET]
@@ -128,7 +133,28 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: jobs, Status: http.StatusOK}, nil
+       var ruleIds []uint64
+       for _, job := range jobs {
+               if job.TransformationRuleId > 0 {
+                       ruleIds = append(ruleIds, job.TransformationRuleId)
+               }
+       }
+       var rules []models.JenkinsTransformationRule
+       if len(ruleIds) > 0 {
+               err = BasicRes.GetDal().All(&rules, dal.Where("id IN (?)", 
ruleIds))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       names := make(map[uint64]string)
+       for _, rule := range rules {
+               names[rule.ID] = rule.Name
+       }
+       var apiJobs []apiJob
+       for _, job := range jobs {
+               apiJobs = append(apiJobs, apiJob{job, 
names[job.TransformationRuleId]})
+       }
+       return &core.ApiResourceOutput{Body: apiJobs, Status: http.StatusOK}, 
nil
 }
 
 // GetScope get one Jenkins job
@@ -137,7 +163,7 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
 // @Tags plugins/jenkins
 // @Param connectionId path int false "connection ID"
 // @Param fullName path string false "job's full name"
-// @Success 200  {object} models.JenkinsJob
+// @Success 200  {object} apiJob
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/connections/{connectionId}/scopes/{fullName} [GET]
@@ -151,7 +177,14 @@ func GetScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.Err
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: job, Status: http.StatusOK}, nil
+       var rule models.JenkinsJob
+       if job.TransformationRuleId > 0 {
+               err = BasicRes.GetDal().First(&rule, dal.Where("id = ?", 
job.TransformationRuleId))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return &core.ApiResourceOutput{Body: apiJob{job, rule.Name}, Status: 
http.StatusOK}, nil
 }
 
 func extractParam(params map[string]string) (uint64, string, errors.Error) {
diff --git a/plugins/jenkins/models/job.go b/plugins/jenkins/models/job.go
index 86215346..d217c513 100644
--- a/plugins/jenkins/models/job.go
+++ b/plugins/jenkins/models/job.go
@@ -25,7 +25,7 @@ import (
 type JenkinsJob struct {
        ConnectionId         uint64 `gorm:"primaryKey" 
mapstructure:"connectionId,omitempty" json:"connectionId"`
        FullName             string `gorm:"primaryKey;type:varchar(255)" 
mapstructure:"jobFullName" json:"jobFullName"` // "path1/path2/job name"
-       TransformationRuleId uint64 
`mapstructure:"transformationRules,omitempty" json:"transformationRuleId"`
+       TransformationRuleId uint64 
`mapstructure:"transformationRules,omitempty" 
json:"transformationRuleId,omitempty"`
        Name                 string `gorm:"index;type:varchar(255)" 
mapstructure:"-,omitempty" json:"-"` // "job name"
        Path                 string `gorm:"index;type:varchar(511)" 
mapstructure:"-,omitempty" json:"-"` // "job/path1/job/path2"
        Class                string `gorm:"type:varchar(255)" 
mapstructure:"class,omitempty" json:"class"`
@@ -34,7 +34,7 @@ type JenkinsJob struct {
        Url                  string `mapstructure:"url,omitempty" json:"url"`
        Description          string `mapstructure:"description,omitempty" 
json:"description"`
        PrimaryView          string `gorm:"type:varchar(255)" 
mapstructure:"primaryView,omitempty" json:"primaryView"`
-       common.NoPKModel     `mapstructure:"-"`
+       common.NoPKModel     `json:"-" mapstructure:"-"`
 }
 
 func (JenkinsJob) TableName() string {
diff --git a/plugins/jira/api/scope.go b/plugins/jira/api/scope.go
index b4af492d..faad1394 100644
--- a/plugins/jira/api/scope.go
+++ b/plugins/jira/api/scope.go
@@ -29,6 +29,11 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
+type apiBoard struct {
+       models.JiraBoard
+       TransformationRuleName string `json:"transformationRuleName,omitempty"`
+}
+
 type req struct {
        Data []*models.JiraBoard `json:"data"`
 }
@@ -118,7 +123,7 @@ func UpdateScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.
 // @Param connectionId path int false "connection ID"
 // @Param pageSize query int false "page size, default 50"
 // @Param page query int false "page size, default 1"
-// @Success 200  {object} []models.JiraBoard
+// @Success 200  {object} []apiBoard
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jira/connections/{connectionId}/scopes/ [GET]
@@ -133,7 +138,28 @@ func GetScopeList(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: boards, Status: http.StatusOK}, nil
+       var ruleIds []uint64
+       for _, board := range boards {
+               if board.TransformationRuleId > 0 {
+                       ruleIds = append(ruleIds, board.TransformationRuleId)
+               }
+       }
+       var rules []models.JiraTransformationRule
+       if len(ruleIds) > 0 {
+               err = basicRes.GetDal().All(&rules, dal.Where("id IN (?)", 
ruleIds))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       names := make(map[uint64]string)
+       for _, rule := range rules {
+               names[rule.ID] = rule.Name
+       }
+       var apiBoards []apiBoard
+       for _, board := range boards {
+               apiBoards = append(apiBoards, apiBoard{board, 
names[board.TransformationRuleId]})
+       }
+       return &core.ApiResourceOutput{Body: apiBoards, Status: http.StatusOK}, 
nil
 }
 
 // GetScope get one Jira board
@@ -156,7 +182,14 @@ func GetScope(input *core.ApiResourceInput) 
(*core.ApiResourceOutput, errors.Err
        if err != nil {
                return nil, err
        }
-       return &core.ApiResourceOutput{Body: board, Status: http.StatusOK}, nil
+       var rule models.JiraTransformationRule
+       if board.TransformationRuleId > 0 {
+               err = basicRes.GetDal().First(&rule, dal.Where("id = ?", 
board.TransformationRuleId))
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return &core.ApiResourceOutput{Body: apiBoard{board, rule.Name}, 
Status: http.StatusOK}, nil
 }
 
 func extractParam(params map[string]string) (uint64, uint64) {
diff --git a/plugins/jira/models/board.go b/plugins/jira/models/board.go
index c1a3ab65..370c7372 100644
--- a/plugins/jira/models/board.go
+++ b/plugins/jira/models/board.go
@@ -22,10 +22,10 @@ import (
 )
 
 type JiraBoard struct {
-       common.NoPKModel     `mapstructure:"-"`
+       common.NoPKModel     `json:"-" mapstructure:"-"`
        ConnectionId         uint64 `json:"connectionId" 
mapstructure:"connectionId" gorm:"primaryKey"`
        BoardId              uint64 `json:"boardId" mapstructure:"boardId" 
gorm:"primaryKey"`
-       TransformationRuleId uint64 `json:"transformationRuleId" 
mapstructure:"transformationRuleId"`
+       TransformationRuleId uint64 `json:"transformationRuleId,omitempty" 
mapstructure:"transformationRuleId"`
        ProjectId            uint   `json:"projectId" mapstructure:"projectId"`
        Name                 string `json:"name" mapstructure:"name" 
gorm:"type:varchar(255)"`
        Self                 string `json:"self" mapstructure:"self" 
gorm:"type:varchar(255)"`

Reply via email to