Skip to content

Commit

Permalink
fix(api): make panic on `go api.WorkflowSendEvent(context.Background(…
Browse files Browse the repository at this point in the history
…), *p, report)`

Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin committed Oct 19, 2021
1 parent da1ec2e commit 73a9ed6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
21 changes: 13 additions & 8 deletions engine/api/workflow_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ import (

// WorkflowSendEvent Send event on workflow run
func (api *API) WorkflowSendEvent(ctx context.Context, proj sdk.Project, report *workflow.ProcessorReport) {
db := api.mustDB()
if db == nil {
return
}

if report == nil {
return
}
for _, wr := range report.Workflows() {
event.PublishWorkflowRun(ctx, wr, proj.Key)
}
for _, wnr := range report.Nodes() {
wr, err := workflow.LoadRunByID(ctx, api.mustDB(), wnr.WorkflowRunID, workflow.LoadRunOptions{
wr, err := workflow.LoadRunByID(ctx, db, wnr.WorkflowRunID, workflow.LoadRunOptions{
DisableDetailledNodeRun: true,
})
if err != nil {
Expand All @@ -32,40 +37,40 @@ func (api *API) WorkflowSendEvent(ctx context.Context, proj sdk.Project, report
if wnr.SubNumber > 0 {
previousNodeRun = &wnr
} else {
previousNodeRun, err = workflow.PreviousNodeRun(api.mustDB(), wnr, wnr.WorkflowNodeName, wr.WorkflowID)
previousNodeRun, err = workflow.PreviousNodeRun(db, wnr, wnr.WorkflowNodeName, wr.WorkflowID)
if err != nil {
log.Warn(ctx, "workflowSendEvent> Cannot load previous node run: %v", err)
}
}

nr, err := workflow.LoadNodeRunByID(ctx, api.mustDB(), wnr.ID, workflow.LoadRunOptions{
nr, err := workflow.LoadNodeRunByID(ctx, db, wnr.ID, workflow.LoadRunOptions{
DisableDetailledNodeRun: false, // load build parameters, used in notif interpolate below
})
if err != nil {
log.Warn(ctx, "workflowSendEvent > Cannot load workflow node run: %v", err)
continue
}

workDB, err := workflow.LoadWorkflowFromWorkflowRunID(api.mustDB(), wr.ID)
workDB, err := workflow.LoadWorkflowFromWorkflowRunID(db, wr.ID)
if err != nil {
log.Warn(ctx, "WorkflowSendEvent> Unable to load workflow for event: %v", err)
continue
}
eventsNotif := notification.GetUserWorkflowEvents(ctx, api.mustDB(), api.Cache, wr.Workflow.ProjectID, wr.Workflow.ProjectKey, workDB.Name, wr.Workflow.Notifications, previousNodeRun, *nr)
eventsNotif := notification.GetUserWorkflowEvents(ctx, db, api.Cache, wr.Workflow.ProjectID, wr.Workflow.ProjectKey, workDB.Name, wr.Workflow.Notifications, previousNodeRun, *nr)
event.PublishWorkflowNodeRun(ctx, *nr, wr.Workflow, eventsNotif)
e := &workflow.VCSEventMessenger{}
if err := e.SendVCSEvent(ctx, api.mustDB(), api.Cache, proj, *wr, wnr); err != nil {
if err := e.SendVCSEvent(ctx, db, api.Cache, proj, *wr, wnr); err != nil {
log.Warn(ctx, "WorkflowSendEvent> Cannot send vcs notification")
}
}

for _, jobrun := range report.Jobs() {
noderun, err := workflow.LoadNodeRunByID(ctx, api.mustDB(), jobrun.WorkflowNodeRunID, workflow.LoadRunOptions{})
noderun, err := workflow.LoadNodeRunByID(ctx, db, jobrun.WorkflowNodeRunID, workflow.LoadRunOptions{})
if err != nil {
log.Warn(ctx, "workflowSendEvent> Cannot load workflow node run %d: %s", jobrun.WorkflowNodeRunID, err)
continue
}
wr, err := workflow.LoadRunByID(ctx, api.mustDB(), noderun.WorkflowRunID, workflow.LoadRunOptions{
wr, err := workflow.LoadRunByID(ctx, db, noderun.WorkflowRunID, workflow.LoadRunOptions{
WithLightTests: true,
})
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions engine/api/workflow_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3160,10 +3160,13 @@ func Test_postWorkflowRunHandlerRestartOnlyFailed(t *testing.T) {
}

func Test_CheckRegionDuringInitWorkflow(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

featureflipping.Init(gorpmapping.Mapper)
api, db, router := newTestAPI(t)

existingFeat, _ := featureflipping.LoadByName(context.Background(), gorpmapping.Mapper, db, sdk.FeatureRegion)
existingFeat, _ := featureflipping.LoadByName(ctx, gorpmapping.Mapper, db, sdk.FeatureRegion)
featureflipping.Delete(db, existingFeat.ID)
f := &sdk.Feature{
Name: sdk.FeatureRegion,
Expand All @@ -3177,7 +3180,7 @@ func Test_CheckRegionDuringInitWorkflow(t *testing.T) {
u, pass := assets.InsertAdminUser(t, db)
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, api.Cache, key, key)
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
consumer, _ := authentication.LoadConsumerByTypeAndUserID(ctx, db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)

pip := sdk.Pipeline{
ProjectID: proj.ID,
Expand Down Expand Up @@ -3222,11 +3225,11 @@ func Test_CheckRegionDuringInitWorkflow(t *testing.T) {
},
}

proj2, errP := project.Load(context.TODO(), api.mustDB(), proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithGroups, project.LoadOptions.WithIntegrations)
proj2, errP := project.Load(ctx, api.mustDB(), proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithGroups, project.LoadOptions.WithIntegrations)
require.NoError(t, errP)

require.NoError(t, workflow.Insert(context.TODO(), db, api.Cache, *proj2, &w))
w1, err := workflow.Load(context.TODO(), api.mustDB(), api.Cache, *proj, "test_1", workflow.LoadOptions{})
require.NoError(t, workflow.Insert(ctx, db, api.Cache, *proj2, &w))
w1, err := workflow.Load(ctx, api.mustDB(), api.Cache, *proj, "test_1", workflow.LoadOptions{})
require.NoError(t, err)

//Prepare request
Expand Down Expand Up @@ -3265,9 +3268,9 @@ func Test_CheckRegionDuringInitWorkflow(t *testing.T) {
Number: &wr.Number,
AuthConsumerID: consumer.ID,
}
api.initWorkflowRun(context.TODO(), proj2.Key, &wr.Workflow, wr, opts)
api.initWorkflowRun(ctx, proj2.Key, &wr.Workflow, wr, opts)

wr, _ = workflow.LoadRun(context.TODO(), db, proj2.Key, w1.Name, 1, workflow.LoadRunOptions{})
wr, _ = workflow.LoadRun(ctx, db, proj2.Key, w1.Name, 1, workflow.LoadRunOptions{})

require.Equal(t, sdk.StatusFail, wr.Status)
require.Equal(t, 1, len(wr.Infos))
Expand Down

0 comments on commit 73a9ed6

Please sign in to comment.