8000 fix(hooks): use another uuid for branch deletion task and autoclean t… · ovh/cds@73c2de7 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 73c2de7

Browse files
authored
fix(hooks): use another uuid for branch deletion task and autoclean them (#4291)
Signed-off-by: Benjamin Coenen <benjamin.coenen@corp.ovh.com>
1 parent 2a451c3 commit 73c2de7

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

engine/hooks/scheduler.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func (s *Service) deleteTaskExecutionsRoutine(c context.Context) error {
162162
continue
163163
}
164164
for _, t := range tasks {
165+
taskToDelete := false
165166
execs, err := s.Dao.FindAllTaskExecutions(&t)
166167
if err != nil {
167168
log.Error("Hooks> deleteTaskExecutionsRoutine > Unable to find all task executions (%s): %v", t.UUID, err)
@@ -172,10 +173,25 @@ func (s *Service) deleteTaskExecutionsRoutine(c context.Context) error {
172173
})
173174

174175
for i, e := range execs {
175-
if i >= s.Cfg.ExecutionHistory && e.ProcessingTimestamp != 0 {
176-
s.Dao.DeleteTaskExecution(&e)
176+
switch e.Type {
177+
// Delete all branch deletion task execution
178+
case TypeBranchDeletion:
179+
if e.Status == TaskExecutionDone && e.ProcessingTimestamp != 0 {
180+
s.Dao.DeleteTaskExecution(&e)
181+
taskToDelete = true
182+
}
183+
default:
184+
if i >= s.Cfg.ExecutionHistory && e.ProcessingTimestamp != 0 {
185+
s.Dao.DeleteTaskExecution(&e)
186+
}
177187
}
188+
189+
}
190+
191+
if taskToDelete {
192+
s.Dao.DeleteTask(&t)
178193
}
194+
179195
}
180196
}
181197
}

engine/hooks/webhook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (s *Service) executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.Workflow
5353
}
5454
if pushEvent.Deleted {
5555
branch := strings.TrimPrefix(pushEvent.Ref, "refs/heads/")
56-
err := s.enqueueBranchDeletion(t.UUID, projectKey, workflowName, branch)
56+
err := s.enqueueBranchDeletion(projectKey, workflowName, branch)
5757

5858
return nil, sdk.WrapError(err, "cannot enqueue branch deletion")
5959
}
@@ -99,7 +99,7 @@ func (s *Service) executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.Workflow
9999
}
100100
// Branch deletion ( gitlab return 0000000000000000000000000000000000000000 as git hash)
101101
if pushEvent.After == "0000000000000000000000000000000000000000" {
102-
err := s.enqueueBranchDeletion(t.UUID, projectKey, workflowName, strings.TrimPrefix(pushEvent.Ref, "refs/heads/"))
102+
err := s.enqueueBranchDeletion(projectKey, workflowName, strings.TrimPrefix(pushEvent.Ref, "refs/heads/"))
103103
return nil, sdk.WrapError(err, "cannot enqueue branch deletion")
104104
}
105105
payload["git.author"] = pushEvent.UserUsername
@@ -142,7 +142,7 @@ func (s *Service) executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.Workflow
142142

143143
for _, pushChange := range pushEvent.Changes {
144144
if pushChange.Type == "DELETE" {
145-
err := s.enqueueBranchDeletion(t.UUID, projectKey, workflowName, strings.TrimPrefix(pushChange.RefID, "refs/heads/"))
145+
err := s.enqueueBranchDeletion(projectKey, workflowName, strings.TrimPrefix(pushChange.RefID, "refs/heads/"))
146146
if err != nil {
147147
log.Error("cannot enqueue branch deletion: %v", err)
148148
}
@@ -292,7 +292,7 @@ func executeWebHook(t *sdk.TaskExecution) (*sdk.WorkflowNodeRunHookEvent, error)
292292
return &h, nil
293293
}
294294

295-
func (s *Service) enqueueBranchDeletion(uuid, projectKey, workflowName, branch string) error {
295+
func (s *Service) enqueueBranchDeletion(projectKey, workflowName, branch string) error {
296296
config := sdk.WorkflowNodeHookConfig{
297297
"project": sdk.WorkflowNodeHookConfigValue{
298298
Configurable: false,
@@ -313,7 +313,7 @@ func (s *Service) enqueueBranchDeletion(uuid, projectKey, workflowName, branch s
313313
task := sdk.Task{
314314
Config: config,
315315
Type: TypeBranchDeletion,
316-
UUID: uuid,
316+
UUID: sdk.UUID(),
317317
}
318318

319319
_, err := s.startTask(context.Background(), &task)

0 commit comments

Comments
 (0)
0