Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: user cannot archive project if there are any open issue (#2329)

h3n4ld31ae45b9140
diff --git a/api/issue.go b/api/issue.go
index ec10fa8514..6f2845435f 100644
--- a/api/issue.go
+++ b/api/issue.go
@@ -213,7 +213,7 @@ type IssueFind struct {
PipelineID *int
// Find issue where principalID is either creator, assignee or subscriber
PrincipalID *int
- StatusList *[]IssueStatus
+ StatusList []IssueStatus
// If specified, then it will only fetch "Limit" most recently updated issues
Limit *int
}
diff --git a/server/issue.go b/server/issue.go
index a880be6399..9dbd927da6 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -57,7 +57,7 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
for _, status := range strings.Split(issueStatusListStr, ",") {
statusList = append(statusList, api.IssueStatus(status))
}
- issueFind.StatusList = &statusList
+ issueFind.StatusList = statusList
}
if limitStr := c.QueryParam("limit"); limitStr != "" {
limit, err := strconv.Atoi(limitStr)
diff --git a/server/project.go b/server/project.go
index 6feffaae25..7d99ff53f8 100644
--- a/server/project.go
+++ b/server/project.go
@@ -158,14 +158,24 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed patch project request").SetInternal(err)
}
- // Ensure the project has no database before it's archived.
+ // Verify before archiving the project:
+ // 1. the project has no database.
+ // 2. the issue status of this project should be canceled or done.
if v := projectPatch.RowStatus; v != nil && *v == string(api.Archived) {
databases, err := s.store.FindDatabase(ctx, &api.DatabaseFind{ProjectID: &id})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Errorf("failed to find databases in the project %d", id)).SetInternal(err)
}
if len(databases) > 0 {
- return echo.NewHTTPError(http.StatusBadRequest, "You should transfer all databases under the project before archiving the project.")
+ return echo.NewHTTPError(http.StatusBadRequest, "Please transfer all databases under the project before archiving the project.")
+ }
+
+ issueList, err := s.store.FindIssue(ctx, &api.IssueFind{ProjectID: &id, StatusList: []api.IssueStatus{api.IssueOpen}})
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, errors.Errorf("failed to find issues in the project %d", id)).SetInternal(err)
+ }
+ if len(issueList) > 0 {
+ return echo.NewHTTPError(http.StatusBadRequest, "Please resolve all the issues in it before archiving the project.")
}
}
diff --git a/store/issue.go b/store/issue.go
index 95138e6e8a..ee662d857f 100644
--- a/store/issue.go
+++ b/store/issue.go
@@ -538,9 +538,9 @@ func (*Store) findIssueImpl(ctx context.Context, tx *sql.Tx, find *api.IssueFind
args = append(args, *v)
args = append(args, *v)
}
- if v := find.StatusList; v != nil {
+ if len(find.StatusList) != 0 {
list := []string{}
- for _, status := range *v {
+ for _, status := range find.StatusList {
list = append(list, fmt.Sprintf("$%d", len(args)+1))
args = append(args, status)
}
diff --git a/tests/project_test.go b/tests/project_test.go
new file mode 100644
index 0000000000..be3375b0ef
--- /dev/null
+++ b/tests/project_test.go
@@ -0,0 +1,114 @@
+package tests
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "strconv"
+ "testing"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common/log"
+ "github.com/bytebase/bytebase/plugin/db"
+ resourcemysql "github.com/bytebase/bytebase/resources/mysql"
+ "github.com/bytebase/bytebase/tests/fake"
+ "github.com/stretchr/testify/require"
+ "go.uber.org/zap/zapcore"
+)
+
+func TestArchiveProject(t *testing.T) {
+ a := require.New(t)
+ log.SetLevel(zapcore.DebugLevel)
+ ctx := context.Background()
+ serverPort := getTestPort(t.Name())
+ ctl := &controller{}
+ dataDir := t.TempDir()
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, serverPort)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+ err = ctl.Login()
+ a.NoError(err)
+ err = ctl.setLicense()
+ a.NoError(err)
+
+ mysqlPort := serverPort + 1
+ _, stopInstance := resourcemysql.SetupTestInstance(t, mysqlPort)
+ defer stopInstance()
+
+ environments, err := ctl.getEnvironments()
+ a.NoError(err)
+ prodEnvironment, err := findEnvironment(environments, "Prod")
+ a.NoError(err)
+
+ connCfg := getMySQLConnectionConfig(strconv.Itoa(mysqlPort), "")
+ instance, err := ctl.addInstance(api.InstanceCreate{
+ EnvironmentID: prodEnvironment.ID,
+ Name: "test",
+ Engine: db.MySQL,
+ Host: connCfg.Host,
+ Port: connCfg.Port,
+ Username: connCfg.Username,
+ })
+ a.NoError(err)
+
+ t.Run("ArchiveProjectWithDatbase", func(t *testing.T) {
+ project, err := ctl.createProject(api.ProjectCreate{
+ Name: "ProjectWithDatabase",
+ Key: "PWD",
+ TenantMode: api.TenantModeDisabled,
+ })
+ a.NoError(err)
+
+ databaseName := "db"
+ err = ctl.createDatabase(project, instance, databaseName, "", nil)
+ a.NoError(err)
+
+ status := string(api.Archived)
+ err = ctl.patchProject(api.ProjectPatch{
+ ID: project.ID,
+ RowStatus: &status,
+ UpdaterID: project.Creator.ID,
+ })
+ a.Error(err)
+ })
+
+ t.Run("ArchiveProjectWithOpenIssue", func(t *testing.T) {
+ project, err := ctl.createProject(api.ProjectCreate{
+ Name: "ProjectWithOpenIssue",
+ Key: "PWO",
+ TenantMode: api.TenantModeDisabled,
+ })
+ a.NoError(err)
+
+ databaseName := "fakedb"
+ createDatabaseCtx := &api.CreateDatabaseContext{
+ InstanceID: instance.ID,
+ DatabaseName: databaseName,
+ Labels: "",
+ CharacterSet: "utf8mb4",
+ Collation: "utf8mb4_general_ci",
+ }
+
+ c, err := json.Marshal(createDatabaseCtx)
+ a.NoError(err)
+
+ _, err = ctl.createIssue(api.IssueCreate{
+ ProjectID: project.ID,
+ Name: fmt.Sprintf("create database %q", databaseName),
+ Type: api.IssueDatabaseCreate,
+ Description: fmt.Sprintf("This creates a database %q.", databaseName),
+ // Assign to self.
+ AssigneeID: project.Creator.ID,
+ CreateContext: string(c),
+ })
+ a.NoError(err)
+
+ status := string(api.Archived)
+ err = ctl.patchProject(api.ProjectPatch{
+ ID: project.ID,
+ RowStatus: &status,
+ UpdaterID: project.Creator.ID,
+ })
+ a.Error(err)
+ })
+}
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 7b0f9c945a..c5e73950da 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -439,7 +439,7 @@ func TestVCS(t *testing.T) {
issues, err := ctl.getIssues(
api.IssueFind{
ProjectID: &project.ID,
- StatusList: &openStatus,
+ StatusList: openStatus,
},
)
a.NoError(err)
@@ -476,7 +476,7 @@ func TestVCS(t *testing.T) {
issues, err = ctl.getIssues(
api.IssueFind{
ProjectID: &project.ID,
- StatusList: &openStatus,
+ StatusList: openStatus,
},
)
a.NoError(err)
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 12264de0f9..556fcf77e6 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -450,7 +450,7 @@ func TestTenantVCS(t *testing.T) {
issues, err := ctl.getIssues(
api.IssueFind{
ProjectID: &project.ID,
- StatusList: &openStatus,
+ StatusList: openStatus,
},
)
a.NoError(err)
@@ -903,7 +903,7 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
issues, err := ctl.getIssues(
api.IssueFind{
ProjectID: &project.ID,
- StatusList: &openStatus,
+ StatusList: openStatus,
},
)
a.NoError(err)
diff --git a/tests/tests.go b/tests/tests.go
index b4011fe9bd..6a8acacc9b 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -148,6 +148,8 @@ func getTestPort(testName string) int {
"TestSQLReviewForMySQL",
"TestSQLReviewForPostgreSQL",
+
+ "TestArchiveProject",
}
port := 1234
for _, name := range tests {
@@ -498,6 +500,20 @@ func (ctl *controller) createProject(projectCreate api.ProjectCreate) (*api.Proj
return project, nil
}
+func (ctl *controller) patchProject(projectPatch api.ProjectPatch) error {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, &projectPatch); err != nil {
+ return errors.Wrap(err, "failed to marshal project patch")
+ }
+
+ _, err := ctl.patch(fmt.Sprintf("/project/%d", projectPatch.ID), buf)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
// getProjects gets the environments.
func (ctl *controller) getEnvironments() ([]*api.Environment, error) {
body, err := ctl.get("/environment", nil)
@@ -676,9 +692,9 @@ func (ctl *controller) getIssues(issueFind api.IssueFind) ([]*api.Issue, error)
if issueFind.ProjectID != nil {
params["project"] = fmt.Sprintf("%d", *issueFind.ProjectID)
}
- if issueFind.StatusList != nil && len(*issueFind.StatusList) > 0 {
+ if len(issueFind.StatusList) > 0 {
var sl []string
- for _, status := range *issueFind.StatusList {
+ for _, status := range issueFind.StatusList {
sl = append(sl, string(status))
}
params["status"] = strings.Join(sl, ",")