Skip to content

Commit

Permalink
feat: discord channels for test/deployment failure notifications (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
smrz2001 authored Nov 7, 2024
1 parent 2e0ea0a commit 4a845aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
19 changes: 15 additions & 4 deletions cd/manager/notifs/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,36 @@ func parseDiscordWebhookUrl(urlEnv string) (webhook.Client, error) {
return nil, nil
}

func webhooksForLabels(labels []string) ([]webhook.Client, error) {
func webhooksForLabels(labels []string) ([]webhook.Client, []webhook.Client, error) {
webhooks := make([]webhook.Client, 0)
failureWebhooks := make([]webhook.Client, 0)
for _, label := range labels {
switch label {
case job.WorkflowJobLabel_Test:
if t, err := parseDiscordWebhookUrl("DISCORD_TESTS_WEBHOOK"); err != nil {
return nil, err
return nil, nil, err
} else {
webhooks = append(webhooks, t)
}
if f, err := parseDiscordWebhookUrl("DISCORD_TEST_FAILURES_WEBHOOK"); err != nil {
return nil, nil, err
} else {
failureWebhooks = append(failureWebhooks, f)
}
case job.WorkflowJobLabel_Deploy:
if t, err := parseDiscordWebhookUrl("DISCORD_DEPLOYMENTS_WEBHOOK"); err != nil {
return nil, err
return nil, nil, err
} else {
webhooks = append(webhooks, t)
}
if f, err := parseDiscordWebhookUrl("DISCORD_DEPLOYMENT_FAILURES_WEBHOOK"); err != nil {
return nil, nil, err
} else {
failureWebhooks = append(failureWebhooks, f)
}
}
}
return webhooks, nil
return webhooks, failureWebhooks, nil
}

func (n JobNotifs) NotifyJob(jobs ...job.JobState) {
Expand Down
16 changes: 10 additions & 6 deletions cd/manager/notifs/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@ const (
)

type workflowNotif struct {
state job.JobState
workflow job.Workflow
workflowWebhooks []webhook.Client
state job.JobState
workflow job.Workflow
webhooks []webhook.Client
failureWebhooks []webhook.Client
}

func newWorkflowNotif(jobState job.JobState) (jobNotif, error) {
if workflow, err := job.CreateWorkflowJob(jobState); err != nil {
return nil, err
} else if webhooks, err := webhooksForLabels(workflow.Labels); err != nil {
} else if webhooks, failureWebhooks, err := webhooksForLabels(workflow.Labels); err != nil {
return nil, err
} else {
return &workflowNotif{jobState, workflow, webhooks}, nil
return &workflowNotif{jobState, workflow, webhooks, failureWebhooks}, nil
}
}

func (w workflowNotif) getChannels() []webhook.Client {
if w.state.Stage == job.JobStage_Failed {
return w.failureWebhooks
} else
// Skip "started" notifications so that the channel doesn't get too noisy
if w.state.Stage != job.JobStage_Started {
return w.workflowWebhooks
return w.webhooks
}
return nil
}
Expand Down

0 comments on commit 4a845aa

Please sign in to comment.