Repository navigation
feat(integrations): connect slack in a new tab from every slack picker #106427
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a6f99d8
feat(integrations): connect Slack in a new tab from every Slack picker
rafaeelaudibert 1814f54
fix(integrations): only select the slack workspace this user connected
rafaeelaudibert 4b53ce9
fix(integrations): stop waiting for slack after a timeout and gate th…
rafaeelaudibert 51088db
fix(integrations): offer a retry when the slack banner cannot load wo…
rafaeelaudibert 6484bb7
chore(integrations): regenerate slack connect logic types
rafaeelaudibert 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
113 changes: 113 additions & 0 deletions
113
frontend/src/lib/integrations/slackConnectLogic.test.ts
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,113 @@ | ||
| import { MOCK_DEFAULT_BASIC_USER, MOCK_DEFAULT_USER } from 'lib/api.mock' | ||
|
|
||
| import { expectLogic } from 'kea-test-utils' | ||
|
|
||
| import { userLogic } from 'scenes/userLogic' | ||
|
|
||
| import { useMocks } from '~/mocks/jest' | ||
| import { initKeaTests } from '~/test/init' | ||
| import { IntegrationType } from '~/types' | ||
|
|
||
| import { integrationsLogic } from './integrationsLogic' | ||
| import { SLACK_CONNECT_TIMEOUT_MS, slackConnectLogic } from './slackConnectLogic' | ||
|
|
||
| const OTHER_MEMBER = { ...MOCK_DEFAULT_BASIC_USER, id: 999, uuid: 'other-member-uuid' } | ||
|
|
||
| const integration = ( | ||
| id: number, | ||
| kind: IntegrationType['kind'], | ||
| createdBy: IntegrationType['created_by'] = MOCK_DEFAULT_BASIC_USER | ||
| ): IntegrationType => | ||
| ({ | ||
| id, | ||
| kind, | ||
| display_name: `Workspace ${id}`, | ||
| icon_url: '', | ||
| config: {}, | ||
| errors: '', | ||
| created_by: createdBy, | ||
| }) as IntegrationType | ||
|
|
||
| describe('slackConnectLogic', () => { | ||
| let onConnected: jest.Mock | ||
| let logic: ReturnType<typeof slackConnectLogic.build> | ||
|
|
||
| beforeEach(() => { | ||
| useMocks({ get: { '/api/projects/:team_id/integrations/': () => [200, { results: [] }] } }) | ||
| initKeaTests() | ||
| userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER) | ||
| integrationsLogic.mount() | ||
| onConnected = jest.fn() | ||
| logic = slackConnectLogic({ connectKey: 'test', onConnected }) | ||
| logic.mount() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| logic.unmount() | ||
| }) | ||
|
|
||
| it('selects the workspace that appears after the click and stops polling', async () => { | ||
| integrationsLogic.actions.loadIntegrationsSuccess([integration(1, 'slack')]) | ||
| expect(onConnected).not.toHaveBeenCalled() | ||
|
|
||
| await expectLogic(logic, () => logic.actions.connectSlackClicked()).toMatchValues({ waitingForSlack: true }) | ||
| expect(integrationsLogic.values.pollingSubscribers).toBe(1) | ||
|
|
||
| integrationsLogic.actions.loadIntegrationsSuccess([integration(1, 'slack'), integration(2, 'github')]) | ||
| expect(onConnected).not.toHaveBeenCalled() | ||
|
|
||
| integrationsLogic.actions.loadIntegrationsSuccess([ | ||
| integration(1, 'slack'), | ||
| integration(2, 'github'), | ||
| integration(3, 'slack'), | ||
| ]) | ||
|
|
||
| expect(onConnected).toHaveBeenCalledWith(3) | ||
| expect(logic.values.waitingForSlack).toBe(false) | ||
| expect(integrationsLogic.values.pollingSubscribers).toBe(0) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { | ||
| name: 'a workspace another member adds while this user connects', | ||
| before: [integration(1, 'slack')], | ||
| after: [integration(1, 'slack'), integration(2, 'slack', OTHER_MEMBER)], | ||
| }, | ||
| { | ||
| name: 'an existing workspace when the list had not loaded at the click', | ||
| before: null, | ||
| after: [integration(1, 'slack')], | ||
| }, | ||
| ])('does not select $name', ({ before, after }) => { | ||
| integrationsLogic.actions.loadIntegrationsSuccess(before as IntegrationType[]) | ||
| logic.actions.connectSlackClicked() | ||
|
|
||
| integrationsLogic.actions.loadIntegrationsSuccess(after) | ||
|
|
||
| expect(onConnected).not.toHaveBeenCalled() | ||
| expect(logic.values.waitingForSlack).toBe(true) | ||
| }) | ||
|
|
||
| it('stops waiting and polling when Slack never reports back', () => { | ||
| jest.useFakeTimers() | ||
| integrationsLogic.actions.loadIntegrationsSuccess([]) | ||
| logic.actions.connectSlackClicked() | ||
|
|
||
| jest.advanceTimersByTime(SLACK_CONNECT_TIMEOUT_MS) | ||
|
|
||
| expect(logic.values.waitingForSlack).toBe(false) | ||
| expect(integrationsLogic.values.pollingSubscribers).toBe(0) | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| it('stops polling when the banner unmounts before Slack connects', () => { | ||
| logic.actions.connectSlackClicked() | ||
| logic.actions.connectSlackClicked() | ||
| expect(integrationsLogic.values.pollingSubscribers).toBe(1) | ||
|
|
||
| logic.unmount() | ||
|
|
||
| expect(integrationsLogic.values.pollingSubscribers).toBe(0) | ||
| logic.mount() | ||
| }) | ||
| }) |
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,142 @@ | ||
| import { LogicWrapper, MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers } from 'kea' | ||
|
|
||
| import { integrationsLogic } from 'lib/integrations/integrationsLogic' | ||
| import { userLogic } from 'scenes/userLogic' | ||
|
|
||
| import { IntegrationType, UserType } from '~/types' | ||
|
|
||
| export interface SlackConnectLogicProps { | ||
| connectKey: string | ||
| onConnected?: (integrationId: number) => void | ||
| } | ||
|
|
||
| // Generated by kea-typegen. Update if you're an agent, ignore if you're human. | ||
| export interface slackConnectLogicValues { | ||
| slackIntegrations: IntegrationType[] | undefined // integrationsLogic | ||
| user: UserType | null // userLogic | ||
| waitingForSlack: boolean | ||
| } | ||
|
|
||
| // Generated by kea-typegen. Update if you're an agent, ignore if you're human. | ||
| export interface slackConnectLogicActions { | ||
| loadIntegrationsSuccess: ( | ||
| integrations: IntegrationType[], | ||
| payload?: any | ||
| ) => { | ||
| integrations: IntegrationType[] | ||
| payload?: any | ||
| } // integrationsLogic | ||
| startPolling: () => { | ||
| value: true | ||
| } // integrationsLogic | ||
| stopPolling: () => { | ||
| value: true | ||
| } // integrationsLogic | ||
| connectSlackClicked: () => { | ||
| value: true | ||
| } | ||
| slackConnectTimedOut: () => { | ||
| value: true | ||
| } | ||
| slackConnected: (integrationId: number) => { | ||
| integrationId: number | ||
| } | ||
| } | ||
|
|
||
| // Generated by kea-typegen. Update if you're an agent, ignore if you're human. | ||
| export interface slackConnectLogicMeta { | ||
| key: string | ||
| } | ||
|
|
||
| export type slackConnectLogicType = MakeLogicType< | ||
| slackConnectLogicValues, | ||
| slackConnectLogicActions, | ||
| SlackConnectLogicProps, | ||
| slackConnectLogicMeta | ||
| > | ||
|
|
||
| /** | ||
| * Connecting Slack runs OAuth in another tab, so the page keeps what the user was filling in. The new | ||
| * workspace reaches this tab only through the integrations poller, which also refetches on focus. | ||
| */ | ||
| // An abandoned OAuth tab never reports back, so stop polling after this long and let the user retry. | ||
| export const SLACK_CONNECT_TIMEOUT_MS = 10 * 60 * 1000 | ||
|
|
||
| export const slackConnectLogic: LogicWrapper<slackConnectLogicType> = kea<slackConnectLogicType>([ | ||
| props({} as SlackConnectLogicProps), | ||
| key((props) => props.connectKey), | ||
| path((key) => ['lib', 'integrations', 'slackConnectLogic', key]), | ||
| connect(() => ({ | ||
| values: [integrationsLogic, ['slackIntegrations'], userLogic, ['user']], | ||
| actions: [integrationsLogic, ['startPolling', 'stopPolling', 'loadIntegrationsSuccess']], | ||
| })), | ||
| actions({ | ||
| connectSlackClicked: true, | ||
| slackConnected: (integrationId: number) => ({ integrationId }), | ||
| slackConnectTimedOut: true, | ||
| }), | ||
| reducers({ | ||
| waitingForSlack: [ | ||
| false, | ||
| { | ||
| connectSlackClicked: () => true, | ||
| slackConnected: () => false, | ||
| slackConnectTimedOut: () => false, | ||
| }, | ||
| ], | ||
| }), | ||
| listeners(({ actions, values, props, cache }) => ({ | ||
| connectSlackClicked: () => { | ||
| // Before the first successful load there is nothing to compare against, so the next load sets the baseline. | ||
| cache.knownSlackIntegrationIds = values.slackIntegrations | ||
| ? new Set(values.slackIntegrations.map((integration) => integration.id)) | ||
| : null | ||
| // The poller counts subscribers, so the same key keeps a second click from adding one that | ||
| // nothing stops. It keeps running while this tab is hidden, because the user is in the | ||
| // Slack tab then and the focus refetch is what brings the workspace back. | ||
| cache.disposables.add( | ||
| () => { | ||
|
rafaeelaudibert marked this conversation as resolved.
Resolved
|
||
| actions.startPolling() | ||
| return () => actions.stopPolling() | ||
| }, | ||
| 'integrationsPolling', | ||
| { pauseOnPageHidden: false } | ||
| ) | ||
| cache.disposables.add( | ||
| () => { | ||
| const timeout = window.setTimeout(() => actions.slackConnectTimedOut(), SLACK_CONNECT_TIMEOUT_MS) | ||
| return () => window.clearTimeout(timeout) | ||
| }, | ||
| 'slackConnectTimeout', | ||
| { pauseOnPageHidden: false } | ||
| ) | ||
| }, | ||
| loadIntegrationsSuccess: ({ integrations }) => { | ||
| if (!values.waitingForSlack) { | ||
| return | ||
| } | ||
| const slackIntegrations = integrations.filter((integration) => integration.kind === 'slack') | ||
| const known: Set<number> | null = cache.knownSlackIntegrationIds | ||
| if (!known) { | ||
| cache.knownSlackIntegrationIds = new Set(slackIntegrations.map((integration) => integration.id)) | ||
| return | ||
| } | ||
| // Another member can add a workspace to the project meanwhile; only this user's OAuth counts. | ||
| const workspace = slackIntegrations.find( | ||
| (integration) => | ||
| !known.has(integration.id) && !!values.user && integration.created_by?.uuid === values.user.uuid | ||
| ) | ||
| if (workspace) { | ||
| actions.slackConnected(workspace.id) | ||
| } | ||
| }, | ||
| slackConnected: ({ integrationId }) => { | ||
| cache.disposables.dispose('integrationsPolling') | ||
| cache.disposables.dispose('slackConnectTimeout') | ||
| props.onConnected?.(integrationId) | ||
| }, | ||
| slackConnectTimedOut: () => { | ||
| cache.disposables.dispose('integrationsPolling') | ||
| }, | ||
| })), | ||
| ]) | ||
Oops, something went wrong.
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.