8000 fix(api): fix hooks with empty UUID (#5098) · ovh/cds@5386f91 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5386f91

Browse files
authored
fix(api): fix hooks with empty UUID (#5098)
Signed-off-by: francois samin <francois.samin@corp.ovh.com>
1 parent 88e8ccf commit 5386f91

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

engine/api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ func (a *API) Serve(ctx context.Context) error {
707707
return migrate.CleanDuplicateHooks(ctx, a.DBConnectionFactory.GetDBMap(), a.Cache, false)
708708
}})
709709

710+
migrate.Add(ctx, sdk.Migration{Name: "FixEmptyUUIDHooks", Release: "0.44.0", Blocker: false, Automatic: false, ExecFunc: func(ctx context.Context) error {
711+
return migrate.FixEmptyUUIDHooks(ctx, a.DBConnectionFactory.GetDBMap(), a.Cache)
712+
}})
713+
710714
isFreshInstall, errF := version.IsFreshInstall(a.mustDB())
711715
if errF != nil {
712716
return sdk.WrapError(errF, "Unable to check if it's a fresh installation of CDS")

engine/api/migrate/clean_duplicate_hooks.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,80 @@ func cleanDuplicateHooks(ctx context.Context, db *gorp.DbMap, store cache.Store,
135135

136136
return nil
137137
}
138+
139+
func FixEmptyUUIDHooks(ctx context.Context, db *gorp.DbMap, store cache.Store) error {
140+
q := "select distinct(workflow.id) from w_node_hook join w_node on w_node.id = w_node_hook.node_id join workflow on workflow.id = w_node.workflow_id where uuid = ''"
141+
var ids []int64
142+
143+
if _, err := db.Select(&ids, q); err != nil {
144+
return sdk.WrapError(err, "unable to select workflow")
145+
}
146+
147+
var mError = new(sdk.MultiError)
148+
for _, id := range ids {
149+
if err := fixEmptyUUIDHooks(ctx, db, store, id); err != nil {
150+
mError.Append(err)
151+
log.Error(ctx, "migrate.FixEmptyUUIDHooks> unable to clean workflow %d: %v", id, err)
152+
}
153+
}
154+
155+
if mError.IsEmpty() {
156+
return nil
157+
}
158+
return mError
159+
}
160+
161+
func fixEmptyUUIDHooks(ctx context.Context, db *gorp.DbMap, store cache.Store, workflowID int64) error {
162+
tx, err := db.Begin()
163+
if err != nil {
164+
return sdk.WithStack(err)
165+
}
166+
167+
defer tx.Rollback() // nolint
168+
169+
projectID, err := tx.SelectInt("SELECT project_id FROM workflow WHERE id = $1", workflowID)
170+
if err != nil {
171+
if err == sql.ErrNoRows {
172+
return nil
173+
}
174+
return sdk.WithStack(err)
175+
}
176+
177+
if projectID == 0 {
178+
return nil
179+
}
180+
181+
proj, err := project.LoadByID(tx, store, projectID,
182+
project.LoadOptions.WithApplicationWithDeploymentStrategies,
183+
project.LoadOptions.WithPipelines,
184+
project.LoadOptions.WithEnvironments,
185+
project.LoadOptions.WithIntegrations)
186+
if err != nil {
187+
return sdk.WrapError(err, "unable to load project %d", projectID)
188+
}
189+
190+
w, err := workflow.LoadAndLockByID(ctx, tx, store, *proj, workflowID, workflow.LoadOptions{})
191+
if err != nil {
192+
if sdk.ErrorIs(err, sdk.ErrNotFound) {
193+
return nil
194+
}
195+
return err
196+
}
197+
198+
for i, h := range w.WorkflowData.Node.Hooks {
199+
if h.UUID == "" {
200+
w.WorkflowData.Node.Hooks[i].UUID = sdk.UUID()
201+
}
202+
}
203+
204+
if err := workflow.Update(ctx, tx, store, *proj, w, workflow.UpdateOptions{}); err != nil {
205+
return err
206+
}
207+
208+
if err := tx.Commit(); err != nil {
209+
return err
210+
}
211+
log.Info(ctx, "migrate.fixEmptyUUIDHooks> workflow %s/%s (%d) has been cleaned", proj.Name, w.Name, w.ID)
212+
213+
return nil
214+
}

0 commit comments

Comments
 (0)
0