Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add AuthorName and AuthorEmail for GitOps (#4978)
rebelice07798b1bf6b0
diff --git a/backend/plugin/vcs/github/github.go b/backend/plugin/vcs/github/github.goindex 8bbeb45ee5..7c594cb597 100644--- a/backend/plugin/vcs/github/github.go+++ b/backend/plugin/vcs/github/github.go@@ -240,8 +240,9 @@ func (p *Provider) TryLogin(ctx context.Context, oauthCtx common.OauthContext, itype CommitAuthor struct {// Date expects corresponding JSON value is a string in RFC 3339 format,// see https://pkg.go.dev/time#Time.MarshalJSON.- Date time.Time `json:"date"`- Name string `json:"name"`+ Date time.Time `json:"date"`+ Name string `json:"name"`+ Email string `json:"email"`}// Commit represents a GitHub API response for a commit.@@ -252,10 +253,11 @@ type Commit struct {// FileCommit represents a GitHub API request for committing a file.type FileCommit struct {- Message string `json:"message"`- Content string `json:"content"`- SHA string `json:"sha,omitempty"`- Branch string `json:"branch,omitempty"`+ Message string `json:"message"`+ Content string `json:"content"`+ SHA string `json:"sha,omitempty"`+ Branch string `json:"branch,omitempty"`+ Author CommitAuthor `json:"author,omitempty"`}// FetchCommitByID fetches the commit data by its ID from the repository.@@ -574,14 +576,19 @@ func (p *Provider) FetchRepositoryFileList(ctx context.Context, oauthCtx common.//// Docs: https://docs.github.com/en/rest/repos/contents#create-or-update-file-contentsfunc (p *Provider) CreateFile(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, filePath string, fileCommitCreate vcs.FileCommitCreate) error {- body, err := json.Marshal(- FileCommit{- Message: fileCommitCreate.CommitMessage,- Content: base64.StdEncoding.EncodeToString([]byte(fileCommitCreate.Content)),- Branch: fileCommitCreate.Branch,- SHA: fileCommitCreate.LastCommitID,- },- )+ fileCommit := FileCommit{+ Message: fileCommitCreate.CommitMessage,+ Content: base64.StdEncoding.EncodeToString([]byte(fileCommitCreate.Content)),+ Branch: fileCommitCreate.Branch,+ SHA: fileCommitCreate.LastCommitID,+ }+ if fileCommitCreate.AuthorName != "" && fileCommitCreate.AuthorEmail != "" {+ fileCommit.Author = CommitAuthor{+ Name: fileCommitCreate.AuthorName,+ Email: fileCommitCreate.AuthorEmail,+ }+ }+ body, err := json.Marshal(fileCommit)if err != nil {return errors.Wrap(err, "marshal file commit")}diff --git a/backend/plugin/vcs/github/github_test.go b/backend/plugin/vcs/github/github_test.goindex d10593ce42..f1f443336a 100644--- a/backend/plugin/vcs/github/github_test.go+++ b/backend/plugin/vcs/github/github_test.go@@ -371,7 +371,7 @@ func TestProvider_CreateFile(t *testing.T) {body, err := io.ReadAll(r.Body)require.NoError(t, err)- wantBody := `{"message":"my commit message","content":"bXkgbmV3IGZpbGUgY29udGVudHM=","branch":"master"}`+ wantBody := `{"message":"my commit message","content":"bXkgbmV3IGZpbGUgY29udGVudHM=","branch":"master","author":{"date":"0001-01-01T00:00:00Z","name":"","email":""}}`assert.Equal(t, wantBody, string(body))return &http.Response{StatusCode: http.StatusOK,@@ -456,7 +456,7 @@ func TestProvider_OverwriteFile(t *testing.T) {body, err := io.ReadAll(r.Body)require.NoError(t, err)- wantBody := `{"message":"update file","content":"bXkgbmV3IGZpbGUgY29udGVudHM=","sha":"7638417db6d59f3c431d3e1f261cc637155684cd","branch":"master"}`+ wantBody := `{"message":"update file","content":"bXkgbmV3IGZpbGUgY29udGVudHM=","sha":"7638417db6d59f3c431d3e1f261cc637155684cd","branch":"master","author":{"date":"0001-01-01T00:00:00Z","name":"","email":""}}`assert.Equal(t, wantBody, string(body))return &http.Response{StatusCode: http.StatusOK,diff --git a/backend/plugin/vcs/gitlab/gitlab.go b/backend/plugin/vcs/gitlab/gitlab.goindex 7456125bdf..f0437909cd 100644--- a/backend/plugin/vcs/gitlab/gitlab.go+++ b/backend/plugin/vcs/gitlab/gitlab.go@@ -110,6 +110,8 @@ type FileCommit struct {Content string `json:"content"`CommitMessage string `json:"commit_message"`LastCommitID string `json:"last_commit_id,omitempty"`+ AuthorName string `json:"author_name,omitempty"`+ AuthorEmail string `json:"author_email,omitempty"`}// RepositoryTreeNode represents a GitLab API response for a repository tree@@ -543,6 +545,8 @@ func (p *Provider) CreateFile(ctx context.Context, oauthCtx common.OauthContext,Branch: fileCommitCreate.Branch,CommitMessage: fileCommitCreate.CommitMessage,Content: fileCommitCreate.Content,+ AuthorName: fileCommitCreate.AuthorName,+ AuthorEmail: fileCommitCreate.AuthorEmail,},)if err != nil {@@ -592,6 +596,8 @@ func (p *Provider) OverwriteFile(ctx context.Context, oauthCtx common.OauthConteContent: fileCommitCreate.Content,CommitMessage: fileCommitCreate.CommitMessage,LastCommitID: fileCommitCreate.LastCommitID,+ AuthorName: fileCommitCreate.AuthorName,+ AuthorEmail: fileCommitCreate.AuthorEmail,},)if err != nil {diff --git a/backend/plugin/vcs/vcs.go b/backend/plugin/vcs/vcs.goindex b9a5cf644e..a4de5a5043 100644--- a/backend/plugin/vcs/vcs.go+++ b/backend/plugin/vcs/vcs.go@@ -25,6 +25,11 @@ const (// SQLReviewAPISecretName is the api secret name used in GitHub action or GitLab CI workflow.SQLReviewAPISecretName = "SQL_REVIEW_API_SECRET"++ // BytebaseAuthorName is the author name of bytebase.+ BytebaseAuthorName = "Bytebase"+ // BytebaseAuthorEmail is the author email of bytebase.+ BytebaseAuthorEmail = "[email protected]")// OAuthToken is the API message for OAuthToken.@@ -73,6 +78,8 @@ type FileCommitCreate struct {Content stringCommitMessage stringLastCommitID string+ AuthorName string+ AuthorEmail string}// FileMeta records the file metadata.diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex 11f77d7cb3..e4ed3ede68 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -589,6 +589,8 @@ func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *Branch: writebackBranch,CommitMessage: commitMessage,Content: schema,+ AuthorName: vcsPlugin.BytebaseAuthorName,+ AuthorEmail: vcsPlugin.BytebaseAuthorEmail,}if createSchemaFile {log.Debug("Create latest schema file",diff --git a/backend/server/webhook.go b/backend/server/webhook.goindex 59b081ff03..80fa463ac6 100644--- a/backend/server/webhook.go+++ b/backend/server/webhook.go@@ -63,6 +63,21 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {}repositoryID := fmt.Sprintf("%v", pushEvent.Project.ID)+ nonBytebaseCommitList := filterGitLabBytebaseCommit(pushEvent.CommitList)+ if len(nonBytebaseCommitList) == 0 {+ var commitList []string+ for _, commit := range pushEvent.CommitList {+ commitList = append(commitList, commit.ID)+ }+ log.Debug("all commits are created by Bytebase",+ zap.String("repoURL", pushEvent.Project.WebURL),+ zap.String("repoName", pushEvent.Project.FullPath),+ zap.String("commits", strings.Join(commitList, ", ")),+ )+ return c.String(http.StatusOK, "OK")+ }+ pushEvent.CommitList = nonBytebaseCommitList+filter := func(repo *api.Repository) (bool, error) {if c.Request().Header.Get("X-Gitlab-Token") != repo.WebhookSecretToken {return false, nil@@ -116,6 +131,21 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {}repositoryID := pushEvent.Repository.FullName+ nonBytebaseCommitList := filterGitHubBytebaseCommit(pushEvent.Commits)+ if len(nonBytebaseCommitList) == 0 {+ var commitList []string+ for _, commit := range pushEvent.Commits {+ commitList = append(commitList, commit.ID)+ }+ log.Debug("all commits are created by Bytebase",+ zap.String("repoURL", pushEvent.Repository.HTMLURL),+ zap.String("repoName", pushEvent.Repository.FullName),+ zap.String("commits", strings.Join(commitList, ", ")),+ )+ return c.String(http.StatusOK, "OK")+ }+ pushEvent.Commits = nonBytebaseCommitList+filter := func(repo *api.Repository) (bool, error) {ok, err := validateGitHubWebhookSignature256(c.Request().Header.Get("X-Hub-Signature-256"), repo.WebhookSecretToken, body)if err != nil {@@ -1346,3 +1376,25 @@ func convertSQLAdviceToGitHubActionResult(adviceMap map[string][]advisor.Advice)Content: messageList,}}++func filterGitHubBytebaseCommit(list []github.WebhookCommit) []github.WebhookCommit {+ var result []github.WebhookCommit+ for _, commit := range list {+ if commit.Author.Name == vcs.BytebaseAuthorName && commit.Author.Email == vcs.BytebaseAuthorEmail {+ continue+ }+ result = append(result, commit)+ }+ return result+}++func filterGitLabBytebaseCommit(list []gitlab.WebhookCommit) []gitlab.WebhookCommit {+ var result []gitlab.WebhookCommit+ for _, commit := range list {+ if commit.Author.Name == vcs.BytebaseAuthorName && commit.Author.Email == vcs.BytebaseAuthorEmail {+ continue+ }+ result = append(result, commit)+ }+ return result+}