From 08ef0d84934a58e7fd3d22ebf7092ddf9702f5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Thu, 30 Sep 2021 11:04:14 +0200 Subject: [PATCH] fix(hooks): remove deprecated branch deletion from hooks (#5955) Signed-off-by: francois samin --- engine/hooks/bitbucket_cloud.go | 20 ---------------- engine/hooks/bitbucket_server.go | 18 -------------- engine/hooks/branch_deletion.go | 41 -------------------------------- engine/hooks/github.go | 12 ---------- engine/hooks/gitlab.go | 14 ----------- engine/hooks/scheduler.go | 18 +++----------- engine/hooks/tasks.go | 9 +------ engine/hooks/webhook.go | 29 ---------------------- 8 files changed, 4 insertions(+), 157 deletions(-) delete mode 100644 engine/hooks/branch_deletion.go diff --git a/engine/hooks/bitbucket_cloud.go b/engine/hooks/bitbucket_cloud.go index c05545aca8..acc48d22bd 100644 --- a/engine/hooks/bitbucket_cloud.go +++ b/engine/hooks/bitbucket_cloud.go @@ -17,9 +17,6 @@ func (s *Service) generatePayloadFromBitbucketCloudRequest(ctx context.Context, return nil, sdk.WrapError(err, "unable ro read bitbucket request: %s", string(t.WebHook.RequestBody)) } - projectKey := t.Config["project"].Value - workflowName := t.Config["workflow"].Value - payload := make(map[string]interface{}) payload[GIT_EVENT] = event getVariableFromBitbucketCloudAuthor(payload, request.Actor) @@ -27,23 +24,6 @@ func (s *Service) generatePayloadFromBitbucketCloudRequest(ctx context.Context, getPayloadStringVariable(ctx, payload, request) for _, pushChange := range request.Push.Changes { - if pushChange.Closed { - if pushChange.Old.Type == "branch" { - if err := s.enqueueBranchDeletion(projectKey, workflowName, strings.TrimPrefix(pushChange.Old.Name, "refs/heads/")); err != nil { - log.Error(ctx, "cannot enqueue branch deletion: %v", err) - } - } - continue - } - - if pushChange.New.Type == "branch" { - branch := strings.TrimPrefix(pushChange.New.Name, "refs/heads/") - if err := s.stopBranchDeletionTask(ctx, branch); err != nil { - log.Error(ctx, "cannot stop branch deletion task for branch %s : %v", branch, err) - } - - } - payloadChange := make(map[string]interface{}) for k, v := range payload { payloadChange[k] = v diff --git a/engine/hooks/bitbucket_server.go b/engine/hooks/bitbucket_server.go index cd69f0cecd..e3b4adad22 100644 --- a/engine/hooks/bitbucket_server.go +++ b/engine/hooks/bitbucket_server.go @@ -5,8 +5,6 @@ import ( "fmt" "strings" - "github.com/rockbears/log" - "github.com/ovh/cds/sdk" ) @@ -43,23 +41,7 @@ func (s *Service) generatePayloadFromBitbucketServerRequest(ctx context.Context, payloads = append(payloads, payload) } - projectKey := t.Config["project"].Value - workflowName := t.Config["workflow"].Value for _, pushChange := range request.Changes { - if pushChange.Type == "DELETE" { - err := s.enqueueBranchDeletion(projectKey, workflowName, strings.TrimPrefix(pushChange.RefID, "refs/heads/")) - if err != nil { - log.Error(ctx, "cannot enqueue branch deletion: %v", err) - } - continue - } - if !strings.HasPrefix(pushChange.RefID, "refs/tags/") { - branch := strings.TrimPrefix(pushChange.RefID, "refs/heads/") - if err := s.stopBranchDeletionTask(ctx, branch); err != nil { - log.Error(ctx, "cannot stop branch deletion task for branch %s : %v", branch, err) - } - } - payloadChanges := make(map[string]interface{}) for k, v := range payload { payloadChanges[k] = v diff --git a/engine/hooks/branch_deletion.go b/engine/hooks/branch_deletion.go deleted file mode 100644 index 6692d51205..0000000000 --- a/engine/hooks/branch_deletion.go +++ /dev/null @@ -1,41 +0,0 @@ -package hooks - -import ( - "context" - - "github.com/rockbears/log" - - "github.com/ovh/cds/sdk" -) - -func (s *Service) doBranchDeletionTaskExecution(t *sdk.TaskExecution) (*sdk.WorkflowNodeRunHookEvent, error) { - log.Debug(context.TODO(), "Hooks> Processing branch deletion task %s", t.UUID) - - projectKey := t.Config["project"].Value - workflowName := t.Config["workflow"].Value - branch := t.Config["branch"].Value - err := s.Client.WorkflowRunsDeleteByBranch(projectKey, workflowName, branch) - - return nil, sdk.WrapError(err, "cannot mark to delete workflow runs") -} - -func (s *Service) stopBranchDeletionTask(ctx context.Context, branch string) error { - if branch == "" { - return nil - } - keys, err := s.Dao.FindAllKeysMatchingPattern(branch + "*") - if err != nil { - return sdk.WrapError(err, "cannot find keys matching pattern %s", branch+"*") - } - for _, key := range keys { - t := s.Dao.FindTask(ctx, key) - if t == nil || t.Type != TypeBranchDeletion { - continue - } - if err := s.stopTask(ctx, t); err != nil { - log.Error(ctx, "cannot stop task %s : %v", t.UUID, err) - } - } - - return nil -} diff --git a/engine/hooks/github.go b/engine/hooks/github.go index c06401e756..51e2dc09ab 100644 --- a/engine/hooks/github.go +++ b/engine/hooks/github.go @@ -4,15 +4,10 @@ import ( "context" "strings" - "github.com/rockbears/log" - "github.com/ovh/cds/sdk" ) func (s *Service) generatePayloadFromGithubRequest(ctx context.Context, t *sdk.TaskExecution, event string) (map[string]interface{}, error) { - projectKey := t.Config["project"].Value - workflowName := t.Config["workflow"].Value - var request GithubWebHookEvent if err := sdk.JSONUnmarshal(t.WebHook.RequestBody, &request); err != nil { return nil, sdk.WrapError(err, "unable ro read github request: %s", string(t.WebHook.RequestBody)) @@ -23,13 +18,6 @@ func (s *Service) generatePayloadFromGithubRequest(ctx context.Context, t *sdk.T if request.Ref != "" { branch := strings.TrimPrefix(request.Ref, "refs/heads/") - if request.Deleted { - err := s.enqueueBranchDeletion(projectKey, workflowName, branch) - return nil, sdk.WrapError(err, "cannot enqueue branch deletion") - } - if err := s.stopBranchDeletionTask(ctx, branch); err != nil { - log.Error(ctx, "cannot stop branch deletion task for branch %s : %v", branch, err) - } if !strings.HasPrefix(request.Ref, "refs/tags/") { payload[GIT_BRANCH] = branch diff --git a/engine/hooks/gitlab.go b/engine/hooks/gitlab.go index 3449de31f5..aacf857cfd 100644 --- a/engine/hooks/gitlab.go +++ b/engine/hooks/gitlab.go @@ -4,26 +4,15 @@ import ( "context" "strings" - "github.com/rockbears/log" - "github.com/ovh/cds/sdk" ) func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.TaskExecution, event string) (map[string]interface{}, error) { - projectKey := t.Config["project"].Value - workflowName := t.Config["workflow"].Value - var request GitlabEvent if err := sdk.JSONUnmarshal(t.WebHook.RequestBody, &request); err != nil { return nil, sdk.WrapError(err, "unable ro read gitlab request: %s", string(t.WebHook.RequestBody)) } - // Branch deletion ( gitlab return 0000000000000000000000000000000000000000 as git hash) - if request.After == "0000000000000000000000000000000000000000" { - err := s.enqueueBranchDeletion(projectKey, workflowName, strings.TrimPrefix(request.Ref, "refs/heads/")) - return nil, sdk.WrapError(err, "cannot enqueue branch deletion") - } - payload := make(map[string]interface{}) payload[GIT_EVENT] = event @@ -38,9 +27,6 @@ func (s *Service) generatePayloadFromGitlabRequest(ctx context.Context, t *sdk.T if !strings.HasPrefix(request.Ref, "refs/tags/") { branch := strings.TrimPrefix(request.Ref, "refs/heads/") payload[GIT_BRANCH] = branch - if err := s.stopBranchDeletionTask(ctx, branch); err != nil { - log.Error(ctx, "cannot stop branch deletion task for branch %s : %v", branch, err) - } } else { payload[GIT_TAG] = strings.TrimPrefix(request.Ref, "refs/tags/") } diff --git a/engine/hooks/scheduler.go b/engine/hooks/scheduler.go index 96d05eeae7..fde8d549d2 100644 --- a/engine/hooks/scheduler.go +++ b/engine/hooks/scheduler.go @@ -210,23 +210,11 @@ func (s *Service) deleteTaskExecutionsRoutine(ctx context.Context) error { }) for i, e := range execs { - switch e.Type { - // Delete all branch deletion task execution - case TypeBranchDeletion: - if e.Status == TaskExecutionDone && e.ProcessingTimestamp != 0 { - if err := s.Dao.DeleteTaskExecution(&e); err != nil { - log.Error(ctx, "deleteTaskExecutionsRoutine > error on DeleteTaskExecution: %v", err) - } - taskToDelete = true - } - default: - if i >= s.Cfg.ExecutionHistory && e.ProcessingTimestamp != 0 { - if err := s.Dao.DeleteTaskExecution(&e); err != nil { - log.Error(ctx, "deleteTaskExecutionsRoutine > error on DeleteTaskExecution: %v", err) - } + if i >= s.Cfg.ExecutionHistory && e.ProcessingTimestamp != 0 { + if err := s.Dao.DeleteTaskExecution(&e); err != nil { + log.Error(ctx, "deleteTaskExecutionsRoutine > error on DeleteTaskExecution: %v", err) } } - } if taskToDelete { diff --git a/engine/hooks/tasks.go b/engine/hooks/tasks.go index ff0b46e60b..76d7003674 100644 --- a/engine/hooks/tasks.go +++ b/engine/hooks/tasks.go @@ -20,7 +20,6 @@ const ( TypeWebHook = "Webhook" TypeScheduler = "Scheduler" TypeRepoPoller = "RepoPoller" - TypeBranchDeletion = "BranchDeletion" TypeKafka = "Kafka" TypeGerrit = "Gerrit" TypeRabbitMQ = "RabbitMQ" @@ -287,7 +286,7 @@ func (s *Service) startTask(ctx context.Context, t *sdk.Task) (*sdk.TaskExecutio switch t.Type { case TypeWebHook, TypeRepoManagerWebHook, TypeWorkflowHook: return nil, nil - case TypeScheduler, TypeRepoPoller, TypeBranchDeletion: + case TypeScheduler, TypeRepoPoller: return nil, s.prepareNextScheduledTaskExecution(ctx, t) case TypeKafka: return nil, s.startKafkaHook(ctx, t) @@ -352,10 +351,6 @@ func (s *Service) prepareNextScheduledTaskExecution(ctx context.Context, t *sdk. nextSchedule = time.Unix(nextExec, 0) } } - - case TypeBranchDeletion: - now := time.Now() - nextSchedule = now.Add(24 * time.Hour) } //Craft a new execution @@ -425,8 +420,6 @@ func (s *Service) doTask(ctx context.Context, t *sdk.Task, e *sdk.TaskExecution) //Populate next execution hs, err = s.doPollerTaskExecution(ctx, t, e) doRestart = true - case e.ScheduledTask != nil && e.Type == TypeBranchDeletion: - _, err = s.doBranchDeletionTaskExecution(e) case e.Kafka != nil && e.Type == TypeKafka: h, err = s.doKafkaTaskExecution(e) case e.RabbitMQ != nil && e.Type == TypeRabbitMQ: diff --git a/engine/hooks/webhook.go b/engine/hooks/webhook.go index a194153659..856217a9cc 100644 --- a/engine/hooks/webhook.go +++ b/engine/hooks/webhook.go @@ -212,35 +212,6 @@ func executeWebHook(t *sdk.TaskExecution) (*sdk.WorkflowNodeRunHookEvent, error) return &h, nil } -func (s *Service) enqueueBranchDeletion(projectKey, workflowName, branch string) error { - config := sdk.WorkflowNodeHookConfig{ - "project": sdk.WorkflowNodeHookConfigValue{ - Configurable: false, - Type: sdk.HookConfigTypeProject, - Value: projectKey, - }, - "workflow": sdk.WorkflowNodeHookConfigValue{ - Configurable: false, - Type: sdk.HookConfigTypeWorkflow, - Value: workflowName, - }, - "branch": sdk.WorkflowNodeHookConfigValue{ - Configurable: false, - Type: sdk.HookConfigTypeString, - Value: branch, - }, - } - task := sdk.Task{ - Config: config, - Type: TypeBranchDeletion, - UUID: branch + "-" + sdk.UUID(), - } - - _, err := s.startTask(context.Background(), &task) - - return sdk.WrapError(err, "cannot start task") -} - func copyValues(dst, src url.Values) { for k, vs := range src { for _, value := range vs {