11package incident
22
33import (
4+ "context"
45 "fmt"
56 "github.com/icinga/icinga-notifications/internal/event"
67 "github.com/icinga/icinga-notifications/internal/recipient"
@@ -14,7 +15,7 @@ import (
1415// Sync initiates an *incident.IncidentRow from the current incident state and syncs it with the database.
1516// Before syncing any incident related database entries, this method should be called at least once.
1617// Returns an error on db failure.
17- func (i * Incident ) Sync (tx * sqlx.Tx ) error {
18+ func (i * Incident ) Sync (ctx context. Context , tx * sqlx.Tx ) error {
1819 incidentRow := & IncidentRow {
1920 ID : i .incidentRowID ,
2021 ObjectID : i .Object .ID ,
@@ -23,7 +24,7 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
2324 Severity : i .Severity (),
2425 }
2526
26- err := incidentRow .Sync (tx , i .db , i .incidentRowID != 0 )
27+ err := incidentRow .Sync (ctx , tx , i .db , i .incidentRowID != 0 )
2728 if err != nil {
2829 return err
2930 }
@@ -33,19 +34,19 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
3334 return nil
3435}
3536
36- func (i * Incident ) AddHistory (tx * sqlx.Tx , historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
37+ func (i * Incident ) AddHistory (ctx context. Context , tx * sqlx.Tx , historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
3738 historyRow .IncidentID = i .incidentRowID
3839
3940 stmt := utils .BuildInsertStmtWithout (i .db , historyRow , "id" )
4041 if fetchId {
41- historyId , err := utils .InsertAndFetchId (tx , stmt , historyRow )
42+ historyId , err := utils .InsertAndFetchId (ctx , tx , stmt , historyRow )
4243 if err != nil {
4344 return types.Int {}, err
4445 }
4546
4647 return utils .ToDBInt (historyId ), nil
4748 } else {
48- _ , err := tx .NamedExec ( stmt , historyRow )
49+ _ , err := tx .NamedExecContext ( ctx , stmt , historyRow )
4950 if err != nil {
5051 return types.Int {}, err
5152 }
@@ -54,30 +55,30 @@ func (i *Incident) AddHistory(tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool)
5455 return types.Int {}, nil
5556}
5657
57- func (i * Incident ) AddEscalationTriggered (tx * sqlx.Tx , state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
58+ func (i * Incident ) AddEscalationTriggered (ctx context. Context , tx * sqlx.Tx , state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
5859 state .IncidentID = i .incidentRowID
5960
6061 stmt , _ := i .db .BuildUpsertStmt (state )
61- _ , err := tx .NamedExec ( stmt , state )
62+ _ , err := tx .NamedExecContext ( ctx , stmt , state )
6263 if err != nil {
6364 return types.Int {}, err
6465 }
6566
66- return i .AddHistory (tx , hr , true )
67+ return i .AddHistory (ctx , tx , hr , true )
6768}
6869
6970// AddEvent Inserts incident history record to the database and returns an error on db failure.
70- func (i * Incident ) AddEvent (tx * sqlx.Tx , ev * event.Event ) error {
71+ func (i * Incident ) AddEvent (ctx context. Context , tx * sqlx.Tx , ev * event.Event ) error {
7172 ie := & EventRow {IncidentID : i .incidentRowID , EventID : ev .ID }
7273 stmt , _ := i .db .BuildInsertStmt (ie )
73- _ , err := tx .NamedExec ( stmt , ie )
74+ _ , err := tx .NamedExecContext ( ctx , stmt , ie )
7475
7576 return err
7677}
7778
7879// AddRecipient adds recipient from the given *rule.Escalation to this incident.
7980// Syncs also all the recipients with the database and returns an error on db failure.
80- func (i * Incident ) AddRecipient (tx * sqlx.Tx , escalation * rule.Escalation , eventId int64 ) error {
81+ func (i * Incident ) AddRecipient (ctx context. Context , tx * sqlx.Tx , escalation * rule.Escalation , eventId int64 ) error {
8182 newRole := RoleRecipient
8283 if i .HasManager () {
8384 newRole = RoleSubscriber
@@ -110,7 +111,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
110111 OldRecipientRole : oldRole ,
111112 }
112113
113- _ , err := i .AddHistory (tx , hr , false )
114+ _ , err := i .AddHistory (ctx , tx , hr , false )
114115 if err != nil {
115116 return err
116117 }
@@ -119,7 +120,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
119120 }
120121
121122 stmt , _ := i .db .BuildUpsertStmt (cr )
122- _ , err := tx .NamedExec ( stmt , cr )
123+ _ , err := tx .NamedExecContext ( ctx , stmt , cr )
123124 if err != nil {
124125 return fmt .Errorf ("failed to upsert incident contact %s: %w" , r , err )
125126 }
@@ -130,18 +131,18 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
130131
131132// AddRuleMatchedHistory syncs the given *rule.Rule and history entry to the database.
132133// Returns an error on database failure.
133- func (i * Incident ) AddRuleMatchedHistory (tx * sqlx.Tx , r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
134+ func (i * Incident ) AddRuleMatchedHistory (ctx context. Context , tx * sqlx.Tx , r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
134135 rr := & RuleRow {IncidentID : i .incidentRowID , RuleID : r .ID }
135136 stmt , _ := i .db .BuildUpsertStmt (rr )
136- _ , err := tx .NamedExec ( stmt , rr )
137+ _ , err := tx .NamedExecContext ( ctx , stmt , rr )
137138 if err != nil {
138139 return types.Int {}, err
139140 }
140141
141- return i .AddHistory (tx , hr , true )
142+ return i .AddHistory (ctx , tx , hr , true )
142143}
143144
144- func (i * Incident ) AddSourceSeverity (tx * sqlx.Tx , severity event.Severity , sourceID int64 ) error {
145+ func (i * Incident ) AddSourceSeverity (ctx context. Context , tx * sqlx.Tx , severity event.Severity , sourceID int64 ) error {
145146 i .SeverityBySource [sourceID ] = severity
146147
147148 sourceSeverity := & SourceSeverity {
@@ -151,7 +152,7 @@ func (i *Incident) AddSourceSeverity(tx *sqlx.Tx, severity event.Severity, sourc
151152 }
152153
153154 stmt , _ := i .db .BuildUpsertStmt (sourceSeverity )
154- _ , err := tx .NamedExec ( stmt , sourceSeverity )
155+ _ , err := tx .NamedExecContext ( ctx , stmt , sourceSeverity )
155156
156157 return err
157158}
0 commit comments