Skip to content

Commit

Permalink
fix(api): don't try to load cds.scheduler user (#5239)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Jun 8, 2020
1 parent b9b5453 commit 37334d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
28 changes: 18 additions & 10 deletions engine/api/notification/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions engine/hooks/scheduled_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sdk/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 37334d6

Please sign in to comment.