Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: refactoring to webhook.go (#2903)
Danny Xu40965dbcad13
diff --git a/server/webhook.go b/server/webhook.goindex fdecfa0603..2a34a1133e 100644--- a/server/webhook.go+++ b/server/webhook.go@@ -52,27 +52,20 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {filter := func(token string) (bool, error) {return c.Request().Header.Get("X-Gitlab-Token") == token, nil}- repoList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)+ repositoryList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)if err != nil {return err}- if len(repoList) == 0 {+ if len(repositoryList) == 0 {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", repoList))+ log.Debug("Process push event in repos", zap.Any("repos", repositoryList))commitList, err := convertGitLabCommitList(pushEvent.CommitList)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)))- return c.String(http.StatusOK, "OK")- }baseVCSPushEvent := vcs.PushEvent{Ref: pushEvent.Ref,RepositoryID: repositoryID,@@ -82,7 +75,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {CommitList: commitList,}- createdMessages, err := s.createIssuesFromCommits(ctx, repoList, commitList, baseVCSPushEvent)+ createdMessages, err := s.createIssuesFromCommits(ctx, repositoryList, baseVCSPushEvent)if err != nil {return err}@@ -121,24 +114,17 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {}return ok, nil}- repoList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)+ repositoryList, err := s.filterRepository(ctx, c.Param("id"), pushEvent.Ref, repositoryID, filter)if err != nil {return err}- if len(repoList) == 0 {+ if len(repositoryList) == 0 {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", repoList))+ log.Debug("Process push event in repos", zap.Any("repos", repositoryList))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)))- return c.String(http.StatusOK, "OK")- }baseVCSPushEvent := vcs.PushEvent{Ref: pushEvent.Ref,RepositoryID: repositoryID,@@ -148,7 +134,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {CommitList: commitList,}- createdMessages, err := s.createIssuesFromCommits(ctx, repoList, commitList, baseVCSPushEvent)+ createdMessages, err := s.createIssuesFromCommits(ctx, repositoryList, baseVCSPushEvent)if err != nil {return err}@@ -156,20 +142,25 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {})}-func (s *Server) createIssuesFromCommits(ctx context.Context, filteredRepos []*api.Repository, commitList []vcs.Commit, baseVCSPushEvent vcs.PushEvent) ([]string, error) {- distinctFileList := dedupMigrationFilesFromCommitList(commitList)+func (s *Server) createIssuesFromCommits(ctx context.Context, repositoryList []*api.Repository, baseVCSPushEvent vcs.PushEvent) ([]string, error) {+ distinctFileList := getDistinctFileList(baseVCSPushEvent.CommitList)if len(distinctFileList) == 0 {+ var commitIDs []string+ for _, c := range baseVCSPushEvent.CommitList {+ commitIDs = append(commitIDs, c.ID)+ }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)))+ zap.String("commits", strings.Join(commitIDs, ",")))return nil, nil}+var createdMessages []stringfor _, item := range distinctFileList {var createdMessageList []stringrepoID2ActivityCreateList := make(map[int][]*api.ActivityCreate)- for _, repo := range filteredRepos {+ for _, repo := range repositoryList {pushEvent := baseVCSPushEventpushEvent.VCSType = repo.VCS.TypepushEvent.BaseDirectory = repo.BaseDirectory@@ -200,7 +191,7 @@ func (s *Server) createIssuesFromCommits(ctx context.Context, filteredRepos []*arepoID2ActivityCreateList[repo.ID] = append(repoID2ActivityCreateList[repo.ID], activityCreateList...)}if len(createdMessageList) == 0 {- for _, repo := range filteredRepos {+ for _, repo := range repositoryList {if activityCreateList, ok := repoID2ActivityCreateList[repo.ID]; ok {for _, activityCreate := range activityCreateList {if _, err := s.ActivityManager.CreateActivity(ctx, activityCreate, &ActivityMeta{}); err != nil {@@ -214,9 +205,10 @@ func (s *Server) createIssuesFromCommits(ctx context.Context, filteredRepos []*a}createdMessages = append(createdMessages, createdMessageList...)}+if len(createdMessages) == 0 {var repoURLs []string- for _, repo := range filteredRepos {+ for _, repo := range repositoryList {repoURLs = append(repoURLs, repo.WebURL)}log.Warn("Ignored push event because no applicable file found in the commit list", zap.Any("repos", repoURLs))@@ -224,7 +216,9 @@ func (s *Server) createIssuesFromCommits(ctx context.Context, filteredRepos []*areturn createdMessages, nil}-func (s *Server) getRepositoryList(ctx context.Context, webhookEndpointID string) ([]*api.Repository, error) {+type repositoryFilter func(string) (bool, error)++func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string, pushEventRef, pushEventRepositoryID string, filter repositoryFilter) ([]*api.Repository, error) {repos, err := s.store.FindRepository(ctx, &api.RepositoryFind{WebhookEndpointID: &webhookEndpointID})if err != nil {return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to respond webhook event for endpoint: %v", webhookEndpointID)).SetInternal(err)@@ -232,16 +226,6 @@ func (s *Server) getRepositoryList(ctx context.Context, webhookEndpointID stringif len(repos) == 0 {return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Repository for webhook endpoint %s not found", webhookEndpointID))}- return repos, nil-}--type repositoryFilter func(string) (bool, error)--func (s *Server) filterRepository(ctx context.Context, webhookEndpointID string, pushEventRef, pushEventRepositoryID string, filter repositoryFilter) ([]*api.Repository, error) {- repos, err := s.getRepositoryList(ctx, webhookEndpointID)- if err != nil {- return nil, err- }branch, err := parseBranchNameFromRefs(pushEventRef)if err != nil {@@ -389,7 +373,7 @@ type distinctFileItem struct {itemType fileItemType}-func dedupMigrationFilesFromCommitList(commitList []vcs.Commit) []distinctFileItem {+func getDistinctFileList(commitList []vcs.Commit) []distinctFileItem {// Use list instead of map because we need to maintain the relative commit order in the source branch.var distinctFileList []distinctFileItemfor _, commit := range commitList {@@ -407,16 +391,18 @@ func dedupMigrationFilesFromCommitList(commitList []vcs.Commit) []distinctFileIt}for i, file := range distinctFileList {// For the migration file with the same name, keep the one from the latest commit- if item.fileName == file.fileName {- if file.createdTs < commit.CreatedTs {- // A file can be added and then modified in a later commit. We should consider the item as added.- if file.itemType == fileItemTypeAdded {- item.itemType = fileItemTypeAdded- }- distinctFileList[i] = item+ if item.fileName != file.fileName {+ continue+ }+ if file.createdTs < commit.CreatedTs {+ // A file can be added and then modified in a later commit. We should consider the item as added.+ if file.itemType == fileItemTypeAdded {+ item.itemType = fileItemTypeAdded}- return+ distinctFileList[i] = item}+ // The fileName should be unique within the distinctFileList.+ return}distinctFileList = append(distinctFileList, item)}@@ -855,14 +841,6 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, pushEvent *vcs.Pureturn 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/server/webhook_test.go b/server/webhook_test.goindex 00ce8bf8f9..783e95dd85 100644--- a/server/webhook_test.go+++ b/server/webhook_test.go@@ -10,7 +10,7 @@ import ("github.com/bytebase/bytebase/plugin/vcs")-func TestDedupMigrationFiles(t *testing.T) {+func TestGetDistinctFileList(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"@@ -442,7 +442,7 @@ func TestDedupMigrationFiles(t *testing.T) {}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- got := dedupMigrationFilesFromCommitList(tt.commitList)+ got := getDistinctFileList(tt.commitList)assert.Equal(t, tt.want, got)})}