Skip to content

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

Store push event as task payload

天舟5e3710176f1a
diff --git a/backend/api/task.go b/backend/api/task.go
index 0dccead313..66cdd2d9c6 100644
--- a/backend/api/task.go
+++ b/backend/api/task.go
@@ -45,8 +45,9 @@ const (
// So we annotate with json tag using camelCase naming which is consistent with normal
// json naming convention
type TaskDatabaseSchemaUpdatePayload struct {
- Statement string `json:"statement,omitempty"`
- RollbackStatement string `json:"rollbackStatement,omitempty"`
+ Statement string `json:"statement,omitempty"`
+ RollbackStatement string `json:"rollbackStatement,omitempty"`
+ VCSPushEvent VCSPushEvent `json:"pushEvent,omitempty"`
}
type Task struct {
@@ -94,7 +95,8 @@ type TaskCreate struct {
Type TaskType `jsonapi:"attr,type"`
Statement string `jsonapi:"attr,statement"`
RollbackStatement string `jsonapi:"attr,rollbackStatement"`
- // Payload is dirived from Statement, RollbackStatement
+ VCSPushEvent *VCSPushEvent
+ // Payload is dirived from Statement, RollbackStatement, VCSPushEvent
Payload string
}
diff --git a/backend/api/vcs.go b/backend/api/vcs.go
index 43d22f9353..2151c821fc 100644
--- a/backend/api/vcs.go
+++ b/backend/api/vcs.go
@@ -97,6 +97,28 @@ type VCSDelete struct {
DeleterId int
}
+// These payload types are only used when marshalling to the json format for saving into the database.
+// So we annotate with json tag using camelCase naming which is consistent with normal
+// json naming convention
+type VCSFileCommit struct {
+ ID string `json:"id"`
+ Title string `json:"title"`
+ Message string `json:"message"`
+ CreatedTs int64 `json:"createdTs"`
+ URL string `json:"url"`
+ AuthorName string `json:"authorName"`
+ Added string `json:"added"`
+}
+
+type VCSPushEvent struct {
+ Ref string `json:"ref"`
+ RepositoryID string `json:"repoId"`
+ RepositoryURL string `json:"repoUrl"`
+ RepositoryFullPath string `json:"repoFullPath"`
+ AuthorName string `json:"authorName"`
+ FileCommit VCSFileCommit `json:"fileCommit"`
+}
+
type VCSService interface {
CreateVCS(ctx context.Context, create *VCSCreate) (*VCS, error)
FindVCSList(ctx context.Context, find *VCSFind) ([]*VCS, error)
diff --git a/backend/external/gitlab/gitlab.go b/backend/external/gitlab/gitlab.go
index a99c44e519..befb0a2297 100644
--- a/backend/external/gitlab/gitlab.go
+++ b/backend/external/gitlab/gitlab.go
@@ -40,21 +40,19 @@ type WebhookCommitAuthor struct {
}
type WebhookCommit struct {
- ID string `json:"id"`
- Title string `json:"title"`
- Message string `json:"message"`
- Timestamp string `json:"timestamp"`
- URL string `json:"url"`
- Author WebhookCommitAuthor `json:"author"`
- AddedList []string `json:"added"`
- ModifiedList []string `json:"modified"`
- RemovedList []string `json:"removed"`
+ ID string `json:"id"`
+ Title string `json:"title"`
+ Message string `json:"message"`
+ Timestamp string `json:"timestamp"`
+ URL string `json:"url"`
+ Author WebhookCommitAuthor `json:"author"`
+ AddedList []string `json:"added"`
}
type WebhookPushEvent struct {
ObjectKind GitLabWebhookType `json:"object_kind"`
Ref string `json:"ref"`
- Username string `json:"user_name"`
+ AuthorName string `json:"user_name"`
Project WebhookProject `json:"project"`
CommitList []WebhookCommit `json:"commits"`
}
diff --git a/backend/server/issue.go b/backend/server/issue.go
index a3d6dfe720..3a52a2f90b 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -386,6 +386,9 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
if taskCreate.RollbackStatement != "" {
payload.RollbackStatement = taskCreate.RollbackStatement
}
+ if taskCreate.VCSPushEvent != nil {
+ payload.VCSPushEvent = *taskCreate.VCSPushEvent
+ }
bytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to create task for issue. Error %w", err)
diff --git a/backend/server/webhook.go b/backend/server/webhook.go
index 25cee0d82e..4b5dfeb720 100644
--- a/backend/server/webhook.go
+++ b/backend/server/webhook.go
@@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "time"
"github.com/bytebase/bytebase"
"github.com/bytebase/bytebase/api"
@@ -118,13 +119,34 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}
// Compose the new issue
+ createdTime, err := time.Parse(time.RFC3339, commit.Timestamp)
+ if err != nil {
+ s.l.Warn("Failed to parse timestamp. Ignored", zap.String("filename", filename), zap.Error(err))
+ }
+ vcsPushEvent := api.VCSPushEvent{
+ Ref: pushEvent.Ref,
+ RepositoryID: strconv.Itoa(pushEvent.Project.ID),
+ RepositoryURL: pushEvent.Project.WebURL,
+ RepositoryFullPath: pushEvent.Project.FullPath,
+ AuthorName: pushEvent.AuthorName,
+ FileCommit: api.VCSFileCommit{
+ ID: commit.ID,
+ Title: commit.Title,
+ Message: commit.Message,
+ CreatedTs: createdTime.Unix(),
+ URL: commit.URL,
+ AuthorName: commit.Author.Name,
+ Added: added,
+ },
+ }
task := &api.TaskCreate{
- InstanceId: database.InstanceId,
- DatabaseId: database.ID,
- Name: description,
- Status: "PENDING",
- Type: api.TaskDatabaseSchemaUpdate,
- Statement: string(b),
+ InstanceId: database.InstanceId,
+ DatabaseId: database.ID,
+ Name: description,
+ Status: "PENDING",
+ Type: api.TaskDatabaseSchemaUpdate,
+ Statement: string(b),
+ VCSPushEvent: &vcsPushEvent,
}
stage := &api.StageCreate{
EnvironmentId: database.Instance.EnvironmentId,