@pagerduty/backstage-plugin-common 0.0.3-next.4 → 0.0.3-next.6

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
@@ -66,7 +66,20 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
66
66
  if (!clientId || !clientSecret || !subDomain) {
67
67
  throw new Error("Missing required PagerDuty OAuth parameters.");
68
68
  }
69
- const scopes = "abilities.read change_events.read escalation_policies.read incidents.read incidents.write oncalls.read priorities.read schedules.read services.read teams.read users.read users:contact_methods.read vendors.read";
69
+ const scopes = `
70
+ abilities.read
71
+ change_events.read
72
+ escalation_policies.read
73
+ incidents.read
74
+ oncalls.read
75
+ priorities.read
76
+ schedules.read
77
+ services.read
78
+ teams.read
79
+ users.read
80
+ users:contact_methods.read
81
+ vendors.read
82
+ `;
70
83
  const urlencoded = new URLSearchParams();
71
84
  urlencoded.append("grant_type", "client_credentials");
72
85
  urlencoded.append("client_id", clientId);
@@ -88,9 +101,9 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
88
101
  }
89
102
  switch (response.status) {
90
103
  case 400:
91
- throw new HttpError("Invalid arguments provided.", 400);
104
+ throw new HttpError("Failed to retrieve valid token. Bad Request - Invalid arguments provided.", 400);
92
105
  case 401:
93
- throw new HttpError("Unauthorized. Invalid credentials provided.", 401);
106
+ throw new HttpError("Failed to retrieve valid token. Forbidden - Invalid credentials provided.", 401);
94
107
  }
95
108
  const authResponse = await response.json();
96
109
  authPersistence.authTokenExpiryDate = Date.now() + authResponse.expires_in * 1e3;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/constants.ts","../src/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 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 } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\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 } else {\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 = \"abilities.read change_events.read escalation_policies.read incidents.read incidents.write oncalls.read priorities.read schedules.read services.read teams.read users.read users:contact_methods.read vendors.read\";\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(\"Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Unauthorized. 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,GAAA;AAAA,MACd,MAAA;AAAA,MACA,MAAA;AAAA,MACA,S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CAAA,CAAA;AAAA,GAC9D;AAEA,EAAA,QAAQ,SAAS,MAAQ;AAAA,IACrB,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,6BAAA,EAA+B,GAAG,CAAA,CAAA;AAAA,IAC1D,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,6CAAA,EAA+C,GAAG,CAAA,CAAA;AAEtE,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.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 teams.read \n users.read \n users:contact_methods.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,Q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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
@@ -62,7 +62,20 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
62
62
  if (!clientId || !clientSecret || !subDomain) {
63
63
  throw new Error("Missing required PagerDuty OAuth parameters.");
64
64
  }
65
- const scopes = "abilities.read change_events.read escalation_policies.read incidents.read incidents.write oncalls.read priorities.read schedules.read services.read teams.read users.read users:contact_methods.read vendors.read";
65
+ const scopes = `
66
+ abilities.read
67
+ change_events.read
68
+ escalation_policies.read
69
+ incidents.read
70
+ oncalls.read
71
+ priorities.read
72
+ schedules.read
73
+ services.read
74
+ teams.read
75
+ users.read
76
+ users:contact_methods.read
77
+ vendors.read
78
+ `;
66
79
  const urlencoded = new URLSearchParams();
67
80
  urlencoded.append("grant_type", "client_credentials");
68
81
  urlencoded.append("client_id", clientId);
@@ -84,9 +97,9 @@ async function getOAuthToken(clientId, clientSecret, subDomain, region) {
84
97
  }
85
98
  switch (response.status) {
86
99
  case 400:
87
- throw new HttpError("Invalid arguments provided.", 400);
100
+ throw new HttpError("Failed to retrieve valid token. Bad Request - Invalid arguments provided.", 400);
88
101
  case 401:
89
- throw new HttpError("Unauthorized. Invalid credentials provided.", 401);
102
+ throw new HttpError("Failed to retrieve valid token. Forbidden - Invalid credentials provided.", 401);
90
103
  }
91
104
  const authResponse = await response.json();
92
105
  authPersistence.authTokenExpiryDate = Date.now() + authResponse.expires_in * 1e3;
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/types.ts","../src/constants.ts","../src/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 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 } else if (!config.getOptionalString('pagerDuty.oauth.clientId') || !config.getOptionalString('pagerDuty.oauth.clientSecret') || !config.getOptionalString('pagerDuty.oauth.subDomain')) {\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 } else {\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 = \"abilities.read change_events.read escalation_policies.read incidents.read incidents.write oncalls.read priorities.read schedules.read services.read teams.read users.read users:contact_methods.read vendors.read\";\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(\"Invalid arguments provided.\", 400);\n case 401:\n throw new HttpError(\"Unauthorized. 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,GAAA;AAAA,MACd,MAA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gCAAA,EAAA,KAAK,CAAE,CAAA,CAAA,CAAA;AAAA,GAC9D;AAEA,EAAA,QAAQ,SAAS,MAAQ;AAAA,IACrB,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,6BAAA,EAA+B,GAAG,CAAA,CAAA;AAAA,IAC1D,KAAK,GAAA;AACD,MAAM,MAAA,IAAI,SAAU,CAAA,6CAAA,EAA+C,GAAG,CAAA,CAAA;AAEtE,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 priorities.read \n schedules.read \n services.read \n teams.read \n users.read \n users:contact_methods.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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A;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.4",
4
+ "version": "0.0.3-next.6",
5
5
  "main": "dist/index.cjs.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "license": "Apache-2.0",