Skip to content

Commit

Permalink
feat: bamboo dora config (#8045)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeizn authored and mintsweet committed Sep 14, 2024
1 parent d849b12 commit 4bb34cc
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
125 changes: 125 additions & 0 deletions backend/plugins/bamboo/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions backend/plugins/bamboo/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/bamboo/tasks/plan_build_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/bamboo/tasks/plan_build_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/bamboo/tasks/plan_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/bamboo/tasks/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/bamboo/tasks/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4bb34cc

Please sign in to comment.