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 a85ba8317f3f61b83c1f3ae045559ff50892a5b7
Author: abeizn <zikuan...@merico.dev>
AuthorDate: Thu Sep 12 15:30:30 2024 +0800

    feat: bitbucket dora config (#8043)
---
 backend/plugins/bitbucket/api/connection_api.go | 96 +++++++++++++++++++++++++
 backend/plugins/bitbucket/impl/impl.go          |  3 +
 2 files changed, 99 insertions(+)

diff --git a/backend/plugins/bitbucket/api/connection_api.go 
b/backend/plugins/bitbucket/api/connection_api.go
index 15851e3ab..62459ec93 100644
--- a/backend/plugins/bitbucket/api/connection_api.go
+++ b/backend/plugins/bitbucket/api/connection_api.go
@@ -19,6 +19,7 @@ package api
 
 import (
        "context"
+       "fmt"
        "net/http"
 
        "github.com/apache/incubator-devlake/server/api/shared"
@@ -173,3 +174,98 @@ func ListConnections(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
 func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, 
errors.Error) {
        return dsHelper.ConnApi.GetDetail(input)
 }
+
+// GetConnectionTransformToDeployments return one connection deployments
+// @Summary return one connection deployments
+// @Description return one connection deployments
+// @Tags plugins/bitbucket
+// @Param id path int true "id"
+// @Param connectionId path int true "connectionId"
+// @Success 200  {object} map[string]interface{}
+// @Failure 400  {object} shared.ApiBody "Bad Request"
+// @Failure 500  {object} shared.ApiBody "Internal Error"
+// @Router 
/plugins/bitbucket/connections/{connectionId}/transform-to-deployments [POST]
+func 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 build_number, ref_name, repo_id, web_url, 
bitbucket_created_on
+               FROM(
+                       SELECT build_number, ref_name, repo_id, web_url, 
bitbucket_created_on
+                       FROM _tool_bitbucket_pipelines
+                       WHERE connection_id = ? AND ref_name REGEXP ?
+                       AND ref_name REGEXP ?
+                       UNION
+                       SELECT build_number, ref_name, p.repo_id, 
web_url,bitbucket_created_on
+                       FROM _tool_bitbucket_pipelines p
+                       LEFT JOIN _tool_bitbucket_pipeline_steps s on 
s.pipeline_id = p.bitbucket_id
+                       WHERE s.connection_id = ? AND s.name REGEXP ?
+                       AND s.name REGEXP ?
+               ) AS t
+               ORDER BY bitbucket_created_on DESC
+       `, connectionId, deploymentPattern, productionPattern, connectionId, 
deploymentPattern, productionPattern)
+       if err != nil {
+               return nil, errors.Default.Wrap(err, "error on get")
+       }
+       defer cursor.Close()
+
+       type selectFileds struct {
+               BuildNumber int
+               RefName     string
+               RepoId      string
+               WebUrl      string
+       }
+       type transformedFields struct {
+               Name string
+               URL  string
+       }
+       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.BuildNumber, 
sf.RepoId),
+                       URL:  fmt.Sprintf("%s%s/pipelines/results/%d", 
BITBUCKET_CLOUD_URL, sf.RepoId, sf.BuildNumber),
+               }
+               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
+}
+
+const BITBUCKET_CLOUD_URL = "https://bitbucket.org/";
diff --git a/backend/plugins/bitbucket/impl/impl.go 
b/backend/plugins/bitbucket/impl/impl.go
index 9a23ab1b3..953819024 100644
--- a/backend/plugins/bitbucket/impl/impl.go
+++ b/backend/plugins/bitbucket/impl/impl.go
@@ -247,6 +247,9 @@ func (p Bitbucket) ApiResources() 
map[string]map[string]plugin.ApiResourceHandle
                        "POST": api.PostScopeConfig,
                        "GET":  api.GetScopeConfigList,
                },
+               "connections/:connectionId/transform-to-deployments": {
+                       "POST": api.GetConnectionTransformToDeployments,
+               },
                "connections/:connectionId/scope-configs/:scopeConfigId": {
                        "PATCH":  api.PatchScopeConfig,
                        "GET":    api.GetScopeConfig,

Reply via email to