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

Add schedule annotation #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions docs/who-is-on-call-on-component-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ annotations:
```

This annotation accepts any valid OpsGenie team name.

If your OpsGenie teams do not directly own schedules (ie. shared schedule), specify the schedule name instead of a team
name and add an annotation to specify that the team name is actually a schedule:

```yml
annotations:
opsgenie.com/team: 'Awesome Schedule'
opsgenie.com/isSchedule: 'true'
```
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Opsgenie {

getSchedules(): Promise<Schedule[]>;
getSchedulesForTeam(name: string): Promise<Schedule[]>;
getSchedulesByName(name: string): Promise<Schedule[]>;
getOnCall(scheduleId: string): Promise<OnCallParticipantRef[]>;

getTeams(): Promise<Team[]>;
Expand Down Expand Up @@ -177,6 +178,12 @@ export class OpsgenieApi implements Opsgenie {
return response.data.filter(schedule => schedule.ownerTeam && schedule.ownerTeam.name === name);
}

async getSchedulesByName(name: string): Promise<Schedule[]> {
const response = await this.fetch<SchedulesResponse>("/v2/schedules");

return response.data.filter(schedule => schedule.name === name);
}

async getTeams(): Promise<Team[]> {
const response = await this.fetch<TeamsResponse>("/v2/teams");

Expand Down
16 changes: 12 additions & 4 deletions src/components/Entity/OnCallListCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { useEntity } from '@backstage/plugin-catalog-react';
import { InfoCard, InfoCardVariants, MissingAnnotationEmptyState } from '@backstage/core-components';
import { OPSGENIE_TEAM_ANNOTATION } from '../../integration';
import { OPSGENIE_IS_SCHEDULE_ANNOTATION, OPSGENIE_TEAM_ANNOTATION } from '../../integration';
import { opsgenieApiRef } from '../../api';
import { OnCallForScheduleList } from '../OnCallList/OnCallList';
import { Progress } from '@backstage/core-components';
Expand All @@ -16,11 +16,18 @@ type OnCallListCardProps = {

type OnCallListContentProps = {
teamName: string;
isSchedule: boolean;
}

const OnCallListCardContent = ({teamName}: OnCallListContentProps) => {
const OnCallListCardContent = ({teamName, isSchedule}: OnCallListContentProps) => {
const opsgenieApi = useApi(opsgenieApiRef);
const { value, loading, error } = useAsync(async () => await opsgenieApi.getSchedulesForTeam(teamName));

const {value, loading, error} = useAsync(async () => {
if (isSchedule) {
return await opsgenieApi.getSchedulesByName(teamName);
}
return await opsgenieApi.getSchedulesForTeam(teamName);
});

if (loading) {
return <Progress />;
Expand All @@ -42,14 +49,15 @@ const OnCallListCardContent = ({teamName}: OnCallListContentProps) => {
export const OnCallListCard = ({ title, variant }: OnCallListCardProps) => {
const { entity } = useEntity();
const teamName = entity.metadata.annotations?.[OPSGENIE_TEAM_ANNOTATION];
const isSchedule = entity.metadata.annotations?.[OPSGENIE_IS_SCHEDULE_ANNOTATION]?.toLowerCase() === 'true' || false;

if (!teamName) {
return <MissingAnnotationEmptyState annotation={OPSGENIE_TEAM_ANNOTATION} />;
}

return (
<InfoCard title={title || "Opsgenie – Who is on-call?"} variant={variant || "gridItem"}>
<OnCallListCardContent teamName={teamName} />
<OnCallListCardContent teamName={teamName} isSchedule={isSchedule}/>
</InfoCard>
)
};
1 change: 1 addition & 0 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Entity } from "@backstage/catalog-model";

export const OPSGENIE_ANNOTATION = 'opsgenie.com/component-selector';
export const OPSGENIE_TEAM_ANNOTATION = 'opsgenie.com/team';
export const OPSGENIE_IS_SCHEDULE_ANNOTATION = 'opsgenie.com/isSchedule';

export const isOpsgenieAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[OPSGENIE_ANNOTATION]);
export const isOpsgenieOnCallListAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[OPSGENIE_TEAM_ANNOTATION]);