github.com/wtfutil/wtf@v0.43.0/modules/pagerduty/client.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/PagerDuty/go-pagerduty"
     7  )
     8  
     9  const (
    10  	queryTimeFmt = "2006-01-02T15:04:05Z07:00"
    11  )
    12  
    13  // GetOnCalls returns a list of people currently on call
    14  func GetOnCalls(apiKey string, scheduleIDs []string) ([]pagerduty.OnCall, error) {
    15  	client := pagerduty.NewClient(apiKey)
    16  
    17  	var results []pagerduty.OnCall
    18  	var queryOpts pagerduty.ListOnCallOptions
    19  
    20  	queryOpts.ScheduleIDs = scheduleIDs
    21  	queryOpts.Since = time.Now().Format(queryTimeFmt)
    22  	queryOpts.Until = time.Now().Format(queryTimeFmt)
    23  
    24  	oncalls, err := client.ListOnCalls(queryOpts)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	results = append(results, oncalls.OnCalls...)
    30  
    31  	for oncalls.APIListObject.More {
    32  		queryOpts.APIListObject.Offset = oncalls.APIListObject.Offset
    33  		oncalls, err = client.ListOnCalls(queryOpts)
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  		results = append(results, oncalls.OnCalls...)
    38  	}
    39  
    40  	return results, nil
    41  }
    42  
    43  // GetIncidents returns a list of unresolved incidents
    44  func GetIncidents(apiKey string, teamIDs []string, userIDs []string) ([]pagerduty.Incident, error) {
    45  	client := pagerduty.NewClient(apiKey)
    46  
    47  	var results []pagerduty.Incident
    48  
    49  	var queryOpts pagerduty.ListIncidentsOptions
    50  	queryOpts.DateRange = "all"
    51  	queryOpts.Statuses = []string{"triggered", "acknowledged"}
    52  	queryOpts.TeamIDs = teamIDs
    53  	queryOpts.UserIDs = userIDs
    54  
    55  	items, err := client.ListIncidents(queryOpts)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	results = append(results, items.Incidents...)
    60  
    61  	for items.APIListObject.More {
    62  		queryOpts.APIListObject.Offset = items.APIListObject.Offset
    63  		items, err = client.ListIncidents(queryOpts)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		results = append(results, items.Incidents...)
    68  	}
    69  
    70  	return results, nil
    71  }