Skip to content

Commit 62b67c1

Browse files
authored
refactor: opsgenie adopts the new dshelpers (#7341)
1 parent 04aef2e commit 62b67c1

File tree

11 files changed

+80
-206
lines changed

11 files changed

+80
-206
lines changed

backend/helpers/pluginhelper/api/ds_scope_api_helper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ import (
2424
"github.com/apache/incubator-devlake/core/errors"
2525
"github.com/apache/incubator-devlake/core/models/common"
2626
"github.com/apache/incubator-devlake/core/plugin"
27+
serviceHelper "github.com/apache/incubator-devlake/helpers/pluginhelper/services"
2728
"github.com/apache/incubator-devlake/helpers/srvhelper"
2829
"github.com/apache/incubator-devlake/server/api/shared"
2930
)
3031

32+
type ScopeRefDoc = serviceHelper.BlueprintProjectPairs
33+
3134
type PutScopesReqBody[T any] struct {
3235
Data []*T `json:"data"`
3336
}

backend/helpers/pluginhelper/api/scope_generic_helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ type (
6060
Blueprints []*models.Blueprint `mapstructure:"blueprints,omitempty" json:"blueprints"`
6161
}
6262
// Alias, for swagger purposes
63-
ScopeRefDoc = serviceHelper.BlueprintProjectPairs
6463
ScopeRes[Scope plugin.ToolLayerScope, ScopeConfig any] struct {
6564
Scope Scope `mapstructure:"scope,omitempty" json:"scope,omitempty"`
6665
ScopeConfig *ScopeConfig `mapstructure:"scopeConfig,omitempty" json:"scopeConfig,omitempty"`

backend/helpers/srvhelper/model_service_helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func (srv *ModelSrvHelper[M]) ValidateModel(model *M) errors.Error {
9696

9797
// Create validates given model and insert it into database if validation passed
9898
func (srv *ModelSrvHelper[M]) Create(model *M) errors.Error {
99-
println("create model")
10099
err := srv.ValidateModel(model)
101100
if err != nil {
102101
return err

backend/plugins/bitbucket_server/api/scope.go renamed to backend/plugins/bitbucket_server/api/scope_api.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,16 @@ import (
2626
"github.com/apache/incubator-devlake/plugins/bitbucket_server/models"
2727
)
2828

29-
type ScopeRes struct {
30-
models.BitbucketServerRepo
31-
api.ScopeResDoc[models.BitbucketServerScopeConfig]
32-
}
33-
34-
type ScopeReq api.ScopeReq[models.BitbucketServerRepo]
29+
type PutScopesReqBody api.PutScopesReqBody[models.BitbucketServerRepo]
30+
type ScopeDetail api.ScopeDetail[models.BitbucketServerRepo, models.BitbucketServerScopeConfig]
3531

3632
// PutScope create or update repo
3733
// @Summary create or update repo
3834
// @Description Create or update repo
3935
// @Tags plugins/bitbucket_server
4036
// @Accept application/json
4137
// @Param connectionId path int true "connection ID"
42-
// @Param scope body ScopeReq true "json"
38+
// @Param scope body PutScopesReqBody true "json"
4339
// @Success 200 {object} []models.BitbucketServerRepo
4440
// @Failure 400 {object} shared.ApiBody "Bad Request"
4541
// @Failure 500 {object} shared.ApiBody "Internal Error"
@@ -74,7 +70,7 @@ func UpdateScope(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, err
7470
// @Param pageSize query int false "page size, default 50"
7571
// @Param page query int false "page size, default 1"
7672
// @Param blueprints query bool false "also return blueprints using these scopes as part of the payload"
77-
// @Success 200 {object} []ScopeRes
73+
// @Success 200 {object} []ScopeDetail
7874
// @Failure 400 {object} shared.ApiBody "Bad Request"
7975
// @Failure 500 {object} shared.ApiBody "Internal Error"
8076
// @Router /plugins/bitbucket_server/connections/{connectionId}/scopes/ [GET]
@@ -97,7 +93,7 @@ func GetScopeDispatcher(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutp
9793
// @Tags plugins/bitbucket_server
9894
// @Param connectionId path int true "connection ID"
9995
// @Param scopeId path string true "repo ID"
100-
// @Success 200 {object} ScopeRes
96+
// @Success 200 {object} ScopeDetail
10197
// @Failure 400 {object} shared.ApiBody "Bad Request"
10298
// @Failure 500 {object} shared.ApiBody "Internal Error"
10399
// @Router /plugins/bitbucket_server/connections/{connectionId}/scopes/{scopeId} [GET]
File renamed without changes.

backend/plugins/circleci/tasks/task_data.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import (
2626
type CircleciOptions struct {
2727
ConnectionId uint64 `json:"connectionId" mapstructure:"connectionId"`
2828
ProjectSlug string `json:"projectSlug" mapstructure:"projectSlug"`
29-
PageSize uint64 `mapstruct:"pageSize" mapstructure:"pageSize,omitempty"`
30-
ScopeConfigId uint64 `json:"scopeConfigId" mapstructure:"scopeConfigId,omitempty"`
31-
ScopeConfig *models.CircleciScopeConfig `json:"scopeConfig" mapstructure:"scopeConfig,omitempty"`
29+
PageSize uint64 `json:"pageSize,omitempty" mapstructure:"pageSize,omitempty"`
30+
ScopeConfigId uint64 `json:"scopeConfigId,omitempty" mapstructure:"scopeConfigId,omitempty"`
31+
ScopeConfig *models.CircleciScopeConfig `json:"scopeConfig,omitempty" mapstructure:"scopeConfig,omitempty"`
3232
}
3333

3434
type CircleciTaskData struct {

backend/plugins/opsgenie/api/blueprint_v200.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,23 @@ func MakeDataSourcePipelinePlanV200(
3535
connectionId uint64,
3636
bpScopes []*coreModels.BlueprintScope,
3737
) (coreModels.PipelinePlan, []plugin.Scope, errors.Error) {
38-
// get the connection info for url
39-
connection := &models.OpsgenieConnection{}
40-
err := connectionHelper.FirstById(connection, connectionId)
38+
connection, err := dsHelper.ConnSrv.FindByPk(connectionId)
4139
if err != nil {
4240
return nil, nil, err
4341
}
4442
scopeDetails, err := dsHelper.ScopeSrv.MapScopeDetails(connectionId, bpScopes)
4543
if err != nil {
4644
return nil, nil, err
4745
}
48-
49-
plan, err := makeDataSourcePipelinePlanV200(subtaskMetas, scopeDetails, connection)
46+
plan, err := makePipelinePlanV200(subtaskMetas, scopeDetails, connection)
5047
if err != nil {
5148
return nil, nil, err
5249
}
5350
scopes, err := makeScopesV200(scopeDetails, connection)
54-
if err != nil {
55-
return nil, nil, err
56-
}
57-
58-
return plan, scopes, nil
51+
return plan, scopes, err
5952
}
6053

61-
func makeDataSourcePipelinePlanV200(
54+
func makePipelinePlanV200(
6255
subtaskMetas []plugin.SubTaskMeta,
6356
scopeDetails []*srvhelper.ScopeDetail[models.Service, models.OpsenieScopeConfig],
6457
connection *models.OpsgenieConnection,
@@ -77,7 +70,7 @@ func makeDataSourcePipelinePlanV200(
7770
subtaskMetas,
7871
scopeConfig.Entities,
7972
OpsgenieTaskOptions{
80-
ConnectionId: scope.ConnectionId,
73+
ConnectionId: connection.ID,
8174
ServiceId: scope.Id,
8275
ServiceName: scope.Name,
8376
},
@@ -95,15 +88,16 @@ func makeDataSourcePipelinePlanV200(
9588

9689
func makeScopesV200(
9790
scopeDetails []*srvhelper.ScopeDetail[models.Service, models.OpsenieScopeConfig],
98-
connection *models.OpsgenieConnection) ([]plugin.Scope, errors.Error) {
91+
connection *models.OpsgenieConnection,
92+
) ([]plugin.Scope, errors.Error) {
9993
scopes := make([]plugin.Scope, 0)
10094
for _, scopeDetail := range scopeDetails {
10195
opService, scopeConfig := scopeDetail.Scope, scopeDetail.ScopeConfig
10296
// add service to scopes
10397
if utils.StringsContains(scopeConfig.Entities, plugin.DOMAIN_TYPE_TICKET) {
10498
domainBoard := &ticket.Board{
10599
DomainEntity: domainlayer.DomainEntity{
106-
Id: didgen.NewDomainIdGenerator(&models.Service{}).Generate(opService.ConnectionId, opService.Id),
100+
Id: didgen.NewDomainIdGenerator(&models.Service{}).Generate(connection.ID, opService.Id),
107101
},
108102
Name: opService.Name,
109103
}

backend/plugins/opsgenie/api/connection.go renamed to backend/plugins/opsgenie/api/connection_api.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ func testOpsgenieConn(ctx context.Context, connection models.OpsgenieConn) (*plu
6868
// @Failure 500 {string} errcode.Error "Internal Error"
6969
// @Router /plugins/opsgenie/connections/{connectionId}/test [POST]
7070
func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
71-
connection := &models.OpsgenieConnection{}
72-
err := connectionHelper.First(connection, input.Params)
71+
connection, err := dsHelper.ConnApi.GetMergedConnection(input)
7372
if err != nil {
7473
return nil, err
7574
}
@@ -114,12 +113,7 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
114113
// @Failure 500 {string} errcode.Error "Internal Error"
115114
// @Router /plugins/opsgenie/connections [POST]
116115
func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
117-
connection := &models.OpsgenieConnection{}
118-
err := connectionHelper.Create(connection, input)
119-
if err != nil {
120-
return nil, err
121-
}
122-
return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status: http.StatusOK}, nil
116+
return dsHelper.ConnApi.Post(input)
123117
}
124118

125119
// @Summary patch opsgenie connection
@@ -131,17 +125,7 @@ func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
131125
// @Failure 500 {string} errcode.Error "Internal Error"
132126
// @Router /plugins/opsgenie/connections/{connectionId} [PATCH]
133127
func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
134-
connection := &models.OpsgenieConnection{}
135-
if err := connectionHelper.First(&connection, input.Params); err != nil {
136-
return nil, err
137-
}
138-
if err := (&models.OpsgenieConnection{}).MergeFromRequest(connection, input.Body); err != nil {
139-
return nil, errors.Convert(err)
140-
}
141-
if err := connectionHelper.SaveWithCreateOrUpdate(connection); err != nil {
142-
return nil, err
143-
}
144-
return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status: http.StatusOK}, nil
128+
return dsHelper.ConnApi.Patch(input)
145129
}
146130

147131
// @Summary delete opsgenie connection
@@ -153,7 +137,7 @@ func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
153137
// @Failure 500 {string} errcode.Error "Internal Error"
154138
// @Router /plugins/opsgenie/connections/{connectionId} [DELETE]
155139
func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
156-
return connectionHelper.Delete(&models.OpsgenieConnection{}, input)
140+
return dsHelper.ConnApi.Delete(input)
157141
}
158142

159143
// @Summary list opsgenie connections
@@ -164,15 +148,7 @@ func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput
164148
// @Failure 500 {string} errcode.Error "Internal Error"
165149
// @Router /plugins/opsgenie/connections [GET]
166150
func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
167-
var connections []models.OpsgenieConnection
168-
err := connectionHelper.List(&connections)
169-
if err != nil {
170-
return nil, err
171-
}
172-
for idx, c := range connections {
173-
connections[idx] = c.Sanitize()
174-
}
175-
return &plugin.ApiResourceOutput{Body: connections}, nil
151+
return dsHelper.ConnApi.GetAll(input)
176152
}
177153

178154
// @Summary get opsgenie connection
@@ -183,10 +159,5 @@ func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
183159
// @Failure 500 {string} errcode.Error "Internal Error"
184160
// @Router /plugins/opsgenie/connections/{connectionId} [GET]
185161
func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
186-
connection := &models.OpsgenieConnection{}
187-
err := connectionHelper.First(connection, input.Params)
188-
if err != nil {
189-
return nil, err
190-
}
191-
return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, nil
162+
return dsHelper.ConnApi.GetDetail(input)
192163
}

backend/plugins/opsgenie/api/init.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,15 @@ import (
2626
)
2727

2828
var vld *validator.Validate
29-
var connectionHelper *api.ConnectionApiHelper
30-
var dsHelper *api.DsHelper[models.OpsgenieConnection, models.Service, models.OpsenieScopeConfig]
29+
3130
var basicRes context.BasicRes
31+
var dsHelper *api.DsHelper[models.OpsgenieConnection, models.Service, models.OpsenieScopeConfig]
32+
var raProxy *api.DsRemoteApiProxyHelper[models.OpsgenieConnection]
33+
var raScopeList *api.DsRemoteApiScopeListHelper[models.OpsgenieConnection, models.Service, OpsgenieRemotePagination]
3234

3335
func Init(br context.BasicRes, p plugin.PluginMeta) {
34-
3536
basicRes = br
3637
vld = validator.New()
37-
connectionHelper = api.NewConnectionHelper(
38-
basicRes,
39-
vld,
40-
p.Name(),
41-
)
42-
4338
dsHelper = api.NewDataSourceHelper[
4439
models.OpsgenieConnection, models.Service, models.OpsenieScopeConfig,
4540
](
@@ -52,5 +47,6 @@ func Init(br context.BasicRes, p plugin.PluginMeta) {
5247
nil,
5348
nil,
5449
)
55-
50+
raProxy = api.NewDsRemoteApiProxyHelper[models.OpsgenieConnection](dsHelper.ConnApi.ModelApiHelper)
51+
raScopeList = api.NewDsRemoteApiScopeListHelper[models.OpsgenieConnection, models.Service, OpsgenieRemotePagination](raProxy, listOpsgenieRemoteScopes)
5652
}

0 commit comments

Comments
 (0)