Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send notification about stage executions #5440

Merged
merged 8 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pkg/app/piped/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,14 @@
))
defer span.End()

s.notifyStageStartEvent(ps)

Check warning on line 349 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L348-L349

Added lines #L348 - L349 were not covered by tests
result = s.executeStage(sig, *ps, func(in executor.Input) (executor.Executor, bool) {
return s.executorRegistry.Executor(model.Stage(ps.Name), in)
})

s.notifyStageEndEvent(ps, result)

Check warning on line 355 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L354-L355

Added lines #L354 - L355 were not covered by tests
switch result {
case model.StageStatus_STAGE_SUCCESS:
span.SetStatus(codes.Ok, statusReason)
Expand Down Expand Up @@ -450,10 +454,14 @@
))
defer span.End()

s.notifyStageStartEvent(&rbs)

Check warning on line 458 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L457-L458

Added lines #L457 - L458 were not covered by tests
result := s.executeStage(sig, rbs, func(in executor.Input) (executor.Executor, bool) {
return s.executorRegistry.RollbackExecutor(s.deployment.Kind, in)
})

s.notifyStageEndEvent(&rbs, result)

Check warning on line 464 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L463-L464

Added lines #L463 - L464 were not covered by tests
switch result {
case model.StageStatus_STAGE_SUCCESS:
span.SetStatus(codes.Ok, statusReason)
Expand Down Expand Up @@ -850,3 +858,52 @@
func (a appAnalysisResultStore) PutLatestAnalysisResult(ctx context.Context, analysisResult *model.AnalysisResult) error {
return a.store.PutLatestAnalysisResult(ctx, a.applicationID, analysisResult)
}

// notifyStageStartEvent sends notification evnet STAGE_STARTED
func (s *scheduler) notifyStageStartEvent(stage *model.PipelineStage) {
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_STARTED,
Metadata: &model.NotificationEventStageStarted{
Deployment: s.deployment,
Stage: stage,
},
})

Check warning on line 870 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L863-L870

Added lines #L863 - L870 were not covered by tests
}

// notifyStageEndEvent sends notification event based on the stage result.
func (s *scheduler) notifyStageEndEvent(stage *model.PipelineStage, result model.StageStatus) {
switch result {
case model.StageStatus_STAGE_SUCCESS, model.StageStatus_STAGE_EXITED: // Exit stage is treated as success.
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_SUCCEEDED,
Metadata: &model.NotificationEventStageSucceeded{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_FAILURE:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_FAILED,
Metadata: &model.NotificationEventStageFailed{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_CANCELLED:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_CANCELLED,
Metadata: &model.NotificationEventStageCancelled{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_SKIPPED:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_SKIPPED,
Metadata: &model.NotificationEventStageSkipped{
Deployment: s.deployment,
Stage: stage,
},
})

Check warning on line 907 in pkg/app/piped/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/controller/scheduler.go#L874-L907

Added lines #L874 - L907 were not covered by tests
}
}
45 changes: 45 additions & 0 deletions pkg/app/piped/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@
}
}

generateStageEventData := func(d *model.Deployment, s *model.PipelineStage, accounts []string, groups []string) {
accountsStr := getAccountsAsString(accounts)
groupsStr := getGroupsAsString(groups)
link = fmt.Sprintf("%s/deployments/%s?project=%s", webURL, d.Id, d.ProjectId)
fields = []slackField{
{"Project", truncateText(d.ProjectId, 8), true},
{"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true},
{"Kind", strings.ToLower(d.Kind.String()), true},
{"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true},
{"Stage", s.Name, true},
{"Triggered By", d.TriggeredBy(), true},
{"Mention To Users", accountsStr, true},
{"Mention To Groups", groupsStr, true},
}
}

Check warning on line 272 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L258-L272

Added lines #L258 - L272 were not covered by tests

switch event.Type {
case model.NotificationEventType_EVENT_DEPLOYMENT_TRIGGERED:
md := event.Metadata.(*model.NotificationEventDeploymentTriggered)
Expand Down Expand Up @@ -337,6 +353,35 @@
title = "A piped has been stopped"
generatePipedEventData(md.Id, md.Name, md.Version, md.ProjectId, s.config.MentionedAccounts, s.config.MentionedGroups)

case model.NotificationEventType_EVENT_STAGE_STARTED:
md := event.Metadata.(*model.NotificationEventStageStarted)
title = fmt.Sprintf("Stage %q was started", md.Stage.Name)
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 359 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L356-L359

Added lines #L356 - L359 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_SKIPPED:
md := event.Metadata.(*model.NotificationEventStageSkipped)
title = fmt.Sprintf("Stage %q was skipped", md.Stage.Name)
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 364 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L361-L364

Added lines #L361 - L364 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_SUCCEEDED:
md := event.Metadata.(*model.NotificationEventStageSucceeded)
title = fmt.Sprintf("Stage %q was completed successfully", md.Stage.Name)
color = slackSuccessColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 370 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L366-L370

Added lines #L366 - L370 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_FAILED:
md := event.Metadata.(*model.NotificationEventStageFailed)
title = fmt.Sprintf("Stage %q was failed", md.Stage.Name)
text = md.Stage.StatusReason
color = slackErrorColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 377 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L372-L377

Added lines #L372 - L377 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_CANCELLED:
md := event.Metadata.(*model.NotificationEventStageCancelled)
title = fmt.Sprintf("Stage %q was cancelled", md.Stage.Name)
color = slackWarnColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 383 in pkg/app/piped/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/notifier/slack.go#L379-L383

Added lines #L379 - L383 were not covered by tests

// TODO: Support application type of notification event.
default:
return slackMessage{}, false
Expand Down
57 changes: 57 additions & 0 deletions pkg/app/pipedv1/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,12 @@
))
defer span.End()

s.notifyStageStartEvent(ps)

Check warning on line 324 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L323-L324

Added lines #L323 - L324 were not covered by tests
result = s.executeStage(sig, ps)

s.notifyStageEndEvent(ps, result)

Check warning on line 328 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L327-L328

Added lines #L327 - L328 were not covered by tests
switch result {
case model.StageStatus_STAGE_SUCCESS:
span.SetStatus(codes.Ok, statusReason)
Expand Down Expand Up @@ -423,8 +427,12 @@
))
defer span.End()

