@@ -169,19 +169,19 @@ func AssignedToAnyUsers(i pagerduty.Incident, ids []string) bool {
169169
170170// ShouldBeAcknowledged returns true if the incident is assigned to the given user,
171171// the user has not acknowledged the incident yet, and autoAcknowledge is enabled
172- func ShouldBeAcknowledged (i pagerduty.Incident , id string , autoAcknowledge bool ) bool {
172+ func ShouldBeAcknowledged (p * pd. Config , i pagerduty.Incident , id string , autoAcknowledge bool ) bool {
173173 assigned := AssignedToUser (i , id )
174174 acknowledged := AcknowledgedByUser (i , id )
175- doIt := assigned && ! acknowledged && autoAcknowledge
176- if doIt {
177- log .Debug (
178- "commands.ShouldBeAcknowledged" ,
179- "assigned" , assigned ,
180- "acknowledged" , acknowledged ,
181- "autoAcknowledge" , autoAcknowledge ,
182- "doIt " , doIt ,
183- )
184- }
175+ userIsOnCall := UserIsOnCall ( p , id )
176+ doIt := assigned && ! acknowledged && autoAcknowledge && userIsOnCall
177+ log .Debug (
178+ "commands.ShouldBeAcknowledged" ,
179+ "assigned" , assigned ,
180+ "acknowledged" , acknowledged ,
181+ "autoAcknowledge" , autoAcknowledge ,
182+ "userIsOnCall " , userIsOnCall ,
183+ "doIt" , doIt ,
184+ )
185185 return AssignedToUser (i , id ) && ! AcknowledgedByUser (i , id ) && autoAcknowledge
186186}
187187
@@ -205,6 +205,43 @@ func AcknowledgedByUser(i pagerduty.Incident, id string) bool {
205205 return false
206206}
207207
208+ // UserIsOnCall returns true if the current time is between any of the current user's pagerduty.OnCalls in the next six hours
209+ func UserIsOnCall (p * pd.Config , id string ) bool {
210+ var timeLayout = "2006-01-02T15:04:05Z"
211+ opts := pagerduty.ListOnCallOptions {
212+ UserIDs : []string {id },
213+ Since : time .Now ().String (),
214+ Until : time .Now ().Add (time .Hour * 6 ).String (),
215+ }
216+
217+ onCalls , err := pd .GetUserOnCalls (p .Client , id , opts )
218+ if err != nil {
219+ log .Debug ("commands.UserIsOnCall" , "error" , err )
220+ return false
221+ }
222+
223+ for _ , o := range onCalls {
224+ log .Debug ("commands.UserIsOnCall" , "on-call" , o )
225+
226+ start , err := time .Parse (timeLayout , o .Start )
227+ if err != nil {
228+ log .Debug ("commands.UserIsOnCall" , "error parsing on-call start time" , err )
229+ return false
230+ }
231+ end , err := time .Parse (timeLayout , o .End )
232+ if err != nil {
233+ log .Debug ("commands.UserIsOnCall" , "error parsing on-call end time" , err )
234+ return false
235+ }
236+
237+ if start .Before (time .Now ()) && end .After (time .Now ()) {
238+ return true
239+ }
240+ }
241+
242+ return false
243+ }
244+
208245// TODO: Can we use a single function and struct to handle
209246// the openEditorCmd, login and openBrowserCmd commands?
210247
0 commit comments