Skip to content

Commit

Permalink
fix: github dora config api (#8032)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeizn authored Sep 11, 2024
1 parent 76e89ee commit 4fde19e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 129 deletions.
121 changes: 121 additions & 0 deletions backend/plugins/github/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/mitchellh/mapstructure"

"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"
Expand Down Expand Up @@ -402,3 +403,123 @@ func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResource
}
return &plugin.ApiResourceOutput{Body: testConnectionResult, Status: http.StatusOK}, nil
}

// GetConnectionDeployments return one connection deployments
// @Summary return one connection deployments
// @Description return one connection deployments
// @Tags plugins/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/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.GithubDeployment{}),
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/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/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 r.run_number, r.name, r.head_branch, r.html_url, r.run_started_at
FROM (
SELECT id, run_number, name, head_branch, html_url, run_started_at
FROM _tool_github_runs
WHERE connection_id = ? AND name REGEXP ?
AND (name REGEXP ? OR head_branch REGEXP ?)
UNION
SELECT r.id, r.run_number, r.name, r.head_branch, r.html_url, r.run_started_at
FROM _tool_github_jobs j
LEFT JOIN _tool_github_runs r ON j.run_id = r.id
WHERE j.connection_id = ? AND j.name REGEXP ?
AND j.name REGEXP ?
) r
ORDER BY r.run_started_at DESC
`, connectionId, deploymentPattern, productionPattern, productionPattern, connectionId, deploymentPattern, productionPattern)
if err != nil {
return nil, errors.Default.Wrap(err, "error on get")
}
defer cursor.Close()

type selectFileds struct {
RunNumber int
Name string
HeadBranch string
HTMLURL 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.RunNumber, sf.Name),
URL: sf.HTMLURL,
}
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
}
125 changes: 0 additions & 125 deletions backend/plugins/github/api/scope_config_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ limitations under the License.
package api

import (
"fmt"

"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"
githubModels "github.com/apache/incubator-devlake/plugins/github/models"
)

// PostScopeConfig create scope config for Github
Expand Down Expand Up @@ -114,123 +109,3 @@ func GetProjectsByScopeConfig(input *plugin.ApiResourceInput) (*plugin.ApiResour
func DeleteScopeConfig(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeConfigApi.Delete(input)
}

// GetScopeConfig return one scope config deployments
// @Summary return one scope config deployments
// @Description return one scope config deployments
// @Tags plugins/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/connections/{connectionId}/scope-configs/{id}/deployments [GET]
func GetScopeConfigDeployments(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
db := basicRes.GetDal()
connectionId := input.Params["connectionId"]
var environments []string
err := db.All(&environments,
dal.From(&githubModels.GithubDeployment{}),
dal.Where("connection_id = ?", connectionId),
dal.Select("DISTINCT environment"))
if err != nil {
return nil, err
}

return &plugin.ApiResourceOutput{
Body: environments,
}, nil
}

// GetScopeConfig return one scope config deployments
// @Summary return one scope config deployments
// @Description return one scope config deployments
// @Tags plugins/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/connections/{connectionId}/scope-configs/{id}/transform-to-deployments [POST]
func GetScopeConfigTransformToDeployments(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 r.run_number, r.name, r.head_branch, r.html_url, r.run_started_at
FROM (
SELECT id, run_number, name, head_branch, html_url, run_started_at
FROM _tool_github_runs
WHERE connection_id = ? AND name REGEXP ?
AND (name REGEXP ? OR head_branch REGEXP ?)
UNION
SELECT r.id, r.run_number, r.name, r.head_branch, r.html_url, r.run_started_at
FROM _tool_github_jobs j
LEFT JOIN _tool_github_runs r ON j.run_id = r.id
WHERE j.connection_id = ? AND j.name REGEXP ?
AND j.name REGEXP ?
) r
ORDER BY r.run_started_at DESC
`, connectionId, deploymentPattern, productionPattern, productionPattern, connectionId, deploymentPattern, productionPattern)
if err != nil {
return nil, errors.Default.Wrap(err, "error on get")
}
defer cursor.Close()

type selectFileds struct {
RunNumber int
Name string
HeadBranch string
HTMLURL 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.RunNumber, sf.Name),
URL: sf.HTMLURL,
}
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
}
8 changes: 4 additions & 4 deletions backend/plugins/github/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ func (p Github) ApiResources() map[string]map[string]plugin.ApiResourceHandler {
"GET": api.GetScopeConfig,
"DELETE": api.DeleteScopeConfig,
},
"connections/:connectionId/scope-configs/:scopeConfigId/deployments": {
"GET": api.GetScopeConfigDeployments,
"connections/:connectionId/deployments": {
"GET": api.GetConnectionDeployments,
},
"connections/:connectionId/scope-configs/:scopeConfigId/transform-to-deployments": {
"POST": api.GetScopeConfigTransformToDeployments,
"connections/:connectionId/transform-to-deployments": {
"POST": api.GetConnectionTransformToDeployments,
},
"connections/:connectionId/remote-scopes": {
"GET": api.RemoteScopes,
Expand Down

0 comments on commit 4fde19e

Please sign in to comment.