serviceIncidents.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(statuspageIncidents: StatuspageIncident[]): {
  18. incidents: SentryServiceIncident[];
  19. indicator: IncidentImpact;
  20. } {
  21. if (statuspageIncidents === null || statuspageIncidents.length === 0) {
  22. return {incidents: [], indicator: 'none'};
  23. }
  24. let isMajor = false;
  25. const incidents: SentryServiceIncident[] = [];
  26. statuspageIncidents.forEach(item => {
  27. if (!isMajor && item.impact === 'major') {
  28. isMajor = true;
  29. }
  30. incidents.push({
  31. id: item.id,
  32. name: item.name,
  33. updates: item.incident_updates.map(update => update.body),
  34. url: item.shortlink,
  35. status: item.status,
  36. });
  37. });
  38. return {incidents, indicator: isMajor ? 'major' : 'minor'};
  39. }
  40. export async function loadIncidents(): Promise<SentryServiceStatus | null> {
  41. const cfg = ConfigStore.get('statuspage');
  42. if (!cfg || !cfg.id) {
  43. return null;
  44. }
  45. const response = await fetch(
  46. `https://${cfg.id}.${cfg.api_host}/api/v2/incidents/unresolved.json`
  47. );
  48. if (!response.ok) {
  49. return null;
  50. }
  51. const data = await response.json();
  52. const {incidents, indicator} = getIncidentsFromIncidentResponse(data.incidents);
  53. return {
  54. incidents,
  55. indicator,
  56. url: data.page.url,
  57. };
  58. }