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 157f2b88b2af574f5346e92cd4a81334b0092c71
Author: abeizn <zikuan...@merico.dev>
AuthorDate: Thu Sep 12 10:48:10 2024 +0800

    feat: circleci dora config (#8038)
---
 backend/plugins/circleci/api/connection_api.go | 94 ++++++++++++++++++++++++++
 backend/plugins/circleci/impl/impl.go          |  3 +
 2 files changed, 97 insertions(+)

diff --git a/backend/plugins/circleci/api/connection_api.go 
b/backend/plugins/circleci/api/connection_api.go
index 37f8389ee..5e0a64c37 100644
--- a/backend/plugins/circleci/api/connection_api.go
+++ b/backend/plugins/circleci/api/connection_api.go
@@ -167,3 +167,97 @@ 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/circleci
+// @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/circleci/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 pipeline_number, name, project_slug, 
created_date
+               FROM(
+                       SELECT pipeline_number, name, project_slug, created_date
+                       FROM _tool_circleci_workflows
+                       WHERE connection_id = ? AND name REGEXP ?
+                       AND name REGEXP ?
+                       UNION
+                       SELECT w.pipeline_number, w.name, w.project_slug, 
w.created_date
+                       FROM _tool_circleci_jobs j 
+                       LEFT JOIN _tool_circleci_workflows w on w.id = 
j.workflow_id
+                       WHERE j.connection_id = ? AND j.name REGEXP ?
+                       AND j.name REGEXP ?
+               ) AS t
+               ORDER BY created_date 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 {
+               PipelineNumber int
+               Name           string
+               ProjectSlug    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.PipelineNumber, 
sf.Name),
+                       URL:  CIRCLECI_URL + sf.ProjectSlug,
+               }
+               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 CIRCLECI_URL = "https://app.circleci.com/pipelines/";
diff --git a/backend/plugins/circleci/impl/impl.go 
b/backend/plugins/circleci/impl/impl.go
index 8e052711a..c2ae4a8bf 100644
--- a/backend/plugins/circleci/impl/impl.go
+++ b/backend/plugins/circleci/impl/impl.go
@@ -208,6 +208,9 @@ func (p Circleci) ApiResources() 
map[string]map[string]plugin.ApiResourceHandler
                        "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