This is an automated email from the ASF dual-hosted git repository. mintsweet pushed a commit to branch feat-dora-config in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit fcb46f23adda962793a0085b775c569aef8d6ae5 Author: abeizn <[email protected]> AuthorDate: Sat Sep 14 11:50:58 2024 +0800 feat: azure dora config (#8053) --- .../services/remote/plugin/connection_api.go | 89 +++++++++++++++++++++- .../server/services/remote/plugin/default_api.go | 3 + 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/backend/server/services/remote/plugin/connection_api.go b/backend/server/services/remote/plugin/connection_api.go index 38c832ee7..7b8b185cb 100644 --- a/backend/server/services/remote/plugin/connection_api.go +++ b/backend/server/services/remote/plugin/connection_api.go @@ -20,13 +20,16 @@ package plugin import ( "encoding/json" "fmt" + "github.com/spf13/cast" + "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/server/api/shared" "github.com/apache/incubator-devlake/server/services/remote/bridge" - "net/http" ) type TestConnectionResult struct { @@ -181,3 +184,87 @@ func (pa *pluginAPI) PatchConnection(input *plugin.ApiResourceInput) (*plugin.Ap func (pa *pluginAPI) DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { return pa.connhelper.Delete(pa.connType.New(), input) } + +func (pa *pluginAPI) GetConnectionTransformToDeployments(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { + db := basicRes.GetDal() + connectionId := input.Params["connectionId"] + deploymentPattern := input.Body["deploymentPattern"] + productionPattern := input.Body["productionPattern"] + page, err := api.ParsePageParam(input.Body, "page", 1) + if err != nil { + return nil, errors.Default.New("invalid page value") + } + pageSize, err := api.ParsePageParam(input.Body, "pageSize", 10) + if err != nil { + return nil, errors.Default.New("invalid pageSize value") + } + + cursor, err := db.RawCursor(` + SELECT DISTINCT id, name, url, start_time + FROM( + SELECT id, name, url, start_time + FROM _tool_azuredevops_builds + WHERE connection_id = ? + AND (? = '' OR name REGEXP ?) + AND (? = '' OR name REGEXP ?) + UNION + SELECT b.id, b.name, b.url, b.start_time + FROM _tool_azuredevops_jobs j + LEFT JOIN _tool_azuredevops_builds b on CONCAT('azuredevops:Build:', b.connection_id, ':', b.id) = j.build_id + WHERE j.connection_id = ? + AND (? = '' OR j.name REGEXP ?) + AND (? = '' OR j.name REGEXP ?) + ) AS t + ORDER BY start_time DESC + `, connectionId, deploymentPattern, deploymentPattern, productionPattern, productionPattern, connectionId, deploymentPattern, deploymentPattern, productionPattern, productionPattern) + if err != nil { + return nil, errors.Default.Wrap(err, "error on get") + } + defer cursor.Close() + + type selectFileds struct { + Id int + Name string + URL string + } + type transformedFields struct { + Name string `json:"name"` + URL string `json:"url"` + } + var allRuns []transformedFields + for cursor.Next() { + sf := &selectFileds{} + err = db.Fetch(cursor, sf) + if err != nil { + return nil, errors.Default.Wrap(err, "error on fetch") + } + // Directly transform and append to allRuns + transformed := transformedFields{ + Name: fmt.Sprintf("#%d - %s", sf.Id, sf.Name), + URL: sf.URL, + } + allRuns = append(allRuns, transformed) + } + // Calculate total count + totalCount := len(allRuns) + + // Paginate in memory + start := (page - 1) * pageSize + end := start + pageSize + if start > totalCount { + start = totalCount + } + if end > totalCount { + end = totalCount + } + pagedRuns := allRuns[start:end] + + // Return result containing paged runs and total count + result := map[string]interface{}{ + "total": totalCount, + "data": pagedRuns, + } + return &plugin.ApiResourceOutput{ + Body: result, + }, nil +} diff --git a/backend/server/services/remote/plugin/default_api.go b/backend/server/services/remote/plugin/default_api.go index 83ead1327..963fe1cc0 100644 --- a/backend/server/services/remote/plugin/default_api.go +++ b/backend/server/services/remote/plugin/default_api.go @@ -82,6 +82,9 @@ func GetDefaultAPI( "POST": papi.PostScopeConfigs, "GET": papi.ListScopeConfigs, }, + "connections/:connectionId/transform-to-deployments": { + "POST": papi.GetConnectionTransformToDeployments, + }, "connections/:connectionId/scope-configs/:id": { "GET": papi.GetScopeConfig, "PATCH": papi.PatchScopeConfig,
