Skip to content

Commit

Permalink
feat: aggregate release by git branch (#3941)
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigMChen authored Jan 25, 2022
1 parent 2e10655 commit b1351dc
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 126 deletions.
62 changes: 62 additions & 0 deletions .erda/migrations/dicehub/20220125-release-branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Generated by Erda Migrator.
Please implement the function entry, and add it to the list entries.
"""

import json

import django.db.models


class DiceRelease(django.db.models.Model):
"""
generated by erda-cli
"""

release_id = django.db.models.CharField(primary_key=True)
release_name = django.db.models.CharField()
desc = django.db.models.TextField()
dice = django.db.models.TextField()
addon = django.db.models.TextField()
labels = django.db.models.CharField()
version = django.db.models.CharField()
org_id = django.db.models.BigIntegerField()
project_id = django.db.models.BigIntegerField()
application_id = django.db.models.BigIntegerField()
project_name = django.db.models.CharField()
application_name = django.db.models.CharField()
user_id = django.db.models.CharField()
cluster_name = django.db.models.CharField()
cross_cluster = django.db.models.BooleanField()
resources = django.db.models.TextField()
reference = django.db.models.BigIntegerField()
created_at = django.db.models.DateTimeField()
updated_at = django.db.models.DateTimeField()
changelog = django.db.models.TextField()
is_stable = django.db.models.BooleanField()
is_formal = django.db.models.BooleanField()
is_project_release = django.db.models.BooleanField()
application_release_list = django.db.models.TextField()
tags = django.db.models.CharField()
git_branch = django.db.models.CharField()

class Meta:
db_table = "dice_release"


def entry():
releases = DiceRelease.objects.all()

for release in releases:
try:
fmt_labels = json.loads(release.labels)
gitBranch = fmt_labels["gitBranch"]
release.git_branch = gitBranch
release.save()
except Exception as e:
print("release %s git branch parse error, exception: %s, skip" % (release.release_id, e))
pass

entries: [callable] = [
entry,
]
18 changes: 18 additions & 0 deletions .erda/migrations/dicehub/20220125-release-branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2021 Terminus, Inc.
*
* Licensed 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.
*/

ALTER table `dice_release`
ADD COLUMN `git_branch` varchar(255) NOT NULL DEFAULT '' COMMENT '分支'
4 changes: 4 additions & 0 deletions .erda/migrations/dicehub/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Django==3.2.4
mysql-connector-python==8.0.26
pytz==2021.1
sqlparse==0.4.1
9 changes: 9 additions & 0 deletions apistructs/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ type ReleaseCreateRequest struct {

// CrossCluster 跨集群
CrossCluster bool `json:"crossCluster,omitempty"`

// 分支
GitBranch string `json:"gitBranch,omitempty"`
}

type ReleaseUploadRequest struct {
Expand Down Expand Up @@ -371,6 +374,9 @@ type ReleaseListRequest struct {
// 分支名
Branch string `json:"-" query:"branchName"`

// 是否为每个分支的最新制品
Latest bool `json:"-" query:"latest"`

// stable表示非临时制品
IsStable *bool `json:"-" query:"isStable"`

Expand Down Expand Up @@ -467,6 +473,9 @@ func (req ReleaseListRequest) ConvertToQueryParams() url.Values {
if req.Branch != "" {
values.Add("branchName", req.Branch)
}
if req.Latest {
values.Add("latest", "true")
}
if req.IsStable != nil {
values.Add("isStable", strconv.FormatBool(*req.IsStable))
}
Expand Down
2 changes: 2 additions & 0 deletions conf/dop/i18n/cp/scenarios/release-manage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ zh:
referencedReleases: 查看引用制品
releaseID: 制品ID
inputReleaseID: 请输入制品ID
aggregateByBranch: 按分支聚合
en:
searchByVersion: Search by version
application: Application
Expand Down Expand Up @@ -56,3 +57,4 @@ en:
referencedReleases: Referenced Releases
releaseID: Release ID
inputReleaseID: Input release ID
aggregateByBranch: Aggregate by branch
100 changes: 51 additions & 49 deletions modules/dicehub/dbclient/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/jinzhu/gorm"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/database/dbengine"
"github.com/erda-project/erda/pkg/strutil"
)
Expand Down Expand Up @@ -48,6 +49,8 @@ type Release struct {
ApplicationReleaseList string `json:"applicationReleaseList" gorm:"type:text"`
// Labels 用于release分类,描述release类别,map类型, 最大长度1000, 选填
Labels string `json:"labels" gorm:"type:varchar(1000)"`
// GitBranch 为分支名,自动从labels中解析,选填
GitBranch string `json:"gitBranch" gorm:"type:varchar(255)"`
// Tags
Tags string `json:"tags" gorm:"type:varchar(100)"`
// Version 存储release版本信息, 同一企业同一项目同一应用下唯一,最大长度100,选填
Expand Down Expand Up @@ -121,68 +124,63 @@ func (client *DBClient) GetReleases(releaseIDs []string) ([]Release, error) {

// GetReleasesByParams 根据参数过滤Release
func (client *DBClient) GetReleasesByParams(
orgID, projectID int64, applicationID []string,
keyword, releaseName, branch string,
isStable, isFormal, isProjectRelease *bool,
userID []string, version, releaseID string, commitID, tags,
cluster string, crossCluster *bool, isVersion bool, crossClusterOrSpecifyCluster *string,
startTime, endTime, pageNum, pageSize int64,
orderBy, order string) (int64, []Release, error) {
orgID int64, req *apistructs.ReleaseListRequest) (int64, []Release, error) {

var releases []Release
db := client.DB.Debug()
if orgID > 0 {
db = db.Where("org_id = ?", orgID)
}
if len(applicationID) > 0 {
db = db.Where("application_id in (?)", applicationID)
if len(req.ApplicationID) > 0 {
db = db.Where("application_id in (?)", req.ApplicationID)
}

if projectID > 0 {
db = db.Where("project_id = ?", projectID)
if req.ProjectID > 0 {
db = db.Where("project_id = ?", req.ProjectID)
}
if keyword != "" {
db = db.Where("release_id LIKE ? or release_name LIKE ? or version LIKE ?", "%"+keyword+"%",
"%"+keyword+"%", "%"+keyword+"%")
} else if releaseName != "" {
db = db.Where("release_name = ?", releaseName)
if req.Query != "" {
db = db.Where("release_id LIKE ? or release_name LIKE ? or version LIKE ?", "%"+req.Query+"%",
"%"+req.Query+"%", "%"+req.Query+"%")
} else if req.ReleaseName != "" {
db = db.Where("release_name = ?", req.ReleaseName)
}
if isVersion {

if req.IsVersion {
db = db.Not("version", "")
}

if cluster != "" {
db = db.Where("cluster_name = ?", cluster)
if req.Cluster != "" {
db = db.Where("cluster_name = ?", req.Cluster)
}
if crossCluster != nil {
db = db.Where("cross_cluster = ?", *crossCluster)
if req.CrossCluster != nil {
db = db.Where("cross_cluster = ?", *req.CrossCluster)
}
if crossClusterOrSpecifyCluster != nil {
db = db.Where("(cluster_name = ? AND cross_cluster = 0) OR cross_cluster = 1", *crossClusterOrSpecifyCluster)
if req.CrossClusterOrSpecifyCluster != nil {
db = db.Where("(cluster_name = ? AND cross_cluster = 0) OR cross_cluster = 1", *req.CrossClusterOrSpecifyCluster)
}
if branch != "" {
db = db.Where("labels LIKE ?", "%"+fmt.Sprintf("\"gitBranch\":\"%s\"", branch)+"%")
if req.Branch != "" {
db = db.Where("labels LIKE ?", "%"+fmt.Sprintf("\"gitBranch\":\"%s\"", req.Branch)+"%")
}

if isStable != nil {
db = db.Where("is_stable = ?", isStable)
if req.IsStable != nil {
db = db.Where("is_stable = ?", req.IsStable)
}

if isProjectRelease != nil {
db = db.Where("is_project_release = ?", isProjectRelease)
if req.IsProjectRelease != nil {
db = db.Where("is_project_release = ?", req.IsProjectRelease)
}

if isFormal != nil {
db = db.Where("is_formal = ?", *isFormal)
if req.IsFormal != nil {
db = db.Where("is_formal = ?", *req.IsFormal)
}

if len(userID) > 0 {
db = db.Where("user_id in (?)", userID)
if len(req.UserID) > 0 {
db = db.Where("user_id in (?)", req.UserID)
}

if version != "" {
if req.Version != "" {
var versions []string
splits := strings.Split(version, ",")
splits := strings.Split(req.Version, ",")
for _, v := range splits {
versions = append(versions, strings.TrimSpace(v))
}
Expand All @@ -194,9 +192,9 @@ func (client *DBClient) GetReleasesByParams(
}
}

if releaseID != "" {
if req.ReleaseID != "" {
var releaseIDs []string
splits := strings.Split(releaseID, ",")
splits := strings.Split(req.ReleaseID, ",")
for _, id := range splits {
releaseIDs = append(releaseIDs, strings.TrimSpace(id))
}
Expand All @@ -208,30 +206,34 @@ func (client *DBClient) GetReleasesByParams(
}
}

if commitID != "" {
db = db.Where("labels LIKE ?", fmt.Sprintf("%%\"gitCommitId\":\"%s\"%%", commitID))
if req.CommitID != "" {
db = db.Where("labels LIKE ?", fmt.Sprintf("%%\"gitCommitId\":\"%s\"%%", req.CommitID))
}

if tags != "" {
db = db.Where("tags = ?", tags)
if req.Tags != "" {
db = db.Where("tags = ?", req.Tags)
}

if startTime > 0 {
db = db.Where("created_at > ?", startTime/1000)
if req.StartTime > 0 {
db = db.Where("created_at > ?", req.StartTime/1000)
}

if endTime > 0 {
db = db.Where("created_at <= ?", endTime/1000)
if req.EndTime > 0 {
db = db.Where("created_at <= ?", req.EndTime/1000)
}

if orderBy != "" {
db = db.Order(orderBy + " " + order)
if req.OrderBy != "" {
db = db.Order(req.Order + " " + req.Order)
} else {
db = db.Order("created_at DESC")
}

if err := db.Offset((pageNum - 1) * pageSize).
Limit(pageSize).Find(&releases).Error; err != nil {
if req.Latest {
db = db.Select("*, max(created_at)").Group("project_id, application_name, git_branch")
}

if err := db.Offset((req.PageNum - 1) * req.PageSize).
Limit(req.PageSize).Find(&releases).Error; err != nil {
return 0, nil, err
}

Expand Down
9 changes: 9 additions & 0 deletions modules/dicehub/endpoints/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,14 @@ func (e *Endpoints) getListParams(r *http.Request, vars map[string]string) (*api
isProjectReleasePtr = &isProjectRelease
}

var latest bool
if s := r.URL.Query().Get("latest"); s != "" {
latest, err = strconv.ParseBool(s)
if err != nil {
return nil, err
}
}

userIDStr := r.URL.Query()["userId"]
var userID []string
for _, id := range userIDStr {
Expand Down Expand Up @@ -938,6 +946,7 @@ func (e *Endpoints) getListParams(r *http.Request, vars map[string]string) (*api
ReleaseName: releaseName,
Cluster: clusterName,
Branch: branch,
Latest: latest,
IsStable: isStablePtr,
IsFormal: isFormalPtr,
IsProjectRelease: isProjectReleasePtr,
Expand Down
11 changes: 2 additions & 9 deletions modules/dicehub/service/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,7 @@ func (r *Release) Get(orgID int64, releaseID string) (*apistructs.ReleaseGetResp

// List 根据搜索条件进行搜索
func (r *Release) List(orgID int64, req *apistructs.ReleaseListRequest) (*apistructs.ReleaseListResponseData, error) {
total, releases, err := r.db.GetReleasesByParams(
orgID, req.ProjectID, req.ApplicationID,
req.Query, req.ReleaseName, req.Branch,
req.IsStable, req.IsFormal, req.IsProjectRelease,
req.UserID, req.Version, req.ReleaseID, req.CommitID, req.Tags,
req.Cluster, req.CrossCluster, req.IsVersion,
req.CrossClusterOrSpecifyCluster,
req.StartTime, req.EndTime, req.PageNum, req.PageSize,
req.OrderBy, req.Order)
total, releases, err := r.db.GetReleasesByParams(orgID, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -913,6 +905,7 @@ func (r *Release) Convert(releaseRequest *apistructs.ReleaseCreateRequest, appRe
return nil, err
}
release.Labels = string(labelBytes)
release.GitBranch = releaseRequest.Labels["gitBranch"]
}

if len(releaseRequest.Tags) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Values struct {
BranchID string `json:"branchID,omitempty"`
CommitID string `json:"commitID,omitempty"`
CreatedAtStartEnd []int64 `json:"createdAtStartEnd,omitempty"`
Latest bool `json:"latest"`
ReleaseID string `json:"releaseID,omitempty"`
UserIDs []string `json:"userIDs,omitempty"`
}
Expand Down
Loading

0 comments on commit b1351dc

Please sign in to comment.