This is an automated email from the ASF dual-hosted git repository.
abeizn pushed a commit to branch zk-dora-config-911
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/zk-dora-config-911 by this
push:
new 0c90bcad3 fix: github dora config api
0c90bcad3 is described below
commit 0c90bcad3bcbd443c9fd7a053eb36e04526fe2f2
Author: abeizn <[email protected]>
AuthorDate: Wed Sep 11 13:56:59 2024 +0800
fix: github dora config api
---
backend/plugins/github/api/connection_api.go | 121 ++++++++++++++++++++++++
backend/plugins/github/api/scope_config_api.go | 125 -------------------------
backend/plugins/github/impl/impl.go | 8 +-
3 files changed, 125 insertions(+), 129 deletions(-)
diff --git a/backend/plugins/github/api/connection_api.go
b/backend/plugins/github/api/connection_api.go
index a5f0e922d..02c0265de 100644
--- a/backend/plugins/github/api/connection_api.go
+++ b/backend/plugins/github/api/connection_api.go
@@ -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"
@@ -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
+}
diff --git a/backend/plugins/github/api/scope_config_api.go
b/backend/plugins/github/api/scope_config_api.go
index 6d72f6933..b109341e7 100644
--- a/backend/plugins/github/api/scope_config_api.go
+++ b/backend/plugins/github/api/scope_config_api.go
@@ -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
@@ -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
-}
diff --git a/backend/plugins/github/impl/impl.go
b/backend/plugins/github/impl/impl.go
index ac8f4e829..1c09a7afe 100644
--- a/backend/plugins/github/impl/impl.go
+++ b/backend/plugins/github/impl/impl.go
@@ -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,