@pagerduty/backstage-plugin-scaffolder-actions 0.2.1 → 0.2.2-alpha.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.
package/config.d.ts CHANGED
@@ -14,39 +14,42 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import { PagerDutyAccountConfig, PagerDutyOAuthConfig } from '@pagerduty/backstage-plugin-common';
17
+ import {
18
+ PagerDutyAccountConfig,
19
+ PagerDutyOAuthConfig,
20
+ } from '@pagerduty/backstage-plugin-common';
18
21
 
19
22
  export interface Config {
23
+ /**
24
+ * Configuration for the PagerDuty plugin
25
+ * @visibility frontend
26
+ */
27
+ pagerDuty?: {
20
28
  /**
21
- * Configuration for the PagerDuty plugin
29
+ * Optional Events Base URL to override the default.
22
30
  * @visibility frontend
23
31
  */
24
- pagerDuty?: {
25
- /**
26
- * Optional Events Base URL to override the default.
27
- * @visibility frontend
28
- */
29
- eventsBaseUrl?: string;
30
- /**
31
- * Optional API Base URL to override the default.
32
- * @visibility frontend
33
- */
34
- apiBaseUrl?: string;
35
- /**
36
- * Optional PagerDuty API Token used in API calls from the backend component.
37
- * @visibility secret
38
- */
39
- apiToken?: string;
40
- /**
41
- * Optional PagerDuty Scoped OAuth Token used in API calls from the backend component.
42
- * @deepVisibility secret
43
- */
44
- oauth?: PagerDutyOAuthConfig;
32
+ eventsBaseUrl?: string;
33
+ /**
34
+ * Optional API Base URL to override the default.
35
+ * @visibility frontend
36
+ */
37
+ apiBaseUrl?: string;
38
+ /**
39
+ * Optional PagerDuty API Token used in API calls from the backend component.
40
+ * @visibility secret
41
+ */
42
+ apiToken?: string;
43
+ /**
44
+ * Optional PagerDuty Scoped OAuth Token used in API calls from the backend component.
45
+ * @deepVisibility secret
46
+ */
47
+ oauth?: PagerDutyOAuthConfig;
45
48
 
46
- /**
47
- * Optional PagerDuty multi-account configuration
48
- * @deepVisibility secret
49
- */
50
- accounts?: PagerDutyAccountConfig[];
51
- };
49
+ /**
50
+ * Optional PagerDuty multi-account configuration
51
+ * @deepVisibility secret
52
+ */
53
+ accounts?: PagerDutyAccountConfig[];
54
+ };
52
55
  }
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
4
+ var zod = require('zod');
5
+ var pagerduty = require('../apis/pagerduty.cjs.js');
6
+ var auth = require('../auth/auth.cjs.js');
7
+ var backendCommon = require('@backstage/backend-common');
8
+
9
+ const createPagerDutyServiceAction = (props) => {
10
+ let loggerService;
11
+ return pluginScaffolderNode.createTemplateAction({
12
+ id: "pagerduty:service:create",
13
+ schema: {
14
+ input: zod.z.object({
15
+ name: zod.z.string().min(1, "name is required").describe("Name of the service"),
16
+ description: zod.z.string().min(1, "description is required").describe("Description of the service"),
17
+ escalationPolicyId: zod.z.string().min(1, "Escalation policy is required").describe("Escalation policy ID"),
18
+ alertGrouping: zod.z.string().optional().describe("Alert grouping parameters")
19
+ }),
20
+ output: zod.z.object({
21
+ serviceUrl: zod.z.string().describe("PagerDuty Service URL"),
22
+ serviceId: zod.z.string().describe("PagerDuty Service ID"),
23
+ integrationKey: zod.z.string().describe("Backstage Integration Key")
24
+ })
25
+ },
26
+ async handler(ctx) {
27
+ try {
28
+ loggerService = props?.logger ? props.logger : ctx.logger;
29
+ const configService = props?.config;
30
+ const legacyConfig = await backendCommon.loadBackendConfig({
31
+ logger: loggerService,
32
+ argv: []
33
+ });
34
+ await auth.loadAuthConfig({
35
+ config: configService,
36
+ legacyConfig,
37
+ logger: loggerService
38
+ });
39
+ pagerduty.loadPagerDutyEndpointsFromConfig({
40
+ config: configService,
41
+ legacyConfig,
42
+ logger: loggerService
43
+ });
44
+ const account = await pagerduty.getAccountByEscalationPolicyId(
45
+ ctx.input.escalationPolicyId
46
+ );
47
+ loggerService.info(
48
+ `Creating service '${ctx.input.name}' in account '${account}'.`
49
+ );
50
+ const service = await pagerduty.createService({
51
+ name: ctx.input.name,
52
+ description: ctx.input.description,
53
+ escalationPolicyId: ctx.input.escalationPolicyId,
54
+ account,
55
+ alertGrouping: ctx.input.alertGrouping
56
+ });
57
+ loggerService.info(`Service '${ctx.input.name}' created successfully!`);
58
+ loggerService.info(`Alert grouping set to '${service.alertGrouping}'`);
59
+ ctx.output("serviceUrl", service.url);
60
+ ctx.output("serviceId", service.id);
61
+ ctx.output("account", account);
62
+ const backstageIntegrationId = "PRO19CT";
63
+ loggerService.info(
64
+ `Creating Backstage Integration for service '${ctx.input.name}' in account '${account}'.`
65
+ );
66
+ const integrationKey = await pagerduty.createServiceIntegration({
67
+ serviceId: service.id,
68
+ vendorId: backstageIntegrationId,
69
+ account
70
+ });
71
+ loggerService.info(
72
+ `Backstage Integration for service '${ctx.input.name}' created successfully!`
73
+ );
74
+ ctx.output("integrationKey", integrationKey);
75
+ } catch (error) {
76
+ loggerService.error(`${error}`);
77
+ }
78
+ }
79
+ });
80
+ };
81
+
82
+ exports.createPagerDutyServiceAction = createPagerDutyServiceAction;
83
+ //# sourceMappingURL=custom.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom.cjs.js","sources":["../../src/actions/custom.ts"],"sourcesContent":["import { createTemplateAction } from '@backstage/plugin-scaffolder-node';\nimport { z } from 'zod';\nimport * as api from '../apis/pagerduty';\nimport { CreateServiceResponse } from '../types';\nimport { loadAuthConfig } from '../auth/auth';\nimport {\n LoggerService,\n RootConfigService,\n} from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport { loadBackendConfig } from '@backstage/backend-common';\nimport {\n loadPagerDutyEndpointsFromConfig,\n getAccountByEscalationPolicyId,\n} from '../apis/pagerduty';\n\nexport type CreatePagerDutyServiceActionProps = {\n config: RootConfigService;\n logger: LoggerService;\n};\n\nexport const createPagerDutyServiceAction = (\n props?: CreatePagerDutyServiceActionProps,\n) => {\n let loggerService: LoggerService;\n\n return createTemplateAction<{\n name: string;\n description: string;\n escalationPolicyId: string;\n alertGrouping?: string;\n }>({\n id: 'pagerduty:service:create',\n schema: {\n input: z.object({\n name: z\n .string()\n .min(1, 'name is required')\n .describe('Name of the service'),\n description: z\n .string()\n .min(1, 'description is required')\n .describe('Description of the service'),\n escalationPolicyId: z\n .string()\n .min(1, 'Escalation policy is required')\n .describe('Escalation policy ID'),\n alertGrouping: z\n .string()\n .optional()\n .describe('Alert grouping parameters'),\n }),\n output: z.object({\n serviceUrl: z.string().describe('PagerDuty Service URL'),\n serviceId: z.string().describe('PagerDuty Service ID'),\n integrationKey: z.string().describe('Backstage Integration Key'),\n }),\n },\n\n async handler(ctx) {\n try {\n loggerService = props?.logger ? props.logger : ctx.logger;\n const configService = props?.config;\n\n const legacyConfig: Config = await loadBackendConfig({\n logger: loggerService,\n argv: [],\n });\n\n // Load the auth configuration\n await loadAuthConfig({\n config: configService,\n legacyConfig: legacyConfig,\n logger: loggerService,\n });\n\n // Load endpoint configuration\n loadPagerDutyEndpointsFromConfig({\n config: configService,\n legacyConfig: legacyConfig,\n logger: loggerService,\n });\n\n const account: string = await getAccountByEscalationPolicyId(\n ctx.input.escalationPolicyId,\n );\n\n // Create service in PagerDuty\n loggerService.info(\n `Creating service '${ctx.input.name}' in account '${account}'.`,\n );\n const service: CreateServiceResponse = await api.createService({\n name: ctx.input.name,\n description: ctx.input.description,\n escalationPolicyId: ctx.input.escalationPolicyId,\n account: account,\n alertGrouping: ctx.input.alertGrouping,\n });\n loggerService.info(`Service '${ctx.input.name}' created successfully!`);\n loggerService.info(`Alert grouping set to '${service.alertGrouping}'`);\n\n ctx.output('serviceUrl', service.url);\n ctx.output('serviceId', service.id);\n ctx.output('account', account);\n\n // Create Backstage Integration in PagerDuty service\n const backstageIntegrationId = 'PRO19CT'; // ID for Backstage integration\n\n loggerService.info(\n `Creating Backstage Integration for service '${ctx.input.name}' in account '${account}'.`,\n );\n\n const integrationKey = await api.createServiceIntegration({\n serviceId: service.id,\n vendorId: backstageIntegrationId,\n account,\n });\n loggerService.info(\n `Backstage Integration for service '${ctx.input.name}' created successfully!`,\n );\n\n ctx.output('integrationKey', integrationKey);\n } catch (error) {\n loggerService.error(`${error}`);\n }\n },\n });\n};\n"],"names":["createTemplateAction","z","loadBackendConfig","loadAuthConfig","loadPagerDutyEndpointsFromConfig","getAccountByEscalationPolicyId","api.createService","api.createServiceIntegration"],"mappings":"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"}
@@ -0,0 +1,451 @@
1
+ 'use strict';
2
+
3
+ var fetch = require('node-fetch');
4
+ var auth = require('../auth/auth.cjs.js');
5
+ var backstagePluginCommon = require('@pagerduty/backstage-plugin-common');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
8
+
9
+ var fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
10
+
11
+ const EndpointConfig = {};
12
+ let fallbackEndpointConfig;
13
+ let isLegacyConfig = false;
14
+ let _config;
15
+ let _legacyConfig;
16
+ let _logger;
17
+ function setFallbackEndpointConfig(account) {
18
+ fallbackEndpointConfig = {
19
+ eventsBaseUrl: account.eventsBaseUrl !== void 0 ? account.eventsBaseUrl : "https://events.pagerduty.com/v2",
20
+ apiBaseUrl: account.apiBaseUrl !== void 0 ? account.apiBaseUrl : "https://api.pagerduty.com"
21
+ };
22
+ }
23
+ function insertEndpointConfig(account) {
24
+ EndpointConfig[account.id] = {
25
+ eventsBaseUrl: account.eventsBaseUrl !== void 0 ? account.eventsBaseUrl : "https://events.pagerduty.com/v2",
26
+ apiBaseUrl: account.apiBaseUrl !== void 0 ? account.apiBaseUrl : "https://api.pagerduty.com"
27
+ };
28
+ }
29
+ function loadPagerDutyEndpointsFromConfig({
30
+ config,
31
+ legacyConfig,
32
+ logger
33
+ }) {
34
+ _config = config;
35
+ _legacyConfig = legacyConfig;
36
+ _logger = logger;
37
+ if (readOptionalObject("pagerDuty.accounts")) {
38
+ _logger.debug(
39
+ `New accounts configuration detected. Loading PagerDuty endpoints from config.`
40
+ );
41
+ isLegacyConfig = false;
42
+ const accounts = JSON.parse(
43
+ JSON.stringify(readOptionalObject("pagerDuty.accounts"))
44
+ );
45
+ if (accounts?.length === 1) {
46
+ _logger.debug(
47
+ `Single account configuration detected. Loading PagerDuty endpoints from config to 'default'.`
48
+ );
49
+ EndpointConfig.default = {
50
+ eventsBaseUrl: accounts[0].eventsBaseUrl !== void 0 ? accounts[0].eventsBaseUrl : "https://events.pagerduty.com/v2",
51
+ apiBaseUrl: accounts[0].apiBaseUrl !== void 0 ? accounts[0].apiBaseUrl : "https://api.pagerduty.com"
52
+ };
53
+ } else {
54
+ _logger.debug(
55
+ `Multiple account configuration detected. Loading PagerDuty endpoints from config.`
56
+ );
57
+ accounts?.forEach((account) => {
58
+ if (account.isDefault) {
59
+ setFallbackEndpointConfig(account);
60
+ }
61
+ insertEndpointConfig(account);
62
+ });
63
+ }
64
+ } else {
65
+ _logger.debug(`Loading legacy PagerDuty endpoints from config.`);
66
+ isLegacyConfig = true;
67
+ EndpointConfig.default = {
68
+ eventsBaseUrl: readOptionalString("pagerDuty.eventsBaseUrl") !== void 0 ? readString("pagerDuty.eventsBaseUrl") : "https://events.pagerduty.com/v2",
69
+ apiBaseUrl: readOptionalString("pagerDuty.apiBaseUrl") !== void 0 ? readString("pagerDuty.apiBaseUrl") : "https://api.pagerduty.com"
70
+ };
71
+ }
72
+ }
73
+ function getApiBaseUrl(account) {
74
+ if (isLegacyConfig === true) {
75
+ return EndpointConfig.default.apiBaseUrl;
76
+ }
77
+ if (account) {
78
+ return EndpointConfig[account].apiBaseUrl;
79
+ }
80
+ return fallbackEndpointConfig.apiBaseUrl;
81
+ }
82
+ async function createService({
83
+ name,
84
+ description,
85
+ escalationPolicyId,
86
+ account,
87
+ alertGrouping
88
+ }) {
89
+ let alertGroupingParameters = "null";
90
+ let response;
91
+ const apiBaseUrl = getApiBaseUrl(account);
92
+ const baseUrl = `${apiBaseUrl}/services`;
93
+ let body = JSON.stringify({
94
+ service: {
95
+ type: "service",
96
+ name,
97
+ description,
98
+ alert_creation: "create_alerts_and_incidents",
99
+ auto_pause_notifications_parameters: {
100
+ enabled: true,
101
+ timeout: 300
102
+ },
103
+ escalation_policy: {
104
+ id: escalationPolicyId,
105
+ type: "escalation_policy_reference"
106
+ }
107
+ }
108
+ });
109
+ if (await isEventNoiseReductionEnabled(account) && alertGrouping !== void 0) {
110
+ alertGroupingParameters = alertGrouping;
111
+ switch (alertGroupingParameters) {
112
+ case "intelligent":
113
+ body = JSON.stringify({
114
+ service: {
115
+ type: "service",
116
+ name,
117
+ description,
118
+ escalation_policy: {
119
+ id: escalationPolicyId,
120
+ type: "escalation_policy_reference"
121
+ },
122
+ alert_creation: "create_alerts_and_incidents",
123
+ alert_grouping_parameters: {
124
+ type: alertGroupingParameters
125
+ },
126
+ auto_pause_notifications_parameters: {
127
+ enabled: true,
128
+ timeout: 300
129
+ }
130
+ }
131
+ });
132
+ break;
133
+ case "time":
134
+ body = JSON.stringify({
135
+ service: {
136
+ type: "service",
137
+ name,
138
+ description,
139
+ escalation_policy: {
140
+ id: escalationPolicyId,
141
+ type: "escalation_policy_reference"
142
+ },
143
+ alert_creation: "create_alerts_and_incidents",
144
+ alert_grouping_parameters: {
145
+ type: alertGroupingParameters,
146
+ config: {
147
+ timeout: 0
148
+ }
149
+ },
150
+ auto_pause_notifications_parameters: {
151
+ enabled: true,
152
+ timeout: 300
153
+ }
154
+ }
155
+ });
156
+ break;
157
+ case "content_based":
158
+ body = JSON.stringify({
159
+ service: {
160
+ type: "service",
161
+ name,
162
+ description,
163
+ escalation_policy: {
164
+ id: escalationPolicyId,
165
+ type: "escalation_policy_reference"
166
+ },
167
+ alert_creation: "create_alerts_and_incidents",
168
+ alert_grouping_parameters: {
169
+ type: alertGroupingParameters,
170
+ config: {
171
+ aggregate: "all",
172
+ time_window: 0,
173
+ fields: ["source", "summary"]
174
+ }
175
+ },
176
+ auto_pause_notifications_parameters: {
177
+ enabled: true,
178
+ timeout: 300
179
+ }
180
+ }
181
+ });
182
+ break;
183
+ }
184
+ }
185
+ const token = await auth.getAuthToken(account);
186
+ const options = {
187
+ method: "POST",
188
+ body,
189
+ headers: {
190
+ Authorization: token,
191
+ Accept: "application/vnd.pagerduty+json;version=2",
192
+ "Content-Type": "application/json"
193
+ }
194
+ };
195
+ try {
196
+ response = await fetch__default.default(baseUrl, options);
197
+ } catch (error) {
198
+ throw new Error(`Failed to create service: ${error}`);
199
+ }
200
+ switch (response.status) {
201
+ case 400:
202
+ throw new Error(
203
+ `Failed to create service. Caller provided invalid arguments.`
204
+ );
205
+ case 401:
206
+ throw new Error(
207
+ `Failed to create service. Caller did not supply credentials or did not provide the correct credentials.`
208
+ );
209
+ case 402:
210
+ throw new Error(
211
+ `Failed to create service. Account does not have the abilities to perform the action.`
212
+ );
213
+ case 403:
214
+ throw new Error(
215
+ `Failed to create service. Caller is not authorized to view the requested resource.`
216
+ );
217
+ }
218
+ let result;
219
+ try {
220
+ result = await response.json();
221
+ const createServiceResult = {
222
+ url: result.service.html_url,
223
+ id: result.service.id,
224
+ alertGrouping: alertGroupingParameters
225
+ };
226
+ return createServiceResult;
227
+ } catch (error) {
228
+ throw new Error(`Failed to parse service information: ${error}`);
229
+ }
230
+ }
231
+ async function createServiceIntegration({
232
+ serviceId,
233
+ vendorId,
234
+ account
235
+ }) {
236
+ let response;
237
+ const apiBaseUrl = getApiBaseUrl(account);
238
+ const baseUrl = `${apiBaseUrl}/services`;
239
+ const token = await auth.getAuthToken(account);
240
+ const options = {
241
+ method: "POST",
242
+ body: JSON.stringify({
243
+ integration: {
244
+ name: "Backstage",
245
+ service: {
246
+ id: serviceId,
247
+ type: "service_reference"
248
+ },
249
+ vendor: {
250
+ id: vendorId,
251
+ type: "vendor_reference"
252
+ }
253
+ }
254
+ }),
255
+ headers: {
256
+ Authorization: token,
257
+ Accept: "application/vnd.pagerduty+json;version=2",
258
+ "Content-Type": "application/json"
259
+ }
260
+ };
261
+ try {
262
+ response = await fetch__default.default(`${baseUrl}/${serviceId}/integrations`, options);
263
+ } catch (error) {
264
+ throw new Error(`Failed to create service integration: ${error}`);
265
+ }
266
+ switch (response.status) {
267
+ case 400:
268
+ throw new Error(
269
+ `Failed to create service integration. Caller provided invalid arguments.`
270
+ );
271
+ case 401:
272
+ throw new Error(
273
+ `Failed to create service integration. Caller did not supply credentials or did not provide the correct credentials.`
274
+ );
275
+ case 403:
276
+ throw new Error(
277
+ `Failed to create service integration. Caller is not authorized to view the requested resource.`
278
+ );
279
+ case 429:
280
+ throw new Error(
281
+ `Failed to create service integration. Rate limit exceeded.`
282
+ );
283
+ }
284
+ let result;
285
+ try {
286
+ result = await response.json();
287
+ return result.integration.integration_key ?? "";
288
+ } catch (error) {
289
+ throw new Error(`Failed to parse service information: ${error}`);
290
+ }
291
+ }
292
+ async function isEventNoiseReductionEnabled(account) {
293
+ let response;
294
+ const baseUrl = getApiBaseUrl(account);
295
+ const token = await auth.getAuthToken(account);
296
+ const options = {
297
+ method: "GET",
298
+ headers: {
299
+ Authorization: token,
300
+ Accept: "application/vnd.pagerduty+json;version=2",
301
+ "Content-Type": "application/json"
302
+ }
303
+ };
304
+ try {
305
+ response = await fetch__default.default(`${baseUrl}/abilities`, options);
306
+ } catch (error) {
307
+ throw new Error(`Failed to read abilities: ${error}`);
308
+ }
309
+ switch (response.status) {
310
+ case 401:
311
+ throw new Error(
312
+ `Failed to read abilities. Caller did not supply credentials or did not provide the correct credentials.`
313
+ );
314
+ case 403:
315
+ throw new Error(
316
+ `Failed to read abilities. Caller is not authorized to view the requested resource.`
317
+ );
318
+ case 429:
319
+ throw new Error(`Failed to read abilities. Rate limit exceeded.`);
320
+ }
321
+ let result;
322
+ try {
323
+ result = await response.json();
324
+ if (result.abilities.includes("preview_intelligent_alert_grouping") && result.abilities.includes("time_based_alert_grouping")) {
325
+ return true;
326
+ }
327
+ return false;
328
+ } catch (error) {
329
+ throw new Error(`Failed to parse abilities information: ${error}`);
330
+ }
331
+ }
332
+ function readOptionalString(key) {
333
+ if (!_config) {
334
+ return _legacyConfig.getOptionalString(key);
335
+ }
336
+ return _config.getOptionalString(key);
337
+ }
338
+ function readOptionalObject(key) {
339
+ if (!_config) {
340
+ return _legacyConfig.getOptional(key);
341
+ }
342
+ return _config.getOptional(key);
343
+ }
344
+ function readString(key) {
345
+ if (!_config) {
346
+ return _legacyConfig.getString(key);
347
+ }
348
+ return _config.getString(key);
349
+ }
350
+ async function getAccountByEscalationPolicyId(escalationPolicyId) {
351
+ const escalationPoliciesList = await getAllEscalationPolicies();
352
+ const escalationPolicy = escalationPoliciesList.find(
353
+ (policy) => policy.id === escalationPolicyId
354
+ );
355
+ return escalationPolicy?.account ?? "";
356
+ }
357
+ async function getEscalationPolicies(offset, limit, account) {
358
+ let response;
359
+ const params = `total=true&sort_by=name&offset=${offset}&limit=${limit}`;
360
+ const options = {
361
+ method: "GET",
362
+ headers: {
363
+ Authorization: await auth.getAuthToken(account),
364
+ Accept: "application/vnd.pagerduty+json;version=2",
365
+ "Content-Type": "application/json"
366
+ }
367
+ };
368
+ const apiBaseUrl = getApiBaseUrl(account);
369
+ const baseUrl = `${apiBaseUrl}/escalation_policies`;
370
+ try {
371
+ response = await fetch__default.default(`${baseUrl}?${params}`, options);
372
+ } catch (error) {
373
+ throw new Error(`Failed to retrieve escalation policies: ${error}`);
374
+ }
375
+ switch (response.status) {
376
+ case 400:
377
+ throw new backstagePluginCommon.HttpError(
378
+ "Failed to list escalation policies. Caller provided invalid arguments.",
379
+ 400
380
+ );
381
+ case 401:
382
+ throw new backstagePluginCommon.HttpError(
383
+ "Failed to list escalation policies. Caller did not supply credentials or did not provide the correct credentials.",
384
+ 401
385
+ );
386
+ case 403:
387
+ throw new backstagePluginCommon.HttpError(
388
+ "Failed to list escalation policies. Caller is not authorized to view the requested resource.",
389
+ 403
390
+ );
391
+ case 429:
392
+ throw new backstagePluginCommon.HttpError(
393
+ "Failed to list escalation policies. Rate limit exceeded.",
394
+ 429
395
+ );
396
+ }
397
+ let result;
398
+ try {
399
+ result = await response.json();
400
+ return [result.more ?? false, result.escalation_policies];
401
+ } catch (error) {
402
+ throw new backstagePluginCommon.HttpError(
403
+ `Failed to parse escalation policy information: ${error}`,
404
+ 500
405
+ );
406
+ }
407
+ }
408
+ async function getAllEscalationPolicies() {
409
+ const limit = 50;
410
+ let offset = 0;
411
+ let moreResults = false;
412
+ let results = [];
413
+ await Promise.all(
414
+ Object.keys(EndpointConfig).map(async (account) => {
415
+ try {
416
+ offset = 0;
417
+ do {
418
+ const res = await getEscalationPolicies(offset, limit, account);
419
+ res[1].forEach((policy) => {
420
+ policy.account = account;
421
+ });
422
+ results = results.concat(res[1]);
423
+ if (res[0] === true) {
424
+ moreResults = true;
425
+ offset += limit;
426
+ } else {
427
+ moreResults = false;
428
+ }
429
+ } while (moreResults === true);
430
+ } catch (error) {
431
+ if (error instanceof backstagePluginCommon.HttpError) {
432
+ throw error;
433
+ } else {
434
+ throw new backstagePluginCommon.HttpError(`${error}`, 500);
435
+ }
436
+ }
437
+ })
438
+ );
439
+ return results;
440
+ }
441
+
442
+ exports.createService = createService;
443
+ exports.createServiceIntegration = createServiceIntegration;
444
+ exports.getAccountByEscalationPolicyId = getAccountByEscalationPolicyId;
445
+ exports.getAllEscalationPolicies = getAllEscalationPolicies;
446
+ exports.getApiBaseUrl = getApiBaseUrl;
447
+ exports.insertEndpointConfig = insertEndpointConfig;
448
+ exports.isEventNoiseReductionEnabled = isEventNoiseReductionEnabled;
449
+ exports.loadPagerDutyEndpointsFromConfig = loadPagerDutyEndpointsFromConfig;
450
+ exports.setFallbackEndpointConfig = setFallbackEndpointConfig;
451
+ //# sourceMappingURL=pagerduty.cjs.js.map