Skip to content
Closed
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
19 changes: 19 additions & 0 deletions .dredd/hooks/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var integrationMatchID int
var workflowID int
var workflowRunID int
var workflowNodeID int
var projectAlert *db.Alert
Comment thread
fiftin marked this conversation as resolved.

var capabilities = map[string][]string{
"user": {},
Expand All @@ -51,6 +52,7 @@ var capabilities = map[string][]string{
"task": {"template"},
"schedule": {"template"},
"view": {},
"alert": {"project"},
"integration": {"project", "template"},
"integrationextractvalue": {"integration"},
"integrationmatcher": {"integration"},
Expand Down Expand Up @@ -97,6 +99,8 @@ func resolveCapability(caps []string, resolved []string, uid string) {
schedule = addSchedule()
case "view":
view = addView()
case "alert":
projectAlert = addAlert()
case "user":
userPathTestUser = addUser()
case "project":
Expand Down Expand Up @@ -256,6 +260,12 @@ var pathSubPatterns = []func() string{
func() string {
return strconv.Itoa(workflowNodeID)
}, // node_id, x-example: 20
func() string {
if projectAlert == nil {
return "0"
}
return strconv.Itoa(projectAlert.ID)
}, // alert_id, x-example: 21
}

// alterRequestPath with the above slice of functions
Expand Down Expand Up @@ -300,6 +310,15 @@ func alterRequestBody(t *trans.Transaction) {

bodyFieldProcessor("environment_id", environmentID, &request)
bodyFieldProcessor("environment_ids", []int{environmentID}, &request)
// Dredd fills integer arrays with a dummy ID. Never reuse a leftover
// projectAlert here: that alert belongs to an earlier fixture project.
bodyFieldProcessor("alert_ids", []int{}, &request)
if typ, ok := request["type"].(string); ok && typ == "telegram" {
chat, _ := request["chat_id"].(string)
if strings.TrimSpace(chat) == "" {
request["chat_id"] = "12345"
}
}
bodyFieldProcessor("inventory_id", inventoryID, &request)
bodyFieldProcessor("repository_id", repoID, &request)
bodyFieldProcessor("template_id", templateID, &request)
Expand Down
19 changes: 19 additions & 0 deletions .dredd/hooks/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func truncateAll() {
"project__user",
"user",
"project__view",
"project__alert",
"project__template_alert",
"project__schedule_alert",
"task__alert_send",
"project__integration",
"project__integration_extract_value",
"project__integration_matcher",
Expand Down Expand Up @@ -203,6 +207,21 @@ func addView() *db.View {
return &view
}

func addAlert() *db.Alert {
chatID := "12345"
alert, err := store.CreateAlert(db.Alert{
ProjectID: userProject.ID,
Name: "ITA-" + getUUID(),
Type: db.AlertTypeTelegram,
Enabled: true,
ChatID: &chatID,
})
if err != nil {
panic(err)
}
return &alert
}

func addInvite() *db.ProjectInvite {
invite, err := store.CreateProjectInvite(db.ProjectInvite{
ProjectID: userProject.ID,
Expand Down
13 changes: 12 additions & 1 deletion .dredd/hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,19 @@ func main() {
h.Before("project > /api/project/{project_id}/views/{view_id} > Updates view > 204 > application/json", capabilityWrapper("view"))
h.Before("project > /api/project/{project_id}/views/{view_id} > Removes view > 204 > application/json", capabilityWrapper("view"))

h.Before("project > /api/project/{project_id}/alerts > Create alert > 201 > application/json", func(t *trans.Transaction) {
// project_id must be present so setupObjectsAndPaths can replace it
// with the fixture project (same pattern as other POST bodies).
t.Request.Body = `{"name":"ITA-dredd","type":"telegram","enabled":true,"project_id":1,"chat_id":"12345"}`
})
h.Before("project > /api/project/{project_id}/alerts/{alert_id} > Get alert > 200 > application/json", capabilityWrapper("alert"))
h.Before("project > /api/project/{project_id}/alerts/{alert_id} > Update alert > 204 > application/json", capabilityWrapper("alert"))
h.Before("project > /api/project/{project_id}/alerts/{alert_id} > Delete alert > 204 > application/json", capabilityWrapper("alert"))
h.Before("project > /api/project/{project_id}/alerts/{alert_id}/refs > Get objects that reference this alert > 200 > application/json", capabilityWrapper("alert"))
h.Before("project > /api/project/{project_id}/alerts/{alert_id}/test > Send a test message for this alert > 204 > application/json", skipTest)

h.Before("project > /api/project/{project_id}/backup > Get backup > 200 > application/json", func(t *trans.Transaction) {
addCapabilities([]string{"repository", "inventory", "environment", "view", "template"})
addCapabilities([]string{"repository", "inventory", "environment", "view", "template", "alert"})
})

// global runners (admin)
Expand Down
227 changes: 226 additions & 1 deletion api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,20 @@ definitions:
type: boolean
suppress_error_alerts:
type: boolean
alert_mode:
type: string
enum: [default, ids]
description: default uses the project's default alerts; ids uses alert_ids (empty means silent).
alert_ids:
type: array
items:
type: integer
example: []
description: Used when alert_mode is ids. Empty means this job sends no alerts.
alert_on_success:
type: boolean
alert_on_error:
type: boolean
app:
type: string
example: ansible
Expand Down Expand Up @@ -1043,6 +1057,20 @@ definitions:
type: boolean
suppress_error_alerts:
type: boolean
alert_mode:
type: string
enum: [default, ids]
description: default uses the project's default alerts; ids uses alert_ids (empty means silent).
alert_ids:
type: array
items:
type: integer
example: []
description: Used when alert_mode is ids. Empty means this job sends no alerts.
alert_on_success:
type: boolean
alert_on_error:
type: boolean
Comment thread
coderabbitai[bot] marked this conversation as resolved.
app:
type: string
git_branch:
Expand Down Expand Up @@ -1149,6 +1177,21 @@ definitions:
enum: ['', 'run_at']
task_params:
$ref: '#/definitions/TaskPrams'
alert_mode:
type: string
enum: [inherit, ids]
description: inherit uses the template alert list; ids uses alert_ids (may be empty).
alert_ids:
type: array
items:
type: integer
example: []
alert_on_success:
type: boolean
x-nullable: true
alert_on_error:
type: boolean
x-nullable: true

Schedule:
type: object
Expand All @@ -1173,6 +1216,21 @@ definitions:
enum: ['', 'run_at']
task_params:
$ref: '#/definitions/TaskPrams'
alert_mode:
type: string
enum: [inherit, ids]
description: inherit uses the template alert list; ids uses alert_ids (may be empty).
alert_ids:
type: array
items:
type: integer
example: []
alert_on_success:
type: boolean
x-nullable: true
alert_on_error:
type: boolean
x-nullable: true

ViewRequest:
type: object
Expand Down Expand Up @@ -1211,6 +1269,44 @@ definitions:
sort_reverse:
type: boolean

Alert:
type: object
properties:
id:
type: integer
project_id:
type: integer
name:
type: string
example: Ops Telegram
type:
type: string
enum: [email, telegram, slack, teams, rocketchat, dingtalk, gotify]
example: telegram
enabled:
type: boolean
example: true
is_default:
type: boolean
description: When true, jobs with alert_mode=default send this destination.
chat_id:
type: string
example: '12345'
thread_id:
type: string
pattern: '^[1-9][0-9]*$'
description: Optional positive Telegram forum topic ID (message_thread_id).
example: '42'
url:
type: string
token:
type: string
description: Write-only Gotify token. Omitted in responses. Omit or send null on update to keep the stored token.
recipients:
type: string
body:
type: string

Runner:
type: object
properties:
Expand Down Expand Up @@ -1612,6 +1708,13 @@ parameters:
type: integer
required: true
x-example: 10
alert_id:
name: alert_id
description: alert ID
in: path
type: integer
required: true
x-example: 21
integration_id:
name: integration_id
description: integration ID
Expand Down Expand Up @@ -3427,6 +3530,128 @@ paths:
204:
description: view removed

/project/{project_id}/alerts:
parameters:
- $ref: "#/parameters/project_id"
get:
tags:
- project
summary: Get alerts
responses:
200:
description: alerts
schema:
type: array
items:
$ref: "#/definitions/Alert"
post:
tags:
- project
summary: Create alert
parameters:
- name: alert
in: body
required: true
schema:
$ref: "#/definitions/Alert"
responses:
201:
description: alert created
schema:
$ref: "#/definitions/Alert"
/project/{project_id}/alerts/defaults:
parameters:
- $ref: "#/parameters/project_id"
get:
tags:
- project
summary: Get default message templates per alert type
responses:
200:
description: default bodies keyed by alert type
schema:
type: object
properties:
email:
type: string
telegram:
type: string
slack:
type: string
teams:
type: string
rocketchat:
type: string
dingtalk:
type: string
gotify:
type: string
/project/{project_id}/alerts/{alert_id}:
parameters:
- $ref: "#/parameters/project_id"
- $ref: "#/parameters/alert_id"
get:
tags:
- project
summary: Get alert
responses:
200:
description: alert
schema:
$ref: "#/definitions/Alert"
put:
tags:
- project
summary: Update alert
parameters:
- name: alert
in: body
required: true
schema:
$ref: "#/definitions/Alert"
responses:
204:
description: alert updated
delete:
tags:
- project
summary: Delete alert
responses:
204:
description: alert removed
/project/{project_id}/alerts/{alert_id}/refs:
parameters:
- $ref: "#/parameters/project_id"
- $ref: "#/parameters/alert_id"
get:
tags:
- project
summary: Get objects that reference this alert
responses:
200:
description: referrers
schema:
type: object
properties:
templates:
type: array
items:
type: object
schedules:
type: array
items:
type: object
/project/{project_id}/alerts/{alert_id}/test:
parameters:
- $ref: "#/parameters/project_id"
- $ref: "#/parameters/alert_id"
post:
tags:
- project
summary: Send a test message for this alert
responses:
204:
description: test sent

# tasks
/project/{project_id}/tasks:
Expand Down Expand Up @@ -3600,7 +3825,7 @@ paths:
- $ref: "#/parameters/project_id"
responses:
409:
description: Alerts not enabled for the project
description: No enabled project alerts exist
# 204:
# description: Test notification dispatched (or alerts disabled)

Expand Down
Loading
Loading