Skip to content

Commit

Permalink
refactor: delete iterations and their sub items together
Browse files Browse the repository at this point in the history
feat: initialize issueService

feat: add option to delete only iterations or all

feat: add BatchUpdateIssueIterationID

feat: batch deletion of functions and addition of transactions
  • Loading branch information
jing-zhi committed Feb 6, 2024
1 parent 7f5a3bc commit 7f83688
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 26 deletions.
11 changes: 10 additions & 1 deletion internal/apps/dop/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/internal/apps/dop/dao"
"github.com/erda-project/erda/internal/apps/dop/event"
"github.com/erda-project/erda/internal/apps/dop/providers/issue/core"
issuequery "github.com/erda-project/erda/internal/apps/dop/providers/issue/core/query"
issuedao "github.com/erda-project/erda/internal/apps/dop/providers/issue/dao"
"github.com/erda-project/erda/internal/apps/dop/providers/projectpipeline"
Expand Down Expand Up @@ -462,7 +463,7 @@ func (e *Endpoints) Routes() []httpserver.Endpoint {

{Path: "/api/iterations", Method: http.MethodPost, Handler: e.CreateIteration},
{Path: "/api/iterations/{id}", Method: http.MethodPut, Handler: e.UpdateIteration},
{Path: "/api/iterations/{id}", Method: http.MethodDelete, Handler: e.DeleteIteration},
{Path: "/api/iterations/{id}/{onlyIteration}", Method: http.MethodDelete, Handler: e.DeleteIteration},
{Path: "/api/iterations/{id}", Method: http.MethodGet, Handler: e.GetIteration},
{Path: "/api/iterations", Method: http.MethodGet, Handler: e.PagingIterations},

Expand Down Expand Up @@ -630,6 +631,7 @@ type Endpoints struct {
namespace *namespace.Namespace
envConfig *environment.EnvConfig
issue *issue.Issue
issueService core.IssueService
issueState *issuestate.IssueState
workBench *workbench.Workbench
iteration *iteration.Iteration
Expand Down Expand Up @@ -895,6 +897,13 @@ func WithIssue(issue *issue.Issue) Option {
}
}

// WithIssueService Configure issueService
func WithIssueService(issueService *core.IssueService) Option {
return func(e *Endpoints) {
e.issueService = *issueService

Check warning on line 903 in internal/apps/dop/endpoints/endpoints.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/endpoints.go#L901-L903

Added lines #L901 - L903 were not covered by tests
}
}

func WithIssueState(state *issuestate.IssueState) Option {
return func(e *Endpoints) {
e.issueState = state
Expand Down
28 changes: 28 additions & 0 deletions internal/apps/dop/endpoints/iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/erda-project/erda-proto-go/dop/issue/core/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/apps/dop/dao"
"github.com/erda-project/erda/internal/apps/dop/services/apierrors"
"github.com/erda-project/erda/internal/pkg/user"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/http/httpserver"
"github.com/erda-project/erda/pkg/http/httpserver/errorresp"
"github.com/erda-project/erda/pkg/strutil"
Expand Down Expand Up @@ -205,6 +207,7 @@ func (e *Endpoints) DeleteIteration(ctx context.Context, r *http.Request, vars m
}

iterationID, err := strconv.ParseUint(vars["id"], 10, 64)
onlyIteration, err := strconv.ParseBool(vars["onlyIteration"])

Check warning on line 210 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L210

Added line #L210 was not covered by tests
if err != nil {
return apierrors.ErrDeleteIteration.InvalidParameter("id").ToResp(), nil
}
Expand Down Expand Up @@ -235,6 +238,31 @@ func (e *Endpoints) DeleteIteration(ctx context.Context, r *http.Request, vars m
return errorresp.ErrResp(err)
}

itemIDs, err := e.issueService.DBClient().GetIssuesIDByIterationID(iterationID)
if err != nil {
return apierrors.ErrDeleteIteration.InternalError(err).ToResp(), nil

Check warning on line 243 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L241-L243

Added lines #L241 - L243 were not covered by tests
}

if len(itemIDs) > 0 {
if onlyIteration {
err = e.issueService.DBClient().BatchUpdateIssueIterationID(itemIDs, -1)
if err != nil {
return apierrors.ErrDeleteIteration.InternalError(err).ToResp(), nil

Check warning on line 250 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L246-L250

Added lines #L246 - L250 were not covered by tests
}
goto come
} else {
req := &pb.BatchDeleteIssueRequest{
Ids: strutil.ToStrSlice(itemIDs),
ProjectID: iteration.ProjectID,

Check warning on line 256 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L252-L256

Added lines #L252 - L256 were not covered by tests
}
ctx = apis.WithUserIDContext(ctx, identityInfo.UserID)
_, err = e.issueService.BatchDeleteIssues(ctx, req)
if err != nil {
return apierrors.ErrDeleteIteration.InternalError(err).ToResp(), nil

Check warning on line 261 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L258-L261

Added lines #L258 - L261 were not covered by tests
}
}
}
come:
// delete relation labels
if err = e.db.DeleteLabelRelations(apistructs.LabelTypeIteration, strconv.FormatUint(iterationID, 10), nil); err != nil {
return apierrors.ErrDeleteIteration.InternalError(err).ToResp(), nil
Expand Down
1 change: 1 addition & 0 deletions internal/apps/dop/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ func (p *provider) initEndpoints(db *dao.DBClient) (*endpoints.Endpoints, error)
endpoints.WithNamespace(ns),
endpoints.WithEnvConfig(env),
endpoints.WithIssue(issue),
endpoints.WithIssueService(p.IssueCoreSvc),
endpoints.WithIssueState(issueState),
endpoints.WithIteration(itr),
endpoints.WithPublisher(pub),
Expand Down
12 changes: 10 additions & 2 deletions internal/apps/dop/providers/issue/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,13 @@ func (i *IssueService) BatchDeleteIssues(ctx context.Context, req *pb.BatchDelet
}

issues, _, err := i.query.Paging(pb.PagingIssueRequest{IDs: ids, ProjectID: req.ProjectID})

if err != nil {
return nil, err
}

// start transaction
tx := i.db.Begin()

Check warning on line 580 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L580

Added line #L580 was not covered by tests

for k, issue := range issues {
rels, err := i.GetTestPlanCaseRels(uint64(issue.Id))
if err != nil {
Expand Down Expand Up @@ -616,25 +618,30 @@ func (i *IssueService) BatchDeleteIssues(ctx context.Context, req *pb.BatchDelet
// 删除测试计划用例关联
if issue.Type == pb.IssueTypeEnum_BUG {
if err := i.db.DeleteIssueTestCaseRelationsByIssueIDs([]uint64{numSlice[k]}); err != nil {
tx.Rollback()

Check warning on line 621 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L621

Added line #L621 was not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}
}
}

if err := i.db.BatchCleanIssueRelation(numSlice); err != nil {
tx.Rollback()

Check warning on line 628 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L628

Added line #L628 was not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

if err := i.db.BatchDeletePropertyRelationByIssueID(ids); err != nil {
tx.Rollback()

Check warning on line 633 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L633

Added line #L633 was not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

// delete issue state transition
if err = i.db.BatchDeleteIssuesStateTransition(numSlice); err != nil {
tx.Rollback()

Check warning on line 639 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L639

Added line #L639 was not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

if err = i.db.BatchDeleteIssues(numSlice); err != nil {
tx.Rollback()

Check warning on line 644 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L644

Added line #L644 was not covered by tests
return nil, err
}

Expand All @@ -643,10 +650,11 @@ func (i *IssueService) BatchDeleteIssues(ctx context.Context, req *pb.BatchDelet
ProjectID: issue.ProjectID,
ActiveTime: time.Now(),
}); err != nil {
tx.Rollback()

Check warning on line 653 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L653

Added line #L653 was not covered by tests
logrus.Errorf("update project active time err: %v", err)
}
}

tx.Commit()

Check warning on line 657 in internal/apps/dop/providers/issue/core/service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/providers/issue/core/service.go#L657

Added line #L657 was not covered by tests
return &pb.BatchDeleteIssueResponse{Data: issues}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions internal/apps/dop/providers/issue/dao/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (client *DBClient) UpdateIssues(requirementID uint64, fields map[string]int
Updates(fields).Error
}

func (client *DBClient) BatchUpdateIssueIterationID(ids []uint64, iterationID int64) error {
return client.Model(Issue{}).Where("deleted = 0").Where("id IN (?)", ids).Updates(Issue{IterationID: iterationID}).Error
}

// UpdateIssueType 转换issueType
func (client *DBClient) UpdateIssueType(issue *Issue) error {
return client.Model(Issue{}).Save(issue).Error
Expand Down Expand Up @@ -405,6 +409,15 @@ func (client *DBClient) GetIssueWithOutDelete(id int64) (Issue, error) {
return issue, err
}

func (client *DBClient) GetIssuesIDByIterationID(iterationID uint64) (ids []uint64, err error) {
var issues []Issue
err = client.Model(Issue{}).Where("deleted = 0").Where("iteration_id = ?", iterationID).Select("id").Find(&issues).Error
for _, v := range issues {
ids = append(ids, v.ID)
}
return ids, err
}

func (client *DBClient) ListIssueByIDs(issueIDs []int64) ([]Issue, error) {
var issues []Issue
if err := client.Where("`id` IN (?)", issueIDs).Find(&issues).Error; err != nil {
Expand Down
22 changes: 0 additions & 22 deletions internal/apps/dop/services/iteration/iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package iteration
import (
"github.com/jinzhu/gorm"

"github.com/erda-project/erda-proto-go/dop/issue/core/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/apps/dop/dao"
"github.com/erda-project/erda/internal/apps/dop/providers/issue/core/query"
Expand Down Expand Up @@ -159,27 +158,6 @@ func (itr *Iteration) GetByTitle(projectID uint64, title string) (*dao.Iteration

// Delete 删除 iteration
func (itr *Iteration) Delete(id uint64) error {
iteration, err := itr.db.GetIteration(id)
if err != nil {
return err
}

// 检查 iteration 下是否有需求/任务/bug; 若有,不可删除
issueReq := pb.PagingIssueRequest{
ProjectID: iteration.ProjectID,
IterationID: int64(id),
External: true,
PageNo: 1,
PageSize: 1,
}
issues, _, err := itr.issue.Paging(issueReq)
if err != nil {
return err
}
if len(issues) > 0 {
return apierrors.ErrDeleteIteration.InvalidParameter("该迭代下存在事件,请先删除事件后再删除迭代")
}

if err := itr.db.DeleteIteration(id); err != nil {
return apierrors.ErrDeleteIteration.InternalError(err)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/extension/db/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,12 @@ func (client *Client) ListExtensionVersions(names []string, all bool) (map[strin

return extensions, err
}

func (client *Client) IsExtensionPublicVersionExist(name string) (bool, error) {
var count int64
err := client.Model(&ExtensionVersion{}).
Where("name = ? ", name).
Where("public = ?", true).
Count(&count).Error
return count > 0, err
}
15 changes: 14 additions & 1 deletion internal/pkg/extension/extension.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,15 @@ func (s *provider) CreateExtensionVersionByRequest(req *pb.ExtensionVersionCreat
}
s.triggerPushEvent(specData, "update")
if req.All {
isPublic, err := s.isExtensionPublic(req.Name, req.Public)
if err != nil {
return nil, apierrors.ErrQueryExtension.InternalError(err)

Check warning on line 316 in internal/pkg/extension/extension.service.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/extension/extension.service.go#L314-L316

Added lines #L314 - L316 were not covered by tests
}
extModel.Category = specData.Category
extModel.LogoUrl = specData.LogoUrl
extModel.DisplayName = specData.DisplayName
extModel.Desc = specData.Desc
extModel.Public = req.Public
extModel.Public = isPublic

Check warning on line 322 in internal/pkg/extension/extension.service.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/extension/extension.service.go#L322

Added line #L322 was not covered by tests
extModel.Labels = labels
err = s.db.Save(&extModel).Error
if err != nil {
Expand Down Expand Up @@ -360,6 +364,15 @@ func (s *provider) CreateExtensionVersionByRequest(req *pb.ExtensionVersionCreat
}
}

func (s *provider) isExtensionPublic(name string, isPublic bool) (bool, error) {
isExist, err := s.db.IsExtensionPublicVersionExist(name)
if err != nil {
return false, fmt.Errorf("query extension public version error, %v", err)

Check warning on line 370 in internal/pkg/extension/extension.service.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/extension/extension.service.go#L370

Added line #L370 was not covered by tests
}

return isExist || isPublic, nil
}

func (s *provider) DeleteExtensionVersion(name, version string) error {
if len(name) == 0 {
return apierrors.ErrDeleteExtensionVersion.MissingParameter("name")
Expand Down
50 changes: 50 additions & 0 deletions internal/pkg/extension/extension.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func mockClient() *db.Client {
}
return res, nil
})
monkey.PatchInstanceMethod(reflect.TypeOf(client), "IsExtensionPublicVersionExist", func(_ *db.Client, name string) (bool, error) {
if name == "deprecated-addon" {
return false, nil
}
return true, nil
})
monkey.PatchInstanceMethod(reflect.TypeOf(client.DB), "Save", func(_ *gorm.DB, value interface{}) *gorm.DB {
return &gorm.DB{
Error: nil,
Expand Down Expand Up @@ -643,3 +649,47 @@ func Test_extensionService_QueryExtensionVersions(t *testing.T) {
})
}
}

func Test_isExtensionPublic(t *testing.T) {
p := &provider{
db: cli,
}

type args struct {
name string
isPublic bool
}

tests := []struct {
name string
args args
expect bool
}{
{
name: "update processing extension is private",
args: args{
name: "mysql",
isPublic: false,
},
expect: true,
},
{
name: "all private",
args: args{
name: "deprecated-addon",
isPublic: false,
},
expect: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := p.isExtensionPublic(tt.args.name, tt.args.isPublic)
assert.NoError(t, err)
if got != tt.expect {
t.Fatalf("expect: %v, got: %v", tt.expect, got)
}
})
}
}

0 comments on commit 7f83688

Please sign in to comment.