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
2 changes: 2 additions & 0 deletions backend/plugins/table_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
taiga "github.com/apache/incubator-devlake/plugins/taiga/impl"
tapd "github.com/apache/incubator-devlake/plugins/tapd/impl"
teambition "github.com/apache/incubator-devlake/plugins/teambition/impl"
tempo "github.com/apache/incubator-devlake/plugins/tempo/impl"
testmo "github.com/apache/incubator-devlake/plugins/testmo/impl"
trello "github.com/apache/incubator-devlake/plugins/trello/impl"
webhook "github.com/apache/incubator-devlake/plugins/webhook/impl"
Expand Down Expand Up @@ -97,6 +98,7 @@ func Test_GetPluginTablesInfo(t *testing.T) {
checker.FeedIn("taiga/models", taiga.Taiga{}.GetTablesInfo)
checker.FeedIn("tapd/models", tapd.Tapd{}.GetTablesInfo)
checker.FeedIn("teambition/models", teambition.Teambition{}.GetTablesInfo)
checker.FeedIn("tempo/models", tempo.Tempo{}.GetTablesInfo)
checker.FeedIn("testmo/models", testmo.Testmo{}.GetTablesInfo)
checker.FeedIn("trello/models", trello.Trello{}.GetTablesInfo)
checker.FeedIn("webhook/models", webhook.Webhook{}.GetTablesInfo)
Expand Down
120 changes: 120 additions & 0 deletions backend/plugins/tempo/api/blueprint_v200.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
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 (
"context"

"github.com/apache/incubator-devlake/core/errors"
coreModels "github.com/apache/incubator-devlake/core/models"
"github.com/apache/incubator-devlake/core/models/domainlayer"
"github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
"github.com/apache/incubator-devlake/core/models/domainlayer/ticket"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/helpers/srvhelper"
"github.com/apache/incubator-devlake/plugins/tempo/models"
)

func MakeDataSourcePipelinePlanV200(
subtaskMetas []plugin.SubTaskMeta,
connectionId uint64,
bpScopes []*coreModels.BlueprintScope,
) (pp coreModels.PipelinePlan, sc []plugin.Scope, err errors.Error) {
// 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
_, err = api.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(scopeDetails, connection)
if err != nil {
return nil, nil, err
}

return plan, scopes, nil
}

func makeDataSourcePipelinePlanV200(
subtaskMetas []plugin.SubTaskMeta,
scopeDetails []*srvhelper.ScopeDetail[models.TempoTeam, models.TempoScopeConfig],
connection *models.TempoConnection,
) (coreModels.PipelinePlan, errors.Error) {
plan := make(coreModels.PipelinePlan, len(scopeDetails))
for i, scopeDetail := range scopeDetails {
stage := plan[i]
if stage == nil {
stage = coreModels.PipelineStage{}
}

scope := scopeDetail.Scope
// Construct task options for Tempo
task, err := api.MakePipelinePlanTask(
"tempo",
subtaskMetas,
nil, // No entities to select for Tempo
map[string]interface{}{
"connectionId": scope.ConnectionId,
"teamId": scope.TeamId,
},
)
if err != nil {
return nil, err
}

stage = append(stage, task)
plan[i] = stage
}

return plan, nil
}

func makeScopesV200(
scopeDetails []*srvhelper.ScopeDetail[models.TempoTeam, models.TempoScopeConfig],
connection *models.TempoConnection,
) ([]plugin.Scope, errors.Error) {
scopes := make([]plugin.Scope, 0)
for _, scopeDetail := range scopeDetails {
tempoTeam := scopeDetail.Scope
// Add team to scopes
domainTeam := &ticket.Board{
DomainEntity: domainlayer.DomainEntity{
Id: didgen.NewDomainIdGenerator(&models.TempoTeam{}).Generate(tempoTeam.ConnectionId, tempoTeam.TeamId),
},
Name: tempoTeam.Name,
Url: "", // Tempo doesn't provide a direct URL for teams
Type: "team",
}
scopes = append(scopes, domainTeam)
}
return scopes, nil
}
211 changes: 211 additions & 0 deletions backend/plugins/tempo/api/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
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 (
"context"
"net/http"

"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/tempo/models"
"github.com/apache/incubator-devlake/server/api/shared"
)

type TempoTestConnResponse struct {
shared.ApiBody
Connection *models.TempoConnection
}

func testConnection(ctx context.Context, connection models.TempoConnection) (*TempoTestConnResponse, errors.Error) {
// Create API client
apiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &connection)
if err != nil {
return nil, err
}

// Test connection by fetching teams
res, err := apiClient.Get("teams", nil, nil)
if err != nil {
return nil, errors.Default.Wrap(err, "failed to test connection to Tempo API")
}

if res.StatusCode != http.StatusOK {
return nil, errors.HttpStatus(res.StatusCode).New("failed to connect to Tempo API")
}

// Sanitize and return response
connection = connection.Sanitize()
body := TempoTestConnResponse{}
body.Success = true
body.Message = "success"
body.Connection = &connection

return &body, nil
}

