serviceIncidents.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import ConfigStore from 'app/stores/configStore';
  2. import {SentryServiceIncident, SentryServiceStatus} from 'app/types';
  3. type IncidentImpact = SentryServiceStatus['indicator'];
  4. /**
  5. * This is a partial typing of the statuspage API [0]
  6. *
  7. * [0]: https://doers.statuspage.io/api/v2/incidents/
  8. */
  9. type StatuspageIncident = {
  10. id: string;
  11. name: string;
  12. status: string;
  13. impact: IncidentImpact;
  14. shortlink: string;
  15. incident_updates: {body: string}[];
  16. };
  17. function getIncidentsFromIncidentResponse(
  18. statuspageIncidents: StatuspageIncident[]
  19. ): {incidents: SentryServiceIncident[]; indicator: IncidentImpact} {
  20. if (statuspageIncidents === null || statuspageIncidents.length === 0) {
  21. return {incidents: [], indicator: 'none'};
  22. }
  23. let isMajor = false;
  24. const incidents: SentryServiceIncident[] = [];
  25. statuspageIncidents.forEach(item => {
  26. if (!isMajor && item.impact === 'major') {
  27. isMajor = true;
  28. }
  29. incidents.push({
  30. id: item.id,
  31. name: item.name,
  32. updates: item.incident_updates.map(update => update.body),
  33. url: item.shortlink,
  34. status: item.status,
  35. });
  36. });
  37. return {incidents, indicator: isMajor ? 'major' : 'minor'};
  38. }
  39. export async function loadIncidents(): Promise<SentryServiceStatus | null> {
  40. const cfg = ConfigStore.get('statuspage');
  41. if (!cfg || !cfg.id) {
  42. return null;
  43. }
  44. const response = await fetch(
  45. `https://${cfg.id}.${cfg.api_host}/api/v2/incidents/unresolved.json`
  46. );
  47. if (!response.ok) {
  48. return null;
  49. }
  50. const data = await response.json();
  51. const {incidents, indicator} = getIncidentsFromIncidentResponse(data.incidents);
  52. return {
  53. incidents,
  54. indicator,
  55. url: data.page.url,
  56. };
  57. }