@pagerduty/backstage-plugin-common 0.0.3-next.8 → 0.0.3-next.9

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.
package/dist/index.cjs.js CHANGED
@@ -68,57 +68,15 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
68
68
  }
69
69
  const scopes = `
70
70
  abilities.read
71
- addons.read
72
- addons.write
73
- analytics.read
74
- audit_records.read
75
71
  change_events.read
76
- change_events.write
77
- custom_fields.read
78
- custom_fields.write
79
72
  escalation_policies.read
80
- escalation_policies.write
81
- event_orchestrations.read
82
- event_orchestrations.write
83
- event_rules.read
84
- event_rules.write
85
- extension_schemas.read
86
- extensions.read
87
- extensions.write
88
- incident_workflows.read
89
- incident_workflows.write
90
- incident_workflows:instances.write
91
73
  incidents.read
92
- incidents.write
93
- licenses.read
94
- notifications.read
95
74
  oncalls.read
96
- priorities.read
97
- response_plays.read
98
- response_plays.write
99
75
  schedules.read
100
- schedules.write
101
76
  services.read
102
77
  services.write
103
- standards.read
104
- standards.write
105
- status_dashboards.read
106
- status_pages.read
107
- status_pages.write
108
- subscribers.read
109
- subscribers.write
110
- tags.read
111
- tags.write
112
78
  teams.read
113
- teams.write
114
- templates.read
115
- templates.write
116
79
  users.read
117
- users.write
118
- users:contact_methods.read
119
- users:contact_methods.write
120
- users:sessions.read
121
- users:sessions.write
122
80
  vendors.read
123
81
  `;
