Skip to content

Commit

Permalink
refactor: jira adopts new dshelpers (#6562)
Browse files Browse the repository at this point in the history
* docs: update SearchRemoteScopes swag doc

* fix: jenkins scope configs path var name

* refactor: jira adopts new dshelpers
  • Loading branch information
klesh authored Dec 5, 2023
1 parent 8a80099 commit 5ebf329
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 445 deletions.
6 changes: 3 additions & 3 deletions backend/plugins/github/api/remote_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, er
return raScopeList.Get(input)
}

// SearchRemoteScopes searches projects on the remote server
// @Summary searches projects on the remote server
// @Description searches projects on the remote server
// SearchRemoteScopes searches scopes on the remote server
// @Summary searches scopes on the remote server
// @Description searches scopes on the remote server
// @Accept application/json
// @Param connectionId path int false "connection ID"
// @Param search query string false "search"
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/jenkins/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (p Jenkins) ApiResources() map[string]map[string]plugin.ApiResourceHandler
"POST": api.CreateScopeConfig,
"GET": api.GetScopeConfigList,
},
"connections/:connectionId/scope-configs/:id": {
"connections/:connectionId/scope-configs/:scopeConfigId": {
"PATCH": api.UpdateScopeConfig,
"GET": api.GetScopeConfig,
"DELETE": api.DeleteScopeConfig,
Expand Down
72 changes: 42 additions & 30 deletions backend/plugins/jira/api/blueprint_v200.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package api

import (
"context"

"github.com/apache/incubator-devlake/core/errors"
coreModels "github.com/apache/incubator-devlake/core/models"
"github.com/apache/incubator-devlake/core/models/domainlayer"
Expand All @@ -26,6 +28,7 @@ import (
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/core/utils"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/helpers/srvhelper"
"github.com/apache/incubator-devlake/plugins/jira/models"
)

Expand All @@ -34,12 +37,28 @@ func MakeDataSourcePipelinePlanV200(
connectionId uint64,
bpScopes []*coreModels.BlueprintScope,
) (coreModels.PipelinePlan, []plugin.Scope, errors.Error) {
plan := make(coreModels.PipelinePlan, len(bpScopes))
plan, err := makeDataSourcePipelinePlanV200(subtaskMetas, plan, bpScopes, connectionId)
// load connection, scope and scopeConfig from the db
connection, err := dsHelper.ConnSrv.FindByPk(connectionId)
if err != nil {
return nil, nil, err
}
scopeDetails, err := dsHelper.ScopeSrv.MapScopeDetails(connectionId, bpScopes)
if err != nil {
return nil, nil, err
}

// needed for the connection to populate its access tokens
// if AppKey authentication method is selected
_, err = helper.NewApiClientFromConnection(context.TODO(), basicRes, connection)
if err != nil {
return nil, nil, err
}

plan, err := makeDataSourcePipelinePlanV200(subtaskMetas, scopeDetails, connection)
if err != nil {
return nil, nil, err
}
scopes, err := makeScopesV200(bpScopes, connectionId)
scopes, err := makeScopesV200(scopeDetails, connection)
if err != nil {
return nil, nil, err
}
Expand All @@ -49,52 +68,45 @@ func MakeDataSourcePipelinePlanV200(

func makeDataSourcePipelinePlanV200(
subtaskMetas []plugin.SubTaskMeta,
plan coreModels.PipelinePlan,
bpScopes []*coreModels.BlueprintScope,
connectionId uint64,
scopeDetails []*srvhelper.ScopeDetail[models.JiraBoard, models.JiraScopeConfig],
connection *models.JiraConnection,
) (coreModels.PipelinePlan, errors.Error) {
for i, bpScope := range bpScopes {
plan := make(coreModels.PipelinePlan, len(scopeDetails))
for i, scopeDetail := range scopeDetails {
stage := plan[i]
if stage == nil {
stage = coreModels.PipelineStage{}
}
// construct task options for Jira
options := make(map[string]interface{})
options["scopeId"] = bpScope.ScopeId
options["connectionId"] = connectionId

// get scope config from db
_, scopeConfig, err := scopeHelper.DbHelper().GetScopeAndConfig(connectionId, bpScope.ScopeId)
scope, scopeConfig := scopeDetail.Scope, scopeDetail.ScopeConfig
// construct task options for Jira
task, err := helper.MakePipelinePlanTask(
"jira",
subtaskMetas,
scopeConfig.Entities,
JiraTaskOptions{
ConnectionId: scope.ConnectionId,
BoardId: scope.BoardId,
},
)
if err != nil {
return nil, err
}

subtasks, err := helper.MakePipelinePlanSubtasks(subtaskMetas, scopeConfig.Entities)
if err != nil {
return nil, err
}
stage = append(stage, &coreModels.PipelineTask{
Plugin: "jira",
Subtasks: subtasks,
Options: options,
})
stage = append(stage, task)
plan[i] = stage
}

return plan, nil
}

func makeScopesV200(
bpScopes []*coreModels.BlueprintScope,
connectionId uint64,
scopeDetails []*srvhelper.ScopeDetail[models.JiraBoard, models.JiraScopeConfig],
connection *models.JiraConnection,
) ([]plugin.Scope, errors.Error) {
scopes := make([]plugin.Scope, 0)
for _, bpScope := range bpScopes {
// get board and scope config from db
jiraBoard, scopeConfig, err := scopeHelper.DbHelper().GetScopeAndConfig(connectionId, bpScope.ScopeId)
if err != nil {
return nil, err
}
for _, scopeDetail := range scopeDetails {
jiraBoard, scopeConfig := scopeDetail.Scope, scopeDetail.ScopeConfig
// add board to scopes
if utils.StringsContains(scopeConfig.Entities, plugin.DOMAIN_TYPE_TICKET) {
domainBoard := &ticket.Board{
Expand Down
110 changes: 0 additions & 110 deletions backend/plugins/jira/api/blueprint_v200_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/{connectionId}/test [POST]
func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.JiraConnection{}
err := connectionHelper.First(connection, input.Params)
connection, err := dsHelper.ConnApi.FindByPk(input)
if err != nil {
return nil, errors.BadInput.Wrap(err, "find connection from db")
return nil, err
}
// test connection
result, err := testConnection(context.TODO(), connection.JiraConn)
Expand All @@ -173,13 +172,7 @@ func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResource
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/connections [POST]
func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
// update from request and save to database
connection := &models.JiraConnection{}
err := connectionHelper.Create(connection, input)
if err != nil {
return nil, err
}
return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status: http.StatusOK}, nil
return dsHelper.ConnApi.Post(input)
}

// @Summary patch jira connection
Expand All @@ -191,12 +184,7 @@ func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/connections/{connectionId} [PATCH]
func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.JiraConnection{}
err := connectionHelper.Patch(connection, input)
if err != nil {
return nil, err
}
return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, nil
return dsHelper.ConnApi.Patch(input)
}

// @Summary delete a jira connection
Expand All @@ -208,14 +196,7 @@ func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/connections/{connectionId} [DELETE]
func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
conn := &models.JiraConnection{}
output, err := connectionHelper.Delete(conn, input)
if err != nil {
return output, err
}
output.Body = conn.Sanitize()
return output, nil

return dsHelper.ConnApi.Delete(input)
}

// @Summary get all jira connections
Expand All @@ -226,15 +207,7 @@ func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/connections [GET]
func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
var connections []models.JiraConnection
err := connectionHelper.List(&connections)
if err != nil {
return nil, err
}
for idx, c := range connections {
connections[idx] = c.Sanitize()
}
return &plugin.ApiResourceOutput{Body: connections, Status: http.StatusOK}, nil
return dsHelper.ConnApi.GetAll(input)
}

// @Summary get jira connection detail
Expand All @@ -245,10 +218,5 @@ func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/jira/connections/{connectionId} [GET]
func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.JiraConnection{}
err := connectionHelper.First(connection, input.Params)
if err != nil {
return nil, err
}
return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, err
return dsHelper.ConnApi.GetDetail(input)
}
Loading

0 comments on commit 5ebf329

Please sign in to comment.