Skip to content

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

Add a file commit activity after committing the latest schema after migration

天舟570ef5e80e03
diff --git a/api/activity.go b/api/activity.go
index d5a352e59d..1310e2894a 100644
--- a/api/activity.go
+++ b/api/activity.go
@@ -17,6 +17,7 @@ const (
ActivityIssueFieldUpdate ActivityType = "bb.issue.field.update"
ActivityIssueStatusUpdate ActivityType = "bb.issue.status.update"
ActivityPipelineTaskStatusUpdate ActivityType = "bb.pipeline.task.status.update"
+ ActivityPipelineTaskFileCommit ActivityType = "bb.pipeline.task.file.commit"
// Member related
ActivityMemberCreate ActivityType = "bb.member.create"
@@ -44,6 +45,8 @@ func (e ActivityType) String() string {
return "bb.issue.status.update"
case ActivityPipelineTaskStatusUpdate:
return "bb.pipeline.task.status.update"
+ case ActivityPipelineTaskFileCommit:
+ return "bb.pipeline.task.file.commit"
case ActivityMemberCreate:
return "bb.member.create"
case ActivityMemberRoleUpdate:
@@ -126,6 +129,15 @@ type ActivityPipelineTaskStatusUpdatePayload struct {
TaskName string `json:"taskName"`
}
+type ActivityPipelineTaskFileCommitPayload struct {
+ TaskId int `json:"taskId"`
+ VCSInstanceURL string `json:"vcsInstanceUrl,omitempty"`
+ RepositoryFullPath string `json:"repositoryFullPath,omitempty"`
+ Branch string `json:"branch,omitempty"`
+ FilePath string `json:"filePath,omitempty"`
+ CommitId string `json:"commitId,omitempty"`
+}
+
type ActivityMemberCreatePayload struct {
PrincipalId int `json:"principalId"`
PrincipalName string `json:"principalName"`
diff --git a/frontend/src/components/IssueActivityPanel.vue b/frontend/src/components/IssueActivityPanel.vue
index 69e76841ec..9ecbd70834 100644
--- a/frontend/src/components/IssueActivityPanel.vue
+++ b/frontend/src/components/IssueActivityPanel.vue
@@ -279,6 +279,37 @@
</div>
</div>
</template>
+ <template v-else-if="actionIcon(activity) == 'commit'">
+ <div class="relative pl-0.5">
+ <div
+ class="
+ w-7
+ h-7
+ bg-control-bg
+ rounded-full
+ ring-4 ring-white
+ flex
+ items-center
+ justify-center
+ "
+ >
+ <svg
+ class="w-5 h-5 text-control"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4"
+ ></path>
+ </svg>
+ </div>
+ </div>
+ </template>
<div class="ml-3 min-w-0 flex-1">
<div class="min-w-0 flex-1 pt-1 flex justify-between">
<div class="text-sm text-control-light">
@@ -461,6 +492,30 @@
<template v-else>
{{ activity.comment }}
</template>
+ <template
+ v-if="activity.type == 'bb.pipeline.task.file.commit'"
+ >
+ <a
+ :href="`${activity.payload.vcsInstanceUrl}/${activity.payload.repositoryFullPath}/-/commit/${activity.payload.commitId}`"
+ target="__blank"
+ class="normal-link flex flex-row items-center"
+ >View commit
+ <svg
+ class="w-4 h-4"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
+ ></path>
+ </svg>
+ </a>
+ </template>
</div>
</div>
</div>
@@ -573,6 +628,7 @@ import {
SYSTEM_BOT_ID,
ActivityCreate,
IssueSubscriber,
+ ActivityTaskFileCommitPayload,
} from "../types";
import {
findTaskById,
@@ -604,7 +660,8 @@ type ActionIconType =
| "approve"
| "cancel"
| "fail"
- | "complete";
+ | "complete"
+ | "commit";
export default {
name: "IssueActivityPanel",
@@ -792,6 +849,8 @@ export default {
return "fail";
}
}
+ } else if (activity.type == "bb.pipeline.task.file.commit") {
+ return "commit";
}
return activity.creator.id == SYSTEM_BOT_ID ? "system" : "avatar";
@@ -870,6 +929,10 @@ export default {
}
return str;
}
+ case "bb.pipeline.task.file.commit": {
+ const payload = activity.payload as ActivityTaskFileCommitPayload;
+ return `committed ${payload.filePath} to ${payload.branch}@${payload.repositoryFullPath}`;
+ }
}
return "";
};
diff --git a/frontend/src/types/activity.ts b/frontend/src/types/activity.ts
index 8597f27126..f28b9f813f 100644
--- a/frontend/src/types/activity.ts
+++ b/frontend/src/types/activity.ts
@@ -11,7 +11,8 @@ export type IssueActivityType =
| "bb.issue.comment.create"
| "bb.issue.field.update"
| "bb.issue.status.update"
- | "bb.pipeline.task.status.update";
+ | "bb.pipeline.task.status.update"
+ | "bb.pipeline.task.file.commit";
export type MemberActivityType =
| "bb.member.create"
@@ -43,6 +44,8 @@ export function activityName(type: ActivityType): string {
return "Update issue status";
case "bb.pipeline.task.status.update":
return "Update issue task status";
+ case "bb.pipeline.task.file.commit":
+ return "Commit file";
case "bb.member.create":
return "Create member";
case "bb.member.role.update":
@@ -96,6 +99,15 @@ export type ActivityTaskStatusUpdatePayload = {
taskName: string;
};
+export type ActivityTaskFileCommitPayload = {
+ taskId: TaskId;
+ vcsInstanceUrl: string;
+ repositoryFullPath: string;
+ branch: string;
+ filePath: string;
+ commitId: string;
+};
+
export type ActivityMemberCreatePayload = {
principalId: PrincipalId;
principalName: string;
@@ -136,6 +148,7 @@ export type ActionPayloadType =
| ActivityIssueFieldUpdatePayload
| ActivityIssueStatusUpdatePayload
| ActivityTaskStatusUpdatePayload
+ | ActivityTaskFileCommitPayload
| ActivityMemberCreatePayload
| ActivityMemberRoleUpdatePayload
| ActivityMemberActivateDeactivatePayload
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index adddf51750..9f8b9a9669 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -160,30 +160,79 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
return true, "", fmt.Errorf("failed to sync schema file %s after applying migration %s to %q", latestSchemaFile, mi.Version, databaseName)
}
+ // Writes back the latest schema file to the same branch as the push event.
+ // Ref format refs/heads/<<branch>>
+ refComponents := strings.Split(payload.VCSPushEvent.Ref, "/")
+ branch := refComponents[len(refComponents)-1]
+
bytebaseURL := ""
if issue != nil {
bytebaseURL = fmt.Sprintf("%s:%d/issue/%s?stage=%d", server.frontendHost, server.frontendPort, api.IssueSlug(issue), task.StageId)
}
- if err := writeBackLatestSchema(server, repository, payload.VCSPushEvent, mi, latestSchemaFile, schema, bytebaseURL); err != nil {
+ commitId, err := writeBackLatestSchema(server, repository, payload.VCSPushEvent, mi, branch, latestSchemaFile, schema, bytebaseURL)
+ if err != nil {
return true, "", err
}
+
+ // Create file commit activity
+ {
+ payload, err := json.Marshal(api.ActivityPipelineTaskFileCommitPayload{
+ TaskId: task.ID,
+ VCSInstanceURL: repository.VCS.InstanceURL,
+ RepositoryFullPath: payload.VCSPushEvent.RepositoryFullPath,
+ Branch: branch,
+ FilePath: latestSchemaFile,
+ CommitId: commitId,
+ })
+ if err != nil {
+ exec.l.Error("Failed to marshal file commit activity after writing back the latest schema",
+ zap.Int("task_id", task.ID),
+ zap.String("repository", repository.WebURL),
+ zap.String("file_path", latestSchemaFile),
+ zap.Error(err),
+ )
+ }
+
+ containerId := task.PipelineId
+ if issue != nil {
+ containerId = issue.ID
+ }
+ activityCreate := &api.ActivityCreate{
+ CreatorId: task.CreatorId,
+ ContainerId: containerId,
+ Type: api.ActivityPipelineTaskFileCommit,
+ Level: api.ACTIVITY_INFO,
+ Comment: fmt.Sprintf("Committed the latest schema after applying migration version %s to %q.",
+ mi.Version,
+ mi.Database,
+ ),
+ Payload: string(payload),
+ }
+
+ _, err = server.ActivityManager.CreateActivity(context.Background(), activityCreate, &ActivityMeta{})
+ if err != nil {
+ exec.l.Error("Failed to create file commit activity after writing back the latest schema",
+ zap.Int("task_id", task.ID),
+ zap.String("repository", repository.WebURL),
+ zap.String("file_path", latestSchemaFile),
+ zap.Error(err),
+ )
+ }
+ }
}
- detail = fmt.Sprintf("Applied migration version %s to database %q", mi.Version, databaseName)
+ detail = fmt.Sprintf("Applied migration version %s to database %q.", mi.Version, databaseName)
if mi.Type == db.Baseline {
- detail = fmt.Sprintf("Established baseline version %s for database %q", mi.Version, databaseName)
+ detail = fmt.Sprintf("Established baseline version %s for database %q.", mi.Version, databaseName)
}
return true, detail, nil
}
// Writes back the latest schema to the repository after migration
-func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent *common.VCSPushEvent, mi *db.MigrationInfo, latestSchemaFile string, schema string, bytebaseURL string) error {
- // Writes back the latest schema file to the same branch as the push event.
- // Ref format refs/heads/<<branch>>
- refComponents := strings.Split(pushEvent.Ref, "/")
- branch := refComponents[len(refComponents)-1]
+// Returns the commit id on success.
+func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent *common.VCSPushEvent, mi *db.MigrationInfo, branch string, latestSchemaFile string, schema string, bytebaseURL string) (string, error) {
filePath := fmt.Sprintf("projects/%s/repository/files/%s", repository.ExternalId, url.QueryEscape(latestSchemaFile))
getFilePath := filePath + "?ref=" + url.QueryEscape(branch)
@@ -193,14 +242,14 @@ func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent
repository.AccessToken,
)
if err != nil {
- return fmt.Errorf("failed to fetch latest schema file from %s, err: %w", repository.VCS.InstanceURL, err)
+ return "", fmt.Errorf("failed to fetch latest schema file from %s, err: %w", repository.VCS.InstanceURL, err)
}
defer getResp.Body.Close()
createSchemaFile := false
verb := "Update"
if getResp.StatusCode >= 300 && getResp.StatusCode != 404 {
- return fmt.Errorf("failed to fetch latest schema file from %s, status code: %d",
+ return "", fmt.Errorf("failed to fetch latest schema file from %s, status code: %d",
repository.VCS.InstanceURL,
getResp.StatusCode,
)
@@ -228,17 +277,17 @@ func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent
if createSchemaFile {
body, err := json.Marshal(schemaFileCommit)
if err != nil {
- return fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
+ return "", fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
}
resp, err := gitlab.POST(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
if err != nil {
- return fmt.Errorf("failed to create file %s after applying migration %s to %q, err: %w", filePath, mi.Version, mi.Database, err)
+ return "", fmt.Errorf("failed to create file %s after applying migration %s to %q, err: %w", filePath, mi.Version, mi.Database, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
- return fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
+ return "", fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
filePath,
mi.Version,
mi.Database,
@@ -248,23 +297,23 @@ func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent
} else {
file := &gitlab.File{}
if err := json.NewDecoder(getResp.Body).Decode(file); err != nil {
- return fmt.Errorf("failed to unmarshal file response %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
+ return "", fmt.Errorf("failed to unmarshal file response %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
}
schemaFileCommit.LastCommitId = file.LastCommitId
body, err := json.Marshal(schemaFileCommit)
if err != nil {
- return fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
+ return "", fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
}
resp, err := gitlab.PUT(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
if err != nil {
- return fmt.Errorf("failed to create file %s after applying migration %s to %q, error: %w", filePath, mi.Version, mi.Database, err)
+ return "", fmt.Errorf("failed to create file %s after applying migration %s to %q, error: %w", filePath, mi.Version, mi.Database, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
- return fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
+ return "", fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
filePath,
mi.Version,
mi.Database,
@@ -272,5 +321,28 @@ func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent
)
}
}
- return nil
+
+ // GitLab API doesn't return the commit on write, so we have to call GET again
+ getResp, err = gitlab.GET(
+ repository.VCS.InstanceURL,
+ getFilePath,
+ repository.AccessToken,
+ )
+ if err != nil {
+ return "", fmt.Errorf("failed to fetch latest schema file after update, VCS instance: %s, err: %w", repository.VCS.InstanceURL, err)
+ }
+ defer getResp.Body.Close()
+
+ if getResp.StatusCode >= 300 && getResp.StatusCode != 404 {
+ return "", fmt.Errorf("failed to fetch latest schema file after update, VCS instance: %s, status code: %d",
+ repository.VCS.InstanceURL,
+ getResp.StatusCode,
+ )
+ }
+
+ file := &gitlab.File{}
+ if err := json.NewDecoder(getResp.Body).Decode(file); err != nil {
+ return "", fmt.Errorf("failed to unmarshal file response %s after update, VCS instance: %s", filePath, repository.VCS.InstanceURL)
+ }
+ return file.LastCommitId, nil
}