Repository navigation
feat(slack): Add a React team link page #130560
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
Open
sentry-junior
wants to merge
3
commits into
feat/slack-organization-link-team-api
from
feat/slack-link-team-page
+374
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import {OrganizationFixture} from 'sentry-fixture/organization'; | ||
| import {TeamFixture} from 'sentry-fixture/team'; | ||
|
|
||
| import {render, screen} from 'sentry-test/reactTestingLibrary'; | ||
| import {selectEvent} from 'sentry-test/selectEvent'; | ||
|
|
||
| import SlackLinkTeam from 'sentry/views/slackLinkTeam'; | ||
|
|
||
| const SIGNED_PARAMS = 'signed-params'; | ||
|
|
||
| const initialRouterConfig = { | ||
| route: '/extensions/slack/link-team/:signedParams/', | ||
| location: {pathname: `/extensions/slack/link-team/${SIGNED_PARAMS}/`}, | ||
| }; | ||
|
|
||
| describe('SlackLinkTeam', () => { | ||
| const usOrg = OrganizationFixture({ | ||
| slug: 'us-org', | ||
| name: 'US Org', | ||
| links: { | ||
| organizationUrl: 'https://us-org.sentry.io', | ||
| regionUrl: 'https://us.sentry.io', | ||
| }, | ||
| }); | ||
| const deOrg = OrganizationFixture({ | ||
| id: '2', | ||
| slug: 'de-org', | ||
| name: 'DE Org', | ||
| links: { | ||
| organizationUrl: 'https://de-org.sentry.io', | ||
| regionUrl: 'https://de.sentry.io', | ||
| }, | ||
| }); | ||
| const team = TeamFixture({id: '10', slug: 'backend'}); | ||
|
|
||
| function mockContext(organizations = [usOrg, deOrg]) { | ||
| MockApiClient.addMockResponse({ | ||
| url: `/integrations/slack/link-team/${SIGNED_PARAMS}/`, | ||
| body: {channelName: 'alerts', channelUrl: 'slack://channel', organizations}, | ||
| }); | ||
| } | ||
|
|
||
| function mockTeams(organization = deOrg, teams = [team]) { | ||
| return MockApiClient.addMockResponse({ | ||
| url: `/organizations/${organization.slug}/slack/link-team/${SIGNED_PARAMS}/`, | ||
| body: teams, | ||
| match: [(_url, options) => options.host === organization.links.regionUrl], | ||
| }); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| MockApiClient.clearMockResponses(); | ||
| }); | ||
|
|
||
| it('lists teams from the selected organization cell', async () => { | ||
| mockContext(); | ||
| const teamsMock = mockTeams(); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| await selectEvent.select(await screen.findByLabelText('Organization'), 'DE Org'); | ||
| await selectEvent.openMenu(screen.getByLabelText('Team')); | ||
|
|
||
| expect(teamsMock).toHaveBeenCalled(); | ||
| expect(screen.getByText('#backend')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('lists teams from every page', async () => { | ||
| mockContext(); | ||
| MockApiClient.addMockResponse({ | ||
| url: `/organizations/${deOrg.slug}/slack/link-team/${SIGNED_PARAMS}/`, | ||
| body: [team], | ||
| match: [(_url, options) => options.host === deOrg.links.regionUrl], | ||
| headers: { | ||
| Link: | ||
| '<https://de.sentry.io>; rel="previous"; results="false"; cursor="0:0:1", ' + | ||
| '<https://de.sentry.io>; rel="next"; results="true"; cursor="0:100:0"', | ||
| }, | ||
| }); | ||
| MockApiClient.addMockResponse({ | ||
| url: `/organizations/${deOrg.slug}/slack/link-team/${SIGNED_PARAMS}/`, | ||
| body: [TeamFixture({id: '11', slug: 'frontend'})], | ||
| match: [MockApiClient.matchQuery({cursor: '0:100:0'})], | ||
| }); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| await selectEvent.select(await screen.findByLabelText('Organization'), 'DE Org'); | ||
| await selectEvent.openMenu(screen.getByLabelText('Team')); | ||
|
|
||
| expect(await screen.findByText('#frontend')).toBeInTheDocument(); | ||
| expect(screen.getByText('#backend')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows why the teams could not be listed', async () => { | ||
| mockContext(); | ||
| MockApiClient.addMockResponse({ | ||
| url: `/organizations/${deOrg.slug}/slack/link-team/${SIGNED_PARAMS}/`, | ||
| statusCode: 404, | ||
| body: {detail: 'The Slack workspace is not installed in this organization.'}, | ||
| }); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| await selectEvent.select(await screen.findByLabelText('Organization'), 'DE Org'); | ||
|
|
||
| expect( | ||
| await screen.findByText( | ||
| 'The Slack workspace is not installed in this organization.' | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByLabelText('Team')).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('selects the only organization', async () => { | ||
| mockContext([usOrg]); | ||
| mockTeams(usOrg, []); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| expect( | ||
| await screen.findByText( | ||
| /You don't have any teams you can link in this organization/ | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('explains when no organization has the workspace installed', async () => { | ||
| mockContext([]); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| expect( | ||
| await screen.findByText(/None of your organizations have this Slack workspace/) | ||
| ).toBeInTheDocument(); | ||
| expect(screen.queryByText(/Choose a team/)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows an invalid link error', async () => { | ||
| MockApiClient.addMockResponse({ | ||
| url: `/integrations/slack/link-team/${SIGNED_PARAMS}/`, | ||
| statusCode: 400, | ||
| body: {detail: 'This link is invalid or has expired. Run the command again.'}, | ||
| }); | ||
|
|
||
| render(<SlackLinkTeam />, {initialRouterConfig}); | ||
|
|
||
| expect( | ||
| await screen.findByText( | ||
| 'This link is invalid or has expired. Run the command again.' | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import {useQuery} from '@tanstack/react-query'; | ||
|
|
||
| import {Alert} from '@sentry/scraps/alert'; | ||
| import {Stack} from '@sentry/scraps/layout'; | ||
| import {Heading, Text} from '@sentry/scraps/text'; | ||
|
|
||
| import {LoadingIndicator} from 'sentry/components/loadingIndicator'; | ||
| import {NarrowLayout} from 'sentry/components/narrowLayout'; | ||
| import {SentryDocumentTitle} from 'sentry/components/sentryDocumentTitle'; | ||
| import {t, tct} from 'sentry/locale'; | ||
| import {apiOptions} from 'sentry/utils/api/apiOptions'; | ||
| import {getRequestErrorUserMessage} from 'sentry/utils/requestError/getRequestErrorUserMessage'; | ||
| import {useParams} from 'sentry/utils/useParams'; | ||
|
|
||
| import {LinkTeamForm} from './linkTeamForm'; | ||
| import type {LinkTeamContext} from './types'; | ||
|
|
||
| function SlackLinkTeamBody({signedParams}: {signedParams: string}) { | ||
| const { | ||
| data: context, | ||
| isPending, | ||
| isError, | ||
| error, | ||
| } = useQuery({ | ||
| ...apiOptions.as<LinkTeamContext>()('/integrations/slack/link-team/$signedParams/', { | ||
| path: {signedParams}, | ||
| staleTime: Infinity, | ||
| }), | ||
| retry: false, | ||
| }); | ||
|
|
||
| if (isPending) { | ||
| return <LoadingIndicator />; | ||
| } | ||
|
|
||
| if (isError) { | ||
| return <Alert variant="danger">{getRequestErrorUserMessage(error)}</Alert>; | ||
| } | ||
|
|
||
| const heading = ( | ||
| <Heading as="h3" size="xl"> | ||
| {tct('Link a team to [channel]', {channel: `#${context.channelName}`})} | ||
| </Heading> | ||
| ); | ||
|
|
||
| if (context.organizations.length === 0) { | ||
| return ( | ||
| <Stack gap="xl"> | ||
| {heading} | ||
| <Alert variant="warning"> | ||
| {t( | ||
| 'None of your organizations have this Slack workspace installed. Ask an organization owner or manager to install it first.' | ||
| )} | ||
| </Alert> | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Stack gap="xl"> | ||
| <Stack gap="md"> | ||
| {heading} | ||
| <Text as="p"> | ||
| {t( | ||
| 'Choose a team. Its issue alert notifications will be sent to this Slack channel.' | ||
| )} | ||
| </Text> | ||
| </Stack> | ||
| <LinkTeamForm organizations={context.organizations} signedParams={signedParams} /> | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
| export default function SlackLinkTeam() { | ||
| const {signedParams} = useParams<{signedParams: string}>(); | ||
|
|
||
| return ( | ||
| <NarrowLayout> | ||
| <SentryDocumentTitle title={t('Link team to Slack')} /> | ||
| <SlackLinkTeamBody signedParams={signedParams} /> | ||
| </NarrowLayout> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import {skipToken, useInfiniteQuery} from '@tanstack/react-query'; | ||
| import {z} from 'zod'; | ||
|
|
||
| import {Alert} from '@sentry/scraps/alert'; | ||
| import {defaultFormOptions, useScrapsForm, useStore} from '@sentry/scraps/form'; | ||
| import {Stack} from '@sentry/scraps/layout'; | ||
|
|
||
| import {t} from 'sentry/locale'; | ||
| import type {Team} from 'sentry/types/organization'; | ||
| import {useFetchAllPages} from 'sentry/utils/api/apiFetch'; | ||
| import {apiOptions} from 'sentry/utils/api/apiOptions'; | ||
| import {getRequestErrorUserMessage} from 'sentry/utils/requestError/getRequestErrorUserMessage'; | ||
|
|
||
| import type {LinkTeamOrganization} from './types'; | ||
|
|
||
| const schema = z.object({ | ||
| organization: z | ||
| .string() | ||
| .nullable() | ||
| .refine(value => value !== null, t('Select an organization')), | ||
| team: z | ||
| .string() | ||
| .nullable() | ||
| .refine(value => value !== null, t('Select a team')), | ||
| }); | ||
|
|
||
| interface LinkTeamFormProps { | ||
| organizations: LinkTeamOrganization[]; | ||
| signedParams: string; | ||
| } | ||
|
|
||
| export function LinkTeamForm({organizations, signedParams}: LinkTeamFormProps) { | ||
| const defaultValues: z.input<typeof schema> = { | ||
| organization: organizations.length === 1 ? (organizations[0]?.slug ?? null) : null, | ||
| team: null, | ||
| }; | ||
|
|
||
| const form = useScrapsForm({ | ||
| ...defaultFormOptions, | ||
| defaultValues, | ||
| validators: {onDynamic: schema}, | ||
| }); | ||
|
|
||
| const selectedSlug = useStore(form.store, state => state.values.organization); | ||
| const selectedOrganization = organizations.find(org => org.slug === selectedSlug); | ||
|
|
||
| const teamsQuery = useInfiniteQuery({ | ||
| ...apiOptions.asInfinite<Team[]>()( | ||
| '/organizations/$organizationIdOrSlug/slack/link-team/$signedParams/', | ||
| { | ||
| path: selectedOrganization | ||
| ? {organizationIdOrSlug: selectedOrganization.slug, signedParams} | ||
| : skipToken, | ||
| host: selectedOrganization?.links.regionUrl, | ||
| staleTime: 0, | ||
| } | ||
| ), | ||
| select: ({pages}) => pages.flatMap(page => page.json), | ||
| retry: false, | ||
| }); | ||
| useFetchAllPages({result: teamsQuery}); | ||
| const { | ||
| data: teams, | ||
| isFetching: isFetchingTeams, | ||
| isError: isTeamsError, | ||
| error: teamsError, | ||
| } = teamsQuery; | ||
|
|
||
| const hasNoTeams = teams !== undefined && teams.length === 0; | ||
|
|
||
| return ( | ||
| <form.AppForm form={form}> | ||
| <Stack gap="xl"> | ||
| <form.AppField | ||
| name="organization" | ||
| listeners={{onChange: () => form.setFieldValue('team', null)}} | ||
| > | ||
| {field => ( | ||
| <field.Layout.Stack label={t('Organization')} required> | ||
| <field.Select | ||
| value={field.state.value} | ||
| onChange={field.handleChange} | ||
| options={organizations.map(org => ({ | ||
| label: org.name, | ||
| value: org.slug, | ||
| }))} | ||
| /> | ||
| </field.Layout.Stack> | ||
| )} | ||
| </form.AppField> | ||
| <form.AppField name="team"> | ||
| {field => ( | ||
| <field.Layout.Stack label={t('Team')} required> | ||
| <field.Select | ||
| value={field.state.value} | ||
| onChange={field.handleChange} | ||
| isLoading={isFetchingTeams} | ||
| disabled={teams === undefined || isTeamsError || hasNoTeams} | ||
| options={(teams ?? []).map(team => ({ | ||
| label: `#${team.slug}`, | ||
| value: team.id, | ||
| }))} | ||
| /> | ||
| </field.Layout.Stack> | ||
| )} | ||
| </form.AppField> | ||
| {isTeamsError && ( | ||
| <Alert variant="danger">{getRequestErrorUserMessage(teamsError)}</Alert> | ||
| )} | ||
| {hasNoTeams && ( | ||
| <Alert variant="warning"> | ||
| {t( | ||
| "You don't have any teams you can link in this organization. You need to be on a team to link it, and unless you're an organization owner, manager, or admin, you also need to be its team admin." | ||
| )} | ||
| </Alert> | ||
| )} | ||
| </Stack> | ||
| </form.AppForm> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import type {OrganizationSummary} from 'sentry/types/organization'; | ||
|
|
||
| export type LinkTeamOrganization = Pick< | ||
| OrganizationSummary, | ||
| 'id' | 'slug' | 'name' | 'links' | ||
| >; | ||
|
|
||
| export interface LinkTeamContext { | ||
| channelName: string; | ||
| channelUrl: string; | ||
| organizations: LinkTeamOrganization[]; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.