Files
Tsering PaljorandJeremie Pardou 250748c8dc feat: Workspace level integrations (#6547)
* Add ADR-013 for workspace level integrations

* Add implementation

* Improve context section and remove technical points

* Move bulk replacement point to v2

* Remove workspace-level ownership on import since manual promotion should handle this.

* Add a glossary section

* Do Lazy creation + Celery migration

* Remove demotion

* Tighten the Options considered section

* Add Decision describing where the integrations are managed

* Update implementation plan after changes to ADR

* Sync plan and adr

* Rename adr and implementation

* Add the shared application that owns workspace integrations

* Create the shared application of every workspace

* Let only workspace admins manage workspace integrations

* Let applications use the integrations of their workspace.

* Trash workspace integrations in their workspace.

* Copy workspace integrations into exports and keep them in duplicates.

* Workspace admins can share integrations with every application in the workspace.

* Load the shared application of each workspace in the frontend and keep it out of application lists.

* Manage workspace integrations from an Integrations page in workspace settings.

* Offer workspace integrations in the integration dropdowns of every application.

* Move promoted integrations to the workspace list in other open tabs.

* Let admins make an app's integration available to the whole workspace from its integration settings.

* Preselect a workspace integration for new nodes and data sources when the app has none of its own.

* fix tests

* Delete implementation file

* More fixes after rebase

* Fix tests

* Let builders of an application see and use the workspace integrations from it.

* Show the workspace integrations, read-only, in the integration settings of applications.

* Keep a dashboard's integration out of promotion to the workspace.

* Leave the shared applications out of the admin dashboard's application count.

* Create the shared application of the workspaces made by the fill commands.

* Say in the promotion modal that an integration nobody authorized will run as the promoting admin.

* Say in the promotion modal that undoing it won't make the integration run as the previous person again.

* Ignore integration ids in sample data when publishing copies the workspace integrations an app uses.

* Let only undoing a promotion move an integration out of the workspace.

* Let the shared application backfill stop itself after 5 minutes and skip workspaces another transaction has locked.

* Say in the workspace integrations settings that published applications and workflows keep their own copy until they're published again.

* Leave the shared application out of the assistant evals' application counts.

* Refresh the workspace integrations when the Builder's data, events, users and integrations panels open.

* Say that anything new using a promoted integration blocks undoing the promotion, even from its own application.

* Warn when a database button sends through a workspace integration with no host or token.

* Refuse roles and invitations on the shared application.

* Fix tests

* Show an application only the workspace integrations of types it can use.

* Say in the promotion modal what builders of other applications can do as the integration.

* Leave the shared application type out of the API docs for creating and updating applications.

* Check who receives a workspace integration event in one permission call.

* Say which workspaces have a shared application and who creates it.

* Let a redone promotion record what uses the integration again.

* Update docstrings

* Stop template visitors from listing the integrations of the template's workspace.

* Let database buttons find their own copy of a workspace integration after an import.

* Say what holds the workspace row lock while a shared application is created.

* Fix tests

* Stop the workspace integration form component from being made reactive.

* Make test runs use a new copy of the workflow after an integration it uses changes.

* Hide the Members settings tab from members who can't list the workspace members.

* Add changelog

* Fix migration conflict

* Fix migration conflict

* Fix test

* Update comments and ADR number

* Link to the workspace integrations from the sidebar's Settings menu.

* fix: refresh agent action errors when workspace integrations change

* fix: move workspace integrations into workspace settings

* fix: use share icon for workspace integration promotion

* refactor: use explicit shared application relations

* feat: centralize default integration selection

* refactor: share integration change notifications

* refactor: prepare integration mappings before import

* refactor: collect integration references during export

* refactor: prepare integration ownership changes without a new hook

* test: restore registered service model after mocking

* test: cover shared integration defaults in Builder

* test: include Slack in Agent workspace integration choices

* Fix migrations

* Stop template visitors from listing the workspace integrations through an application of the template.

* Only offer integrations of the user source's integration type when updating a user source.

* Say the workspace integration copy means published Application Builder apps and automation workflows.

* Use the same spacing under both integration settings sections and enlarge their headings.

* Keep the shared application out of automatic snapshot schedules.

* Fix test after rebase

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-27 15:49:32 +04:00

580 lines
19 KiB
JavaScript

import IntegrationService from '@baserow/modules/core/services/integration'
// How many times a fetch asks again when the list changed while it was open.
// Bounded: a list changing this often is not settling, and the caller is told
// nothing was loaded rather than made to wait.
const FETCH_ATTEMPTS = 3
// Bumped by every write, so a fetch can tell its answer is older than
// the list it would overwrite.
const state = () => ({ generation: {} })
const updateContext = {
updateTimeout: null,
promiseResolve: null,
lastUpdatedValues: null,
}
const mutations = {
BUMP_GENERATION(state, { application }) {
state.generation = {
...state.generation,
[application.id]: (state.generation[application.id] || 0) + 1,
}
},
ADD_ITEM(state, { application, integration, beforeId = null }) {
if (beforeId === null) {
application.integrations.push(integration)
} else {
const insertionIndex = application.integrations.findIndex(
(e) => e.id === beforeId
)
application.integrations.splice(insertionIndex, 0, integration)
}
},
UPDATE_ITEM(
state,
{ application, integration: integrationToUpdate, values }
) {
application.integrations.forEach((integration) => {
if (integration.id === integrationToUpdate.id) {
Object.assign(integration, values)
}
})
},
DELETE_ITEM(state, { application, integrationId }) {
const index = application.integrations.findIndex(
(integration) => integration.id === integrationId
)
if (index > -1) {
application.integrations.splice(index, 1)
}
},
MOVE_ITEM_APPLICATION(
state,
{ applicationSource, applicationDest, integrationId }
) {
const index = applicationSource.integrations.findIndex(
(integration) => integration.id === integrationId
)
if (index > -1) {
const [integration] = applicationSource.integrations.splice(index, 1)
integration.application_id = applicationDest.id
applicationDest.integrations.push(integration)
}
},
MOVE_ITEM(state, { application, index, oldIndex }) {
application.integrations.splice(
index,
0,
application.integrations.splice(oldIndex, 1)[0]
)
},
CLEAR_ITEMS(state, { application }) {
application.integrations = []
},
/**
* Replaces the application's integrations of the given types, and keeps the
* others.
*/
REPLACE_ITEMS_OF_TYPES(state, { application, types, integrations }) {
application.integrations = [
...application.integrations.filter(
(integration) => !types.includes(integration.type)
),
...integrations,
]
},
}
const actions = {
forceCreate({ commit }, { application, integration, beforeId = null }) {
commit('ADD_ITEM', { application, integration, beforeId })
commit('BUMP_GENERATION', { application })
},
forceUpdate({ commit }, { application, integration, values }) {
commit('UPDATE_ITEM', { application, integration, values })
commit('BUMP_GENERATION', { application })
},
forceDelete({ commit, getters }, { application, integrationId }) {
commit('DELETE_ITEM', { application, integrationId })
commit('BUMP_GENERATION', { application })
},
forceMove(
{ commit, getters },
{ application, integrationId, beforeIntegrationId }
) {
const currentOrder = getters
.getIntegrations(application)
.map((integration) => integration.id)
const oldIndex = currentOrder.findIndex((id) => id === integrationId)
const index = beforeIntegrationId
? currentOrder.findIndex((id) => id === beforeIntegrationId)
: getters.getIntegrations.length
// If the integration is before the beforeIntegration we must decrease the target index by
// one to compensate the removed integration.
if (oldIndex < index) {
commit('MOVE_ITEM', { application, index: index - 1, oldIndex })
} else {
commit('MOVE_ITEM', { application, index, oldIndex })
}
commit('BUMP_GENERATION', { application })
},
/**
* Moves the integration to the list of another application, like a promotion to
* the shared application of the workspace and its undo do. It goes last, where
* the backend orders it.
*/
forceMoveToApplication(
{ commit },
{ applicationSource, applicationDest, integrationId }
) {
commit('MOVE_ITEM_APPLICATION', {
applicationSource,
applicationDest,
integrationId,
})
commit('BUMP_GENERATION', { application: applicationSource })
commit('BUMP_GENERATION', { application: applicationDest })
},
/**
* Moves the integration to another application of the workspace, which the
* backend only accepts towards the workspace's shared application: a
* promotion. Unlike `update`, the list only changes once the backend accepted
* the move, and with the values the move changed, like the person a Local
* Baserow integration was authorized as. The tab moving it receives no
* realtime event of its own move.
*/
async moveToApplication(
{ dispatch, getters },
{ application, applicationDest, integrationId }
) {
const { data: movedIntegration } = await IntegrationService(
this.$client
).update(integrationId, { application_id: applicationDest.id })
const integration = getters
.getIntegrations(application)
.find(({ id }) => id === integrationId)
if (integration) {
await dispatch('forceUpdate', {
application,
integration,
values: movedIntegration,
})
await dispatch('forceMoveToApplication', {
applicationSource: application,
applicationDest,
integrationId,
})
}
return movedIntegration
},
async create(
{ dispatch },
{ application, integrationType, values, beforeId = null }
) {
const { $registry, $i18n, $client, $config } = this
const { data: integration } = await IntegrationService($client).create(
application.id,
integrationType,
values,
beforeId
)
await dispatch('forceCreate', { application, integration, beforeId })
return integration
},
async update({ dispatch, getters }, { application, integrationId, values }) {
const { $registry, $i18n, $client, $config } = this
const integrationsOfApplication = getters.getIntegrations(application)
const integration = integrationsOfApplication.find(
({ id }) => id === integrationId
)
const oldValues = {}
const newValues = {}
Object.keys(values).forEach((name) => {
if (Object.prototype.hasOwnProperty.call(integration, name)) {
oldValues[name] = integration[name]
newValues[name] = values[name]
}
})
await dispatch('forceUpdate', {
application,
integration,
values: newValues,
})
try {
const { data: updatedIntegration } = await IntegrationService(
$client
).update(integration.id, values)
// Only the response carries the updated `has_*` credential flags.
await dispatch('forceUpdate', {
application,
integration,
values: updatedIntegration,
})
} catch (error) {
await dispatch('forceUpdate', {
application,
integration,
values: oldValues,
})
throw error
}
},
async debouncedUpdate(
{ dispatch, getters },
{ application, integrationId, values }
) {
const { $registry, $i18n, $client, $config } = this
const integration = getters
.getIntegrations(application)
.find(({ id }) => id === integrationId)
const oldValues = {}
const newValues = {}
Object.keys(values).forEach((name) => {
if (Object.prototype.hasOwnProperty.call(integration, name)) {
oldValues[name] = integration[name]
newValues[name] = values[name]
}
})
await dispatch('forceUpdate', {
application,
integration,
values: newValues,
})
return new Promise((resolve, reject) => {
const fire = async () => {
try {
await IntegrationService($client).update(integration.id, values)
updateContext.lastUpdatedValues = values
resolve()
} catch (error) {
// Revert to old values on error
await dispatch('forceUpdate', {
application,
integration,
values: updateContext.lastUpdatedValues,
})
reject(error)
}
}
if (updateContext.promiseResolve) {
updateContext.promiseResolve()
updateContext.promiseResolve = null
}
clearTimeout(updateContext.updateTimeout)
if (!updateContext.lastUpdatedValues) {
updateContext.lastUpdatedValues = oldValues
}
updateContext.updateTimeout = setTimeout(fire, 500)
updateContext.promiseResolve = resolve
})
},
async delete({ dispatch, getters }, { application, integrationId }) {
const { $registry, $i18n, $client, $config } = this
const integrationsOfApplication = getters.getIntegrations(application)
const integrationIndex = integrationsOfApplication.findIndex(
(integration) => integration.id === integrationId
)
const integrationToDelete = integrationsOfApplication[integrationIndex]
const beforeId =
integrationIndex !== integrationsOfApplication.length - 1
? integrationsOfApplication[integrationIndex + 1].id
: null
await dispatch('forceDelete', { application, integrationId })
try {
await IntegrationService($client).delete(integrationId)
} catch (error) {
await dispatch('forceCreate', {
application,
integration: integrationToDelete,
beforeId,
})
throw error
}
},
async move(
{ dispatch },
{ application, integrationId, beforeIntegrationId }
) {
const { $registry, $i18n, $client, $config } = this
await dispatch('forceMove', {
application,
integrationId,
beforeIntegrationId,
})
try {
await IntegrationService($client).move(integrationId, beforeIntegrationId)
} catch (error) {
await dispatch('forceMove', {
application,
integrationId: beforeIntegrationId,
beforeIntegrationId: integrationId,
})
throw error
}
},
async fetch({ commit, state, rootGetters }, { application }) {
const { $client } = this
const applicationId = application.id
for (let attempt = 0; attempt < FETCH_ATTEMPTS; attempt++) {
const before = state.generation[applicationId] || 0
const { data: integrations } =
await IntegrationService($client).fetchAll(applicationId)
// Changed while this was open, so the answer is older than the list and
// writing it would lose the change. Asked again rather than discarded:
// most callers await this and then mark the application loaded, and
// would keep an empty list until a reload.
if ((state.generation[applicationId] || 0) !== before) {
continue
}
// `forceSetAll` can have replaced the object this started with, and
// filling that one leaves the one on screen empty.
const current =
rootGetters['application/get'](applicationId) || application
// Committed rather than dispatched through `forceCreate`: writing the
// answer is not a change to back off from, and a bump here would make
// the next attempt back off from this one.
commit('CLEAR_ITEMS', { application: current })
integrations.forEach((integration) =>
commit('ADD_ITEM', { application: current, integration })
)
return integrations
}
// Still moving. Null rather than a list older than the screen, so a
// caller does not remember a load that did not happen.
return null
},
/**
* Loads the workspace integrations the application can use, at most once per
* application unless forced. Realtime events keep the list and what's stored
* on each integration current, but not what the backend works out when it
* sends them, like the tables and views a Local Baserow integration can reach
* (`context_data`). So the panels that offer them fetch them again through
* `fetchUsable`, like they fetch the application's own integrations again.
*
* They're fetched through the application, so that a user with a role on that
* application only gets them too, with the shared application that owns them,
* which the application listing leaves out for that user. Nothing is loaded
* when the user can't list the integrations of the application, or isn't a
* member of the workspace, like the one of a template preview.
*
* The backend sends only the types the application can use, and says which,
* so a database isn't sent a Local Baserow integration. The shared
* application keeps one list for the whole workspace, so an answer replaces
* only the integrations of those types, and another application that uses
* other types still finds them after it. For the same reason, the
* application remembers the types it loaded (`_workspaceIntegrationTypes`),
* not the shared one.
*
* @param {Object} application An application of the workspace.
* @param {boolean} force Fetches them even when they were loaded already. A
* forced fetch that gives up keeps the list it would have replaced.
* @returns {Promise<Array|null>} The workspace integrations of the types the
* application can use, or null when their list changed under the fetch and
* it gave up, like `fetch`.
*/
async fetchWorkspaceIntegrations(
{ commit, dispatch, getters, state, rootGetters },
{ application, force = false }
) {
const workspace =
application.workspace &&
rootGetters['workspace/get'](application.workspace.id)
if (
!workspace ||
!this.app.$hasPermission(
'application.list_integrations',
application,
workspace.id
)
) {
return []
}
// Looked up again after awaiting, as `forceSetAll` can replace it.
const consumer = () =>
rootGetters['application/get'](application.id) || application
const loadedTypes = consumer()._workspaceIntegrationTypes
if (!force && loadedTypes) {
return getters
.getWorkspaceIntegrations(consumer())
.filter((integration) => loadedTypes.includes(integration.type))
}
for (let attempt = 0; attempt < FETCH_ATTEMPTS; attempt++) {
// Taken for every application, as the shared one isn't known before the
// answer when the store doesn't have it.
const before = { ...state.generation }
const { data } = await IntegrationService(
this.$client
).fetchAllOfWorkspace(application.id)
if (!data.shared_application) {
return []
}
const sharedApplicationId = data.shared_application.id
// Changed while this was open, see `fetch`.
if (
(state.generation[sharedApplicationId] || 0) !==
(before[sharedApplicationId] || 0)
) {
continue
}
// Also replaced by `forceSetAll` while the fetch was open, or missing
// when the application listing leaves it out.
const current =
rootGetters['application/get'](sharedApplicationId) ||
(await dispatch('application/forceCreate', data.shared_application, {
root: true,
}))
// Committed for the same reason as in `fetch`.
commit('REPLACE_ITEMS_OF_TYPES', {
application: current,
types: data.integration_types,
integrations: data.integrations,
})
// Never unset: it says the list was loaded at least once, which stays
// true when a later forced fetch gives up.
await dispatch(
'application/forceUpdate',
{
application: consumer(),
data: { _workspaceIntegrationTypes: data.integration_types },
},
{ root: true }
)
return data.integrations
}
// Left unmarked, so the next application that asks fetches again.
return null
},
/**
* Loads the workspace integrations once, for the paths that load an
* application, see `fetchWorkspaceIntegrations`.
*/
fetchWorkspaceIntegrationsOnce({ dispatch }, { application }) {
return dispatch('fetchWorkspaceIntegrations', { application })
},
/**
* Fetches again every integration the application can use, its own and the
* workspace ones, for a panel that offers them with what they can reach, like
* the tables and views of a Local Baserow integration. Those change with no
* realtime event about the integration, like when a table is created.
*
* @param {Object} application The application the integrations are used from.
* @returns {Promise<Array|null>} The usable integrations, or null when either
* list changed under its fetch and it gave up.
*/
async fetchUsable({ dispatch }, { application }) {
const [own, workspace] = await Promise.all([
dispatch('fetch', { application }),
dispatch('fetchWorkspaceIntegrations', { application, force: true }),
])
if (own === null || workspace === null) {
return null
}
return [...own, ...workspace]
},
async duplicate({ getters, dispatch }, { application, integrationId }) {
const integration = getters.getIntegrations.find(
(e) => e.id === integrationId
)
await dispatch('create', {
application,
integrationType: integration.type,
beforeId: integration.id,
})
},
}
const getters = {
/**
* The integrations the application owns. A settings page lists these, while a
* picker offers `getUsableIntegrations`.
*/
getIntegrations: (state) => (application) => {
return application?.integrations || []
},
/**
* The integrations of the application's workspace, which every application of
* the workspace can use. Empty for the shared application itself, which owns
* them, and until `fetchWorkspaceIntegrations` loaded them.
*/
getWorkspaceIntegrations:
(state, getters, rootState, rootGetters) => (application) => {
if (!application?.workspace) {
return []
}
const sharedApplication = rootGetters['application/getSharedApplication'](
application.workspace
)
if (!sharedApplication || sharedApplication.id === application.id) {
return []
}
return getters.getIntegrations(sharedApplication)
},
/**
* The integrations the application can use: its own, then the workspace ones.
* Their `application_id` tells them apart.
*/
getUsableIntegrations: (state, getters) => (application) => {
return [
...getters.getIntegrations(application),
...getters.getWorkspaceIntegrations(application),
]
},
/**
* A number that changes with every change to the integrations of the
* application, so a component listing them knows when to refresh.
*/
getGeneration: (state) => (application) => {
return state.generation[application.id] || 0
},
/**
* One of the integrations the application can use, its own or a workspace
* one, so a service using a workspace integration finds it too.
*/
getIntegrationById: (state, getters) => (application, id) => {
return getters
.getUsableIntegrations(application)
.find((integration) => integration.id === id)
},
}
export default {
namespaced: true,
state,
getters,
actions,
mutations,
}