// TestConnection test tempo connection
// @Summary test tempo connection
// @Description Test Tempo Connection
// @Tags plugins/tempo
// @Param body body models.TempoConnection true "json body"
// @Success 200 {object} TempoTestConnResponse "Success"
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/test [POST]
func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
// Decode
var connection models.TempoConnection
if err := api.Decode(input.Body, &connection, nil); err != nil {
return nil, err
}
// Test connection
result, err := testConnection(context.TODO(), connection)
if err != nil {
return nil, plugin.WrapTestConnectionErrResp(basicRes, err)
}
return &plugin.ApiResourceOutput{Body: result, Status: http.StatusOK}, nil
}

// TestExistingConnection test tempo connection
// @Summary test tempo connection
// @Description Test Tempo Connection
// @Tags plugins/tempo
// @Param connectionId path int true "connection ID"
// @Success 200 {object} TempoTestConnResponse "Success"
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections/{connectionId}/test [POST]
func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection, err := dsHelper.ConnApi.GetMergedConnection(input)
if err != nil {
return nil, errors.Convert(err)
}
// Test connection
if result, err := testConnection(context.TODO(), *connection); err != nil {
return nil, plugin.WrapTestConnectionErrResp(basicRes, err)
} else {
return &plugin.ApiResourceOutput{Body: result, Status: http.StatusOK}, nil
}
}

// PostConnections create tempo connection
// @Summary create tempo connection
// @Description Create Tempo connection
// @Tags plugins/tempo
// @Param body body models.TempoConnection true "json body"
// @Success 200 {object} models.TempoConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections [POST]
func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.Post(input)
}

// PatchConnection patch tempo connection
// @Summary patch tempo connection
// @Description Patch Tempo connection
// @Tags plugins/tempo
// @Param body body models.TempoConnection true "json body"
// @Success 200 {object} models.TempoConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections/{connectionId} [PATCH]
func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.Patch(input)
}

// DeleteConnection delete a tempo connection
// @Summary delete a tempo connection
// @Description Delete a Tempo connection
// @Tags plugins/tempo
// @Success 200 {object} models.TempoConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 409 {object} srvhelper.DsRefs "References exist to this connection"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections/{connectionId} [DELETE]
func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.Delete(input)
}

// ListConnections get all tempo connections
// @Summary get all tempo connections
// @Description Get all Tempo connections
// @Tags plugins/tempo
// @Success 200 {object} []models.TempoConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections [GET]
func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.GetAll(input)
}

// GetConnection get tempo connection detail
// @Summary get tempo connection detail
// @Description Get Tempo connection detail
// @Tags plugins/tempo
// @Success 200 {object} models.TempoConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/tempo/connections/{connectionId} [GET]
func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.GetDetail(input)
}

// GetTeams get teams for a connection
// @Summary get teams
// @Description Get teams for a Tempo connection
// @Tags plugins/tempo
// @Param connectionId path int true "connection ID"
// @Success 200 {object} []models.TempoTeam
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/tempo/connections/{connectionId}/teams [GET]
func GetTeams(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection, err := dsHelper.ConnApi.FindByPk(input)
if err != nil {
return nil, err
}

// Create API client
apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, connection)
if err != nil {
return nil, err
}

// Get teams from Tempo API
res, err := apiClient.Get("teams", nil, nil)
if err != nil {
return nil, errors.Default.Wrap(err, "failed to get teams from Tempo API")
}

var teams []models.TempoTeamResponse
err = api.UnmarshalResponse(res, &teams)
if err != nil {
return nil, errors.Default.Wrap(err, "failed to unmarshal teams response")
}

// Convert to tool layer models
result := make([]models.TempoTeam, 0, len(teams))
for _, t := range teams {
result = append(result, *t.ConvertToToolLayer(connection.ID))
}

return &plugin.ApiResourceOutput{Body: result, Status: http.StatusOK}, nil
}
Loading
Loading