Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding support for API base url override #30

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface Config {
* @visibility frontend
*/
eventsBaseUrl?: string;
/**
* Optional API Base URL to override the default.
* @visibility frontend
*/
apiBaseUrl?: string;
/**
* Optional PagerDuty API Token used in API calls from the backend component.
* @visibility frontend
Expand Down
21 changes: 13 additions & 8 deletions src/apis/pagerduty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import {
HttpError
} from '@pagerduty/backstage-plugin-common';

let apiBaseUrl = 'https://api.pagerduty.com';
export function setAPIBaseUrl(url: string): void {
apiBaseUrl = url;
}

// Supporting custom actions
export async function createService(name: string, description: string, escalationPolicyId: string, alertGrouping?: string): Promise<CreateServiceResponse> {
let alertGroupingParameters = "null";
let response: Response;
const baseUrl = 'https://api.pagerduty.com/services';
const baseUrl = `${apiBaseUrl}/services`;

// Set default body
let body = JSON.stringify({
Expand Down Expand Up @@ -176,7 +181,7 @@ export async function createService(name: string, description: string, escalatio

export async function createServiceIntegration(serviceId: string, vendorId: string): Promise<string> {
let response: Response;
const baseUrl = 'https://api.pagerduty.com/services';
const baseUrl = `${apiBaseUrl}/services`;
const options: RequestInit = {
method: 'POST',
body: JSON.stringify({
Expand Down Expand Up @@ -242,7 +247,7 @@ async function getEscalationPolicies(offset: number, limit: number): Promise<[Bo
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/escalation_policies';
const baseUrl = `${apiBaseUrl}/escalation_policies`;

try {
response = await fetch(`${baseUrl}?${params}`, options);
Expand Down Expand Up @@ -353,7 +358,7 @@ export async function getOncallUsers(escalationPolicy: string): Promise<PagerDut
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/oncalls';
const baseUrl = `${apiBaseUrl}/oncalls`;

try {
response = await fetch(`${baseUrl}?${params}`, options);
Expand Down Expand Up @@ -425,7 +430,7 @@ export async function getServiceById(serviceId: string): Promise<PagerDutyServic
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/services';
const baseUrl = `${apiBaseUrl}/services`;

try {
response = await fetch(`${baseUrl}/${serviceId}?${params}`, options);
Expand Down Expand Up @@ -467,7 +472,7 @@ export async function getServiceByIntegrationKey(integrationKey: string): Promis
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/services';
const baseUrl = `${apiBaseUrl}/services`;

try {
response = await fetch(`${baseUrl}?${params}`, options);
Expand Down Expand Up @@ -513,7 +518,7 @@ export async function getChangeEvents(serviceId: string): Promise<PagerDutyChang
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/services';
const baseUrl = `${apiBaseUrl}/services`;

try {
response = await fetch(`${baseUrl}/${serviceId}/change_events?${params}`, options);
Expand Down Expand Up @@ -556,7 +561,7 @@ export async function getIncidents(serviceId: string): Promise<PagerDutyIncident
'Content-Type': 'application/json',
},
};
const baseUrl = 'https://api.pagerduty.com/incidents';
const baseUrl = `${apiBaseUrl}/incidents`;

try {
response = await fetch(`${baseUrl}?${params}`, options);
Expand Down
8 changes: 6 additions & 2 deletions src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { getAllEscalationPolicies, getChangeEvents, getIncidents, getOncallUsers, getServiceById, getServiceByIntegrationKey } from '../apis/pagerduty';
import { getAllEscalationPolicies, getChangeEvents, getIncidents, getOncallUsers, getServiceById, getServiceByIntegrationKey, setAPIBaseUrl } from '../apis/pagerduty';
import { HttpError, PagerDutyChangeEventsResponse, PagerDutyIncidentsResponse, PagerDutyOnCallUsersResponse, PagerDutyServiceResponse } from '@pagerduty/backstage-plugin-common';
import { loadAuthConfig } from '../auth/auth';

Expand All @@ -20,8 +20,12 @@ export async function createRouter(
// Get authentication Config
await loadAuthConfig(logger, config);

// Get the PagerDuty API Base URL from config
const baseUrl = config.getOptionalString('pagerDuty.apiBaseUrl') !== undefined ? config.getString('pagerDuty.apiBaseUrl') : 'https://api.pagerduty.com';
setAPIBaseUrl(baseUrl);

// Create the router
const router = Router();
const router = Router();
router.use(express.json());

// Add routes
Expand Down
Loading