Skip to content

Commit

Permalink
feat: jenkins dora config (#8033)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeizn authored Sep 11, 2024
1 parent 4fde19e commit 310a40e
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 12 deletions.
4 changes: 2 additions & 2 deletions backend/plugins/github/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResource
// @Tags plugins/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Success 200 {array} string "List of Environment Names"
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/connections/{connectionId}/deployments [GET]
Expand All @@ -437,7 +437,7 @@ func GetConnectionDeployments(input *plugin.ApiResourceInput) (*plugin.ApiResour
// @Tags plugins/github
// @Param id path int true "id"
// @Param connectionId path int true "connectionId"
// @Success 200 {object} models.GithubScopeConfigDeployment
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/github/connections/{connectionId}/transform-to-deployments [POST]
Expand Down
94 changes: 94 additions & 0 deletions backend/plugins/jenkins/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"
"strings"

Expand Down Expand Up @@ -176,3 +177,96 @@ 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/jenkins
// @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/jenkins/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 number, job_name, full_name, url, start_time
FROM(
SELECT number, job_name, full_name, url, start_time
FROM _tool_jenkins_builds
WHERE connection_id = ? AND full_name REGEXP ?
AND full_name REGEXP ?
UNION
SELECT number, job_name, full_name, url, start_time
FROM _tool_jenkins_stages s
LEFT JOIN _tool_jenkins_builds b ON b.full_name = s.build_name
WHERE s.connection_id = ? AND s.name REGEXP ?
AND s.name REGEXP ?
) AS t
ORDER BY start_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 {
Number int
JobName string
FullName string
URL 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.Number, sf.JobName),
URL: sf.URL,
}
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
}
3 changes: 3 additions & 0 deletions backend/plugins/jenkins/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func (p Jenkins) ApiResources() map[string]map[string]plugin.ApiResourceHandler
"POST": api.CreateScopeConfig,
"GET": api.GetScopeConfigList,
},
"connections/:connectionId/transform-to-deployments": {
"POST": api.GetConnectionTransformToDeployments,
},
"connections/:connectionId/scope-configs/:scopeConfigId": {
"PATCH": api.UpdateScopeConfig,
"GET": api.GetScopeConfig,
Expand Down
20 changes: 11 additions & 9 deletions backend/plugins/jenkins/models/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ limitations under the License.
package models

import (
"github.com/apache/incubator-devlake/core/models/common"
"time"

"github.com/apache/incubator-devlake/core/models/common"
)

// JenkinsBuild db entity for jenkins build
type JenkinsBuild struct {
common.NoPKModel
// collected fields
ConnectionId uint64 `gorm:"primaryKey"`
JobName string `gorm:"index;type:varchar(255)"`
JobPath string `gorm:"index;type:varchar(255)"`
Duration float64 // build time
FullName string `gorm:"primaryKey;type:varchar(255)"` // "path/job name#7"
EstimatedDuration float64 // EstimatedDuration
Number int64 `gorm:"index"`
Result string // Result
ConnectionId uint64 `gorm:"primaryKey"`
JobName string `gorm:"index;type:varchar(255)"`
JobPath string `gorm:"index;type:varchar(255)"`
Duration float64 // build time
FullName string `gorm:"primaryKey;type:varchar(255)"` // "path/job name#7"
EstimatedDuration float64 // EstimatedDuration
Number int64 `gorm:"index"`
Result string // Result
Url string
Timestamp int64 // start time
StartTime time.Time // convered by timestamp
Type string `gorm:"index;type:varchar(255)"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migrationscripts

import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
)

type addUrlForBuilds struct{}

type jenkinsBuild20240911 struct {
Url string
}

func (jenkinsBuild20240911) TableName() string {
return "_tool_jenkins_builds"
}

func (script *addUrlForBuilds) Up(basicRes context.BasicRes) errors.Error {
db := basicRes.GetDal()
return db.AutoMigrate(&jenkinsBuild20240911{})
}

func (*addUrlForBuilds) Version() uint64 {
return 20240911231237
}

func (*addUrlForBuilds) Name() string {
return "add url for builds"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ func All() []plugin.MigrationScript {
new(renameTr2ScopeConfig),
new(addRawParamTableForScope),
new(addNumberToJenkinsBuildCommit),
new(addUrlForBuilds),
}
}
1 change: 1 addition & 0 deletions backend/plugins/jenkins/models/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type ApiBuildResponse struct {
Class string `json:"_class"`
Number int64 `json:"number"`
Result string `json:"result"`
Url string `json:"url"`
Building bool `json:"building"`
Actions []Action `json:"actions"`
Duration float64 `json:"duration"`
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/jenkins/tasks/build_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func collectSingleJobApiBuilds(taskCtx plugin.SubTaskContext) errors.Error {
Query: func(reqData *helper.RequestData, createdAfter *time.Time) (url.Values, errors.Error) {
query := url.Values{}
treeValue := fmt.Sprintf(
"allBuilds[timestamp,number,duration,building,estimatedDuration,fullDisplayName,result,actions[lastBuiltRevision[SHA1,branch[name]],remoteUrls,mercurialRevisionNumber,causes[*]],changeSet[kind,revisions[revision]]]{%d,%d}",
"allBuilds[timestamp,number,duration,building,estimatedDuration,fullDisplayName,result,url,actions[lastBuiltRevision[SHA1,branch[name]],remoteUrls,mercurialRevisionNumber,causes[*]],changeSet[kind,revisions[revision]]]{%d,%d}",
reqData.Pager.Skip, reqData.Pager.Skip+reqData.Pager.Size)
query.Set("tree", treeValue)
return query, nil
Expand Down
1 change: 1 addition & 0 deletions backend/plugins/jenkins/tasks/build_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func ExtractApiBuilds(taskCtx plugin.SubTaskContext) errors.Error {
EstimatedDuration: body.EstimatedDuration,
Number: body.Number,
Result: body.Result,
Url: body.Url,
Timestamp: body.Timestamp,
Class: class,
Building: body.Building,
Expand Down

0 comments on commit 310a40e

Please sign in to comment.