124
82
  const urlencoded = new URLSearchParams();
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/constants.ts","../src/auth/auth.ts"],"sourcesContent":["/** @public */\nexport type PagerDutyIncident = {\n id: string;\n title: string;\n status: string;\n html_url: string;\n assignments: [\n {\n assignee: PagerDutyUser;\n },\n ];\n service: PagerDutyService;\n created_at: string;\n}\n\n/** @public */\nexport type PagerDutyUser = {\n id: string;\n summary: string;\n email: string;\n html_url: string;\n name: string;\n avatar_url: string;\n};\n\n/** @public */\nexport type PagerDutyChangeEvent = {\n id: string;\n integration: PagerDutyIntegration[];\n source: string;\n html_url?: string;\n links: [\n {\n href: string;\n text: string;\n },\n ];\n summary: string;\n timestamp: string;\n}\n\n/** @public */\nexport type PagerDutyService = {\n id: string;\n name: string;\n description?: string;\n escalation_policy: PagerDutyEscalationPolicy; \n alert_creation?: string;\n incident_urgency_rule?: PagerDutyIncidentUrgencyRule;\n integrations?: PagerDutyIntegration[];\n teams?: PagerDutyTeam[];\n status?: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url: string;\n}\n\n/** @public */\nexport type PagerDutyEscalationPolicy = {\n id: string;\n name: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyIncidentUrgencyRule = {\n type: string;\n urgency: string;\n}\n\n/** @public */\nexport type PagerDutyIntegration = {\n id: string;\n type: string;\n summary?: string;\n self?: string;\n html_url?: string;\n name?: string;\n service?: PagerDutyService;\n created_at?: string;\n vendor?: PagerDutyVendor;\n integration_key?: string;\n}\n\n/** @public */\nexport type PagerDutyTeam = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyOnCall = {\n user: PagerDutyUser;\n escalation_level: number;\n}\n\n/** @public */\nexport type PagerDutyOnCallUsersResponse = {\n users: PagerDutyUser[];\n}\n\n/** @public */\nexport type PagerDutyVendor = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyServicesResponse = {\n services: PagerDutyService[];\n};\n\n/** @public */\nexport type PagerDutyServiceResponse = {\n service: PagerDutyService;\n};\n\n/** @public */\nexport type PagerDutyIncidentsResponse = {\n incidents: PagerDutyIncident[];\n};\n\n/** @public */\nexport type PagerDutyChangeEventsResponse = {\n change_events: PagerDutyChangeEvent[];\n};\n\n/** @public */\nexport type PagerDutyOnCallsResponse = {\n oncalls: PagerDutyOnCall[];\n};\n\n/** @public */\nexport type PagerDutyIntegrationResponse = {\n integration: PagerDutyIntegration;\n}\n\n/** @public */\nexport type PagerDutyEscalationPoliciesResponse = {\n escalation_policies: PagerDutyEscalationPolicy[];\n limit?: number;\n offset?: number;\n more?: boolean;\n total?: number;\n};\n\n/** @public */\nexport type PagerDutyAbilitiesResponse = {\n abilities: string[];\n};\n\n/** @public */\nexport class HttpError extends Error {\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n }\n\n status: number;\n}\n\n/** @public */\nexport type PagerDutyOAuthConfig = {\n clientId: string;\n clientSecret: string;\n region?: string;\n subDomain: string;\n}\n","/** @public */\nexport const PAGERDUTY_INTEGRATION_KEY = 'pagerduty.com/integration-key';\n\n/** @public */\nexport const PAGERDUTY_SERVICE_ID = 'pagerduty.com/service-id';","import { Logger } from \"winston\";\nimport { Config } from \"@backstage/config\";\nimport { HttpError } from \"../types\";\n\ntype Auth = {\n logger: Logger;\n config: Config;\n authToken: string;\n authTokenExpiryDate: number;\n}\n\nlet authPersistence: Auth;\n\nexport async function getAuthToken(): Promise<string> {\n // check if token already exists and is valid\n if (\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Bearer') &&\n authPersistence.authTokenExpiryDate > Date.now()) // case where OAuth token is still valid\n ||\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Token'))) { // case where API token is used\n return authPersistence.authToken;\n }\n\n await loadAuthConfig(authPersistence.logger, authPersistence.config);\n return authPersistence.authToken;\n}\n\nexport async function loadAuthConfig(logger: Logger, config: Config) {\n try {\n // initiliaze the authPersistence in-memory object\n authPersistence = {\n logger: logger,\n config: config,\n authToken: '',\n authTokenExpiryDate: Date.now()\n };\n\n if (!config.getOptionalString('pagerDuty.apiToken')) {\n logger.warn('No PagerDuty API token found in config file. Trying OAuth token instead...');\n\n if (!config.getOptional('pagerDuty.oauth')) {\n \n logger.error('No PagerDuty OAuth configuration found in config file.');\n throw new Error(\"No PagerDuty 'apiToken' or 'oauth' configuration found in config file.\");\n\n } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\n \n logger.error(\"Missing required PagerDuty OAuth parameters in config file. 'clientId', 'clientSecret', and 'subDomain' are required. 'region' is optional.\");\n throw new Error('Missing required PagerDuty OAuth parameters in config file.');\n\n } else {\n\n authPersistence.authToken = await getOAuthToken(\n config.getString('pagerDuty.oauth.clientId'),\n config.getString('pagerDuty.oauth.clientSecret'),\n config.getString('pagerDuty.oauth.subDomain'),\n config.getOptionalString('pagerDuty.oauth.region') ?? 'us');\n\n logger.info('PagerDuty OAuth configuration loaded successfully.');\n }\n } else {\n authPersistence.authToken = `Token token=${config.getString('pagerDuty.apiToken')}`;\n\n logger.info('PagerDuty API token loaded successfully.');\n }\n }\n catch (error) {\n logger.error(`Unable to retrieve valid PagerDuty AUTH configuration from config file: ${error}`);\n throw error;\n }\n}\n\nasync function getOAuthToken(clientId: string, clientSecret: string, subDomain: string, region: string): Promise<string> {\n // check if required parameters are provided\n if (!clientId || !clientSecret || !subDomain) {\n throw new Error('Missing required PagerDuty OAuth parameters.');\n }\n\n // define the scopes required for the OAuth token\n // const scopes = `\n // abilities.read \n // change_events.read \n // escalation_policies.read \n // incidents.read \n // oncalls.read \n // priorities.read \n // schedules.read \n // services.read\n // services.write \n // teams.read \n // users.read \n // users:contact_methods.read \n // vendors.read\n // `;\n\n const scopes = `\n abilities.read \n addons.read \n addons.write \n analytics.read \n audit_records.read \n change_events.read \n change_events.write \n custom_fields.read \n custom_fields.write \n escalation_policies.read \n escalation_policies.write \n event_orchestrations.read \n event_orchestrations.write \n event_rules.read \n event_rules.write \n extension_schemas.read \n extensions.read \n extensions.write \n incident_workflows.read \n incident_workflows.write \n incident_workflows:instances.write \n incidents.read \n incidents.write \n licenses.read \n notifications.read \n oncalls.read \n priorities.read \n response_plays.read \n response_plays.write \n schedules.read \n schedules.write \n services.read \n services.write \n standards.read \n standards.write \n status_dashboards.read \n status_pages.read \n status_pages.write \n subscribers.read \n subscribers.write \n tags.read \n tags.write \n teams.read \n teams.write \n templates.read \n templates.write \n users.read \n users.write \n users:contact_methods.read \n users:contact_methods.write \n users:sessions.read \n users:sessions.write \n vendors.read\n `;\n\n // encode the parameters for the request\n const urlencoded = new URLSearchParams();\n urlencoded.append(\"grant_type\", \"client_credentials\");\n urlencoded.append(\"client_id\", clientId);\n urlencoded.append(\"client_secret\", clientSecret);\n urlencoded.append(\"scope\", `as_account-${region}.${subDomain} ${scopes}`);\n\n let response: Response;\n const options: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: urlencoded,\n };\n const baseUrl = 'https://identity.pagerduty.com/oauth/token';\n\n try {\n response = await fetch(baseUrl, options);\n } catch (error) {\n throw new Error(`Failed to retrieve oauth token: ${error}`);\n }\n\n switch (response.status) {\n case 400:\n throw new HttpError(\"Failed to retrieve valid token. Bad Request - Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Failed to retrieve valid token. Forbidden - Invalid credentials provided.\", 401);\n default: // 200\n break;\n }\n\n const authResponse = await response.json();\n authPersistence.authTokenExpiryDate = Date.now() + (authResponse.expires_in * 1000);\n return `Bearer ${authResponse.access_token}`;\n}"],"names":[],"mappings":";;;;;;;;;;AAkKO,MAAM,kBAAkB,KAAM,CAAA;AAAA,EACjC,WAAA,CAAY,SAAiB,MAAgB,EAAA;AACzC,IAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAIjB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAHI,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AAAA,GAClB;AAGJ;;ACxKO,MAAM,yBAA4B,GAAA,gCAAA;AAGlC,MAAM,oBAAuB,GAAA;;ACOpC,IAAI,eAAA,CAAA;AAEJ,eAAsB,YAAgC,GAAA;AAElD,EACK,IAAA,eAAA,CAAgB,cAAc,EAC3B,IAAA,eAAA,CAAgB,UAAU,QAAS,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AAK,GAAI,EAAA,GAAK,aAAa,UAAa,GAAA,GAAA,CAAA;AAC9E,EAAO,OAAA,CAAA,OAAA,EAAU,aAAa,YAAY,CAAA,CAAA,CAAA;AAC9C;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/constants.ts","../src/auth/auth.ts"],"sourcesContent":["/** @public */\nexport type PagerDutyIncident = {\n id: string;\n title: string;\n status: string;\n html_url: string;\n assignments: [\n {\n assignee: PagerDutyUser;\n },\n ];\n service: PagerDutyService;\n created_at: string;\n}\n\n/** @public */\nexport type PagerDutyUser = {\n id: string;\n summary: string;\n email: string;\n html_url: string;\n name: string;\n avatar_url: string;\n};\n\n/** @public */\nexport type PagerDutyChangeEvent = {\n id: string;\n integration: PagerDutyIntegration[];\n source: string;\n html_url?: string;\n links: [\n {\n href: string;\n text: string;\n },\n ];\n summary: string;\n timestamp: string;\n}\n\n/** @public */\nexport type PagerDutyService = {\n id: string;\n name: string;\n description?: string;\n escalation_policy: PagerDutyEscalationPolicy; \n alert_creation?: string;\n incident_urgency_rule?: PagerDutyIncidentUrgencyRule;\n integrations?: PagerDutyIntegration[];\n teams?: PagerDutyTeam[];\n status?: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url: string;\n}\n\n/** @public */\nexport type PagerDutyEscalationPolicy = {\n id: string;\n name: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyIncidentUrgencyRule = {\n type: string;\n urgency: string;\n}\n\n/** @public */\nexport type PagerDutyIntegration = {\n id: string;\n type: string;\n summary?: string;\n self?: string;\n html_url?: string;\n name?: string;\n service?: PagerDutyService;\n created_at?: string;\n vendor?: PagerDutyVendor;\n integration_key?: string;\n}\n\n/** @public */\nexport type PagerDutyTeam = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyOnCall = {\n user: PagerDutyUser;\n escalation_level: number;\n}\n\n/** @public */\nexport type PagerDutyOnCallUsersResponse = {\n users: PagerDutyUser[];\n}\n\n/** @public */\nexport type PagerDutyVendor = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyServicesResponse = {\n services: PagerDutyService[];\n};\n\n/** @public */\nexport type PagerDutyServiceResponse = {\n service: PagerDutyService;\n};\n\n/** @public */\nexport type PagerDutyIncidentsResponse = {\n incidents: PagerDutyIncident[];\n};\n\n/** @public */\nexport type PagerDutyChangeEventsResponse = {\n change_events: PagerDutyChangeEvent[];\n};\n\n/** @public */\nexport type PagerDutyOnCallsResponse = {\n oncalls: PagerDutyOnCall[];\n};\n\n/** @public */\nexport type PagerDutyIntegrationResponse = {\n integration: PagerDutyIntegration;\n}\n\n/** @public */\nexport type PagerDutyEscalationPoliciesResponse = {\n escalation_policies: PagerDutyEscalationPolicy[];\n limit?: number;\n offset?: number;\n more?: boolean;\n total?: number;\n};\n\n/** @public */\nexport type PagerDutyAbilitiesResponse = {\n abilities: string[];\n};\n\n/** @public */\nexport class HttpError extends Error {\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n }\n\n status: number;\n}\n\n/** @public */\nexport type PagerDutyOAuthConfig = {\n clientId: string;\n clientSecret: string;\n region?: string;\n subDomain: string;\n}\n","/** @public */\nexport const PAGERDUTY_INTEGRATION_KEY = 'pagerduty.com/integration-key';\n\n/** @public */\nexport const PAGERDUTY_SERVICE_ID = 'pagerduty.com/service-id';","import { Logger } from \"winston\";\nimport { Config } from \"@backstage/config\";\nimport { HttpError } from \"../types\";\n\ntype Auth = {\n logger: Logger;\n config: Config;\n authToken: string;\n authTokenExpiryDate: number;\n}\n\nlet authPersistence: Auth;\n\nexport async function getAuthToken(): Promise<string> {\n // check if token already exists and is valid\n if (\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Bearer') &&\n authPersistence.authTokenExpiryDate > Date.now()) // case where OAuth token is still valid\n ||\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Token'))) { // case where API token is used\n return authPersistence.authToken;\n }\n\n await loadAuthConfig(authPersistence.logger, authPersistence.config);\n return authPersistence.authToken;\n}\n\nexport async function loadAuthConfig(logger: Logger, config: Config) {\n try {\n // initiliaze the authPersistence in-memory object\n authPersistence = {\n logger: logger,\n config: config,\n authToken: '',\n authTokenExpiryDate: Date.now()\n };\n\n if (!config.getOptionalString('pagerDuty.apiToken')) {\n logger.warn('No PagerDuty API token found in config file. Trying OAuth token instead...');\n\n if (!config.getOptional('pagerDuty.oauth')) {\n \n logger.error('No PagerDuty OAuth configuration found in config file.');\n throw new Error(\"No PagerDuty 'apiToken' or 'oauth' configuration found in config file.\");\n\n } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\n \n logger.error(\"Missing required PagerDuty OAuth parameters in config file. 'clientId', 'clientSecret', and 'subDomain' are required. 'region' is optional.\");\n throw new Error('Missing required PagerDuty OAuth parameters in config file.');\n\n } else {\n\n authPersistence.authToken = await getOAuthToken(\n config.getString('pagerDuty.oauth.clientId'),\n config.getString('pagerDuty.oauth.clientSecret'),\n config.getString('pagerDuty.oauth.subDomain'),\n config.getOptionalString('pagerDuty.oauth.region') ?? 'us');\n\n logger.info('PagerDuty OAuth configuration loaded successfully.');\n }\n } else {\n authPersistence.authToken = `Token token=${config.getString('pagerDuty.apiToken')}`;\n\n logger.info('PagerDuty API token loaded successfully.');\n }\n }\n catch (error) {\n logger.error(`Unable to retrieve valid PagerDuty AUTH configuration from config file: ${error}`);\n throw error;\n }\n}\n\nasync function getOAuthToken(clientId: string, clientSecret: string, subDomain: string, region: string): Promise<string> {\n // check if required parameters are provided\n if (!clientId || !clientSecret || !subDomain) {\n throw new Error('Missing required PagerDuty OAuth parameters.');\n }\n\n // define the scopes required for the OAuth token\n const scopes = `\n abilities.read \n change_events.read \n escalation_policies.read \n incidents.read \n oncalls.read \n schedules.read \n services.read \n services.write \n teams.read \n users.read \n vendors.read\n `;\n\n // encode the parameters for the request\n const urlencoded = new URLSearchParams();\n urlencoded.append(\"grant_type\", \"client_credentials\");\n urlencoded.append(\"client_id\", clientId);\n urlencoded.append(\"client_secret\", clientSecret);\n urlencoded.append(\"scope\", `as_account-${region}.${subDomain} ${scopes}`);\n\n let response: Response;\n const options: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: urlencoded,\n };\n const baseUrl = 'https://identity.pagerduty.com/oauth/token';\n\n try {\n response = await fetch(baseUrl, options);\n } catch (error) {\n throw new Error(`Failed to retrieve oauth token: ${error}`);\n }\n\n switch (response.status) {\n case 400:\n throw new HttpError(\"Failed to retrieve valid token. Bad Request - Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Failed to retrieve valid token. Forbidden - Invalid credentials provided.\", 401);\n default: // 200\n break;\n }\n\n const authResponse = await response.json();\n authPersistence.authTokenExpiryDate = Date.now() + (authResponse.expires_in * 1000);\n return `Bearer ${authResponse.access_token}`;\n}"],"names":[],"mappings":";;;;;;;;;;AAkKO,MAAM,kBAAkB,KAAM,CAAA;AAAA,EACjC,WAAA,CAAY,SAAiB,MAAgB,EAAA;AACzC,IAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAIjB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAHI,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AAAA,GAClB;AAGJ;;ACxKO,MAAM,yBAA4B,GAAA,gCAAA;AAGlC,MAAM,oBAAuB,GAAA;;ACOpC,IAAI,eAAA,CAAA;AAEJ,eAAsB,YAAgC,GAAA;AAElD,EACK,IAAA,eAAA,CAAgB,cAAc,EAC3B,IAAA,eAAA,CAAgB,UAAU,QAAS,CAAA,QAAQ,KAC3C,eAAgB,CAAA,mBAAA,GAAsB,KAAK,GAAI,EAAA,IAElD,gBAAgB,SAAc,KAAA,EAAA,IAC3B,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A,MAAM,IAAI,SAAS,CAAA,CAAA,EAAI,MAAM,CAAE,CAAA,CAAA,CAAA;AAExE,EAAI,IAAA,QAAA,CAAA;AACJ,EAAA,MAAM,OAAuB,GAAA;AAAA,IACzB,MAAQ,EAAA,MAAA;AAAA,IACR,OAAS,EAAA;AAAA,MACL,cAAgB,EAAA,mCAAA;AAAA,KACpB;AAAA,IACA,IAAM,EAAA,UAAA;AAAA,GACV,CAAA;AACA,EAAA,MAAM,OAAU,GAAA,4CAAA,CAAA;AAEhB,EAAI,IAAA;AACA,IAAW,QAAA,GAAA,MAAM,KAAM,CAAA,OAAA,EAAS,OAAO,CAAA,CAAA;AAAA,WAClC,KAAO,EAAA;AACZ,IAAA,MAAM,IAAI,KAAA,CAAM,CAAmC,gCAAA,EAAA,KAAK,CAAE,CAAA,CAAA,CAAA;AAAA,GAC9D;AAEA,EAAA,QAAQ,SAAS,MAAQ;AAAA,IACrB,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAAA,IACxG,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAEpG,GACR;AAEA,EAAM,MAAA,YAAA,GAAe,MAAM,QAAA,CAAS,IAAK,EAAA,CAAA;AACzC,EAAA,eAAA,CAAgB,mBAAsB,GAAA,IAAA,CAAK,GAAI,EAAA,GAAK,aAAa,UAAa,GAAA,GAAA,CAAA;AAC9E,EAAO,OAAA,CAAA,OAAA,EAAU,aAAa,YAAY,CAAA,CAAA,CAAA;AAC9C;;;;;;;;"}
package/dist/index.esm.js CHANGED
@@ -64,57 +64,15 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
64
64
  }
