From 23b68e219cc395d6116ef74b977915d26780ce65 Mon Sep 17 00:00:00 2001 From: Flavien David Date: Tue, 23 Jan 2024 10:51:24 +0100 Subject: [PATCH] Retrieve userAccountId when connecting Confluence (#3387) --- connectors/src/connectors/confluence/index.ts | 4 ++++ .../src/connectors/confluence/lib/confluence_api.ts | 8 ++++++++ .../connectors/confluence/lib/confluence_client.ts | 11 +++++++++++ connectors/src/lib/models/confluence.ts | 10 +++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/connectors/src/connectors/confluence/index.ts b/connectors/src/connectors/confluence/index.ts index 572e32ad09db..86a393c358e2 100644 --- a/connectors/src/connectors/confluence/index.ts +++ b/connectors/src/connectors/confluence/index.ts @@ -8,6 +8,7 @@ import type { ConnectorsAPIErrorResponse } from "@dust-tt/types"; import { confluenceConfig } from "@connectors/connectors/confluence/lib/config"; import { getConfluenceCloudInformation, + getConfluenceUserAccountId, listConfluenceSpaces, } from "@connectors/connectors/confluence/lib/confluence_api"; import type { ConfluenceSpaceType } from "@connectors/connectors/confluence/lib/confluence_client"; @@ -52,6 +53,8 @@ export async function createConfluenceConnector( return new Err(new Error("Confluence access token is invalid")); } + const userAccountId = await getConfluenceUserAccountId(confluenceAccessToken); + const { id: cloudId, url: cloudUrl } = confluenceCloudInformation; try { const connector = await sequelize_conn.transaction(async (transaction) => { @@ -70,6 +73,7 @@ export async function createConfluenceConnector( cloudId, connectorId: connector.id, url: cloudUrl, + userAccountId, }, { transaction } ); diff --git a/connectors/src/connectors/confluence/lib/confluence_api.ts b/connectors/src/connectors/confluence/lib/confluence_api.ts index 6cfb964b12d8..fa119f3dade7 100644 --- a/connectors/src/connectors/confluence/lib/confluence_api.ts +++ b/connectors/src/connectors/confluence/lib/confluence_api.ts @@ -18,6 +18,14 @@ export async function getConfluenceCloudInformation(accessToken: string) { } } +export async function getConfluenceUserAccountId(accessToken: string) { + const client = new ConfluenceClient(accessToken); + + const userAccount = await client.getUserAccount(); + + return userAccount.account_id; +} + async function fetchConfluenceConfiguration(connectorId: ModelId) { const confluenceConfig = await ConfluenceConfiguration.findOne({ where: { diff --git a/connectors/src/connectors/confluence/lib/confluence_client.ts b/connectors/src/connectors/confluence/lib/confluence_client.ts index 18fb85b2cb56..09f6a3b1754b 100644 --- a/connectors/src/connectors/confluence/lib/confluence_client.ts +++ b/connectors/src/connectors/confluence/lib/confluence_client.ts @@ -69,6 +69,13 @@ const ConfluenceListPagesCodec = t.type({ }), }); +const ConfluenceUserProfileCodec = t.intersection([ + t.type({ + account_id: t.string, + }), + CatchAllCodec, +]); + function extractCursorFromLinks(links: { next?: string }): string | null { if (!links.next) { return null; @@ -182,4 +189,8 @@ export class ConfluenceClient { ConfluencePageWithBodyCodec ); } + + async getUserAccount() { + return this.request("/me", ConfluenceUserProfileCodec); + } } diff --git a/connectors/src/lib/models/confluence.ts b/connectors/src/lib/models/confluence.ts index 01ca8700d0a8..7a91c6d022e3 100644 --- a/connectors/src/lib/models/confluence.ts +++ b/connectors/src/lib/models/confluence.ts @@ -18,6 +18,7 @@ export class ConfluenceConfiguration extends Model< declare cloudId: string; declare url: string; + declare userAccountId: string; declare connectorId: ForeignKey; } @@ -46,11 +47,18 @@ ConfluenceConfiguration.init( type: DataTypes.STRING, allowNull: false, }, + userAccountId: { + type: DataTypes.STRING, + allowNull: false, + }, }, { sequelize: sequelize_conn, modelName: "confluence_configurations", - indexes: [{ fields: ["connectorId"], unique: true }], + indexes: [ + { fields: ["connectorId"], unique: true }, + { fields: ["userAccountId"] }, + ], } ); Connector.hasOne(ConfluenceConfiguration);