Skip to content
This repository was archived by the owner on Apr 8, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ often as you'd like to refresh your database.

You'll need the following config set in environment variables:

* `PAGERDUTY_SUBDOMAIN`: e.g. `your-company` in `your-company.pagerduty.com`.
* `PAGERDUTY_API_KEY`: a read-only API key from `https://<PAGERDUTY_SUBDOMAIN>.pagerduty.com/api_keys`.
* `DATABASE_URL`: URL to a Postgres database, e.g. `postgres://127.0.0.1:5432/pagerduty`

Expand Down
27 changes: 17 additions & 10 deletions bin/pd2pg
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ class PG2PD
def initialize
# Read config.
database_url = env!("DATABASE_URL")
pagerduty_subdomain = env!("PAGERDUTY_SUBDOMAIN")
pagerduty_api_token = env!("PAGERDUTY_API_KEY")

# Establish API connection.
self.api = Excon::Connection.new(
:scheme => "https",
:host => "#{pagerduty_subdomain}.pagerduty.com",
:host => "api.pagerduty.com",
:port => 443,
:headers => {"Authorization" => "Token token=#{pagerduty_api_token}"})
:headers => {
"Authorization" => "Token token=#{pagerduty_api_token}",
"Accept" => "application/vnd.pagerduty+json;version=2"
})

# Establish DB connection.
self.db = Sequel.connect(database_url)
Expand Down Expand Up @@ -181,7 +183,7 @@ class PG2PD
{
id: i["id"],
incident_number: i["incident_number"],
created_at: i["created_on"],
created_at: i["created_at"],
html_url: i["html_url"],
incident_key: i["incident_key"],
service_id: i["service"] && i["service"]["id"],
Expand All @@ -207,8 +209,12 @@ class PG2PD
end
response = api.request(
method: :get,
path: "/api/v1/#{endpoint}",
query: {'offset' => offset, 'limit' => PAGINATION_LIMIT}.merge(additional_headers),
path: "/#{endpoint}",
query: {
'offset' => offset,
'limit' => PAGINATION_LIMIT,
'total' => 'true'
}.merge(additional_headers),
expects: [200]
)
data = JSON.parse(response.body)
Expand Down Expand Up @@ -261,12 +267,13 @@ class PG2PD
log("refresh_incremental.page", collection: collection, offset: offset, total: total || "?")
response = api.request(
method: :get,
path: "/api/v1/#{collection}",
path: "/#{collection}",
query: {
since: since,
until: through,
since: since.iso8601,
until: through.iso8601,
offset: offset,
limit: PAGINATION_LIMIT
limit: PAGINATION_LIMIT,
total: 'true'
}.merge(query_params),
expects: [200]
)
Expand Down