From da6c673d105e2624d9619a88afa1b09b987f3ca3 Mon Sep 17 00:00:00 2001 From: abeizn Date: Fri, 13 Sep 2024 10:18:14 +0800 Subject: [PATCH] feat: bamboo dora config (#8045) --- backend/plugins/bamboo/api/connection_api.go | 125 ++++++++++++++++++ backend/plugins/bamboo/impl/impl.go | 6 + .../bamboo/tasks/plan_build_convertor.go | 2 +- .../bamboo/tasks/plan_build_extractor.go | 2 +- .../plugins/bamboo/tasks/plan_convertor.go | 2 +- backend/plugins/bamboo/tasks/shared.go | 2 +- backend/plugins/bamboo/tasks/shared_test.go | 2 +- 7 files changed, 136 insertions(+), 5 deletions(-) diff --git a/backend/plugins/bamboo/api/connection_api.go b/backend/plugins/bamboo/api/connection_api.go index 3fd72a22e48..6258765dfc0 100644 --- a/backend/plugins/bamboo/api/connection_api.go +++ b/backend/plugins/bamboo/api/connection_api.go @@ -21,10 +21,12 @@ import ( "context" "net/http" + "github.com/apache/incubator-devlake/core/dal" "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/bamboo/models" + "github.com/apache/incubator-devlake/plugins/bamboo/tasks" "github.com/apache/incubator-devlake/server/api/shared" ) @@ -162,3 +164,126 @@ func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { return dsHelper.ConnApi.GetDetail(input) } + +// GetConnectionDeployments return one connection deployments +// @Summary return one connection deployments +// @Description return one connection deployments +// @Tags plugins/bamboo +// @Param id path int true "id" +// @Param connectionId path int true "connectionId" +// @Success 200 {array} string "List of Environment Names" +// @Failure 400 {object} shared.ApiBody "Bad Request" +// @Failure 500 {object} shared.ApiBody "Internal Error" +// @Router /plugins/bamboo/connections/{connectionId}/deployments [GET] +func GetConnectionDeployments(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { + db := basicRes.GetDal() + connectionId := input.Params["connectionId"] + var environments []string + err := db.All(&environments, + dal.From(&models.BambooDeployBuild{}), + dal.Where("connection_id = ?", connectionId), + dal.Select("DISTINCT environment")) + if err != nil { + return nil, err + } + + return &plugin.ApiResourceOutput{ + Body: environments, + }, nil +} + +// GetConnectionTransformToDeployments return one connection deployments +// @Summary return one connection deployments +// @Description return one connection deployments +// @Tags plugins/bamboo +// @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/bamboo/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 plan_build_key, link_href, build_started_time + FROM( + SELECT plan_build_key, link_href, build_started_time + FROM _tool_bamboo_plan_builds + WHERE connection_id = ? AND plan_name REGEXP ? + AND plan_name REGEXP ? + UNION + SELECT p.plan_build_key, p.link_href, p.build_started_time + FROM _tool_bamboo_job_builds j + LEFT JOIN _tool_bamboo_plan_builds p on p.plan_build_key = j.plan_build_key + WHERE j.connection_id = ? AND j.job_name REGEXP ? + AND j.job_name REGEXP ? + ORDER BY build_started_time DESC + ) AS t + ORDER BY build_started_time 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 { + PlanBuildKey string + LinkHref 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 + url, err := tasks.GetBambooHomePage(sf.LinkHref) + if err != nil { + url = sf.LinkHref + } + transformed := transformedFields{ + Name: sf.PlanBuildKey, + URL: url + "/browse/" + sf.PlanBuildKey, + } + 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/plugins/bamboo/impl/impl.go b/backend/plugins/bamboo/impl/impl.go index 2aae7ad5efa..41950364668 100644 --- a/backend/plugins/bamboo/impl/impl.go +++ b/backend/plugins/bamboo/impl/impl.go @@ -248,6 +248,12 @@ func (p Bamboo) ApiResources() map[string]map[string]plugin.ApiResourceHandler { "connections/:connectionId/search-remote-scopes": { "GET": api.SearchRemoteScopes, }, + "connections/:connectionId/deployments": { + "GET": api.GetConnectionDeployments, + }, + "connections/:connectionId/transform-to-deployments": { + "POST": api.GetConnectionTransformToDeployments, + }, "connections/:connectionId/scopes/:scopeId": { "GET": api.GetScope, "PATCH": api.PatchScope, diff --git a/backend/plugins/bamboo/tasks/plan_build_convertor.go b/backend/plugins/bamboo/tasks/plan_build_convertor.go index c86e8ae5381..02d4b8ec481 100644 --- a/backend/plugins/bamboo/tasks/plan_build_convertor.go +++ b/backend/plugins/bamboo/tasks/plan_build_convertor.go @@ -91,7 +91,7 @@ func ConvertPlanBuilds(taskCtx plugin.SubTaskContext) errors.Error { OriginalStatus: line.LifeCycleState, DisplayTitle: line.GenerateCICDPipeLineName(), } - homepage, err := getBambooHomePage(line.LinkHref) + homepage, err := GetBambooHomePage(line.LinkHref) if err != nil { logger.Warn(err, "get bamboo home") } else { diff --git a/backend/plugins/bamboo/tasks/plan_build_extractor.go b/backend/plugins/bamboo/tasks/plan_build_extractor.go index c5ef6d50c12..8832d9df12a 100644 --- a/backend/plugins/bamboo/tasks/plan_build_extractor.go +++ b/backend/plugins/bamboo/tasks/plan_build_extractor.go @@ -49,7 +49,7 @@ func ExtractPlanBuild(taskCtx plugin.SubTaskContext) errors.Error { body.Environment = data.RegexEnricher.ReturnNameIfMatched(devops.PRODUCTION, body.PlanName) var url string - homepage, errGetHomePage := getBambooHomePage(body.LinkHref) + homepage, errGetHomePage := GetBambooHomePage(body.LinkHref) if errGetHomePage != nil { logger.Warn(errGetHomePage, "get bamboo home") } else { diff --git a/backend/plugins/bamboo/tasks/plan_convertor.go b/backend/plugins/bamboo/tasks/plan_convertor.go index ba04a8ce785..261e6dfb358 100644 --- a/backend/plugins/bamboo/tasks/plan_convertor.go +++ b/backend/plugins/bamboo/tasks/plan_convertor.go @@ -62,7 +62,7 @@ func ConvertPlans(taskCtx plugin.SubTaskContext) errors.Error { Name: bambooPlan.Name, Description: bambooPlan.Description, } - homepage, err := getBambooHomePage(bambooPlan.Href) + homepage, err := GetBambooHomePage(bambooPlan.Href) if err != nil { logger.Warn(err, "get bamboo home") } else { diff --git a/backend/plugins/bamboo/tasks/shared.go b/backend/plugins/bamboo/tasks/shared.go index 182670b954d..544ca698684 100644 --- a/backend/plugins/bamboo/tasks/shared.go +++ b/backend/plugins/bamboo/tasks/shared.go @@ -112,7 +112,7 @@ func GetResultsResult(res *http.Response) ([]json.RawMessage, errors.Error) { } // getBambooHomePage receive endpoint like "http://127.0.0.1:30001/rest/api/latest/" and return bamboo's homepage like "http://127.0.0.1:30001/" -func getBambooHomePage(endpoint string) (string, error) { +func GetBambooHomePage(endpoint string) (string, error) { if endpoint == "" { return "", errors.Default.New("empty endpoint") } diff --git a/backend/plugins/bamboo/tasks/shared_test.go b/backend/plugins/bamboo/tasks/shared_test.go index 941643c4842..18d31c12d17 100644 --- a/backend/plugins/bamboo/tasks/shared_test.go +++ b/backend/plugins/bamboo/tasks/shared_test.go @@ -68,7 +68,7 @@ func Test_getBambooWebURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := getBambooHomePage(tt.args.endpoint) + got, err := GetBambooHomePage(tt.args.endpoint) if (err != nil) != tt.wantErr { t.Errorf("getbambooHomePage() error = %v, wantErr %v", err, tt.wantErr) return