Skip to content

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

fix: VCS workflow, dedup issue creation due to merge request contains duplicate commits (#1757)

tianzhou3e6d56965b26
diff --git a/server/webhook.go b/server/webhook.go
index 182743ff1e..ba47210d8e 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -72,164 +72,156 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
zap.String("project", repo.Project.Name),
)
+ distinctFileList := dedupMigrationFilesFromCommitList(pushEvent.CommitList)
createdMessageList := []string{}
- for _, commit := range pushEvent.CommitList {
- log.Debug("Processing commit...",
- zap.String("id", common.EscapeForLogging(commit.ID)),
- zap.String("title", common.EscapeForLogging(commit.Title)),
+ for _, item := range distinctFileList {
+ commit := item.commit
+ added := item.fileName
+ addedEscaped := common.EscapeForLogging(added)
+ log.Debug("Processing added file...",
+ zap.String("file", addedEscaped),
+ zap.String("commit", common.EscapeForLogging(commit.ID)),
)
- for _, added := range commit.AddedList {
- addedEscaped := common.EscapeForLogging(added)
- log.Debug("Processing added file...",
- zap.String("file", addedEscaped),
- )
-
- if !strings.HasPrefix(addedEscaped, repo.BaseDirectory) {
- log.Debug("Ignored committed file, not under base directory.", zap.String("file", addedEscaped), zap.String("base_directory", repo.BaseDirectory))
- continue
- }
+ if !strings.HasPrefix(addedEscaped, repo.BaseDirectory) {
+ log.Debug("Ignored committed file, not under base directory.", zap.String("file", addedEscaped), zap.String("base_directory", repo.BaseDirectory))
+ continue
+ }
- createdTime, err := time.Parse(time.RFC3339, commit.Timestamp)
- if err != nil {
- log.Warn("Ignored committed file, failed to parse commit timestamp.", zap.String("file", addedEscaped), zap.String("timestamp", common.EscapeForLogging(commit.Timestamp)), zap.Error(err))
- }
+ // Ignore the schema file we auto generated to the repository.
+ if isSkipGeneratedSchemaFile(repo, addedEscaped) {
+ log.Debug("Ignored generated latest schema file.", zap.String("file", addedEscaped))
+ continue
+ }
- // Ignore the schema file we auto generated to the repository.
- if isSkipGeneratedSchemaFile(repo, addedEscaped) {
- log.Debug("Ignored generated latest schema file.", zap.String("file", addedEscaped))
- continue
- }
+ vcsPushEvent := vcs.PushEvent{
+ VCSType: repo.VCS.Type,
+ BaseDirectory: repo.BaseDirectory,
+ Ref: pushEvent.Ref,
+ RepositoryID: strconv.Itoa(pushEvent.Project.ID),
+ RepositoryURL: pushEvent.Project.WebURL,
+ RepositoryFullPath: pushEvent.Project.FullPath,
+ AuthorName: pushEvent.AuthorName,
+ FileCommit: vcs.FileCommit{
+ ID: commit.ID,
+ Title: commit.Title,
+ Message: commit.Message,
+ CreatedTs: item.createdTime.Unix(),
+ URL: commit.URL,
+ AuthorName: commit.Author.Name,
+ Added: addedEscaped,
+ },
+ }
- vcsPushEvent := vcs.PushEvent{
- VCSType: repo.VCS.Type,
- BaseDirectory: repo.BaseDirectory,
- Ref: pushEvent.Ref,
- RepositoryID: strconv.Itoa(pushEvent.Project.ID),
- RepositoryURL: pushEvent.Project.WebURL,
- RepositoryFullPath: pushEvent.Project.FullPath,
- AuthorName: pushEvent.AuthorName,
- FileCommit: vcs.FileCommit{
- ID: commit.ID,
- Title: commit.Title,
- Message: commit.Message,
- CreatedTs: createdTime.Unix(),
- URL: commit.URL,
- AuthorName: commit.Author.Name,
- Added: addedEscaped,
- },
+ // Create a WARNING project activity if committed file is ignored
+ var createIgnoredFileActivity = func(err error) {
+ log.Warn("Ignored committed file", zap.String("file", addedEscaped), zap.Error(err))
+ bytes, marshalErr := json.Marshal(api.ActivityProjectRepositoryPushPayload{
+ VCSPushEvent: vcsPushEvent,
+ })
+ if marshalErr != nil {
+ log.Warn("Failed to construct project activity payload to record ignored repository committed file", zap.Error(marshalErr))
+ return
}
- // Create a WARNING project activity if committed file is ignored
- var createIgnoredFileActivity = func(err error) {
- log.Warn("Ignored committed file", zap.String("file", addedEscaped), zap.Error(err))
- bytes, marshalErr := json.Marshal(api.ActivityProjectRepositoryPushPayload{
- VCSPushEvent: vcsPushEvent,
- })
- if marshalErr != nil {
- log.Warn("Failed to construct project activity payload to record ignored repository committed file", zap.Error(marshalErr))
- return
- }
-
- activityCreate := &api.ActivityCreate{
- CreatorID: api.SystemBotID,
- ContainerID: repo.ProjectID,
- Type: api.ActivityProjectRepositoryPush,
- Level: api.ActivityWarn,
- Comment: fmt.Sprintf("Ignored committed file %q, %s.", addedEscaped, err.Error()),
- Payload: string(bytes),
- }
- _, err = s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{})
- if err != nil {
- log.Warn("Failed to create project activity to record ignored repository committed file", zap.Error(err))
- }
+ activityCreate := &api.ActivityCreate{
+ CreatorID: api.SystemBotID,
+ ContainerID: repo.ProjectID,
+ Type: api.ActivityProjectRepositoryPush,
+ Level: api.ActivityWarn,
+ Comment: fmt.Sprintf("Ignored committed file %q, %s.", addedEscaped, err.Error()),
+ Payload: string(bytes),
}
-
- mi, err := db.ParseMigrationInfo(addedEscaped, filepath.Join(repo.BaseDirectory, repo.FilePathTemplate))
+ _, err = s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{})
if err != nil {
- createIgnoredFileActivity(err)
- continue
+ log.Warn("Failed to create project activity to record ignored repository committed file", zap.Error(err))
}
+ }
- // Retrieve sql by reading the file content
- content, err := vcs.Get(vcs.GitLabSelfHost, vcs.ProviderConfig{}).ReadFileContent(
- ctx,
- common.OauthContext{
- ClientID: repo.VCS.ApplicationID,
- ClientSecret: repo.VCS.Secret,
- AccessToken: repo.AccessToken,
- RefreshToken: repo.RefreshToken,
- Refresher: s.refreshToken(ctx, repo.ID),
- },
- repo.VCS.InstanceURL,
- repo.ExternalID,
- addedEscaped,
- commit.ID,
- )
- if err != nil {
- createIgnoredFileActivity(err)
- continue
- }
+ mi, err := db.ParseMigrationInfo(addedEscaped, filepath.Join(repo.BaseDirectory, repo.FilePathTemplate))
+ if err != nil {
+ createIgnoredFileActivity(err)
+ continue
+ }
- // Create schema update issue.
- var createContext string
- if repo.Project.TenantMode == api.TenantModeTenant {
- if !s.feature(api.FeatureMultiTenancy) {
- return echo.NewHTTPError(http.StatusForbidden, api.FeatureMultiTenancy.AccessErrorMessage())
- }
- createContext, err = s.createTenantSchemaUpdateIssue(ctx, repo, mi, vcsPushEvent, commit, addedEscaped, content)
- } else {
- createContext, err = s.createSchemaUpdateIssue(ctx, repo, mi, vcsPushEvent, commit, addedEscaped, content)
- }
- if err != nil {
- createIgnoredFileActivity(err)
- continue
- }
+ // Retrieve sql by reading the file content
+ content, err := vcs.Get(vcs.GitLabSelfHost, vcs.ProviderConfig{}).ReadFileContent(
+ ctx,
+ common.OauthContext{
+ ClientID: repo.VCS.ApplicationID,
+ ClientSecret: repo.VCS.Secret,
+ AccessToken: repo.AccessToken,
+ RefreshToken: repo.RefreshToken,
+ Refresher: s.refreshToken(ctx, repo.ID),
+ },
+ repo.VCS.InstanceURL,
+ repo.ExternalID,
+ addedEscaped,
+ commit.ID,
+ )
+ if err != nil {
+ createIgnoredFileActivity(err)
+ continue
+ }
- issueType := api.IssueDatabaseSchemaUpdate
- if mi.Type == db.Data {
- issueType = api.IssueDatabaseDataUpdate
- }
- issueCreate := &api.IssueCreate{
- ProjectID: repo.ProjectID,
- Name: commit.Title,
- Type: issueType,
- Description: commit.Message,
- AssigneeID: api.SystemBotID,
- CreateContext: createContext,
+ // Create schema update issue.
+ var createContext string
+ if repo.Project.TenantMode == api.TenantModeTenant {
+ if !s.feature(api.FeatureMultiTenancy) {
+ return echo.NewHTTPError(http.StatusForbidden, api.FeatureMultiTenancy.AccessErrorMessage())
}
- issue, err := s.createIssue(ctx, issueCreate, api.SystemBotID)
- if err != nil {
- errMsg := "Failed to create schema update issue"
- if issueType == api.IssueDatabaseDataUpdate {
- errMsg = "Failed to create data update issue"
- }
- return echo.NewHTTPError(http.StatusInternalServerError, errMsg).SetInternal(err)
+ createContext, err = s.createTenantSchemaUpdateIssue(ctx, repo, mi, vcsPushEvent, commit, addedEscaped, content)
+ } else {
+ createContext, err = s.createSchemaUpdateIssue(ctx, repo, mi, vcsPushEvent, commit, addedEscaped, content)
+ }
+ if err != nil {
+ createIgnoredFileActivity(err)
+ continue
+ }
+
+ issueType := api.IssueDatabaseSchemaUpdate
+ if mi.Type == db.Data {
+ issueType = api.IssueDatabaseDataUpdate
+ }
+ issueCreate := &api.IssueCreate{
+ ProjectID: repo.ProjectID,
+ Name: commit.Title,
+ Type: issueType,
+ Description: commit.Message,
+ AssigneeID: api.SystemBotID,
+ CreateContext: createContext,
+ }
+ issue, err := s.createIssue(ctx, issueCreate, api.SystemBotID)
+ if err != nil {
+ errMsg := "Failed to create schema update issue"
+ if issueType == api.IssueDatabaseDataUpdate {
+ errMsg = "Failed to create data update issue"
}
+ return echo.NewHTTPError(http.StatusInternalServerError, errMsg).SetInternal(err)
+ }
- createdMessageList = append(createdMessageList, fmt.Sprintf("Created issue %q on adding %s", issue.Name, addedEscaped))
+ createdMessageList = append(createdMessageList, fmt.Sprintf("Created issue %q on adding %s", issue.Name, addedEscaped))
- // Create a project activity after successfully creating the issue as the result of the push event
- bytes, err := json.Marshal(api.ActivityProjectRepositoryPushPayload{
- VCSPushEvent: vcsPushEvent,
- IssueID: issue.ID,
- IssueName: issue.Name,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to construct activity payload").SetInternal(err)
- }
+ // Create a project activity after successfully creating the issue as the result of the push event
+ bytes, err := json.Marshal(api.ActivityProjectRepositoryPushPayload{
+ VCSPushEvent: vcsPushEvent,
+ IssueID: issue.ID,
+ IssueName: issue.Name,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to construct activity payload").SetInternal(err)
+ }
- activityCreate := &api.ActivityCreate{
- CreatorID: api.SystemBotID,
- ContainerID: repo.ProjectID,
- Type: api.ActivityProjectRepositoryPush,
- Level: api.ActivityInfo,
- Comment: fmt.Sprintf("Created issue %q.", issue.Name),
- Payload: string(bytes),
- }
- if _, err = s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{}); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create project activity after creating issue from repository push event: %d", issue.ID)).SetInternal(err)
- }
+ activityCreate := &api.ActivityCreate{
+ CreatorID: api.SystemBotID,
+ ContainerID: repo.ProjectID,
+ Type: api.ActivityProjectRepositoryPush,
+ Level: api.ActivityInfo,
+ Comment: fmt.Sprintf("Created issue %q.", issue.Name),
+ Payload: string(bytes),
+ }
+ if _, err = s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{}); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create project activity after creating issue from repository push event: %d", issue.ID)).SetInternal(err)
}
}
@@ -243,6 +235,72 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
})
}
+// We are observing the push webhook event so that we will receive the event either when:
+// 1. A commit is directly pushed to a branch.
+// 2. One or more commits are merged to a branch.
+//
+// There is a complication to deal with the 2nd type. A typical workflow is a developer first
+// commits the migration file to the feature branch, and at a later point, she creates a merge
+// request to merge the commit to the main branch. Even the developer only creates a single commit
+// on the feature branch, that merge request may contain multiple commits (unless both squash and fast-forward merge are used):
+// 1. The original commit on the feature branch.
+// 2. The merge request commit.
+//
+// And both commits would include that added migration file. Since we create an issue per migration file,
+// we need to filter the commit list to prevent creating a duplicated issue. GitLab has a limitation to distinguish
+// whether the commit is a merge commit (https://gitlab.com/gitlab-org/gitlab/-/issues/30914), so we need to dedup
+// ourselves. Below is the filtering algorithm:
+// 1. If we observe the same migration file multiple times, then we should use the latest migration file. This does not matter
+// for change-based migration since a developer would always create different migration file with incremental names, while it
+// will be important for the state-based migration, since the file name is always the same and we need to use the latest snapshot.
+// 2. Maintain the relative commit order between different migration files. If migration file A happens before migration file B,
+// then we should create an issue for migration file A first.
+type distinctFileItem struct {
+ createdTime time.Time
+ commit gitlab.WebhookCommit
+ fileName string
+}
+
+func dedupMigrationFilesFromCommitList(commitList []gitlab.WebhookCommit) []distinctFileItem {
+ // Use list instead of map because we need to maintain the relative commit order in the source branch.
+ var distinctFileList []distinctFileItem
+ for _, commit := range commitList {
+ log.Debug("Pre-processing commit to dedup migration files...",
+ zap.String("id", common.EscapeForLogging(commit.ID)),
+ zap.String("title", common.EscapeForLogging(commit.Title)),
+ )
+
+ createdTime, err := time.Parse(time.RFC3339, commit.Timestamp)
+ if err != nil {
+ log.Warn("Ignored commit, failed to parse commit timestamp.", zap.String("commit", common.EscapeForLogging(commit.ID)), zap.String("timestamp", common.EscapeForLogging(commit.Timestamp)), zap.Error(err))
+ }
+
+ for _, added := range commit.AddedList {
+ new := true
+ item := distinctFileItem{
+ createdTime: createdTime,
+ commit: commit,
+ fileName: added,
+ }
+ for i, file := range distinctFileList {
+ // For the migration file with the same name, keep the one from the latest commit
+ if added == file.fileName {
+ new = false
+ if file.createdTime.Before(createdTime) {
+ distinctFileList[i] = item
+ }
+ break
+ }
+ }
+
+ if new {
+ distinctFileList = append(distinctFileList, item)
+ }
+ }
+ }
+ return distinctFileList
+}
+
func (s *Server) createSchemaUpdateIssue(ctx context.Context, repository *api.Repository, mi *db.MigrationInfo, vcsPushEvent vcs.PushEvent, commit gitlab.WebhookCommit, added string, statement string) (string, error) {
// Find matching database list
databaseFind := &api.DatabaseFind{
diff --git a/server/webhook_test.go b/server/webhook_test.go
new file mode 100644
index 0000000000..9d9d94cf77
--- /dev/null
+++ b/server/webhook_test.go
@@ -0,0 +1,286 @@
+package server
+
+import (
+ "testing"
+ "time"
+
+ "github.com/bytebase/bytebase/plugin/vcs/gitlab"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestDedupMigrationFiles(t *testing.T) {
+ timestamp1 := "2021-01-13T00:00:00+00:00"
+ timestamp2 := "2021-01-14T00:00:00+00:00"
+ timestamp3 := "2021-01-15T00:00:00+00:00"
+ time1, _ := time.Parse(time.RFC3339, timestamp1)
+ time2, _ := time.Parse(time.RFC3339, timestamp2)
+ time3, _ := time.Parse(time.RFC3339, timestamp3)
+
+ tests := []struct {
+ name string
+ commitList []gitlab.WebhookCommit
+ want []distinctFileItem
+ }{
+ {
+ name: "Empty",
+ commitList: []gitlab.WebhookCommit{},
+ want: nil,
+ },
+ {
+ name: "Single commit, single file",
+ commitList: []gitlab.WebhookCommit{
+ {
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ },
+ },
+ },
+ want: []distinctFileItem{
+ {
+ createdTime: time1,
+ commit: gitlab.WebhookCommit{
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ },
+ },
+ fileName: "v1.sql",
+ },
+ },
+ },
+ {
+ name: "Single commit, multiple files",
+ commitList: []gitlab.WebhookCommit{
+ {
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ },
+ },
+ },
+ want: []distinctFileItem{
+ {
+ createdTime: time1,
+ commit: gitlab.WebhookCommit{
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ },
+ },
+ fileName: "v1.sql",
+ },
+ {
+ createdTime: time1,
+ commit: gitlab.WebhookCommit{
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ },
+ },
+ fileName: "v2.sql",
+ },
+ },
+ },
+ {
+ name: "Multi commits, single file",
+ commitList: []gitlab.WebhookCommit{
+ {
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ },
+ },
+ {
+ ID: "2",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp2,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ },
+ },
+ },
+ want: []distinctFileItem{
+ {
+ createdTime: time2,
+ commit: gitlab.WebhookCommit{
+ ID: "2",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp2,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ },
+ },
+ fileName: "v1.sql",
+ },
+ },
+ },
+ {
+ name: "Multi commits, multi files",
+ commitList: []gitlab.WebhookCommit{
+ {
+ ID: "1",
+ Title: "Commit 1",
+ Message: "Update 1",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ },
+ },
+ {
+ ID: "2",
+ Title: "Commit 2",
+ Message: "Update 2",
+ Timestamp: timestamp1,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v3.sql",
+ },
+ },
+ {
+ ID: "3",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp3,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ "v3.sql",
+ },
+ },
+ },
+ want: []distinctFileItem{
+ {
+ createdTime: time3,
+ commit: gitlab.WebhookCommit{
+ ID: "3",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp3,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ "v3.sql",
+ },
+ },
+ fileName: "v1.sql",
+ },
+ {
+ createdTime: time3,
+ commit: gitlab.WebhookCommit{
+ ID: "3",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp3,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ "v3.sql",
+ },
+ },
+ fileName: "v2.sql",
+ },
+ {
+ createdTime: time3,
+ commit: gitlab.WebhookCommit{
+ ID: "3",
+ Title: "Merge branch",
+ Message: "Merge update",
+ Timestamp: timestamp3,
+ URL: "example.com",
+ Author: gitlab.WebhookCommitAuthor{
+ Name: "bob",
+ },
+ AddedList: []string{
+ "v1.sql",
+ "v2.sql",
+ "v3.sql",
+ },
+ },
+ fileName: "v3.sql",
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := dedupMigrationFilesFromCommitList(tt.commitList)
+ assert.Equal(t, got, tt.want)
+ })
+ }
+}