/
clean_duplicate_hooks.go
137 lines (113 loc) · 3.17 KB
/
clean_duplicate_hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package migrate
import (
"context"
"database/sql"
"github.com/ovh/cds/engine/api/project"
"github.com/go-gorp/gorp"
"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
)
func CleanDuplicateHooks(ctx context.Context, db *gorp.DbMap, store cache.Store, dryrun bool) error {
var ids []int64
if _, err := db.Select(&ids, "select id from workflow"); err != nil {
return sdk.WrapError(err, "unable to select workflow")
}
var mError = new(sdk.MultiError)
for _, id := range ids {
if err := cleanDuplicateHooks(ctx, db, store, id, dryrun); err != nil {
mError.Append(err)
log.Error(ctx, "migrate.CleanDuplicateHooks> unable to clean workflow %d: %v", id, err)
}
}
if mError.IsEmpty() {
return nil
}
return mError
}
func cleanDuplicateHooks(ctx context.Context, db *gorp.DbMap, store cache.Store, workflowID int64, dryrun bool) error {
tx, err := db.Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
projectID, err := tx.SelectInt("SELECT project_id FROM workflow WHERE id = $1", workflowID)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
return sdk.WithStack(err)
}
if projectID == 0 {
return nil
}
proj, err := project.LoadByID(tx, store, projectID,
project.LoadOptions.WithApplicationWithDeploymentStrategies,
project.LoadOptions.WithPipelines,
project.LoadOptions.WithEnvironments,
project.LoadOptions.WithIntegrations)
if err != nil {
return sdk.WrapError(err, "unable to load project %d", projectID)
}
w, err := workflow.LoadAndLockByID(ctx, tx, store, *proj, workflowID, workflow.LoadOptions{})
if err != nil {
if sdk.ErrorIs(err, sdk.ErrNotFound) {
return nil
}
return err
}
if w.FromRepository != "" {
return nil
}
if w.FromTemplate != "" {
return nil
}
nbHooks := len(w.WorkflowData.Node.Hooks)
if nbHooks < 2 {
return nil
}
var hookDoublons = []struct {
x int
y int
}{}
for i, h1 := range w.WorkflowData.Node.Hooks {
for j, h2 := range w.WorkflowData.Node.Hooks {
if i != j && i < j && h1.Ref() == h2.Ref() {
hookDoublons = append(hookDoublons, struct{ x, y int }{i, j})
}
}
}
if len(hookDoublons) == 0 {
return nil
}
var idxToRemove []int64
for _, doublon := range hookDoublons {
h1 := w.WorkflowData.Node.Hooks[doublon.x]
h2 := w.WorkflowData.Node.Hooks[doublon.y]
if h1.ID < h2.ID {
idxToRemove = append(idxToRemove, int64(doublon.y))
} else {
idxToRemove = append(idxToRemove, int64(doublon.x))
}
}
var newHooks []sdk.NodeHook
for i, h := range w.WorkflowData.Node.Hooks {
if !sdk.IsInInt64Array(int64(i), idxToRemove) {
newHooks = append(newHooks, h)
}
}
w.WorkflowData.Node.Hooks = newHooks
if err := workflow.Update(ctx, tx, store, *proj, w, workflow.UpdateOptions{DisableHookManagement: dryrun}); err != nil {
return err
}
if dryrun {
log.Info(ctx, "migrate.cleanDuplicateHooks> workflow %s/%s (%d) should been cleaned", proj.Name, w.Name, w.ID)
} else {
if err := tx.Commit(); err != nil {
return err
}
log.Info(ctx, "migrate.cleanDuplicateHooks> workflow %s/%s (%d) has been cleaned", proj.Name, w.Name, w.ID)
}
return nil
}