Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: delete iterations and their sub items together #6264

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"

"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
"github.com/erda-project/erda-proto-go/dop/issue/core/pb"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/internal/apps/dop/providers/issue/core"
)

type BatchOperationTipModal struct {
Expand All @@ -30,7 +30,7 @@ type BatchOperationTipModal struct {
SDK *cptype.SDK
CtxBdl *bundle.Bundle
ctx context.Context
issueSvc pb.IssueCoreServiceServer
issueSvc core.IssueService
}

type Props struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
"github.com/erda-project/erda-proto-go/dop/issue/core/pb"
"github.com/erda-project/erda/internal/apps/dop/component-protocol/types"
"github.com/erda-project/erda/internal/apps/dop/providers/issue/core"
)

func init() {
Expand Down Expand Up @@ -90,7 +91,7 @@
bot.ctx = ctx
cputil.MustObjJSONTransfer(&c.State, &bot.State)
bot.State.Visible = false
issueSvc := ctx.Value(types.IssueService).(pb.IssueCoreServiceServer)
issueSvc := ctx.Value(types.IssueService).(core.IssueService)

Check warning on line 94 in internal/apps/dop/component-protocol/components/issue-manage/batchOperationTipModal/render.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/component-protocol/components/issue-manage/batchOperationTipModal/render.go#L94

Added line #L94 was not covered by tests
bot.issueSvc = issueSvc
_, err = bot.DeleteItems(bot.State.SelectedRowKeys, projectid)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/apps/dop/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"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 @@ -630,6 +631,7 @@
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 @@
}
}

// 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
21 changes: 21 additions & 0 deletions internal/apps/dop/endpoints/iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"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 +206,7 @@
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L209 was not covered by tests
if err != nil {
return apierrors.ErrDeleteIteration.InvalidParameter("id").ToResp(), nil
}
Expand Down Expand Up @@ -235,6 +237,25 @@
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 242 in internal/apps/dop/endpoints/iteration.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/endpoints/iteration.go#L240-L242

Added lines #L240 - L242 were not covered by tests
}

if len(itemIDs) > 0 {
ctx = apis.WithUserIDContext(ctx, identityInfo.UserID)
if onlyIteration {
err = e.issueService.BatchUpdateIssueIterationIDByIterationID(ctx, iterationID, -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#L245-L250

Added lines #L245 - L250 were not covered by tests
}
} else {
err = e.issueService.BatchDeleteIssueByIterationID(ctx, iterationID)
if err != nil {
return apierrors.ErrDeleteIteration.InternalError(err).ToResp(), nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L252 - L255 were not covered by tests
}
}
}
// 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
56 changes: 40 additions & 16 deletions internal/apps/dop/providers/issue/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"github.com/erda-project/erda/internal/apps/dop/services/testcase"
mttestplan "github.com/erda-project/erda/internal/apps/dop/services/testplan"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/database/dbengine"
"github.com/erda-project/erda/pkg/strutil"
)

Expand Down Expand Up @@ -554,16 +555,10 @@
// BatchDeleteIssues 批量删除
func (i *IssueService) BatchDeleteIssues(ctx context.Context, req *pb.BatchDeleteIssueRequest) (*pb.BatchDeleteIssueResponse, error) {
numSlice := make([]uint64, len(req.Ids))
for i, str := range req.Ids {
num, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return nil, apierrors.ErrBatchDeleteIssue.InvalidParameter(err)
}
numSlice[i] = num
}
ids := make([]int64, len(req.Ids))
for k, v := range numSlice {
ids[k] = int64(v)
for i, str := range req.Ids {
numSlice[i], _ = strconv.ParseUint(str, 10, 64)
ids[i] = int64(numSlice[i])

Check warning on line 561 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#L559-L561

Added lines #L559 - L561 were not covered by tests
}

identityInfo := apis.GetIdentityInfo(ctx)
Expand All @@ -572,11 +567,15 @@
}

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

if err != nil {
return nil, err
}

tx := i.db.Begin()
client := &dao.DBClient{
DBEngine: &dbengine.DBEngine{DB: tx},

Check warning on line 576 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#L574-L576

Added lines #L574 - L576 were not covered by tests
}