65
65
  const scopes = `
66
66
  abilities.read
67
- addons.read
68
- addons.write
69
- analytics.read
70
- audit_records.read
71
67
  change_events.read
72
- change_events.write
73
- custom_fields.read
74
- custom_fields.write
75
68
  escalation_policies.read
76
- escalation_policies.write
77
- event_orchestrations.read
78
- event_orchestrations.write
79
- event_rules.read
80
- event_rules.write
81
- extension_schemas.read
82
- extensions.read
83
- extensions.write
84
- incident_workflows.read
85
- incident_workflows.write
86
- incident_workflows:instances.write
87
69
  incidents.read
88
- incidents.write
89
- licenses.read
90
- notifications.read
91
70
  oncalls.read
92
- priorities.read
93
- response_plays.read
94
- response_plays.write
95
71
  schedules.read
96
- schedules.write
97
72
  services.read
98
73
  services.write
99
- standards.read
100
- standards.write
101
- status_dashboards.read
102
- status_pages.read
103
- status_pages.write
104
- subscribers.read
105
- subscribers.write
106
- tags.read
107
- tags.write
108
74
  teams.read
109
- teams.write
110
- templates.read
111
- templates.write
112
75
  users.read
113
- users.write
114
- users:contact_methods.read
115
- users:contact_methods.write
116
- users:sessions.read
117
- users:sessions.write
118
76
  vendors.read
119
77
  `;
