Skip to content

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

feat: add issue openapi for gerrit demo (#4047)

ecmadaofaa5d33e3d02
diff --git a/api/project.go b/api/project.go
index 6434ad3563..b4a98ff72c 100644
--- a/api/project.go
+++ b/api/project.go
@@ -117,6 +117,7 @@ type ProjectFind struct {
Name *string
// If present, will only find project containing PrincipalID as an active member
PrincipalID *int
+ Key *string
}
func (find *ProjectFind) String() string {
diff --git a/api/v1/issue.go b/api/v1/issue.go
new file mode 100644
index 0000000000..9ec10d350e
--- /dev/null
+++ b/api/v1/issue.go
@@ -0,0 +1,20 @@
+package v1
+
+import (
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// IssueCreate is the API message for creating an issue.
+type IssueCreate struct {
+ // Related fields
+ ProjectKey string `json:"projectKey"`
+ Database string `json:"database"`
+ Environment string `json:"environment"`
+
+ // Domain specific fields
+ Name string `json:"name"`
+ Description string `json:"description"`
+ MigrationType db.MigrationType `json:"migrationType"`
+ Statement string `json:"statement"`
+ SchemaVersion string `json:"schemaVersion"`
+}
diff --git a/server/openapi_issue.go b/server/openapi_issue.go
new file mode 100644
index 0000000000..5e9768ed9a
--- /dev/null
+++ b/server/openapi_issue.go
@@ -0,0 +1,86 @@
+package server
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+
+ "github.com/bytebase/bytebase/api"
+ openAPIV1 "github.com/bytebase/bytebase/api/v1"
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+func (s *Server) registerOpenAPIRoutesForIssue(g *echo.Group) {
+ g.POST("/issues", s.createIssueByOpenAPI)
+}
+
+func (s *Server) createIssueByOpenAPI(c echo.Context) error {
+ ctx := c.Request().Context()
+
+ body, err := io.ReadAll(c.Request().Body)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)
+ }
+
+ create := &openAPIV1.IssueCreate{}
+ if err := json.Unmarshal(body, create); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed create instance request").SetInternal(err)
+ }
+
+ project, err := s.store.GetProjectByKey(ctx, create.ProjectKey)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find project with key %s", create.ProjectKey)).SetInternal(err)
+ }
+ if project == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Cannot found project with key %s", create.ProjectKey))
+ }
+
+ issueType := api.IssueDatabaseDataUpdate
+ migrationList := []*api.MigrationDetail{}
+ dbList, err := s.findProjectDatabases(ctx, project.ID, create.Database, create.Environment)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list database").SetInternal(err)
+ }
+
+ for _, database := range dbList {
+ migrationList = append(migrationList, &api.MigrationDetail{
+ DatabaseID: database.ID,
+ MigrationType: create.MigrationType,
+ Statement: create.Statement,
+ SchemaVersion: create.SchemaVersion,
+ })
+ }
+
+ if create.MigrationType == db.Migrate || create.MigrationType == db.Baseline {
+ issueType = api.IssueDatabaseSchemaUpdate
+ }
+
+ createContext, err := json.Marshal(
+ &api.MigrationContext{
+ DetailList: migrationList,
+ },
+ )
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal update schema context").SetInternal(err)
+ }
+
+ issueCreate := &api.IssueCreate{
+ CreatorID: c.Get(getPrincipalIDContextKey()).(int),
+ ProjectID: project.ID,
+ Name: create.Name,
+ Type: issueType,
+ Description: create.Description,
+ AssigneeID: api.SystemBotID,
+ AssigneeNeedAttention: true,
+ CreateContext: string(createContext),
+ }
+
+ if _, err := s.createIssue(ctx, issueCreate); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create the issue").SetInternal(err)
+ }
+
+ return c.String(http.StatusOK, "OK")
+}
diff --git a/server/server.go b/server/server.go
index c88a27e0d2..64a866d677 100644
--- a/server/server.go
+++ b/server/server.go
@@ -479,6 +479,7 @@ func (s *Server) registerOpenAPIRoutes(e *echo.Echo, ce *casbin.Enforcer, prof c
s.registerOpenAPIRoutesForSQL(openAPIGroup)
s.registerOpenAPIRoutesForAuth(openAPIGroup)
s.registerOpenAPIRoutesForInstance(openAPIGroup)
+ s.registerOpenAPIRoutesForIssue(openAPIGroup)
s.registerOpenAPIRoutesForEnvironment(openAPIGroup)
openAPIGroup.GET("/healthz", func(c echo.Context) error {
diff --git a/store/project.go b/store/project.go
index 1e64f259d8..b4d0a2f13d 100644
--- a/store/project.go
+++ b/store/project.go
@@ -79,6 +79,23 @@ func (s *Store) GetProjectByID(ctx context.Context, id int) (*api.Project, error
return project, nil
}
+// GetProjectByKey gets an instance of Project by the unique key.
+func (s *Store) GetProjectByKey(ctx context.Context, key string) (*api.Project, error) {
+ find := &api.ProjectFind{Key: &key}
+ projectRaw, err := s.getProjectRaw(ctx, find)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get Project with key %s", key)
+ }
+ if projectRaw == nil {
+ return nil, nil
+ }
+ project, err := s.composeProject(ctx, projectRaw)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to compose Project with projectRaw[%+v]", projectRaw)
+ }
+ return project, nil
+}
+
// FindProject finds a list of Project instances.
func (s *Store) FindProject(ctx context.Context, find *api.ProjectFind) ([]*api.Project, error) {
projectRawList, err := s.findProjectRaw(ctx, find)
@@ -379,6 +396,9 @@ func findProjectImpl(ctx context.Context, tx *Tx, find *api.ProjectFind) ([]*pro
if v := find.ID; v != nil {
where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.Key; v != nil {
+ where, args = append(where, fmt.Sprintf("key = $%d", len(args)+1)), append(args, *v)
+ }
if v := find.RowStatus; v != nil {
where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, *v)
}