Skip to content

Commit

Permalink
feat: bitbucket dora config (#8043)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeizn authored Sep 12, 2024
1 parent 4823bc6 commit ff25db9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
96 changes: 96 additions & 0 deletions backend/plugins/bitbucket/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package api

import (
"context"
"fmt"
"net/http"

"github.com/apache/incubator-devlake/server/api/shared"
Expand Down Expand Up @@ -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/"
3 changes: 3 additions & 0 deletions backend/plugins/bitbucket/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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,
Expand Down

0 comments on commit ff25db9

Please sign in to comment.