s.notifyStageStartEvent(rbs)

Check warning on line 431 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L430-L431

Added lines #L430 - L431 were not covered by tests
result := s.executeStage(sig, rbs)

s.notifyStageEndEvent(rbs, result)

Check warning on line 435 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L434-L435

Added lines #L434 - L435 were not covered by tests
switch result {
case model.StageStatus_STAGE_SUCCESS:
span.SetStatus(codes.Ok, statusReason)
Expand Down Expand Up @@ -759,3 +767,52 @@

return err
}

// notifyStageStartEvent sends notification evnet STAGE_STARTED
func (s *scheduler) notifyStageStartEvent(stage *model.PipelineStage) {
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_STARTED,
Metadata: &model.NotificationEventStageStarted{
Deployment: s.deployment,

Check warning on line 776 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L770-L776

Added lines #L770 - L776 were not covered by tests
Stage: stage,
},
})
}

// notifyStageEndEvent sends notification event based on the stage result.
func (s *scheduler) notifyStageEndEvent(stage *model.PipelineStage, result model.StageStatus) {
switch result {
case model.StageStatus_STAGE_SUCCESS, model.StageStatus_STAGE_EXITED: // Exit stage is treated as success.
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_SUCCEEDED,
Metadata: &model.NotificationEventStageSucceeded{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_FAILURE:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_FAILED,
Metadata: &model.NotificationEventStageFailed{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_CANCELLED:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_CANCELLED,
Metadata: &model.NotificationEventStageCancelled{
Deployment: s.deployment,
Stage: stage,
},
})
case model.StageStatus_STAGE_SKIPPED:
s.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_STAGE_SKIPPED,
Metadata: &model.NotificationEventStageSkipped{
Deployment: s.deployment,

Check warning on line 813 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L780-L813

Added lines #L780 - L813 were not covered by tests
Stage: stage,
},
})
}
}
45 changes: 45 additions & 0 deletions pkg/app/pipedv1/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@
}
}

generateStageEventData := func(d *model.Deployment, s *model.PipelineStage, accounts []string, groups []string) {
accountsStr := getAccountsAsString(accounts)
groupsStr := getGroupsAsString(groups)
link = fmt.Sprintf("%s/deployments/%s?project=%s", webURL, d.Id, d.ProjectId)
fields = []slackField{
{"Project", truncateText(d.ProjectId, 8), true},
{"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true},
{"Kind", strings.ToLower(d.Kind.String()), true},
{"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true},
{"Stage", s.Name, true},
{"Triggered By", d.TriggeredBy(), true},
{"Mention To Users", accountsStr, true},
{"Mention To Groups", groupsStr, true},
}
}

Check warning on line 272 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L258-L272

Added lines #L258 - L272 were not covered by tests

switch event.Type {
case model.NotificationEventType_EVENT_DEPLOYMENT_TRIGGERED:
md := event.Metadata.(*model.NotificationEventDeploymentTriggered)
Expand Down Expand Up @@ -337,6 +353,35 @@
title = "A piped has been stopped"
generatePipedEventData(md.Id, md.Name, md.Version, md.ProjectId, s.config.MentionedAccounts, s.config.MentionedGroups)

case model.NotificationEventType_EVENT_STAGE_STARTED:
md := event.Metadata.(*model.NotificationEventStageStarted)
title = fmt.Sprintf("Stage %q was started", md.Stage.Name)
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 359 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L356-L359

Added lines #L356 - L359 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_SKIPPED:
md := event.Metadata.(*model.NotificationEventStageSkipped)
title = fmt.Sprintf("Stage %q was skipped", md.Stage.Name)
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 364 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L361-L364

Added lines #L361 - L364 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_SUCCEEDED:
md := event.Metadata.(*model.NotificationEventStageSucceeded)
title = fmt.Sprintf("Stage %q was completed successfully", md.Stage.Name)
color = slackSuccessColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 370 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L366-L370

Added lines #L366 - L370 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_FAILED:
md := event.Metadata.(*model.NotificationEventStageFailed)
title = fmt.Sprintf("Stage %q was failed", md.Stage.Name)
text = md.Stage.StatusReason
color = slackErrorColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 377 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L372-L377

Added lines #L372 - L377 were not covered by tests

case model.NotificationEventType_EVENT_STAGE_CANCELLED:
md := event.Metadata.(*model.NotificationEventStageCancelled)
title = fmt.Sprintf("Stage %q was cancelled", md.Stage.Name)
color = slackWarnColor
generateStageEventData(md.Deployment, md.Stage, s.config.MentionedAccounts, s.config.MentionedGroups)

Check warning on line 383 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L379-L383

Added lines #L379 - L383 were not covered by tests

// TODO: Support application type of notification event.
default:
return slackMessage{}, false
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/notificationevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (e NotificationEvent) Group() NotificationEventGroup {
return NotificationEventGroup_EVENT_APPLICATION_HEALTH
case e.Type < 400:
return NotificationEventGroup_EVENT_PIPED
case e.Type < 500:
return NotificationEventGroup_EVENT_STAGE
default:
return NotificationEventGroup_EVENT_NONE
}
Expand Down
Loading