codeclimate-collector-pagerduty 0.0.1
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 +7 -0
- data/lib/codeclimate-collector-pagerduty.rb +1 -0
- data/lib/codeclimate/collectors/pagerduty.rb +15 -0
- data/lib/codeclimate/collectors/pagerduty/api_client.rb +79 -0
- data/lib/codeclimate/collectors/pagerduty/client.rb +26 -0
- data/lib/codeclimate/collectors/pagerduty/configuration.rb +21 -0
- data/lib/codeclimate/collectors/pagerduty/handlers.rb +4 -0
- data/lib/codeclimate/collectors/pagerduty/handlers/handler.rb +31 -0
- data/lib/codeclimate/collectors/pagerduty/handlers/sync.rb +56 -0
- data/lib/codeclimate/collectors/pagerduty/handlers/verify_configuration.rb +43 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 675fa8d78c199bf937811306e6895008535cf8c99bd9cb8378004add50e52936
|
4
|
+
data.tar.gz: 4d59688b4207397288a60b66d295eaf7af846de51086f8865ad5942918487b7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4190517f9d8d13ee0bb34d0cb625aa633b8f72fbe9617917b0e452cc547525f28de7759b4d21678db641631eafb465da9709cacf41a058eb861c3f17ac4745d7
|
7
|
+
data.tar.gz: bee089f8c83480ba6d70ab2add759f800476413b0ff2ee4dd7e289cd038c398d1cae4ec0c957922d5482a0c0c0ba047f73543777cb160e4e867aa8d06ce74111
|
@@ -0,0 +1 @@
|
|
1
|
+
require "codeclimate/collectors/pagerduty"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "codeclimate-collector-manager"
|
2
|
+
|
3
|
+
require "codeclimate/collectors/pagerduty/handlers"
|
4
|
+
|
5
|
+
require "codeclimate/collectors/pagerduty/api_client"
|
6
|
+
require "codeclimate/collectors/pagerduty/client"
|
7
|
+
require "codeclimate/collectors/pagerduty/configuration"
|
8
|
+
|
9
|
+
module Codeclimate
|
10
|
+
module Collectors
|
11
|
+
module Pagerduty
|
12
|
+
VERSION = File.read(File.expand_path("../../../../VERSION", __FILE__)).strip
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Codeclimate
|
5
|
+
module Collectors
|
6
|
+
module Pagerduty
|
7
|
+
class ApiClient
|
8
|
+
BASE_URL = "https://api.pagerduty.com".freeze
|
9
|
+
|
10
|
+
ResponseError = Class.new(StandardError)
|
11
|
+
NotFound = Class.new(ResponseError)
|
12
|
+
Unauthorized = Class.new(ResponseError)
|
13
|
+
ServerError = Class.new(ResponseError)
|
14
|
+
|
15
|
+
def initialize(api_token)
|
16
|
+
@api_token = api_token
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(path, params = {})
|
20
|
+
uri = construct_uri(path, params)
|
21
|
+
req = Net::HTTP::Get.new(uri)
|
22
|
+
set_headers(req)
|
23
|
+
make_request(req)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :api_token
|
29
|
+
|
30
|
+
def make_request(request)
|
31
|
+
resp = http_client.request(request)
|
32
|
+
|
33
|
+
case resp
|
34
|
+
when Net::HTTPSuccess
|
35
|
+
JSON.parse(resp.body)
|
36
|
+
when Net::HTTPNotFound
|
37
|
+
raise NotFound, "Resource not found"
|
38
|
+
when Net::HTTPUnauthorized
|
39
|
+
raise Unauthorized, "Invalid credentials"
|
40
|
+
when Net::HTTPServerError
|
41
|
+
raise ServerError, "Server error"
|
42
|
+
else
|
43
|
+
raise ResponseError, "Unsuccessful request: status=#{resp.status}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_headers(req)
|
48
|
+
req["Authorization"] = "Token token=#{api_token}"
|
49
|
+
req["Content-Type"] = "application/json"
|
50
|
+
req["Accept"] = "application/vnd.pagerduty+json;version=2"
|
51
|
+
end
|
52
|
+
|
53
|
+
def http_client
|
54
|
+
@http_client ||= begin
|
55
|
+
base_uri = URI.parse(BASE_URL)
|
56
|
+
|
57
|
+
Net::HTTP.new(base_uri.hostname, base_uri.port).tap do |c|
|
58
|
+
c.use_ssl = true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def construct_uri(path, params = {})
|
64
|
+
URI.parse(BASE_URL).tap do |uri|
|
65
|
+
if path.start_with?("/")
|
66
|
+
uri.path = path
|
67
|
+
else
|
68
|
+
uri.path = "/#{path}"
|
69
|
+
end
|
70
|
+
|
71
|
+
if params.any?
|
72
|
+
uri.query = URI.encode_www_form(params)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Codeclimate
|
2
|
+
module Collectors
|
3
|
+
module Pagerduty
|
4
|
+
class Client
|
5
|
+
def self.validate_configuration(configuration:, manager:)
|
6
|
+
Handlers::VerifyConfiguration.new(
|
7
|
+
configuration: Configuration.new(configuration),
|
8
|
+
manager: manager,
|
9
|
+
).run
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.sync(configuration:, manager:, earliest_data_cutoff:)
|
13
|
+
Handlers::Sync.new(
|
14
|
+
configuration: Configuration.new(configuration),
|
15
|
+
manager: manager,
|
16
|
+
earliest_data_cutoff: earliest_data_cutoff,
|
17
|
+
).run
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :configuration, :manager
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Codeclimate
|
2
|
+
module Collectors
|
3
|
+
module Pagerduty
|
4
|
+
class Configuration
|
5
|
+
attr_reader :config
|
6
|
+
|
7
|
+
def initialize(config)
|
8
|
+
@config = config
|
9
|
+
end
|
10
|
+
|
11
|
+
def api_token
|
12
|
+
config.fetch(:api_token)
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
config[:api_token].present?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Codeclimate
|
2
|
+
module Collectors
|
3
|
+
module Pagerduty
|
4
|
+
module Handlers
|
5
|
+
class Handler
|
6
|
+
def initialize(configuration:, manager:)
|
7
|
+
@configuration = configuration
|
8
|
+
@manager = manager
|
9
|
+
end
|
10
|
+
|
11
|
+
def run
|
12
|
+
raise NotImplementedError, "subclasses should implement this"
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
attr_reader :configuration, :manager
|
18
|
+
|
19
|
+
def api_client
|
20
|
+
@api_client ||= ApiClient.new(configuration.api_token)
|
21
|
+
end
|
22
|
+
|
23
|
+
def send_message(message)
|
24
|
+
manager.messages.send_message( message)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Codeclimate
|
2
|
+
module Collectors
|
3
|
+
module Pagerduty
|
4
|
+
module Handlers
|
5
|
+
class Sync < Handler
|
6
|
+
LIMIT = 100
|
7
|
+
|
8
|
+
def initialize(configuration:, manager:, earliest_data_cutoff:)
|
9
|
+
super(configuration: configuration, manager: manager)
|
10
|
+
@earliest_data_cutoff = earliest_data_cutoff
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
page = 1
|
15
|
+
has_more = true
|
16
|
+
|
17
|
+
while has_more
|
18
|
+
page_data = fetch_page(page)
|
19
|
+
|
20
|
+
page_data["incidents"].each do |incident_json|
|
21
|
+
process_incident(incident_json)
|
22
|
+
end
|
23
|
+
|
24
|
+
has_more = page_data["more"]
|
25
|
+
page += 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :earliest_data_cutoff
|
32
|
+
|
33
|
+
def process_incident(incident_json)
|
34
|
+
manager.messages << Messages::Incident.new(
|
35
|
+
external_id: incident_json.fetch("id"),
|
36
|
+
status: incident_json.fetch("status"),
|
37
|
+
number: incident_json.fetch("incident_number"),
|
38
|
+
title: incident_json.fetch("title"),
|
39
|
+
created_at: Time.parse(incident_json.fetch("created_at")),
|
40
|
+
url: incident_json.fetch("html_url"),
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
def fetch_page(page)
|
45
|
+
@api_response = api_client.get(
|
46
|
+
"incidents",
|
47
|
+
limit: LIMIT,
|
48
|
+
offset: (page - 1) * LIMIT,
|
49
|
+
since: earliest_data_cutoff.iso8601,
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Codeclimate
|
2
|
+
module Collectors
|
3
|
+
module Pagerduty
|
4
|
+
module Handlers
|
5
|
+
class VerifyConfiguration < Handler
|
6
|
+
def run
|
7
|
+
if !configuration.valid?
|
8
|
+
send_message(
|
9
|
+
Messages::ConfigurationVerification.new(
|
10
|
+
state: Messages::ConfigurationVerification::ERROR,
|
11
|
+
error_messages: ["API token is missing."],
|
12
|
+
)
|
13
|
+
)
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
if token_valid?
|
18
|
+
send_message(
|
19
|
+
Messages::ConfigurationVerification.new(
|
20
|
+
state: Messages::ConfigurationVerification::SUCCESS,
|
21
|
+
)
|
22
|
+
)
|
23
|
+
else
|
24
|
+
send_message(
|
25
|
+
Messages::ConfigurationVerification.new(
|
26
|
+
state: Messages::ConfigurationVerification::ERROR,
|
27
|
+
error_messages: ["API token is invalid."],
|
28
|
+
)
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def token_valid?
|
34
|
+
api_client.get("abilities")
|
35
|
+
true
|
36
|
+
rescue ApiClient::Unauthorized
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codeclimate-collector-pagerduty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Code Climate
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: codeclimate-collector-manager
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Collector for integrating PagerDuty data into Velocity
|
70
|
+
email:
|
71
|
+
- hello@codeclimate.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/codeclimate-collector-pagerduty.rb
|
77
|
+
- lib/codeclimate/collectors/pagerduty.rb
|
78
|
+
- lib/codeclimate/collectors/pagerduty/api_client.rb
|
79
|
+
- lib/codeclimate/collectors/pagerduty/client.rb
|
80
|
+
- lib/codeclimate/collectors/pagerduty/configuration.rb
|
81
|
+
- lib/codeclimate/collectors/pagerduty/handlers.rb
|
82
|
+
- lib/codeclimate/collectors/pagerduty/handlers/handler.rb
|
83
|
+
- lib/codeclimate/collectors/pagerduty/handlers/sync.rb
|
84
|
+
- lib/codeclimate/collectors/pagerduty/handlers/verify_configuration.rb
|
85
|
+
homepage: https://codeclimate.com
|
86
|
+
licenses:
|
87
|
+
- Nonstandard
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.1.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Code Climate PagerDuty Collector
|
108
|
+
test_files: []
|