Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hooks): remove deprecated branch deletion from hooks #5955

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions engine/hooks/bitbucket_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,13 @@ 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)
getVariableFromBitbucketCloudRepository(payload, request.Repository)
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
Expand Down
18 changes: 0 additions & 18 deletions engine/hooks/bitbucket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"

"github.com/rockbears/log"

"github.com/ovh/cds/sdk"
)

Expand Down Expand Up @@ -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
Expand Down
41 changes: 0 additions & 41 deletions engine/hooks/branch_deletion.go

This file was deleted.

12 changes: 0 additions & 12 deletions engine/hooks/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
14 changes: 0 additions & 14 deletions engine/hooks/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/")
}
Expand Down
18 changes: 3 additions & 15 deletions engine/hooks/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions engine/hooks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
TypeWebHook = "Webhook"
TypeScheduler = "Scheduler"
TypeRepoPoller = "RepoPoller"
TypeBranchDeletion = "BranchDeletion"
TypeKafka = "Kafka"
TypeGerrit = "Gerrit"
TypeRabbitMQ = "RabbitMQ"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 0 additions & 29 deletions engine/hooks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down