pager_duty-connection 2.3.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/standardrb.yaml +13 -0
- data/Gemfile +4 -3
- data/README.md +13 -11
- data/examples/find-users.rb +8 -8
- data/examples/shifts-with-incidents-and-log-entries.rb +15 -15
- data/lib/pager_duty/connection/version.rb +1 -1
- data/lib/pager_duty/connection.rb +57 -26
- data/lib/pager_duty.rb +2 -3
- data/pager_duty-connection.gemspec +13 -16
- metadata +15 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62d5837515647a768626a48a20621bf19c937139f0bf10605d4aa945cdd0bb0a
|
4
|
+
data.tar.gz: c25658b8e8b25eeb8b18839490c90e4c28c424f08cbaaa4a8d209775dcb77d98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cd5c6cb6967d8f2936e2063d3f898ca007045eb94c1bebb48da18a8e259a40edcc8a6e23cc1b3487e480742415c5196aa1759573fd74e30c201cfb8398a5c26
|
7
|
+
data.tar.gz: 42b5626e27ac9a4be206aed56d07546a02f252e8632704d9e997969d11ae3358ef0845ef8d7f9b7ccdf277952e06707fca32649b10e2c28c440024a690d280e5
|
data/Gemfile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pager_duty-connection.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
# for tests & running examples
|
7
7
|
group :development do
|
8
|
-
gem
|
9
|
-
gem
|
8
|
+
gem "dotenv"
|
9
|
+
gem "pry"
|
10
|
+
gem "standardrb"
|
10
11
|
end
|
data/README.md
CHANGED
@@ -31,7 +31,9 @@ And this is what it doesn't do:
|
|
31
31
|
|
32
32
|
Add this line to your application's Gemfile:
|
33
33
|
|
34
|
-
|
34
|
+
```ruby
|
35
|
+
gem "pager_duty-connection"
|
36
|
+
```
|
35
37
|
|
36
38
|
And then execute:
|
37
39
|
|
@@ -54,22 +56,22 @@ pagerduty = PagerDuty::Connection.new(token)
|
|
54
56
|
pagerduty = PagerDuty::Connection.new(token, token_type: :Bearer)
|
55
57
|
|
56
58
|
# setup to use a custom domain
|
57
|
-
pagerduty = PagerDuty::Connection.new(token, token_type: :Bearer, url:
|
59
|
+
pagerduty = PagerDuty::Connection.new(token, token_type: :Bearer, url: "https://custom.domain.com")
|
58
60
|
|
59
61
|
# 4 main methods: `get`, `post`, `put`, and `delete`:
|
60
62
|
|
61
|
-
response = pagerduty.get(
|
62
|
-
response = pagerduty.post(
|
63
|
-
response = pagerduty.delete(
|
64
|
-
response = pagerduty.put(
|
63
|
+
response = pagerduty.get("some/relative/path", params)
|
64
|
+
response = pagerduty.post("some/relative/path", params)
|
65
|
+
response = pagerduty.delete("some/relative/path", params)
|
66
|
+
response = pagerduty.put("some/relative/path", params)
|
65
67
|
|
66
68
|
# use something like irb or pry to poke around the responses
|
67
69
|
# the contents will vary a bit between call, ie:
|
68
70
|
|
69
|
-
response = pagerduty.get(
|
71
|
+
response = pagerduty.get("incidents")
|
70
72
|
response.incidents # an array of incidents
|
71
73
|
|
72
|
-
response = pagerduty.get(
|
74
|
+
response = pagerduty.get("incidents/YYZ")
|
73
75
|
response # the hash/object that represents the array
|
74
76
|
```
|
75
77
|
|
@@ -102,14 +104,14 @@ In general, you can get/put/post/delete a path, with some attributes. Use the [R
|
|
102
104
|
If you are working in Rails, and using only a single PagerDuty account, you'll probably want an initializer:
|
103
105
|
|
104
106
|
```ruby
|
105
|
-
$pagerduty = PagerDuty::Connection.new(
|
107
|
+
$pagerduty = PagerDuty::Connection.new("your-token")
|
106
108
|
```
|
107
109
|
|
108
110
|
And if you are using [dotenv](https://github.com/bkeepers/dotenv), you can use environment variables, and stash them in .env:
|
109
111
|
|
110
112
|
```ruby
|
111
|
-
account = ENV[
|
112
|
-
token = ENV[
|
113
|
+
account = ENV["PAGERDUTY_ACCOUNT"] || raise("Missing ENV['PAGERDUTY_ACCOUNT'], add to .env")
|
114
|
+
token = ENV["PAGERDUTY_TOKEN"] || raise("Missing ENV['PAGERDUTY_TOKEN'], add to .env.#{Rails.env}")
|
113
115
|
$pagerduty = PagerDuty::Connection.new(account, token)
|
114
116
|
```
|
115
117
|
|
data/examples/find-users.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
Dotenv.load ".env.development",
|
3
|
+
require "dotenv"
|
4
|
+
Dotenv.load ".env.development", ".env"
|
5
5
|
|
6
|
-
token = ENV[
|
6
|
+
token = ENV["PAGERDUTY_TOKEN"] || raise("Missing ENV['PAGERDUTY_TOKEN'], add to .env.development")
|
7
7
|
|
8
|
-
require
|
9
|
-
|
8
|
+
require "pager_duty/connection"
|
9
|
+
pagerduty = PagerDuty::Connection.new(token)
|
10
10
|
|
11
11
|
# https://v2.developer.pagerduty.com/v2/page/api-reference#!/Users/get_users
|
12
|
-
response =
|
13
|
-
response[
|
14
|
-
puts "#{user[
|
12
|
+
response = pagerduty.get("users")
|
13
|
+
response["users"].each do |user|
|
14
|
+
puts "#{user["name"]}: #{user["email"]}"
|
15
15
|
end
|
@@ -1,46 +1,46 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
Dotenv.load ".env.development",
|
3
|
+
require "dotenv"
|
4
|
+
Dotenv.load ".env.development", ".env"
|
5
5
|
|
6
|
-
token = ENV[
|
6
|
+
token = ENV["PAGERDUTY_TOKEN"] || raise("Missing ENV['PAGERDUTY_TOKEN'], add to .env.development")
|
7
7
|
|
8
|
-
require
|
9
|
-
|
8
|
+
require "pager_duty/connection"
|
9
|
+
pagerduty = PagerDuty::Connection.new(token)
|
10
10
|
|
11
|
-
schedule_id = ENV[
|
11
|
+
schedule_id = ENV["PAGERDUTY_SCHEDULE_ID"] || raise("Missing ENV['PAGERDUTY_SCHEDULE_ID'], add to .env.development")
|
12
12
|
|
13
13
|
# pull down schedule entires for XXX schedule in the last day (ie who has been on call, and when
|
14
14
|
time_since = 1.day.ago
|
15
15
|
time_until = Time.now
|
16
16
|
|
17
17
|
# https://v2.developer.pagerduty.com/v2/page/api-reference#!/On-Calls/get_oncalls
|
18
|
-
response =
|
18
|
+
response = pagerduty.get("oncalls", query_params: {since: time_since, until: time_until, schedule_ids: [schedule_id]})
|
19
19
|
|
20
|
-
entries = response[
|
20
|
+
entries = response["oncalls"]
|
21
21
|
|
22
22
|
entries.each do |entry|
|
23
|
-
puts "#{entry[
|
23
|
+
puts "#{entry["start"]} - #{entry["end"]}: #{entry["user"]["summary"]}"
|
24
24
|
|
25
25
|
# find incidents during that shift
|
26
26
|
# https://v2.developer.pagerduty.com/v2/page/api-reference#!/Incidents/get_incidents
|
27
|
-
response =
|
27
|
+
response = pagerduty.get("incidents", query_params: {since: entry["start"], until: entry["end"], user_ids: [entry["user"]["id"]]})
|
28
28
|
|
29
|
-
response[
|
29
|
+
response["incidents"].each do |incident|
|
30
30
|
puts "\t#{incident.id}"
|
31
31
|
|
32
32
|
# find log entries (acknowledged, notifications, etc) for incident:
|
33
33
|
# https://v2.developer.pagerduty.com/v2/page/api-reference#!/Incidents/get_incidents_id_log_entries
|
34
|
-
response =
|
34
|
+
response = pagerduty.get("incidents/#{incident.id}/log_entries")
|
35
35
|
|
36
36
|
# select just the notes
|
37
|
-
notes = response[
|
38
|
-
log_entry[
|
37
|
+
notes = response["log_entries"].select do |log_entry|
|
38
|
+
log_entry["channel"] && log_entry["channel"]["type"] == "note"
|
39
39
|
end
|
40
40
|
|
41
41
|
# and print them out:
|
42
42
|
notes.each do |log_entry|
|
43
|
-
puts "\t\t#{log_entry[
|
43
|
+
puts "\t\t#{log_entry["channel"]["summary"]}"
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require "faraday"
|
2
|
+
require "hashie"
|
3
|
+
require "active_support"
|
4
|
+
require "active_support/core_ext"
|
5
|
+
require "active_support/time_with_zone"
|
6
6
|
|
7
7
|
module PagerDuty
|
8
|
-
|
9
8
|
class Connection
|
10
9
|
attr_accessor :connection
|
11
10
|
|
@@ -44,7 +43,6 @@ module PagerDuty
|
|
44
43
|
end
|
45
44
|
end
|
46
45
|
|
47
|
-
|
48
46
|
class RaiseFileNotFoundOn404 < Faraday::Middleware
|
49
47
|
def call(env)
|
50
48
|
response = @app.call env
|
@@ -59,11 +57,13 @@ module PagerDuty
|
|
59
57
|
class RaiseApiErrorOnNon200 < Faraday::Middleware
|
60
58
|
def call(env)
|
61
59
|
response = @app.call env
|
62
|
-
|
60
|
+
if [200, 201, 204].include?(response.status)
|
61
|
+
response
|
62
|
+
else
|
63
63
|
url = response.env[:url].to_s
|
64
64
|
message = "Got HTTP #{response.status}: #{response.reason_phrase}\nFrom #{url}"
|
65
65
|
|
66
|
-
if error = response.body
|
66
|
+
if (error = response.body)
|
67
67
|
begin
|
68
68
|
# TODO May Need to check error.errors too
|
69
69
|
message += "\n#{JSON.parse(error)}"
|
@@ -72,8 +72,6 @@ module PagerDuty
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
raise ApiError, message
|
75
|
-
else
|
76
|
-
response
|
77
75
|
end
|
78
76
|
end
|
79
77
|
end
|
@@ -92,7 +90,6 @@ module PagerDuty
|
|
92
90
|
class ConvertTimesParametersToISO8601 < Faraday::Middleware
|
93
91
|
TIME_KEYS = [:since, :until]
|
94
92
|
def call(env)
|
95
|
-
|
96
93
|
body = env[:body]
|
97
94
|
unless body.nil?
|
98
95
|
TIME_KEYS.each do |key|
|
@@ -106,8 +103,8 @@ module PagerDuty
|
|
106
103
|
end
|
107
104
|
end
|
108
105
|
|
109
|
-
class ParseTimeStrings < Faraday::
|
110
|
-
TIME_KEYS = %w
|
106
|
+
class ParseTimeStrings < Faraday::Middleware
|
107
|
+
TIME_KEYS = %w[
|
111
108
|
at
|
112
109
|
created_at
|
113
110
|
created_on
|
@@ -118,9 +115,9 @@ module PagerDuty
|
|
118
115
|
start
|
119
116
|
started_at
|
120
117
|
start_time
|
121
|
-
|
118
|
+
]
|
122
119
|
|
123
|
-
OBJECT_KEYS = %w
|
120
|
+
OBJECT_KEYS = %w[
|
124
121
|
alert
|
125
122
|
entry
|
126
123
|
incident
|
@@ -129,13 +126,17 @@ module PagerDuty
|
|
129
126
|
note
|
130
127
|
override
|
131
128
|
service
|
132
|
-
|
129
|
+
]
|
133
130
|
|
134
|
-
NESTED_COLLECTION_KEYS = %w
|
131
|
+
NESTED_COLLECTION_KEYS = %w[
|
135
132
|
acknowledgers
|
136
133
|
assigned_to
|
137
134
|
pending_actions
|
138
|
-
|
135
|
+
]
|
136
|
+
|
137
|
+
def on_complete(env)
|
138
|
+
parse(env[:body])
|
139
|
+
end
|
139
140
|
|
140
141
|
def parse(body)
|
141
142
|
case body
|
@@ -169,7 +170,7 @@ module PagerDuty
|
|
169
170
|
end
|
170
171
|
|
171
172
|
def parse_object_times(object)
|
172
|
-
time = Time.zone
|
173
|
+
time = Time.zone || Time
|
173
174
|
|
174
175
|
TIME_KEYS.each do |key|
|
175
176
|
if object.has_key?(key) && object[key].present?
|
@@ -179,15 +180,36 @@ module PagerDuty
|
|
179
180
|
end
|
180
181
|
end
|
181
182
|
|
183
|
+
class Mashify < Faraday::Middleware
|
184
|
+
def on_complete(env)
|
185
|
+
env[:body] = parse(env[:body])
|
186
|
+
end
|
187
|
+
|
188
|
+
def parse(body)
|
189
|
+
case body
|
190
|
+
when Hash
|
191
|
+
::Hashie::Mash.new(body)
|
192
|
+
when Array
|
193
|
+
body.map { |item| parse(item) }
|
194
|
+
else
|
195
|
+
body
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
182
200
|
def initialize(token, token_type: :Token, url: API_PREFIX, debug: false)
|
183
201
|
@connection = Faraday.new do |conn|
|
184
202
|
conn.url_prefix = url
|
185
203
|
|
186
204
|
case token_type
|
187
205
|
when :Token
|
188
|
-
|
206
|
+
if faraday_v1?
|
207
|
+
conn.request :token_auth, token
|
208
|
+
else
|
209
|
+
conn.request :authorization, "Token", token
|
210
|
+
end
|
189
211
|
when :Bearer
|
190
|
-
conn.request :authorization,
|
212
|
+
conn.request :authorization, "Bearer", token
|
191
213
|
else raise ArgumentError, "invalid token_type: #{token_type.inspect}"
|
192
214
|
end
|
193
215
|
|
@@ -199,9 +221,9 @@ module PagerDuty
|
|
199
221
|
|
200
222
|
# json back, mashify it
|
201
223
|
conn.use ParseTimeStrings
|
202
|
-
conn.
|
224
|
+
conn.use Mashify
|
203
225
|
conn.response :json
|
204
|
-
conn.response :logger, ::Logger.new(
|
226
|
+
conn.response :logger, ::Logger.new($stdout), bodies: true if debug
|
205
227
|
|
206
228
|
# Because Faraday::Middleware executes in reverse order of
|
207
229
|
# calls to conn.use, status code error handling goes at the
|
@@ -212,7 +234,7 @@ module PagerDuty
|
|
212
234
|
conn.use RaiseForbiddenOn403
|
213
235
|
conn.use RaiseUnauthorizedOn401
|
214
236
|
|
215
|
-
conn.adapter
|
237
|
+
conn.adapter Faraday.default_adapter
|
216
238
|
end
|
217
239
|
end
|
218
240
|
|
@@ -230,6 +252,7 @@ module PagerDuty
|
|
230
252
|
offset = (page - 1) * limit
|
231
253
|
|
232
254
|
query_params = request[:query_params].merge(offset: offset, limit: limit)
|
255
|
+
query_params.delete(:page)
|
233
256
|
|
234
257
|
run_request(:get, path, **request.merge(query_params: query_params))
|
235
258
|
end
|
@@ -248,8 +271,16 @@ module PagerDuty
|
|
248
271
|
|
249
272
|
private
|
250
273
|
|
274
|
+
def faraday_v1?
|
275
|
+
faraday_version < Gem::Version.new("2")
|
276
|
+
end
|
277
|
+
|
278
|
+
def faraday_version
|
279
|
+
@faraday_version ||= Gem.loaded_specs["faraday"].version
|
280
|
+
end
|
281
|
+
|
251
282
|
def run_request(method, path, body: {}, headers: {}, query_params: {})
|
252
|
-
path = path.gsub(/^\//,
|
283
|
+
path = path.gsub(/^\//, "") # strip leading slash, to make sure relative things happen on the connection
|
253
284
|
|
254
285
|
connection.params = query_params
|
255
286
|
response = connection.run_request(method, path, body, headers)
|
data/lib/pager_duty.rb
CHANGED
@@ -1,25 +1,22 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require "pager_duty/connection/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name
|
8
|
-
gem.version
|
9
|
-
gem.authors
|
10
|
-
gem.email
|
11
|
-
gem.description
|
12
|
-
gem.summary
|
13
|
-
gem.homepage
|
6
|
+
gem.name = "pager_duty-connection"
|
7
|
+
gem.version = PagerDuty::Connection::VERSION
|
8
|
+
gem.authors = ["Josh Nichols"]
|
9
|
+
gem.email = ["josh@technicalpickles.com"]
|
10
|
+
gem.description = "Ruby API wrapper for the PagerDuty REST API"
|
11
|
+
gem.summary = "Written with the power of faraday, pager_duty-connection tries to be a simple and usable Ruby API wrapper for the PagerDuty REST API"
|
12
|
+
gem.homepage = "http://github.com/technicalpickles/pager_duty-connection"
|
14
13
|
|
15
|
-
gem.files
|
16
|
-
gem.executables
|
17
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
16
|
gem.require_paths = ["lib"]
|
19
17
|
|
20
|
-
gem.add_dependency "faraday", "
|
21
|
-
gem.add_dependency "
|
22
|
-
gem.add_dependency "activesupport", ">= 3.2", "< 8.0"
|
18
|
+
gem.add_dependency "faraday", ">= 1.10", "< 3"
|
19
|
+
gem.add_dependency "activesupport", ">= 3.2", "< 9.0"
|
23
20
|
gem.add_dependency "hashie", ">= 1.2"
|
24
21
|
|
25
22
|
gem.add_development_dependency "rake"
|
metadata
CHANGED
@@ -1,43 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pager_duty-connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Nichols
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
17
|
+
- - ">="
|
25
18
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
27
|
-
-
|
28
|
-
name: faraday_middleware
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
19
|
+
version: '1.10'
|
20
|
+
- - "<"
|
32
21
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
22
|
+
version: '3'
|
34
23
|
type: :runtime
|
35
24
|
prerelease: false
|
36
25
|
version_requirements: !ruby/object:Gem::Requirement
|
37
26
|
requirements:
|
38
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.10'
|
30
|
+
- - "<"
|
39
31
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
32
|
+
version: '3'
|
41
33
|
- !ruby/object:Gem::Dependency
|
42
34
|
name: activesupport
|
43
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -47,7 +39,7 @@ dependencies:
|
|
47
39
|
version: '3.2'
|
48
40
|
- - "<"
|
49
41
|
- !ruby/object:Gem::Version
|
50
|
-
version: '
|
42
|
+
version: '9.0'
|
51
43
|
type: :runtime
|
52
44
|
prerelease: false
|
53
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -57,7 +49,7 @@ dependencies:
|
|
57
49
|
version: '3.2'
|
58
50
|
- - "<"
|
59
51
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
52
|
+
version: '9.0'
|
61
53
|
- !ruby/object:Gem::Dependency
|
62
54
|
name: hashie
|
63
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,6 +85,7 @@ executables: []
|
|
93
85
|
extensions: []
|
94
86
|
extra_rdoc_files: []
|
95
87
|
files:
|
88
|
+
- ".github/workflows/standardrb.yaml"
|
96
89
|
- ".gitignore"
|
97
90
|
- ".ruby-version"
|
98
91
|
- Gemfile
|
@@ -124,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
117
|
- !ruby/object:Gem::Version
|
125
118
|
version: '0'
|
126
119
|
requirements: []
|
127
|
-
rubygems_version: 3.4.
|
120
|
+
rubygems_version: 3.4.10
|
128
121
|
signing_key:
|
129
122
|
specification_version: 4
|
130
123
|
summary: Written with the power of faraday, pager_duty-connection tries to be a simple
|