for k, issue := range issues {
rels, err := i.GetTestPlanCaseRels(uint64(issue.Id))
if err != nil {
Expand Down Expand Up @@ -615,26 +614,31 @@

// 删除测试计划用例关联
if issue.Type == pb.IssueTypeEnum_BUG {
if err := i.db.DeleteIssueTestCaseRelationsByIssueIDs([]uint64{numSlice[k]}); err != nil {
if err := client.DeleteIssueTestCaseRelationsByIssueIDs([]uint64{numSlice[k]}); err != nil {
tx.Rollback()

Check warning on line 618 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#L617-L618

Added lines #L617 - L618 were not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}
}
}

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

Check warning on line 625 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#L624-L625

Added lines #L624 - L625 were not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

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

Check warning on line 630 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#L629-L630

Added lines #L629 - L630 were not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

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

Check warning on line 636 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#L635-L636

Added lines #L635 - L636 were not covered by tests
return nil, apierrors.ErrBatchDeleteIssue.InternalError(err)
}

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

Check warning on line 641 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#L640-L641

Added lines #L640 - L641 were not covered by tests
return nil, err
}

Expand All @@ -646,10 +650,30 @@
logrus.Errorf("update project active time err: %v", err)
}
}

tx.Commit()

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
return &pb.BatchDeleteIssueResponse{Data: issues}, nil
}

// BatchDeleteIssueByIterationID 根据迭代 id 批量删除 issue
func (i *IssueService) BatchDeleteIssueByIterationID(ctx context.Context, iterationID uint64) (err error) {
identityInfo := apis.GetIdentityInfo(ctx)
if identityInfo == nil {
return apierrors.ErrUpdateIssue.NotLogin()

Check warning on line 661 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#L658-L661

Added lines #L658 - L661 were not covered by tests
}
err = i.db.BatchDeleteIssueByIterationID(iterationID)
return

Check warning on line 664 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#L663-L664

Added lines #L663 - L664 were not covered by tests
}

// BatchUpdateIssueIterationIDByIterationID 根据迭代 id 批量更新 issue 的 iteration_id
func (i *IssueService) BatchUpdateIssueIterationIDByIterationID(ctx context.Context, iterationID uint64, ID int64) (err error) {
identityInfo := apis.GetIdentityInfo(ctx)
if identityInfo == nil {
return apierrors.ErrUpdateIssue.NotLogin()

Check warning on line 671 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#L668-L671

Added lines #L668 - L671 were not covered by tests
}
err = i.db.BatchUpdateIssueIterationIDByIterationID(iterationID, ID)
return

Check warning on line 674 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#L673-L674

Added lines #L673 - L674 were not covered by tests
}

func (i *IssueService) GetTestPlanCaseRels(issueID uint64) ([]*pb.TestPlanCaseRel, error) {
// 查询关联的测试计划用例
testPlanCaseRels := make([]*pb.TestPlanCaseRel, 0)
Expand Down
26 changes: 26 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) BatchUpdateIssueIterationIDByIterationID(iterationID uint64, ID int64) error {
return client.Model(Issue{}).Where("deleted = 0").Where("iteration_id = ?", iterationID).Updates(map[string]interface{}{"iteration_id": ID}).Error
}

// UpdateIssueType 转换issueType
func (client *DBClient) UpdateIssueType(issue *Issue) error {
return client.Model(Issue{}).Save(issue).Error
Expand Down Expand Up @@ -226,6 +230,19 @@ func (client *DBClient) BatchDeleteIssues(ids []uint64) error {
return client.Model(Issue{}).Where("id IN (?)", ids).Update("deleted", 1).Error
}

func (client *DBClient) BatchDeleteIssueByIterationID(iterationID uint64) error {
sql := fmt.Sprintf(`
UPDATE dice_issues AS dis
LEFT JOIN dice_issue_testcase_relations AS ditr ON ditr.issue_id = dis.id
LEFT JOIN dice_issue_relation AS dir ON dir.issue_id = dis.id
LEFT JOIN dice_issue_property_relation AS dipr ON dipr.issue_id = dis.id
LEFT JOIN erda_issue_state_transition AS eist ON eist.issue_id = dis.id
SET dis.deleted = 1
WHERE dis.iteration_id = ?;
`)
return client.Exec(sql, iterationID).Error
}

// DeleteIssuesByRequirement .
func (client *DBClient) DeleteIssuesByRequirement(requirementID uint64) error {
return client.Model(Issue{}).Where("requirement_id = ?", requirementID).Update("deleted", 1).Error
Expand Down Expand Up @@ -405,6 +422,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
Loading