Skip to content

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

refactor(vcs): add field CommitList in vcs.PushEvent (#2790)

Yilong Li5fffa3c89c1a
diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.go
index dfbe393010..0bf24fb5e4 100644
--- a/plugin/vcs/vcs.go
+++ b/plugin/vcs/vcs.go
@@ -88,14 +88,16 @@ type RepositoryTreeNode struct {
// PushEvent is the API message for a VCS push event.
type PushEvent struct {
- VCSType Type `json:"vcsType"`
- BaseDirectory string `json:"baseDir"`
- Ref string `json:"ref"`
- RepositoryID string `json:"repositoryId"`
- RepositoryURL string `json:"repositoryUrl"`
- RepositoryFullPath string `json:"repositoryFullPath"`
- AuthorName string `json:"authorName"`
- FileCommit FileCommit `json:"fileCommit"`
+ VCSType Type `json:"vcsType"`
+ BaseDirectory string `json:"baseDir"`
+ Ref string `json:"ref"`
+ RepositoryID string `json:"repositoryId"`
+ RepositoryURL string `json:"repositoryUrl"`
+ RepositoryFullPath string `json:"repositoryFullPath"`
+ AuthorName string `json:"authorName"`
+ CommitList []Commit `json:"commits"`
+ // Legacy field, kept for parsing existing data
+ FileCommit FileCommit `json:"fileCommit"`
}
// State is the state of a VCS user account.
diff --git a/server/task_executor.go b/server/task_executor.go
index 591ac83e6f..b5dcf618ef 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -84,7 +84,7 @@ func preMigration(ctx context.Context, server *Server, task *api.Task, migration
}
} else {
mi.Source = db.VCS
- mi.Creator = vcsPushEvent.FileCommit.AuthorName
+ mi.Creator = vcsPushEvent.AuthorName
miPayload := &db.MigrationInfoPayload{
VCSPushEvent: vcsPushEvent,
}
@@ -373,10 +373,17 @@ func writeBackLatestSchema(ctx context.Context, server *Server, repository *api.
commitBody += "\n\n" + bytebaseURL
}
commitBody += "\n\n--------Original migration change--------\n\n"
- commitBody += fmt.Sprintf("%s\n\n%s",
- pushEvent.FileCommit.URL,
- pushEvent.FileCommit.Message,
- )
+ if len(pushEvent.CommitList) == 0 {
+ commitBody += fmt.Sprintf("%s\n\n%s",
+ pushEvent.FileCommit.URL,
+ pushEvent.FileCommit.Message,
+ )
+ } else {
+ commitBody += fmt.Sprintf("%s\n\n%s",
+ pushEvent.CommitList[0].URL,
+ pushEvent.CommitList[0].Message,
+ )
+ }
// Retrieve the latest AccessToken and RefreshToken as the previous VCS call may have
// updated the stored token pair. VCS will fetch and store the new token pair if the
diff --git a/server/webhook.go b/server/webhook.go
index d883879366..6a20f0d2f4 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -73,7 +73,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}
filteredRepos = filterReposGitLab(c.Request().Header, filteredRepos)
if len(filteredRepos) == 0 {
- log.Debug("Empty handle repo list. Skip this push event.")
+ log.Debug("Empty handle repo list. Ignore this push event.")
return c.String(http.StatusOK, "OK")
}
log.Debug("Process push event in repos", zap.Any("repos", filteredRepos))
@@ -82,6 +82,15 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to convert GitLab commits").SetInternal(err)
}
+ if len(commitList) == 0 {
+ log.Debug("No commit in the GitLab push event. Ignore this push event.",
+ zap.String("repoURL", common.EscapeForLogging(pushEvent.Project.WebURL)),
+ zap.String("repoName", pushEvent.Project.FullPath),
+ zap.String("commits", getCommitsMessage(commitList)))
+ c.Response().WriteHeader(http.StatusOK)
+ return nil
+ }
+ baseVCSPushEvent.CommitList = commitList
createdMessages, httpErr := s.createIssuesFromCommits(ctx, webhookEndpointID, filteredRepos, commitList, baseVCSPushEvent)
if httpErr != nil {
return httpErr
@@ -137,12 +146,21 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
return httpErr
}
if len(filteredRepos) == 0 {
- log.Debug("Empty handle repo list. Skip this push event.")
+ log.Debug("Empty handle repo list. Ignore this push event.")
return c.String(http.StatusOK, "OK")
}
log.Debug("Process push event in repos", zap.Any("repos", filteredRepos))
commitList := convertGitHubCommitList(pushEvent.Commits)
+ if len(pushEvent.Commits) == 0 {
+ log.Debug("No commit in the GitHub push event. Ignore this push event.",
+ zap.String("repoURL", common.EscapeForLogging(pushEvent.Repository.HTMLURL)),
+ zap.String("repoName", common.EscapeForLogging(pushEvent.Repository.FullName)),
+ zap.String("commits", getCommitsMessage(commitList)))
+ c.Response().WriteHeader(http.StatusOK)
+ return nil
+ }
+ baseVCSPushEvent.CommitList = commitList
createdMessages, httpErr := s.createIssuesFromCommits(ctx, webhookEndpointID, filteredRepos, commitList, baseVCSPushEvent)
if httpErr != nil {
return httpErr
@@ -153,6 +171,13 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
func (s *Server) createIssuesFromCommits(ctx context.Context, webhookEndpointID string, filteredRepos []*api.Repository, commitList []vcs.Commit, baseVCSPushEvent vcs.PushEvent) ([]string, *echo.HTTPError) {
distinctFileList := dedupMigrationFilesFromCommitList(commitList)
+ if len(distinctFileList) == 0 {
+ log.Warn("No files found from the push event",
+ zap.String("repoURL", common.EscapeForLogging(baseVCSPushEvent.RepositoryURL)),
+ zap.String("repoName", baseVCSPushEvent.RepositoryFullPath),
+ zap.String("commits", getCommitsMessage(commitList)))
+ return nil, nil
+ }
var createdMessages []string
for _, item := range distinctFileList {
var createdMessageList []string
@@ -831,8 +856,8 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, pushEvent *vcs.Pu
return "", false, activityCreateList, echo.NewHTTPError(http.StatusInternalServerError, errMsg).SetInternal(err)
}
- // Create a project activity after successfully creating the issue as the result of the push event
- payload, err := json.Marshal(
+ // Create a project activity after successfully creating the issue from the push event.
+ activityPayload, err := json.Marshal(
api.ActivityProjectRepositoryPushPayload{
VCSPushEvent: *pushEvent,
IssueID: issue.ID,
@@ -849,7 +874,7 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, pushEvent *vcs.Pu
Type: api.ActivityProjectRepositoryPush,
Level: api.ActivityInfo,
Comment: fmt.Sprintf("Created issue %q.", issue.Name),
- Payload: string(payload),
+ Payload: string(activityPayload),
}
if _, err = s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{}); err != nil {
return "", false, activityCreateList, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create project activity after creating issue from repository push event: %d", issue.ID)).SetInternal(err)
@@ -858,6 +883,14 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, pushEvent *vcs.Pu
return fmt.Sprintf("Created issue %q on adding %s", issue.Name, file), true, activityCreateList, nil
}
+func getCommitsMessage(commitList []vcs.Commit) string {
+ var commitIDs []string
+ for _, c := range commitList {
+ commitIDs = append(commitIDs, c.ID)
+ }
+ return strings.Join(commitIDs, ", ")
+}
+
// parseSchemaFileInfo attempts to parse the given schema file path to extract
// the schema file info. It returns (nil, nil) if it doesn't looks like a schema
// file path.
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 57f155cda6..521ef26a1f 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -1004,6 +1004,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
CommitList: []gitlab.WebhookCommit{
{
+ ID: "68211f18905c46e8bda58a8fee98051f2ffe40bb",
Timestamp: "2021-01-13T13:14:00Z",
AddedList: []string{gitFile},
},
@@ -1049,6 +1050,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
CommitList: []gitlab.WebhookCommit{
{
+ ID: "68211f18905c46e8bda58a8fee98051f2ffe40bb",
Timestamp: "2021-01-13T13:14:00Z",
AddedList: []string{gitFile},
},
@@ -1090,6 +1092,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
CommitList: []gitlab.WebhookCommit{
{
+ ID: "68211f18905c46e8bda58a8fee98051f2ffe40bb",
Timestamp: "2021-01-13T13:14:00Z",
AddedList: []string{gitFile},
},
@@ -1132,6 +1135,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
CommitList: []gitlab.WebhookCommit{
{
+ ID: "68211f18905c46e8bda58a8fee98051f2ffe40bb",
Timestamp: "2021-01-13T13:14:00Z",
AddedList: []string{gitFile},
},
@@ -1171,6 +1175,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
CommitList: []gitlab.WebhookCommit{
{
+ ID: "68211f18905c46e8bda58a8fee98051f2ffe40bb",
Timestamp: "2021-01-13T13:14:00Z",
AddedList: []string{gitFile},
},