diff --git a/go.mod b/go.mod index 97977f1..cfc307c 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/trustpilot/pagerduty-notifier go 1.13 require ( - github.com/PagerDuty/go-pagerduty v1.1.2 - github.com/go-ini/ini v1.55.0 + github.com/PagerDuty/go-pagerduty v1.5.0 + github.com/go-ini/ini v1.66.4 github.com/shurcooL/trayhost v0.0.0-20181020202213-114974ef9e16 - github.com/smartystreets/goconvey v1.6.4 // indirect - gopkg.in/ini.v1 v1.55.0 // indirect ) + +require github.com/google/go-querystring v1.1.0 // indirect diff --git a/go.sum b/go.sum index 1b822ed..0d913eb 100644 --- a/go.sum +++ b/go.sum @@ -1,43 +1,37 @@ -github.com/PagerDuty/go-pagerduty v1.1.2 h1:pTY5GKmmR88EeeI+9/LR+dKL2Chohz3L5yroqoUl+lQ= -github.com/PagerDuty/go-pagerduty v1.1.2/go.mod h1:ZKUzEnyuEMTCMwuzP5NyQIwPx+ThSKBNUva2/ns0Op8= +github.com/PagerDuty/go-pagerduty v1.5.0 h1:/p8FGD32G8HGm7MQIjlTPTGXRJ62Qkm8Lmt5BcUVJOo= +github.com/PagerDuty/go-pagerduty v1.5.0/go.mod h1:txr8VbObXdk2RkqF+C2an4qWssdGY99fK26XYUDjh+4= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/go-ini/ini v1.55.0 h1:0wVcG9udk2C3TGgmdIGKK9ScOZHZB5nbG+gwji9fhhc= -github.com/go-ini/ini v1.55.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= -github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= +github.com/go-ini/ini v1.66.4 h1:dKjMqkcbkzfddhIhyglTPgMoJnkvmG+bSLrU9cTHc5M= +github.com/go-ini/ini v1.66.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/shurcooL/trayhost v0.0.0-20181020202213-114974ef9e16 h1:7EdFnfVUAu1Rv5bNVjaygc2dWu7RiZV804h3qOJtlbM= github.com/shurcooL/trayhost v0.0.0-20181020202213-114974ef9e16/go.mod h1:jCGK0NF4lbbRrPGyl1Eonb4Xicbeiy2Ry6okvvZWlnQ= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= -github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/ini.v1 v1.55.0 h1:E8yzL5unfpW3M6fz/eB7Cb5MQAYSZ7GKo4Qth+N2sgQ= -gopkg.in/ini.v1 v1.55.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/pd.go b/src/pd.go index 51be3e7..2d794d5 100644 --- a/src/pd.go +++ b/src/pd.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "fmt" "log" "os" @@ -45,8 +46,11 @@ func pdInit(cfg *ini.File) { } } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2) + defer cancel() + // get team ids - teamList, err := pd.ListTeams(pagerduty.ListTeamOptions{Query: ""}) + teamList, err := pd.ListTeamsWithContext(ctx, pagerduty.ListTeamOptions{Query: ""}) if err != nil { log.Println("Error: Cannot retrieve teams from Pagerduty API") os.Exit(1) @@ -63,7 +67,7 @@ func pdInit(cfg *ini.File) { } // get use ids - userList, err := pd.ListUsers(pagerduty.ListUsersOptions{Query: ""}) + userList, err := pd.ListUsersWithContext(ctx, pagerduty.ListUsersOptions{Query: ""}) if err != nil { log.Println("Error: Cannot retrieve user list from Pagerduty API") os.Exit(1) @@ -87,17 +91,15 @@ func pdInit(cfg *ini.File) { ok := true opts := pagerduty.ListServiceOptions{ - APIListObject: pagerduty.APIListObject{ - Limit: 25, - Offset: 0, - }, + Limit: 25, + Offset: 0, } for k := range serviceConf { for ok { // get service ids that match given name opts.Query = k - serviceList, err := pd.ListServices(opts) + serviceList, err := pd.ListServicesWithContext(ctx, opts) if err != nil { log.Println("Error: Cannot retrieve service list from Pagerduty API") os.Exit(1) @@ -135,7 +137,7 @@ func pdGetIncidents(cfg *ini.File) []pagerduty.Incident { INCIDENTS: for _, i := range pdGetIncidentsSince(lastdate) { lastdate, _ = time.Parse(time.RFC3339, i.CreatedAt) - log.Printf("Incident: %s", i.APIObject.Summary) + log.Printf("Incident: %s", i.Summary) for _, team := range i.Teams { log.Printf("Team: %s", team.Summary) } @@ -149,16 +151,16 @@ INCIDENTS: switch filter.property { case "service": if (filter.notmatch && (i.Service.Summary != filter.match)) || (!filter.notmatch && (i.Service.Summary == filter.match)) { - if filter.filter.Find([]byte(i.APIObject.Summary)) != nil { - log.Printf("Included - service:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.APIObject.Summary) + if filter.filter.Find([]byte(i.Summary)) != nil { + log.Printf("Included - service:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.Summary) goto EXCLUDES } } case "team": for _, t := range i.Teams { if (filter.notmatch && (t.Summary != filter.match)) || (!filter.notmatch && (t.Summary == filter.match)) { - if filter.filter.Find([]byte(i.APIObject.Summary)) != nil { - log.Printf("Included - team:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.APIObject.Summary) + if filter.filter.Find([]byte(i.Summary)) != nil { + log.Printf("Included - team:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.Summary) goto EXCLUDES } } @@ -175,16 +177,16 @@ INCIDENTS: switch filter.property { case "service": if (filter.notmatch && i.Service.Summary != filter.match) || (!filter.notmatch && (i.Service.Summary == filter.match)) { - if filter.filter.Find([]byte(i.APIObject.Summary)) != nil { - log.Printf("Excluded - service:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.APIObject.Summary) + if filter.filter.Find([]byte(i.Summary)) != nil { + log.Printf("Excluded - service:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.Summary) continue INCIDENTS } } case "team": for _, t := range i.Teams { if (filter.notmatch && t.Summary != filter.match) || (!filter.notmatch && (t.Summary == filter.match)) { - if filter.filter.Find([]byte(i.APIObject.Summary)) != nil { - log.Printf("Excluded - team:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.APIObject.Summary) + if filter.filter.Find([]byte(i.Summary)) != nil { + log.Printf("Excluded - team:%v, notmatch: %t, alert:<%s>", filter.match, filter.notmatch, i.Summary) continue INCIDENTS } } @@ -203,10 +205,11 @@ INCIDENTS: } func pdGetIncidentsSince(since time.Time) []pagerduty.Incident { - incidents := make([]pagerduty.Incident, 0) opts := pagerduty.ListIncidentsOptions{ + Limit: 25, + Offset: 0, Since: since.Format(time.RFC3339), Statuses: statuses, TeamIDs: teamIDs, @@ -214,24 +217,23 @@ func pdGetIncidentsSince(since time.Time) []pagerduty.Incident { ServiceIDs: serviceIDs, SortBy: "created_at:ASC", TimeZone: "UTC", - APIListObject: pagerduty.APIListObject{ - Limit: 25, - Offset: 0, - }, } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2) + defer cancel() + ok := true for ok { - log.Printf("API query since: %s, Limit: %v Offset: %v", since, opts.APIListObject.Limit, opts.APIListObject.Offset) - resp, err := pd.ListIncidents(opts) + log.Printf("API query since: %s, Limit: %v Offset: %v", since, opts.Limit, opts.Offset) + resp, err := pd.ListIncidentsWithContext(ctx, opts) if err != nil { log.Println("Error: Cannot list incidents from Pagerduty API:", err) return incidents } - ok = resp.APIListObject.More + ok = resp.More log.Printf("Got %d incidents", len(resp.Incidents)) incidents = append(incidents, resp.Incidents...) - opts.APIListObject.Offset = opts.APIListObject.Offset + opts.APIListObject.Limit + opts.Offset = opts.Offset + opts.Limit } log.Printf("Returning %d incidents total.", len(incidents)) return incidents