From 37334d60650480d83ee590c995d85c3982b1e77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Mon, 8 Jun 2020 18:06:31 +0200 Subject: [PATCH] fix(api): don't try to load cds.scheduler user (#5239) Signed-off-by: francois samin --- engine/api/notification/user.go | 28 ++++++++++++++++++---------- engine/hooks/scheduled_task.go | 4 ++-- sdk/hook.go | 2 ++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/engine/api/notification/user.go b/engine/api/notification/user.go index 76767fb533..eb70e65213 100644 --- a/engine/api/notification/user.go +++ b/engine/api/notification/user.go @@ -23,6 +23,13 @@ func Init(uiurl string) { uiURL = uiurl } +const ( + paramsAuthorEmail = "cds.author.email" + paramsAuthorName = "cds.author" + paramsStatus = "cds.status" + paramsBuildURL = "cds.buildURL" +) + // GetUserWorkflowEvents return events to send for the given workflow run func GetUserWorkflowEvents(ctx context.Context, db gorp.SqlExecutor, store cache.Store, projectID int64, projectKey, workflowName string, notifs []sdk.WorkflowNotification, previousWR *sdk.WorkflowNodeRun, nr sdk.WorkflowNodeRun) []sdk.EventNotif { events := []sdk.EventNotif{} @@ -33,18 +40,18 @@ func GetUserWorkflowEvents(ctx context.Context, db gorp.SqlExecutor, store cache params[p.Name] = p.Value } //Set PipelineBuild UI URL - params["cds.buildURL"] = fmt.Sprintf("%s/project/%s/workflow/%s/run/%d", uiURL, projectKey, workflowName, nr.Number) + params[paramsBuildURL] = fmt.Sprintf("%s/project/%s/workflow/%s/run/%d", uiURL, projectKey, workflowName, nr.Number) if p, ok := params["cds.triggered_by.email"]; ok { - params["cds.author.email"] = p + params[paramsAuthorEmail] = p } else if p, ok := params["git.author.email"]; ok { - params["cds.author.email"] = p + params[paramsAuthorEmail] = p } if p, ok := params["cds.triggered_by.username"]; ok { - params["cds.author"] = p + params[paramsAuthorName] = p } else if p, ok := params["git.author"]; ok { - params["cds.author"] = p + params[paramsAuthorName] = p } - params["cds.status"] = nr.Status + params[paramsStatus] = nr.Status for _, notif := range notifs { if ShouldSendUserWorkflowNotification(ctx, notif, nr, previousWR) { @@ -68,7 +75,7 @@ func GetUserWorkflowEvents(ctx context.Context, db gorp.SqlExecutor, store cache } } if jn.SendToAuthor == nil || *jn.SendToAuthor { - if author, ok := params["cds.author.email"]; ok { + if author, ok := params[paramsAuthorEmail]; ok { jn.Recipients = append(jn.Recipients, author) } } @@ -102,13 +109,14 @@ func GetUserWorkflowEvents(ctx context.Context, db gorp.SqlExecutor, store cache } } if jn.SendToAuthor == nil || *jn.SendToAuthor { - if email, ok := params["cds.author.email"]; ok { + if email, ok := params[paramsAuthorEmail]; ok { jn.Recipients = append(jn.Recipients, email) - } else if author, okA := params["cds.author"]; okA && author != "" { + } else if author, okA := params[paramsAuthorName]; okA && + author != "" && author != sdk.SchedulerUsername { // Load the user au, err := user.LoadByUsername(ctx, db, author) if err != nil { - log.Warning(ctx, "notification[Email].GetUserWorkflowEvents> Cannot load author %s: %s", author, err) + log.Error(ctx, "notification[Email].GetUserWorkflowEvents> Cannot load author %s: %s", author, err) continue } jn.Recipients = append(jn.Recipients, au.GetEmail()) diff --git a/engine/hooks/scheduled_task.go b/engine/hooks/scheduled_task.go index a16a53d482..ad85964436 100644 --- a/engine/hooks/scheduled_task.go +++ b/engine/hooks/scheduled_task.go @@ -49,8 +49,8 @@ func (s *Service) doScheduledTaskExecution(ctx context.Context, t *sdk.TaskExecu payloadValues[k] = v.Value } } - payloadValues["cds.triggered_by.username"] = "cds.scheduler" - payloadValues["cds.triggered_by.fullname"] = "CDS Scheduler" + payloadValues["cds.triggered_by.username"] = sdk.SchedulerUsername + payloadValues["cds.triggered_by.fullname"] = sdk.SchedulerFullname h.Payload = payloadValues return &h, nil diff --git a/sdk/hook.go b/sdk/hook.go index c1bf084842..acbad69ec1 100644 --- a/sdk/hook.go +++ b/sdk/hook.go @@ -36,6 +36,8 @@ const ( RabbitMQHookModelExchangeType = "exchange_type" RabbitMQHookModelExchangeName = "exchange_name" RabbitMQHookModelConsumerTag = "consumer_tag" + SchedulerUsername = "cds.scheduler" + SchedulerFullname = "CDS Scheduler" ) // Here are the default hooks