Skip to content

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

chore: create audit log for push event (#11786)

p0nycb5e3a248fd0
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 467fec3a3e..77f0f92451 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -182,12 +182,14 @@ func validateGitHubWebhookSignature256(signature, key string, body []byte) (bool
func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.ProjectMessage, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, prInfo *pullRequestInfo) (*v1pb.Issue, error) {
creatorID := api.SystemBotID
+ creatorName := common.FormatUserEmail(api.SystemBotEmail)
user, err := s.store.GetUser(ctx, &store.FindUserMessage{Email: &prInfo.email})
if err != nil {
slog.Error("failed to find user by email", slog.String("email", prInfo.email), log.BBError(err))
}
if user != nil {
creatorID = user.ID
+ creatorName = common.FormatUserEmail(user.Email)
}
var sheets []int
@@ -265,6 +267,19 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
return nil, errors.Wrapf(err, "failed to create activity payload")
}
+ if err := s.store.CreateAuditLog(ctx, &storepb.AuditLog{
+ Parent: project.GetName(),
+ Method: store.AuditLogMethodProjectRepositoryPush.String(),
+ Resource: issue.Name,
+ User: creatorName,
+ Severity: storepb.AuditLog_INFO,
+ Request: "",
+ Response: "",
+ Status: nil,
+ }); err != nil {
+ slog.Warn("failed to create audit log after creating issue from push event", "issueUID", issueUID)
+ }
+
activityCreate := &store.ActivityMessage{
CreatorUID: creatorID,
ResourceContainer: project.GetName(),
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index 7c41ad00fa..6d78a2f3b6 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -584,6 +584,10 @@ func FormatSheet(projectID string, sheetUID int) string {
return fmt.Sprintf("%s%s/%s%d", ProjectNamePrefix, projectID, SheetIDPrefix, sheetUID)
}
+func FormatIssue(projectID string, issueUID int) string {
+ return fmt.Sprintf("%s%s/%s%d", ProjectNamePrefix, projectID, IssueNamePrefix, issueUID)
+}
+
func FormatTask(projectID string, pipelineUID, stageUID, taskUID int) string {
return fmt.Sprintf("%s%s/%s%d/%s%d/%s%d", ProjectNamePrefix, projectID, RolloutPrefix, pipelineUID, StagePrefix, stageUID, TaskPrefix, taskUID)
}
diff --git a/backend/store/audit_log.go b/backend/store/audit_log.go
index 7ba149b4f0..24dd3d3d8d 100644
--- a/backend/store/audit_log.go
+++ b/backend/store/audit_log.go
@@ -12,6 +12,15 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
+type AuditLogMethod string
+
+// The methods other than v1 api.
+const AuditLogMethodProjectRepositoryPush AuditLogMethod = "bb.project.repository.push"
+
+func (m AuditLogMethod) String() string {
+ return string(m)
+}
+
type AuditLog struct {
ID int
CreatedTs int64