120
78
  const urlencoded = new URLSearchParams();
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/types.ts","../src/constants.ts","../src/auth/auth.ts"],"sourcesContent":["/** @public */\nexport type PagerDutyIncident = {\n id: string;\n title: string;\n status: string;\n html_url: string;\n assignments: [\n {\n assignee: PagerDutyUser;\n },\n ];\n service: PagerDutyService;\n created_at: string;\n}\n\n/** @public */\nexport type PagerDutyUser = {\n id: string;\n summary: string;\n email: string;\n html_url: string;\n name: string;\n avatar_url: string;\n};\n\n/** @public */\nexport type PagerDutyChangeEvent = {\n id: string;\n integration: PagerDutyIntegration[];\n source: string;\n html_url?: string;\n links: [\n {\n href: string;\n text: string;\n },\n ];\n summary: string;\n timestamp: string;\n}\n\n/** @public */\nexport type PagerDutyService = {\n id: string;\n name: string;\n description?: string;\n escalation_policy: PagerDutyEscalationPolicy; \n alert_creation?: string;\n incident_urgency_rule?: PagerDutyIncidentUrgencyRule;\n integrations?: PagerDutyIntegration[];\n teams?: PagerDutyTeam[];\n status?: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url: string;\n}\n\n/** @public */\nexport type PagerDutyEscalationPolicy = {\n id: string;\n name: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyIncidentUrgencyRule = {\n type: string;\n urgency: string;\n}\n\n/** @public */\nexport type PagerDutyIntegration = {\n id: string;\n type: string;\n summary?: string;\n self?: string;\n html_url?: string;\n name?: string;\n service?: PagerDutyService;\n created_at?: string;\n vendor?: PagerDutyVendor;\n integration_key?: string;\n}\n\n/** @public */\nexport type PagerDutyTeam = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyOnCall = {\n user: PagerDutyUser;\n escalation_level: number;\n}\n\n/** @public */\nexport type PagerDutyOnCallUsersResponse = {\n users: PagerDutyUser[];\n}\n\n/** @public */\nexport type PagerDutyVendor = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyServicesResponse = {\n services: PagerDutyService[];\n};\n\n/** @public */\nexport type PagerDutyServiceResponse = {\n service: PagerDutyService;\n};\n\n/** @public */\nexport type PagerDutyIncidentsResponse = {\n incidents: PagerDutyIncident[];\n};\n\n/** @public */\nexport type PagerDutyChangeEventsResponse = {\n change_events: PagerDutyChangeEvent[];\n};\n\n/** @public */\nexport type PagerDutyOnCallsResponse = {\n oncalls: PagerDutyOnCall[];\n};\n\n/** @public */\nexport type PagerDutyIntegrationResponse = {\n integration: PagerDutyIntegration;\n}\n\n/** @public */\nexport type PagerDutyEscalationPoliciesResponse = {\n escalation_policies: PagerDutyEscalationPolicy[];\n limit?: number;\n offset?: number;\n more?: boolean;\n total?: number;\n};\n\n/** @public */\nexport type PagerDutyAbilitiesResponse = {\n abilities: string[];\n};\n\n/** @public */\nexport class HttpError extends Error {\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n }\n\n status: number;\n}\n\n/** @public */\nexport type PagerDutyOAuthConfig = {\n clientId: string;\n clientSecret: string;\n region?: string;\n subDomain: string;\n}\n","/** @public */\nexport const PAGERDUTY_INTEGRATION_KEY = 'pagerduty.com/integration-key';\n\n/** @public */\nexport const PAGERDUTY_SERVICE_ID = 'pagerduty.com/service-id';","import { Logger } from \"winston\";\nimport { Config } from \"@backstage/config\";\nimport { HttpError } from \"../types\";\n\ntype Auth = {\n logger: Logger;\n config: Config;\n authToken: string;\n authTokenExpiryDate: number;\n}\n\nlet authPersistence: Auth;\n\nexport async function getAuthToken(): Promise<string> {\n // check if token already exists and is valid\n if (\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Bearer') &&\n authPersistence.authTokenExpiryDate > Date.now()) // case where OAuth token is still valid\n ||\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Token'))) { // case where API token is used\n return authPersistence.authToken;\n }\n\n await loadAuthConfig(authPersistence.logger, authPersistence.config);\n return authPersistence.authToken;\n}\n\nexport async function loadAuthConfig(logger: Logger, config: Config) {\n try {\n // initiliaze the authPersistence in-memory object\n authPersistence = {\n logger: logger,\n config: config,\n authToken: '',\n authTokenExpiryDate: Date.now()\n };\n\n if (!config.getOptionalString('pagerDuty.apiToken')) {\n logger.warn('No PagerDuty API token found in config file. Trying OAuth token instead...');\n\n if (!config.getOptional('pagerDuty.oauth')) {\n \n logger.error('No PagerDuty OAuth configuration found in config file.');\n throw new Error(\"No PagerDuty 'apiToken' or 'oauth' configuration found in config file.\");\n\n } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\n \n logger.error(\"Missing required PagerDuty OAuth parameters in config file. 'clientId', 'clientSecret', and 'subDomain' are required. 'region' is optional.\");\n throw new Error('Missing required PagerDuty OAuth parameters in config file.');\n\n } else {\n\n authPersistence.authToken = await getOAuthToken(\n config.getString('pagerDuty.oauth.clientId'),\n config.getString('pagerDuty.oauth.clientSecret'),\n config.getString('pagerDuty.oauth.subDomain'),\n config.getOptionalString('pagerDuty.oauth.region') ?? 'us');\n\n logger.info('PagerDuty OAuth configuration loaded successfully.');\n }\n } else {\n authPersistence.authToken = `Token token=${config.getString('pagerDuty.apiToken')}`;\n\n logger.info('PagerDuty API token loaded successfully.');\n }\n }\n catch (error) {\n logger.error(`Unable to retrieve valid PagerDuty AUTH configuration from config file: ${error}`);\n throw error;\n }\n}\n\nasync function getOAuthToken(clientId: string, clientSecret: string, subDomain: string, region: string): Promise<string> {\n // check if required parameters are provided\n if (!clientId || !clientSecret || !subDomain) {\n throw new Error('Missing required PagerDuty OAuth parameters.');\n }\n\n // define the scopes required for the OAuth token\n // const scopes = `\n // abilities.read \n // change_events.read \n // escalation_policies.read \n // incidents.read \n // oncalls.read \n // priorities.read \n // schedules.read \n // services.read\n // services.write \n // teams.read \n // users.read \n // users:contact_methods.read \n // vendors.read\n // `;\n\n const scopes = `\n abilities.read \n addons.read \n addons.write \n analytics.read \n audit_records.read \n change_events.read \n change_events.write \n custom_fields.read \n custom_fields.write \n escalation_policies.read \n escalation_policies.write \n event_orchestrations.read \n event_orchestrations.write \n event_rules.read \n event_rules.write \n extension_schemas.read \n extensions.read \n extensions.write \n incident_workflows.read \n incident_workflows.write \n incident_workflows:instances.write \n incidents.read \n incidents.write \n licenses.read \n notifications.read \n oncalls.read \n priorities.read \n response_plays.read \n response_plays.write \n schedules.read \n schedules.write \n services.read \n services.write \n standards.read \n standards.write \n status_dashboards.read \n status_pages.read \n status_pages.write \n subscribers.read \n subscribers.write \n tags.read \n tags.write \n teams.read \n teams.write \n templates.read \n templates.write \n users.read \n users.write \n users:contact_methods.read \n users:contact_methods.write \n users:sessions.read \n users:sessions.write \n vendors.read\n `;\n\n // encode the parameters for the request\n const urlencoded = new URLSearchParams();\n urlencoded.append(\"grant_type\", \"client_credentials\");\n urlencoded.append(\"client_id\", clientId);\n urlencoded.append(\"client_secret\", clientSecret);\n urlencoded.append(\"scope\", `as_account-${region}.${subDomain} ${scopes}`);\n\n let response: Response;\n const options: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: urlencoded,\n };\n const baseUrl = 'https://identity.pagerduty.com/oauth/token';\n\n try {\n response = await fetch(baseUrl, options);\n } catch (error) {\n throw new Error(`Failed to retrieve oauth token: ${error}`);\n }\n\n switch (response.status) {\n case 400:\n throw new HttpError(\"Failed to retrieve valid token. Bad Request - Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Failed to retrieve valid token. Forbidden - Invalid credentials provided.\", 401);\n default: // 200\n break;\n }\n\n const authResponse = await response.json();\n authPersistence.authTokenExpiryDate = Date.now() + (authResponse.expires_in * 1000);\n return `Bearer ${authResponse.access_token}`;\n}"],"names":[],"mappings":";;;;;;AAkKO,MAAM,kBAAkB,KAAM,CAAA;AAAA,EACjC,WAAA,CAAY,SAAiB,MAAgB,EAAA;AACzC,IAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAIjB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAHI,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AAAA,GAClB;AAGJ;;ACxKO,MAAM,yBAA4B,GAAA,gCAAA;AAGlC,MAAM,oBAAuB,GAAA;;ACOpC,IAAI,eAAA,CAAA;AAEJ,eAAsB,YAAgC,GAAA;AAElD,EACK,IAAA,eAAA,CAAgB,cAAc,EAC3B,IAAA,eAAA,CAAgB,UAAU,QAAS,CAAA,QAAQ,KAC3C,eAAgB,CAAA,mBAAA,GAAsB,KAAK,GAAI,EAAA,IAElD,gBAAgB,SAAc,KAAA,EAAA,IAC3B,gBAAgB,SAAU,CAAA,QAAA,CAAS,OAAO,CAAI,EAAA;AAClD,IAAA,OAAO,eAAgB,CAAA,SAAA,CAAA;AAAA,GAC3B;AAEA,EAAA,MAAM,cAAe,CAAA,eAAA,CAAgB,MAAQ,EAAA,eAAA,CAAgB,MAAM,CAAA,CAAA;AACnE,EAAA,OAAO,eAAgB,CAAA,SAAA,CAAA;AAC3B,CAAA;AA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AAA,MAAM,IAAI,SAAS,CAAA,CAAA,EAAI,MAAM,CAAE,CAAA,CAAA,CAAA;AAExE,EAAI,IAAA,QAAA,CAAA;AACJ,EAAA,MAAM,OAAuB,GAAA;AAAA,IACzB,MAAQ,EAAA,MAAA;AAAA,IACR,OAAS,EAAA;AAAA,MACL,cAAgB,EAAA,mCAAA;AAAA,KACpB;AAAA,IACA,IAAM,EAAA,UAAA;AAAA,GACV,CAAA;AACA,EAAA,MAAM,OAAU,GAAA,4CAAA,CAAA;AAEhB,EAAI,IAAA;AACA,IAAW,QAAA,GAAA,MAAM,KAAM,CAAA,OAAA,EAAS,OAAO,CAAA,CAAA;AAAA,WAClC,KAAO,EAAA;AACZ,IAAA,MAAM,IAAI,KAAA,CAAM,CAAmC,gCAAA,EAAA,KAAK,CAAE,CAAA,CAAA,CAAA;AAAA,GAC9D;AAEA,EAAA,QAAQ,SAAS,MAAQ;AAAA,IACrB,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAAA,IACxG,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAEpG,GACR;AAEA,EAAM,MAAA,YAAA,GAAe,MAAM,QAAA,CAAS,IAAK,EAAA,CAAA;AACzC,EAAA,eAAA,CAAgB,mBAAsB,GAAA,IAAA,CAAK,GAAI,EAAA,GAAK,aAAa,UAAa,GAAA,GAAA,CAAA;AAC9E,EAAO,OAAA,CAAA,OAAA,EAAU,aAAa,YAAY,CAAA,CAAA,CAAA;AAC9C;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/types.ts","../src/constants.ts","../src/auth/auth.ts"],"sourcesContent":["/** @public */\nexport type PagerDutyIncident = {\n id: string;\n title: string;\n status: string;\n html_url: string;\n assignments: [\n {\n assignee: PagerDutyUser;\n },\n ];\n service: PagerDutyService;\n created_at: string;\n}\n\n/** @public */\nexport type PagerDutyUser = {\n id: string;\n summary: string;\n email: string;\n html_url: string;\n name: string;\n avatar_url: string;\n};\n\n/** @public */\nexport type PagerDutyChangeEvent = {\n id: string;\n integration: PagerDutyIntegration[];\n source: string;\n html_url?: string;\n links: [\n {\n href: string;\n text: string;\n },\n ];\n summary: string;\n timestamp: string;\n}\n\n/** @public */\nexport type PagerDutyService = {\n id: string;\n name: string;\n description?: string;\n escalation_policy: PagerDutyEscalationPolicy; \n alert_creation?: string;\n incident_urgency_rule?: PagerDutyIncidentUrgencyRule;\n integrations?: PagerDutyIntegration[];\n teams?: PagerDutyTeam[];\n status?: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url: string;\n}\n\n/** @public */\nexport type PagerDutyEscalationPolicy = {\n id: string;\n name: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyIncidentUrgencyRule = {\n type: string;\n urgency: string;\n}\n\n/** @public */\nexport type PagerDutyIntegration = {\n id: string;\n type: string;\n summary?: string;\n self?: string;\n html_url?: string;\n name?: string;\n service?: PagerDutyService;\n created_at?: string;\n vendor?: PagerDutyVendor;\n integration_key?: string;\n}\n\n/** @public */\nexport type PagerDutyTeam = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyOnCall = {\n user: PagerDutyUser;\n escalation_level: number;\n}\n\n/** @public */\nexport type PagerDutyOnCallUsersResponse = {\n users: PagerDutyUser[];\n}\n\n/** @public */\nexport type PagerDutyVendor = {\n id: string;\n type?: string;\n summary?: string;\n self?: string;\n html_url?: string;\n}\n\n/** @public */\nexport type PagerDutyServicesResponse = {\n services: PagerDutyService[];\n};\n\n/** @public */\nexport type PagerDutyServiceResponse = {\n service: PagerDutyService;\n};\n\n/** @public */\nexport type PagerDutyIncidentsResponse = {\n incidents: PagerDutyIncident[];\n};\n\n/** @public */\nexport type PagerDutyChangeEventsResponse = {\n change_events: PagerDutyChangeEvent[];\n};\n\n/** @public */\nexport type PagerDutyOnCallsResponse = {\n oncalls: PagerDutyOnCall[];\n};\n\n/** @public */\nexport type PagerDutyIntegrationResponse = {\n integration: PagerDutyIntegration;\n}\n\n/** @public */\nexport type PagerDutyEscalationPoliciesResponse = {\n escalation_policies: PagerDutyEscalationPolicy[];\n limit?: number;\n offset?: number;\n more?: boolean;\n total?: number;\n};\n\n/** @public */\nexport type PagerDutyAbilitiesResponse = {\n abilities: string[];\n};\n\n/** @public */\nexport class HttpError extends Error {\n constructor(message: string, status: number) {\n super(message);\n this.status = status;\n }\n\n status: number;\n}\n\n/** @public */\nexport type PagerDutyOAuthConfig = {\n clientId: string;\n clientSecret: string;\n region?: string;\n subDomain: string;\n}\n","/** @public */\nexport const PAGERDUTY_INTEGRATION_KEY = 'pagerduty.com/integration-key';\n\n/** @public */\nexport const PAGERDUTY_SERVICE_ID = 'pagerduty.com/service-id';","import { Logger } from \"winston\";\nimport { Config } from \"@backstage/config\";\nimport { HttpError } from \"../types\";\n\ntype Auth = {\n logger: Logger;\n config: Config;\n authToken: string;\n authTokenExpiryDate: number;\n}\n\nlet authPersistence: Auth;\n\nexport async function getAuthToken(): Promise<string> {\n // check if token already exists and is valid\n if (\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Bearer') &&\n authPersistence.authTokenExpiryDate > Date.now()) // case where OAuth token is still valid\n ||\n (authPersistence.authToken !== '' &&\n authPersistence.authToken.includes('Token'))) { // case where API token is used\n return authPersistence.authToken;\n }\n\n await loadAuthConfig(authPersistence.logger, authPersistence.config);\n return authPersistence.authToken;\n}\n\nexport async function loadAuthConfig(logger: Logger, config: Config) {\n try {\n // initiliaze the authPersistence in-memory object\n authPersistence = {\n logger: logger,\n config: config,\n authToken: '',\n authTokenExpiryDate: Date.now()\n };\n\n if (!config.getOptionalString('pagerDuty.apiToken')) {\n logger.warn('No PagerDuty API token found in config file. Trying OAuth token instead...');\n\n if (!config.getOptional('pagerDuty.oauth')) {\n \n logger.error('No PagerDuty OAuth configuration found in config file.');\n throw new Error(\"No PagerDuty 'apiToken' or 'oauth' configuration found in config file.\");\n\n } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\n \n logger.error(\"Missing required PagerDuty OAuth parameters in config file. 'clientId', 'clientSecret', and 'subDomain' are required. 'region' is optional.\");\n throw new Error('Missing required PagerDuty OAuth parameters in config file.');\n\n } else {\n\n authPersistence.authToken = await getOAuthToken(\n config.getString('pagerDuty.oauth.clientId'),\n config.getString('pagerDuty.oauth.clientSecret'),\n config.getString('pagerDuty.oauth.subDomain'),\n config.getOptionalString('pagerDuty.oauth.region') ?? 'us');\n\n logger.info('PagerDuty OAuth configuration loaded successfully.');\n }\n } else {\n authPersistence.authToken = `Token token=${config.getString('pagerDuty.apiToken')}`;\n\n logger.info('PagerDuty API token loaded successfully.');\n }\n }\n catch (error) {\n logger.error(`Unable to retrieve valid PagerDuty AUTH configuration from config file: ${error}`);\n throw error;\n }\n}\n\nasync function getOAuthToken(clientId: string, clientSecret: string, subDomain: string, region: string): Promise<string> {\n // check if required parameters are provided\n if (!clientId || !clientSecret || !subDomain) {\n throw new Error('Missing required PagerDuty OAuth parameters.');\n }\n\n // define the scopes required for the OAuth token\n const scopes = `\n abilities.read \n change_events.read \n escalation_policies.read \n incidents.read \n oncalls.read \n schedules.read \n services.read \n services.write \n teams.read \n users.read \n vendors.read\n `;\n\n // encode the parameters for the request\n const urlencoded = new URLSearchParams();\n urlencoded.append(\"grant_type\", \"client_credentials\");\n urlencoded.append(\"client_id\", clientId);\n urlencoded.append(\"client_secret\", clientSecret);\n urlencoded.append(\"scope\", `as_account-${region}.${subDomain} ${scopes}`);\n\n let response: Response;\n const options: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: urlencoded,\n };\n const baseUrl = 'https://identity.pagerduty.com/oauth/token';\n\n try {\n response = await fetch(baseUrl, options);\n } catch (error) {\n throw new Error(`Failed to retrieve oauth token: ${error}`);\n }\n\n switch (response.status) {\n case 400:\n throw new HttpError(\"Failed to retrieve valid token. Bad Request - Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Failed to retrieve valid token. Forbidden - Invalid credentials provided.\", 401);\n default: // 200\n break;\n }\n\n const authResponse = await response.json();\n authPersistence.authTokenExpiryDate = Date.now() + (authResponse.expires_in * 1000);\n return `Bearer ${authResponse.access_token}`;\n}"],"names":[],"mappings":";;;;;;AAkKO,MAAM,kBAAkB,KAAM,CAAA;AAAA,EACjC,WAAA,CAAY,SAAiB,MAAgB,EAAA;AACzC,IAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAIjB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAHI,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AAAA,GAClB;AAGJ;;ACxKO,MAAM,yBAA4B,GAAA,gCAAA;AAGlC,MAAM,oBAAuB,GAAA;;ACOpC,IAAI,eAAA,CAAA;AAEJ,eAAsB,YAAgC,GAAA;AAElD,EACK,IAAA,eAAA,CAAgB,cAAc,EAC3B,IAAA,eAAA,CAAgB,UAAU,QAAS,CAAA,QAAQ,KAC3C,eAAgB,CAAA,mBAAA,GAAsB,KAAK,GAAI,EAAA,IAElD,gBAAgB,SAAc,KAAA,EAAA,IAC3B,gBAAgB,SAAU,CAAA,QAAA,CAAS,OAAO,CAAI,EAAA;AAClD,IAAA,OAAO,eAAgB,CAAA,SAAA,CAAA;AAAA,GAC3B;AAEA,EAAA,MAAM,cAAe,CAAA,eAAA,CAAgB,MAAQ,EAAA,eAAA,CAAgB,MAAM,CAAA,CAAA;AACnE,EAAA,OAAO,eAAgB,CAAA,SAAA,CAAA;AAC3B,CAAA;AAEsB,eAAA,cAAA,CAAe,QAAgB,MAAgB,EAAA;AA7BrE,EAAA,IAAA,EAAA,CAAA;AA8BI,EAAI,IAAA;AAEA,IAAkB,eAAA,GA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AAA,OAAA,EAAS,OAAO,CAAA,CAAA;AAAA,WAClC,KAAO,EAAA;AACZ,IAAA,MAAM,IAAI,KAAA,CAAM,CAAmC,gCAAA,EAAA,KAAK,CAAE,CAAA,CAAA,CAAA;AAAA,GAC9D;AAEA,EAAA,QAAQ,SAAS,MAAQ;AAAA,IACrB,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAAA,IACxG,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,2EAAA,EAA6E,GAAG,CAAA,CAAA;AAEpG,GACR;AAEA,EAAM,MAAA,YAAA,GAAe,MAAM,QAAA,CAAS,IAAK,EAAA,CAAA;AACzC,EAAA,eAAA,CAAgB,mBAAsB,GAAA,IAAA,CAAK,GAAI,EAAA,GAAK,aAAa,UAAa,GAAA,GAAA,CAAA;AAC9E,EAAO,OAAA,CAAA,OAAA,EAAU,aAAa,YAAY,CAAA,CAAA,CAAA;AAC9C;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pagerduty/backstage-plugin-common",
3
3
  "description": "Common components for PagerDuty plugins for Backstage",
4
- "version": "0.0.3-next.8",
4
+ "version": "0.0.3-next.9",
5
5
  "main": "dist/index.cjs.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "license": "Apache-2.0",